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