Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
7 / 7 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| PolymorphicEntityFaker | |
100.00% |
7 / 7 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
| supports | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| fakeFor | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\Faker\Fakers; |
| 3 | |
| 4 | use Apie\Core\Entities\PolymorphicEntityInterface; |
| 5 | use Apie\Core\Other\DiscriminatorMapping; |
| 6 | use Apie\Faker\Interfaces\ApieClassFaker; |
| 7 | use Faker\Generator; |
| 8 | use ReflectionClass; |
| 9 | |
| 10 | /** |
| 11 | * @implements ApieClassFaker<PolymorphicEntityInterface> |
| 12 | */ |
| 13 | class PolymorphicEntityFaker implements ApieClassFaker |
| 14 | { |
| 15 | public function supports(ReflectionClass $class): bool |
| 16 | { |
| 17 | if (!$class->implementsInterface(PolymorphicEntityInterface::class)) { |
| 18 | return false; |
| 19 | } |
| 20 | $method = $class->getMethod('getDiscriminatorMapping'); |
| 21 | return $method->getDeclaringClass()->name === $class->name && !$method->isAbstract(); |
| 22 | } |
| 23 | |
| 24 | public function fakeFor(Generator $generator, ReflectionClass $class): PolymorphicEntityInterface |
| 25 | { |
| 26 | /** @var DiscriminatorMapping */ |
| 27 | $mapping = $class->getMethod('getDiscriminatorMapping')->invoke(null); |
| 28 | $randomConfig = $generator->randomElement($mapping->getConfigs()); |
| 29 | return $generator->fakeClass($randomConfig->getClassname()); |
| 30 | } |
| 31 | } |