1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Tests\Large\Entity\Fields\Traits; |
4
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\AbstractGenerator; |
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\FakerData\FakerDataProviderInterface; |
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\EntitySaver; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\EntityTestInterface; |
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Validation\EntityValidatorFactory; |
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Tests\Assets\AbstractLargeTest; |
12
|
|
|
use Faker\Generator; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Extend this test with your Field Trait test to get basic test coverage. |
16
|
|
|
* |
17
|
|
|
* You should extend your field trait test to test your validation |
18
|
|
|
* |
19
|
|
|
* Class AbstractFieldTraitLargeTest |
20
|
|
|
* |
21
|
|
|
* @package EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits |
22
|
|
|
* @SuppressWarnings(PHPMD.NumberOfChildren) |
23
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
24
|
|
|
* @large |
25
|
|
|
*/ |
26
|
|
|
abstract class AbstractFieldTraitLargeTest extends AbstractLargeTest |
27
|
|
|
{ |
28
|
|
|
protected const TEST_ENTITY_FQN_BASE = self::TEST_PROJECT_ROOT_NAMESPACE |
29
|
|
|
. '\\' . AbstractGenerator::ENTITIES_FOLDER_NAME |
30
|
|
|
. '\\'; |
31
|
|
|
|
32
|
|
|
protected const TEST_FIELD_FQN = 'Override Me'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The expected default value for the field. Most fields are marked as nullable so the default is null. |
36
|
|
|
* Should be overriden in the actual field test for any fields that are not nullable |
37
|
|
|
*/ |
38
|
|
|
protected const TEST_FIELD_DEFAULT = null; |
39
|
|
|
|
40
|
|
|
protected const TEST_FIELD_PROP = 'Override Me'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* set to false for read only fields (with no setter) |
44
|
|
|
*/ |
45
|
|
|
protected const HAS_SETTER = true; |
46
|
|
|
/** |
47
|
|
|
* @var Generator |
48
|
|
|
*/ |
49
|
|
|
protected static $fakerGenerator; |
50
|
|
|
protected $entitySuffix; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
54
|
|
|
*/ |
55
|
|
|
public static function setUpBeforeClass() |
56
|
|
|
{ |
57
|
|
|
parent::setUpBeforeClass(); |
58
|
|
|
self::$fakerGenerator = \Faker\Factory::create(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function setup() |
62
|
|
|
{ |
63
|
|
|
parent::setup(); |
64
|
|
|
$this->entitySuffix = substr(static::class, strrpos(static::class, '\\') + 1); |
65
|
|
|
$this->generateCode(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function generateCode() |
69
|
|
|
{ |
70
|
|
|
$this->getEntityGenerator() |
71
|
|
|
->generateEntity(static::TEST_ENTITY_FQN_BASE . $this->entitySuffix); |
72
|
|
|
$this->getFieldSetter() |
73
|
|
|
->setEntityHasField( |
74
|
|
|
static::TEST_ENTITY_FQN_BASE . $this->entitySuffix, |
75
|
|
|
static::TEST_FIELD_FQN |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
81
|
|
|
* @throws \ReflectionException |
82
|
|
|
* @large |
83
|
|
|
* @test |
84
|
|
|
* @coversNothing |
85
|
|
|
*/ |
86
|
|
|
public function createEntityWithField(): void |
87
|
|
|
{ |
88
|
|
|
$this->setupCopiedWorkDir(); |
89
|
|
|
$entityFqn = $this->getCopiedFqn(static::TEST_ENTITY_FQN_BASE . $this->entitySuffix); |
90
|
|
|
$entity = new $entityFqn($this->container->get(EntityValidatorFactory::class)); |
91
|
|
|
$getter = $this->getGetter($entity); |
92
|
|
|
self::assertTrue(\method_exists($entity, $getter)); |
93
|
|
|
$value = $entity->$getter(); |
94
|
|
|
self::assertSame( |
95
|
|
|
static::TEST_FIELD_DEFAULT, |
96
|
|
|
$value, |
97
|
|
|
'The getter on a newly created entity returns ' . var_export($value, true) |
98
|
|
|
. ' whereas the configured default value is ' . var_export(static::TEST_FIELD_DEFAULT, true) |
99
|
|
|
); |
100
|
|
|
if (false === static::HAS_SETTER) { |
|
|
|
|
101
|
|
|
return; |
102
|
|
|
} |
103
|
|
|
$setValue = $this->setFakerValueForProperty($entity); |
104
|
|
|
self::assertSame($setValue, $entity->$getter()); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param EntityInterface $entity |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
* @throws \Exception |
112
|
|
|
*/ |
113
|
|
|
protected function getGetter(EntityInterface $entity): string |
114
|
|
|
{ |
115
|
|
|
foreach (['get', 'is', 'has'] as $prefix) { |
116
|
|
|
$method = $prefix . static::TEST_FIELD_PROP; |
117
|
|
|
if (\method_exists($entity, $method)) { |
118
|
|
|
return $method; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
throw new \RuntimeException('Failed finding a getter in ' . __METHOD__); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param EntityInterface $entity |
126
|
|
|
* |
127
|
|
|
* @return mixed |
128
|
|
|
* @throws \ReflectionException |
129
|
|
|
* @throws \Exception |
130
|
|
|
*/ |
131
|
|
|
protected function setFakerValueForProperty(EntityInterface $entity) |
132
|
|
|
{ |
133
|
|
|
$setter = 'set' . static::TEST_FIELD_PROP; |
134
|
|
|
$fakerProvider = $this->getFakerDataProvider(); |
135
|
|
|
if ($fakerProvider instanceof FakerDataProviderInterface) { |
136
|
|
|
$setValue = $fakerProvider(); |
137
|
|
|
$entity->$setter($setValue); |
138
|
|
|
|
139
|
|
|
return $setValue; |
140
|
|
|
} |
141
|
|
|
$reflection = new \ts\Reflection\ReflectionClass(\get_class($entity)); |
142
|
|
|
$setterReflection = $reflection->getMethod($setter); |
143
|
|
|
$setParamType = current($setterReflection->getParameters())->getType()->getName(); |
144
|
|
|
switch ($setParamType) { |
145
|
|
|
case 'string': |
146
|
|
|
$setValue = self::$fakerGenerator->text(); |
147
|
|
|
break; |
148
|
|
|
case 'int': |
149
|
|
|
$setValue = self::$fakerGenerator->numberBetween(0, PHP_INT_MAX); |
150
|
|
|
break; |
151
|
|
|
case 'float': |
152
|
|
|
$setValue = self::$fakerGenerator->randomFloat(12, 0, 10000); |
153
|
|
|
break; |
154
|
|
|
case 'bool': |
155
|
|
|
$setValue = self::$fakerGenerator->boolean; |
156
|
|
|
break; |
157
|
|
|
case 'DateTime': |
158
|
|
|
$setValue = self::$fakerGenerator->dateTime; |
159
|
|
|
break; |
160
|
|
|
case 'DateTimeImmutable': |
161
|
|
|
$setValue = new \DateTimeImmutable( |
162
|
|
|
self::$fakerGenerator->dateTime->format('Y-m-d') |
163
|
|
|
); |
164
|
|
|
break; |
165
|
|
|
default: |
166
|
|
|
throw new \RuntimeException('Failed getting a data provider for the property type ' . $setParamType); |
167
|
|
|
} |
168
|
|
|
$entity->$setter($setValue); |
169
|
|
|
|
170
|
|
|
return $setValue; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
protected function getFakerDataProvider(): ?FakerDataProviderInterface |
174
|
|
|
{ |
175
|
|
|
if (isset(EntityTestInterface::FAKER_DATA_PROVIDERS[static::TEST_FIELD_PROP])) { |
176
|
|
|
$provider = EntityTestInterface::FAKER_DATA_PROVIDERS[static::TEST_FIELD_PROP]; |
177
|
|
|
|
178
|
|
|
return new $provider(self::$fakerGenerator); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return null; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @test |
186
|
|
|
* @large |
187
|
|
|
* @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
188
|
|
|
* @throws \ReflectionException |
189
|
|
|
* @coversNothing |
190
|
|
|
*/ |
191
|
|
|
public function createDatabaseSchema() |
192
|
|
|
{ |
193
|
|
|
$this->setupCopiedWorkDirAndCreateDatabase(); |
194
|
|
|
$entityManager = $this->getEntityManager(); |
195
|
|
|
$entityFqn = $this->getCopiedFqn(static::TEST_ENTITY_FQN_BASE . $this->entitySuffix); |
196
|
|
|
$entity = new $entityFqn($this->container->get(EntityValidatorFactory::class)); |
197
|
|
|
$setValue = null; |
198
|
|
|
if (false !== static::HAS_SETTER) { |
|
|
|
|
199
|
|
|
$setValue = $this->setFakerValueForProperty($entity); |
200
|
|
|
} |
201
|
|
|
$saver = $this->container->get(EntitySaver::class); |
202
|
|
|
$saver->save($entity); |
203
|
|
|
$repository = $entityManager->getRepository($entityFqn); |
204
|
|
|
$entities = $repository->findAll(); |
205
|
|
|
$savedEntity = current($entities); |
206
|
|
|
$getter = $this->getGetter($entity); |
207
|
|
|
$gotValue = $savedEntity->$getter(); |
208
|
|
|
if (false !== static::HAS_SETTER) { |
|
|
|
|
209
|
|
|
self::assertEquals($setValue, $gotValue); |
210
|
|
|
|
211
|
|
|
return; |
212
|
|
|
} |
213
|
|
|
self::assertNotNull($gotValue); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|