Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.74% covered (success)
94.74%
18 / 19
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
UploadedFileFaker
94.74% covered (success)
94.74%
18 / 19
66.67% covered (warning)
66.67%
2 / 3
7.01
0.00% covered (danger)
0.00%
0 / 1
 supports
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getApieFileFakers
90.91% covered (success)
90.91%
10 / 11
0.00% covered (danger)
0.00%
0 / 1
3.01
 fakeFor
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2namespace Apie\Faker\Fakers;
3
4use Apie\Core\Attributes\FakeFile;
5use Apie\Core\FileStorage\StoredFile;
6use Apie\Faker\FileFakerFactory;
7use Apie\Faker\Interfaces\ApieClassFaker;
8use Apie\Faker\Interfaces\ApieFileFaker;
9use Apie\Faker\SeededFile;
10use Faker\Generator;
11use Psr\Http\Message\UploadedFileInterface;
12use ReflectionAttribute;
13use ReflectionClass;
14
15/** @implements ApieClassFaker<StoredFile> */
16class UploadedFileFaker implements ApieClassFaker
17{
18    public function supports(ReflectionClass $class): bool
19    {
20        return UploadedFileInterface::class === $class->name
21            || in_array(UploadedFileInterface::class, $class->getInterfaceNames());
22    }
23
24    /**
25     * @param ReflectionClass<ApieFileFaker> $class
26     */
27    private function getApieFileFakers(Generator $generator, ReflectionClass $class): ApieFileFaker
28    {
29        $interfaces = array_map(
30            function (ReflectionAttribute $attribute) {
31                return $attribute->newInstance()->className;
32            },
33            $class->getAttributes(FakeFile::class)
34        );
35        $pickedInterface = empty($interfaces) ? ApieFileFaker::class : $generator->randomElement($interfaces);
36        $supported = FileFakerFactory::getSupportedFileFakers($pickedInterface);
37        if (empty($supported)) {
38            throw new \LogicException('I have no file faker that implements ' . $pickedInterface);
39        }
40        return $generator->randomElement($supported);
41    }
42
43    public function fakeFor(Generator $generator, ReflectionClass $class): UploadedFileInterface
44    {
45        $internal = SeededFile::create(
46            $generator,
47            $this->getApieFileFakers($generator, $class)
48        );
49        $className = $class->name === UploadedFileInterface::class ? StoredFile::class : $class->name;
50        return $className::createFromUploadedFile($internal);
51    }
52}