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