|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\AbstractTest; |
|
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\AbstractGenerator; |
|
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\FakerData\FakerDataProviderInterface; |
|
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface; |
|
9
|
|
|
use Faker\Generator; |
|
10
|
|
|
|
|
11
|
|
|
abstract class AbstractFieldTraitTest extends AbstractTest |
|
12
|
|
|
{ |
|
13
|
|
|
protected const TEST_ENTITY_FQN_BASE = AbstractTest::TEST_PROJECT_ROOT_NAMESPACE |
|
14
|
|
|
.'\\'.AbstractGenerator::ENTITIES_FOLDER_NAME |
|
15
|
|
|
.'\\'; |
|
16
|
|
|
|
|
17
|
|
|
protected const TEST_FIELD_FQN = 'Override Me'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The expected default value for the field. Most fields are marked as nullable so the default is null. |
|
21
|
|
|
* Should be overriden in the actual field test for any fields that are not nullable |
|
22
|
|
|
*/ |
|
23
|
|
|
protected const TEST_FIELD_DEFAULT = null; |
|
24
|
|
|
|
|
25
|
|
|
protected const TEST_FIELD_PROP = 'Override Me'; |
|
26
|
|
|
|
|
27
|
|
|
protected $entitySuffix; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var Generator |
|
31
|
|
|
*/ |
|
32
|
|
|
protected static $fakerGenerator; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
36
|
|
|
*/ |
|
37
|
|
|
public static function setUpBeforeClass() |
|
38
|
|
|
{ |
|
39
|
|
|
/* The :void return type declaration that should be here would cause a BC issue */ |
|
40
|
|
|
self::$fakerGenerator = \Faker\Factory::create(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function setup() |
|
44
|
|
|
{ |
|
45
|
|
|
parent::setup(); |
|
46
|
|
|
$this->entitySuffix = substr(static::class, strrpos(static::class, '\\') + 1); |
|
47
|
|
|
$this->generateCode(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
protected function generateCode() |
|
51
|
|
|
{ |
|
52
|
|
|
$this->getEntityGenerator() |
|
53
|
|
|
->generateEntity(static::TEST_ENTITY_FQN_BASE.$this->entitySuffix); |
|
54
|
|
|
$this->getFieldGenerator() |
|
55
|
|
|
->setEntityHasField( |
|
56
|
|
|
static::TEST_ENTITY_FQN_BASE.$this->entitySuffix, |
|
57
|
|
|
static::TEST_FIELD_FQN |
|
58
|
|
|
); |
|
59
|
|
|
$this->setupCopiedWorkDir(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param EntityInterface $entity |
|
64
|
|
|
* |
|
65
|
|
|
* @return string |
|
66
|
|
|
* @throws \Exception |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function getGetter(EntityInterface $entity): string |
|
69
|
|
|
{ |
|
70
|
|
|
foreach (['get', 'is', 'has'] as $prefix) { |
|
71
|
|
|
$method = $prefix.static::TEST_FIELD_PROP; |
|
72
|
|
|
if (\method_exists($entity, $method)) { |
|
73
|
|
|
return $method; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
throw new \RuntimeException('Failed finding a getter in '.__METHOD__); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
protected function findFakerProvider(): ?FakerDataProviderInterface |
|
80
|
|
|
{ |
|
81
|
|
|
$fakerProviderFqnBase = 'EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\FakerData'; |
|
82
|
|
|
$traitBase = 'EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits'; |
|
83
|
|
|
$fakerFqn = \str_replace( |
|
84
|
|
|
[ |
|
85
|
|
|
$traitBase, |
|
86
|
|
|
'FieldTrait', |
|
87
|
|
|
], |
|
88
|
|
|
[ |
|
89
|
|
|
$fakerProviderFqnBase, |
|
90
|
|
|
'FakerData', |
|
91
|
|
|
], |
|
92
|
|
|
static::TEST_FIELD_FQN |
|
93
|
|
|
); |
|
94
|
|
|
if (class_exists($fakerFqn)) { |
|
95
|
|
|
return new $fakerFqn(self::$fakerGenerator); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return null; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param EntityInterface $entity |
|
103
|
|
|
* |
|
104
|
|
|
* @return mixed |
|
105
|
|
|
* @throws \ReflectionException |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function setFakerValueForProperty(EntityInterface $entity) |
|
108
|
|
|
{ |
|
109
|
|
|
$setter = 'set'.static::TEST_FIELD_PROP; |
|
110
|
|
|
$fakerProvider = $this->findFakerProvider(); |
|
111
|
|
|
if ($fakerProvider instanceof FakerDataProviderInterface) { |
|
112
|
|
|
$setValue = $fakerProvider(); |
|
113
|
|
|
$entity->$setter($setValue); |
|
114
|
|
|
|
|
115
|
|
|
return $setValue; |
|
116
|
|
|
} |
|
117
|
|
|
$reflection = new \ReflectionClass($entity); |
|
118
|
|
|
$setterReflection = $reflection->getMethod($setter); |
|
119
|
|
|
$setParamType = current($setterReflection->getParameters())->getType()->getName(); |
|
120
|
|
|
switch ($setParamType) { |
|
121
|
|
|
case 'string': |
|
122
|
|
|
$setValue = self::$fakerGenerator->text(); |
|
123
|
|
|
break; |
|
124
|
|
|
case 'int': |
|
125
|
|
|
case 'float': |
|
126
|
|
|
$setValue = self::$fakerGenerator->numberBetween(0, 100); |
|
127
|
|
|
break; |
|
128
|
|
|
case 'bool': |
|
129
|
|
|
$setValue = self::$fakerGenerator->boolean; |
|
130
|
|
|
break; |
|
131
|
|
|
case 'DateTime': |
|
132
|
|
|
$setValue = self::$fakerGenerator->dateTime; |
|
133
|
|
|
break; |
|
134
|
|
|
default: |
|
135
|
|
|
throw new \RuntimeException('Failed getting a data provider for the property type '.$setParamType); |
|
136
|
|
|
} |
|
137
|
|
|
$entity->$setter($setValue); |
|
138
|
|
|
|
|
139
|
|
|
return $setValue; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
|
145
|
|
|
* @throws \ReflectionException |
|
146
|
|
|
*/ |
|
147
|
|
|
public function testCreateEntityWithField(): void |
|
148
|
|
|
{ |
|
149
|
|
|
$entityFqn = $this->getCopiedFqn(static::TEST_ENTITY_FQN_BASE.$this->entitySuffix); |
|
150
|
|
|
$entity = new $entityFqn(); |
|
151
|
|
|
$getter = $this->getGetter($entity); |
|
152
|
|
|
$this->assertTrue(\method_exists($entity, $getter)); |
|
153
|
|
|
$value = $entity->$getter(); |
|
154
|
|
|
$this->assertSame(static::TEST_FIELD_DEFAULT, $value); |
|
155
|
|
|
$setValue = $this->setFakerValueForProperty($entity); |
|
156
|
|
|
$this->assertSame($setValue, $entity->$getter()); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|