|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Field; |
|
6
|
|
|
|
|
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\CodeHelper; |
|
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
|
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\PostProcessorInterface; |
|
10
|
|
|
use gossi\codegen\model\PhpClass; |
|
11
|
|
|
use gossi\codegen\model\PhpConstant; |
|
12
|
|
|
use InvalidArgumentException; |
|
13
|
|
|
|
|
14
|
|
|
use function str_replace; |
|
15
|
|
|
use function substr; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class AbstractTestFakerDataProviderUpdater |
|
19
|
|
|
* |
|
20
|
|
|
* @package EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Field |
|
21
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
22
|
|
|
*/ |
|
23
|
|
|
class AbstractTestFakerDataProviderUpdater |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var NamespaceHelper |
|
27
|
|
|
*/ |
|
28
|
|
|
private $namespaceHelper; |
|
29
|
|
|
/** |
|
30
|
|
|
* @var CodeHelper |
|
31
|
|
|
*/ |
|
32
|
|
|
private $codeHelper; |
|
33
|
|
|
/** |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
private $entityFqn; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
private $projectRootPath; |
|
42
|
|
|
/** |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
private $fakerFqn; |
|
46
|
|
|
/** |
|
47
|
|
|
* @var string |
|
48
|
|
|
*/ |
|
49
|
|
|
private $interfaceFqn; |
|
50
|
|
|
/** |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
private $abstractTestPath; |
|
54
|
|
|
/** |
|
55
|
|
|
* @var string |
|
56
|
|
|
*/ |
|
57
|
|
|
private $newPropertyConst; |
|
58
|
|
|
|
|
59
|
|
|
public function __construct(NamespaceHelper $namespaceHelper, CodeHelper $codeHelper) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->namespaceHelper = $namespaceHelper; |
|
62
|
|
|
$this->codeHelper = $codeHelper; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function updateFakerProviderArrayWithFieldFakerData( |
|
66
|
|
|
string $projectRootPath, |
|
67
|
|
|
string $fieldFqn, |
|
68
|
|
|
string $entityFqn |
|
69
|
|
|
): void { |
|
70
|
|
|
$this->projectRootPath = $projectRootPath; |
|
71
|
|
|
$fieldFqnBase = str_replace('FieldTrait', '', $fieldFqn); |
|
72
|
|
|
$this->entityFqn = $entityFqn; |
|
73
|
|
|
$this->fakerFqn = $this->namespaceHelper->tidy( |
|
74
|
|
|
str_replace('\\Traits\\', '\\FakerData\\', $fieldFqnBase) |
|
75
|
|
|
) . 'FakerData'; |
|
76
|
|
|
$this->interfaceFqn = $this->namespaceHelper->tidy( |
|
77
|
|
|
str_replace( |
|
78
|
|
|
'\\Traits\\', |
|
79
|
|
|
'\\Interfaces\\', |
|
80
|
|
|
$fieldFqnBase |
|
81
|
|
|
) . 'FieldInterface' |
|
82
|
|
|
); |
|
83
|
|
|
$this->abstractTestPath = $this->projectRootPath . '/tests/Entities/AbstractEntityTest.php'; |
|
84
|
|
|
$test = PhpClass::fromFile($this->abstractTestPath); |
|
85
|
|
|
$this->newPropertyConst = 'PROP_' . $this->codeHelper->consty($this->namespaceHelper->basename($fieldFqnBase)); |
|
86
|
|
|
try { |
|
87
|
|
|
$constant = $this->updateExisting($test); |
|
88
|
|
|
} catch (InvalidArgumentException $e) { |
|
89
|
|
|
$constant = $this->createNew(); |
|
90
|
|
|
} |
|
91
|
|
|
$test->setConstant($constant); |
|
92
|
|
|
$this->codeHelper->generate( |
|
93
|
|
|
$test, |
|
94
|
|
|
$this->abstractTestPath, |
|
95
|
|
|
new class implements PostProcessorInterface |
|
96
|
|
|
{ |
|
97
|
|
|
public function __invoke(string $generated): string |
|
98
|
|
|
{ |
|
99
|
|
|
return str_replace('// phpcs:enable', '', $generated); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
private function updateExisting(PhpClass $test): PhpConstant |
|
106
|
|
|
{ |
|
107
|
|
|
$constant = $test->getConstant('FAKER_DATA_PROVIDERS'); |
|
108
|
|
|
$test->removeConstant($constant); |
|
109
|
|
|
$expression = $constant->getExpression(); |
|
110
|
|
|
$expression = str_replace( |
|
111
|
|
|
']', |
|
112
|
|
|
",{$this->getLine()}]", |
|
113
|
|
|
$expression |
|
114
|
|
|
); |
|
115
|
|
|
$constant->setExpression($expression); |
|
116
|
|
|
|
|
117
|
|
|
return $constant; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Get the line that we are going to add to the array |
|
122
|
|
|
* |
|
123
|
|
|
* @return string |
|
124
|
|
|
*/ |
|
125
|
|
|
private function getLine(): string |
|
126
|
|
|
{ |
|
127
|
|
|
return "\n'$this->entityFqn-'.\\$this->interfaceFqn::$this->newPropertyConst => \\$this->fakerFqn::class\n"; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
private function createNew(): PhpConstant |
|
131
|
|
|
{ |
|
132
|
|
|
return new PhpConstant( |
|
133
|
|
|
'FAKER_DATA_PROVIDERS', |
|
134
|
|
|
"[\n{$this->getLine()}]", |
|
135
|
|
|
true |
|
136
|
|
|
); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public function updateFakerProviderArrayWithEmbeddableFakerData( |
|
140
|
|
|
string $projectRootPath, |
|
141
|
|
|
string $embeddableFqn, |
|
142
|
|
|
string $entityFqn |
|
143
|
|
|
): void { |
|
144
|
|
|
$this->projectRootPath = $projectRootPath; |
|
145
|
|
|
$this->fakerFqn = str_replace( |
|
146
|
|
|
['\\Traits\\', '\\Has', 'EmbeddableTrait'], |
|
147
|
|
|
['\\FakerData\\', '\\', 'EmbeddableFakerData'], |
|
148
|
|
|
$embeddableFqn |
|
149
|
|
|
); |
|
150
|
|
|
$this->entityFqn = $entityFqn; |
|
151
|
|
|
$this->interfaceFqn = $this->namespaceHelper->tidy( |
|
152
|
|
|
str_replace( |
|
153
|
|
|
'\\Traits\\', |
|
154
|
|
|
'\\Interfaces\\', |
|
155
|
|
|
str_replace('EmbeddableTrait', 'EmbeddableInterface', $embeddableFqn) |
|
156
|
|
|
) |
|
157
|
|
|
); |
|
158
|
|
|
$this->abstractTestPath = $this->projectRootPath . '/tests/Entities/AbstractEntityTest.php'; |
|
159
|
|
|
$test = PhpClass::fromFile($this->abstractTestPath); |
|
160
|
|
|
$this->newPropertyConst = 'PROP_' . $this->codeHelper->consty( |
|
161
|
|
|
substr($this->namespaceHelper->basename($embeddableFqn), 3, -5) |
|
162
|
|
|
); |
|
163
|
|
|
try { |
|
164
|
|
|
$constant = $this->updateExisting($test); |
|
165
|
|
|
} catch (InvalidArgumentException $e) { |
|
166
|
|
|
$constant = $this->createNew(); |
|
167
|
|
|
} |
|
168
|
|
|
$test->setConstant($constant); |
|
169
|
|
|
$this->codeHelper->generate( |
|
170
|
|
|
$test, |
|
171
|
|
|
$this->abstractTestPath, |
|
172
|
|
|
new class implements PostProcessorInterface |
|
173
|
|
|
{ |
|
174
|
|
|
public function __invoke(string $generated): string |
|
175
|
|
|
{ |
|
176
|
|
|
return str_replace('// phpcs:enable', '', $generated); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
); |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|