|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Tests\Large\Entity\Factory; |
|
4
|
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
|
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Factory\EntityDependencyInjector; |
|
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Validation\EntityValidator; |
|
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Tests\Assets\AbstractTest; |
|
9
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
10
|
|
|
|
|
11
|
|
|
class EntityDependencyInjectorTest extends AbstractTest |
|
12
|
|
|
{ |
|
13
|
|
|
public const WORK_DIR = AbstractTest::VAR_PATH . '/' . self::TEST_TYPE_LARGE . '/EntityDependencyInjectorTest/'; |
|
14
|
|
|
|
|
15
|
|
|
private const TEST_ENTITY_FILE = '/src/Entities/Order.php'; |
|
16
|
|
|
private const TEST_ENTITY_FQN = self::TEST_PROJECT_ROOT_NAMESPACE . '\\Entities\\Order'; |
|
17
|
|
|
|
|
18
|
|
|
protected static $buildOnce = true; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var EntityDependencyInjector |
|
22
|
|
|
*/ |
|
23
|
|
|
private $injector; |
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
private $entityFqn; |
|
28
|
|
|
|
|
29
|
|
|
public function setup() |
|
30
|
|
|
{ |
|
31
|
|
|
parent::setUp(); |
|
32
|
|
|
if (false === self::$built) { |
|
33
|
|
|
$this->getTestCodeGenerator() |
|
34
|
|
|
->copyTo(self::WORK_DIR); |
|
35
|
|
|
$this->overrideOrderEntity(); |
|
36
|
|
|
self::$built = true; |
|
37
|
|
|
} |
|
38
|
|
|
$this->setupCopiedWorkDir(); |
|
39
|
|
|
$this->entityFqn = $this->getCopiedFqn(self::TEST_ENTITY_FQN); |
|
40
|
|
|
$this->injector = new EntityDependencyInjector($this->container); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
private function overrideOrderEntity() |
|
44
|
|
|
{ |
|
45
|
|
|
\ts\file_put_contents( |
|
46
|
|
|
self::WORK_DIR . self::TEST_ENTITY_FILE, |
|
47
|
|
|
/** @lang PHP */ |
|
48
|
|
|
<<<'PHP' |
|
49
|
|
|
<?php declare(strict_types=1); |
|
50
|
|
|
|
|
51
|
|
|
namespace My\Test\Project\Entities; |
|
52
|
|
|
// phpcs:disable Generic.Files.LineLength.TooLong |
|
53
|
|
|
|
|
54
|
|
|
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; |
|
55
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
|
56
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity as DSM; |
|
57
|
|
|
use My\Test\Project\Entity\Fields\Traits\BooleanFieldTrait; |
|
58
|
|
|
use My\Test\Project\Entity\Fields\Traits\DatetimeFieldTrait; |
|
59
|
|
|
use My\Test\Project\Entity\Fields\Traits\DecimalFieldTrait; |
|
60
|
|
|
use My\Test\Project\Entity\Fields\Traits\FloatFieldTrait; |
|
61
|
|
|
use My\Test\Project\Entity\Fields\Traits\IntegerFieldTrait; |
|
62
|
|
|
use My\Test\Project\Entity\Fields\Traits\JsonFieldTrait; |
|
63
|
|
|
use My\Test\Project\Entity\Fields\Traits\StringFieldTrait; |
|
64
|
|
|
use My\Test\Project\Entity\Fields\Traits\TextFieldTrait; |
|
65
|
|
|
use My\Test\Project\Entity\Interfaces\OrderInterface; |
|
66
|
|
|
use My\Test\Project\Entity\Relations\Order\Address\Traits\HasOrderAddresses\HasOrderAddressesOneToMany; |
|
67
|
|
|
use My\Test\Project\Entity\Relations\Person\Traits\HasPerson\HasPersonManyToOne; |
|
68
|
|
|
use My\Test\Project\Entity\Repositories\OrderRepository;use Symfony\Component\Filesystem\Filesystem; |
|
69
|
|
|
|
|
70
|
|
|
// phpcs:enable |
|
71
|
|
|
class Order implements |
|
72
|
|
|
OrderInterface |
|
73
|
|
|
{ |
|
74
|
|
|
|
|
75
|
|
|
use DSM\Traits\UsesPHPMetaDataTrait; |
|
76
|
|
|
use DSM\Traits\ValidatedEntityTrait; |
|
77
|
|
|
use DSM\Traits\ImplementNotifyChangeTrackingPolicy; |
|
78
|
|
|
use DSM\Fields\Traits\PrimaryKey\UuidFieldTrait; |
|
79
|
|
|
use StringFieldTrait; |
|
80
|
|
|
use DatetimeFieldTrait; |
|
81
|
|
|
use FloatFieldTrait; |
|
82
|
|
|
use DecimalFieldTrait; |
|
83
|
|
|
use IntegerFieldTrait; |
|
84
|
|
|
use TextFieldTrait; |
|
85
|
|
|
use BooleanFieldTrait; |
|
86
|
|
|
use JsonFieldTrait; |
|
87
|
|
|
use HasPersonManyToOne; |
|
88
|
|
|
use HasOrderAddressesOneToMany; |
|
89
|
|
|
|
|
90
|
|
|
private $filesystem; |
|
91
|
|
|
private static $namespaceHelper; |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* This is called in UsesPHPMetaDataTrait::loadClassDoctrineMetaData |
|
95
|
|
|
* |
|
96
|
|
|
* @SuppressWarnings(PHPMD.UnusedPrivateMethod) |
|
97
|
|
|
* @param ClassMetadataBuilder $builder |
|
98
|
|
|
*/ |
|
99
|
|
|
private static function setCustomRepositoryClass(ClassMetadataBuilder $builder) { |
|
100
|
|
|
$builder->setCustomRepositoryClass(OrderRepository::class); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function __construct() { |
|
104
|
|
|
$this->runInitMethods(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function injectFilesystem(Filesystem $filesystem){ |
|
108
|
|
|
$this->filesystem=$filesystem; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public static function injectNamespaceHelper(NamespaceHelper $namespaceHelper){ |
|
112
|
|
|
self::$namespaceHelper=$namespaceHelper; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function getFilesystem():Filesystem{ |
|
116
|
|
|
return $this->filesystem; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public static function getNamespaceHelper(): NamespaceHelper{ |
|
120
|
|
|
return self::$namespaceHelper; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function getValidator():DSM\Validation\EntityValidator{ |
|
124
|
|
|
return $this->validator; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
PHP |
|
128
|
|
|
); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @test |
|
133
|
|
|
* @large |
|
134
|
|
|
*/ |
|
135
|
|
|
public function itCanInjectDependencies() |
|
136
|
|
|
{ |
|
137
|
|
|
$entity = $this->createOrderEntity(); |
|
138
|
|
|
$this->injector->injectEntityDependencies($entity); |
|
139
|
|
|
self::assertInstanceOf(EntityValidator::class, $entity->getValidator()); |
|
140
|
|
|
self::assertInstanceOf(Filesystem::class, $entity->getFilesystem()); |
|
141
|
|
|
self::assertInstanceOf(NamespaceHelper::class, $entity::getNamespaceHelper()); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
private function createOrderEntity() |
|
145
|
|
|
{ |
|
146
|
|
|
$entityFqn = $this->entityFqn; |
|
147
|
|
|
|
|
148
|
|
|
return new $entityFqn(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @test |
|
153
|
|
|
* @large |
|
154
|
|
|
*/ |
|
155
|
|
|
public function itThrowsAnExceptionIfAnInjectMethodDoesNotHaveOnlyOneParam() |
|
156
|
|
|
{ |
|
157
|
|
|
\ts\file_put_contents( |
|
158
|
|
|
$this->copiedWorkDir . self::TEST_ENTITY_FILE, |
|
159
|
|
|
\str_replace( |
|
160
|
|
|
'public static function injectNamespaceHelper(NamespaceHelper $namespaceHelper){', |
|
161
|
|
|
'public static function injectNamespaceHelper(NamespaceHelper $namespaceHelper, bool $thing){', |
|
162
|
|
|
\ts\file_get_contents($this->copiedWorkDir . self::TEST_ENTITY_FILE) |
|
163
|
|
|
) |
|
164
|
|
|
); |
|
165
|
|
|
$entity = $this->createOrderEntity(); |
|
166
|
|
|
$this->expectException(\RuntimeException::class); |
|
167
|
|
|
$this->expectExceptionMessage( |
|
168
|
|
|
'Invalid method signature for injectNamespaceHelper, ' . |
|
169
|
|
|
'should only take one argument which is the dependency to be injected' |
|
170
|
|
|
); |
|
171
|
|
|
$this->injector->injectEntityDependencies($entity); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @test |
|
176
|
|
|
* @large |
|
177
|
|
|
*/ |
|
178
|
|
|
public function itThrowsAnExceptionIfAnInjectMethodHasNoTypeHint() |
|
179
|
|
|
{ |
|
180
|
|
|
\ts\file_put_contents( |
|
181
|
|
|
$this->copiedWorkDir . self::TEST_ENTITY_FILE, |
|
182
|
|
|
\str_replace( |
|
183
|
|
|
'public static function injectNamespaceHelper(NamespaceHelper $namespaceHelper){', |
|
184
|
|
|
'public static function injectNamespaceHelper($namespaceHelper){', |
|
185
|
|
|
\ts\file_get_contents($this->copiedWorkDir . self::TEST_ENTITY_FILE) |
|
186
|
|
|
) |
|
187
|
|
|
); |
|
188
|
|
|
$entity = $this->createOrderEntity(); |
|
189
|
|
|
$this->expectException(\RuntimeException::class); |
|
190
|
|
|
$this->expectExceptionMessage( |
|
191
|
|
|
'Invalid method signature for injectNamespaceHelper, the object being set must be type hinted' |
|
192
|
|
|
); |
|
193
|
|
|
$this->injector->injectEntityDependencies($entity); |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|