Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
12 / 12 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| CompositeObjectFaker | |
100.00% |
12 / 12 |
|
100.00% |
2 / 2 |
6 | |
100.00% |
1 / 1 |
| supports | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| fakeFor | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | namespace Apie\Faker\Fakers; |
| 3 | |
| 4 | use Apie\Core\ValueObjects\CompositeValueObject; |
| 5 | use Apie\Core\ValueObjects\Interfaces\ValueObjectInterface; |
| 6 | use Apie\Faker\Interfaces\ApieClassFaker; |
| 7 | use Apie\TypeConverter\ReflectionTypeFactory; |
| 8 | use Faker\Generator; |
| 9 | use ReflectionClass; |
| 10 | |
| 11 | /** @implements ApieClassFaker<ValueObjectInterface> */ |
| 12 | class CompositeObjectFaker implements ApieClassFaker |
| 13 | { |
| 14 | public function supports(ReflectionClass $class): bool |
| 15 | { |
| 16 | $interfaces = $class->getInterfaceNames(); |
| 17 | return in_array(ValueObjectInterface::class, $interfaces) && |
| 18 | in_array(CompositeValueObject::class, $class->getTraitNames()); |
| 19 | } |
| 20 | |
| 21 | public function fakeFor(Generator $generator, ReflectionClass $class): ValueObjectInterface |
| 22 | { |
| 23 | $data = []; |
| 24 | $className = $class->name; |
| 25 | // @phpstan-ignore-next-line staticMethod.notFound |
| 26 | foreach ($className::getFields() as $name => $field) { |
| 27 | if ($field->isOptional() && $generator->boolean()) { |
| 28 | continue; |
| 29 | } |
| 30 | $data[$name] = $generator->fakeFromType( |
| 31 | ReflectionTypeFactory::createReflectionType($field->getTypehint()), |
| 32 | ); |
| 33 | } |
| 34 | return $className::fromNative($data); |
| 35 | } |
| 36 | } |