1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Tests\Large\CodeGeneration\PostProcessor; |
4
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\PostProcessor\FileOverrider; |
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Tests\Assets\AbstractTest; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @covers \EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\PostProcessor\FileOverrider |
10
|
|
|
*/ |
11
|
|
|
class FileOverriderTest extends AbstractTest |
12
|
|
|
{ |
13
|
|
|
public const WORK_DIR = AbstractTest::VAR_PATH . '/' . self::TEST_TYPE_LARGE . '/FileOverriderTest'; |
14
|
|
|
|
15
|
|
|
public const TEST_FILE = self::WORK_DIR . '/src/Entity/Factories/Another/Deeply/Nested/ClientFactory.php'; |
16
|
|
|
|
17
|
|
|
protected static $buildOnce = true; |
18
|
|
|
/** |
19
|
|
|
* @var FileOverrider |
20
|
|
|
*/ |
21
|
|
|
private $overrider; |
22
|
|
|
|
23
|
|
|
public function setup() |
24
|
|
|
{ |
25
|
|
|
parent::setUp(); |
26
|
|
|
if (false === self::$built) { |
27
|
|
|
$this->getTestCodeGenerator() |
28
|
|
|
->copyTo(self::WORK_DIR); |
29
|
|
|
mkdir(self::WORK_DIR . '/' . FileOverrider::OVERRIDES_PATH, 0777, true); |
30
|
|
|
self::$built = true; |
31
|
|
|
} |
32
|
|
|
$this->overrider = new FileOverrider(self::WORK_DIR); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @test |
37
|
|
|
* @large |
38
|
|
|
*/ |
39
|
|
|
public function itCanCreateANewOverrideFile(): string |
40
|
|
|
{ |
41
|
|
|
$pathToFileInProject = self::TEST_FILE; |
42
|
|
|
$overridePath = $this->overrider->createNewOverride($pathToFileInProject); |
43
|
|
|
self::assertFileEquals($pathToFileInProject, $overridePath); |
44
|
|
|
$expectedOVerridePathDir = realpath( |
45
|
|
|
self::WORK_DIR . '/' . FileOverrider::OVERRIDES_PATH . '/src/Entity/Factories/Another/Deeply/Nested/' |
46
|
|
|
); |
47
|
|
|
self::assertSame($expectedOVerridePathDir, \dirname($overridePath)); |
48
|
|
|
|
49
|
|
|
return $overridePath; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @test |
54
|
|
|
* @large |
55
|
|
|
* @depends itCanCreateANewOverrideFile |
56
|
|
|
* |
57
|
|
|
* @param string $overridePath |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public function updatedOverrideCanBeApplied(string $overridePath): string |
62
|
|
|
{ |
63
|
|
|
$updatedContents = <<<'PHP' |
64
|
|
|
<?php declare(strict_types=1); |
65
|
|
|
|
66
|
|
|
namespace My\Test\Project\Entity\Factories\Another\Deeply\Nested; |
67
|
|
|
// phpcs:disable -- line length |
68
|
|
|
use My\Test\Project\Entity\Factories\AbstractEntityFactory; |
69
|
|
|
use My\Test\Project\Entities\Another\Deeply\Nested\Client; |
70
|
|
|
use My\Test\Project\Entity\Interfaces\Another\Deeply\Nested\ClientInterface; |
71
|
|
|
// phpcs: enable |
72
|
|
|
class ClientFactory extends AbstractEntityFactory |
73
|
|
|
{ |
74
|
|
|
public function create(array $values = []): ClientInterface |
75
|
|
|
{ |
76
|
|
|
$client=new Client(); |
77
|
|
|
$this->entityFactory->doStuff($client); |
78
|
|
|
return $client; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
PHP; |
82
|
|
|
\ts\file_put_contents($overridePath, $updatedContents); |
83
|
|
|
$this->overrider->applyOverrides(); |
84
|
|
|
self::assertSame($updatedContents, \ts\file_get_contents(self::TEST_FILE)); |
85
|
|
|
|
86
|
|
|
return $overridePath; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @test |
92
|
|
|
* @large |
93
|
|
|
* @depends updatedOverrideCanBeApplied |
94
|
|
|
* |
95
|
|
|
* @param string $overridePath |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
public function updatedProjectFileCanBeSetToOverrides(string $overridePath): string |
100
|
|
|
{ |
101
|
|
|
$updatedContents = <<<'PHP' |
102
|
|
|
<?php declare(strict_types=1); |
103
|
|
|
|
104
|
|
|
namespace My\Test\Project\Entity\Factories\Another\Deeply\Nested; |
105
|
|
|
// phpcs:disable -- line length |
106
|
|
|
use My\Test\Project\Entity\Factories\AbstractEntityFactory; |
107
|
|
|
use My\Test\Project\Entities\Another\Deeply\Nested\Client; |
108
|
|
|
use My\Test\Project\Entity\Interfaces\Another\Deeply\Nested\ClientInterface; |
109
|
|
|
// phpcs: enable |
110
|
|
|
class ClientFactory extends AbstractEntityFactory |
111
|
|
|
{ |
112
|
|
|
public function create(array $values = []): ClientInterface |
113
|
|
|
{ |
114
|
|
|
$client=new Client(); |
115
|
|
|
$this->entityFactory->doStuff($client); |
116
|
|
|
$this->entityFactory->doMoreStuff($client); |
117
|
|
|
return $client; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
PHP; |
121
|
|
|
\ts\file_put_contents(self::TEST_FILE, $updatedContents); |
122
|
|
|
$this->overrider->updateOverrideFiles(); |
123
|
|
|
self::assertSame($updatedContents, \ts\file_get_contents($overridePath)); |
124
|
|
|
|
125
|
|
|
return $overridePath; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @test |
130
|
|
|
* @large |
131
|
|
|
* @depends updatedProjectFileCanBeSetToOverrides |
132
|
|
|
*/ |
133
|
|
|
public function itPreventsYouFromCreatingDuplicateOverrides(): void |
134
|
|
|
{ |
135
|
|
|
$this->expectException(\RuntimeException::class); |
136
|
|
|
$this->expectExceptionMessage( |
137
|
|
|
'Override already exists for path /src/Entity/Factories/Another/Deeply/Nested/ClientFactory.php' |
138
|
|
|
); |
139
|
|
|
$this->overrider->createNewOverride(self::TEST_FILE); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @test |
144
|
|
|
* @large |
145
|
|
|
* @depends updatedProjectFileCanBeSetToOverrides |
146
|
|
|
*/ |
147
|
|
|
public function overridesCanNotBeAppliedIfTheProjectFileHashDoesNotMatch(): void |
148
|
|
|
{ |
149
|
|
|
$updatedContents = <<<'PHP' |
150
|
|
|
<?php declare(strict_types=1); |
151
|
|
|
|
152
|
|
|
namespace My\Test\Project\Entity\Factories\Another\Deeply\Nested; |
153
|
|
|
// phpcs:disable -- line length |
154
|
|
|
use My\Test\Project\Entity\Factories\AbstractEntityFactory; |
155
|
|
|
use My\Test\Project\Entities\Another\Deeply\Nested\Client; |
156
|
|
|
use My\Test\Project\Entity\Interfaces\Another\Deeply\Nested\ClientInterface; |
157
|
|
|
// phpcs: enable |
158
|
|
|
class ClientFactory extends AbstractEntityFactory |
159
|
|
|
{ |
160
|
|
|
private function somethingNewlyGenerated(){ |
161
|
|
|
return 'this represents something new in the generated code that will mean the hash wont work'; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function create(array $values = []): ClientInterface |
165
|
|
|
{ |
166
|
|
|
$client=new Client(); |
167
|
|
|
return $client; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
PHP; |
171
|
|
|
\ts\file_put_contents(self::TEST_FILE, $updatedContents); |
172
|
|
|
$this->expectException(\RuntimeException::class); |
173
|
|
|
$this->expectExceptionMessageRegExp( |
174
|
|
|
'%These file hashes were not up to date:Array.+?' |
175
|
|
|
. '/src/Entity/Factories/Another/Deeply/Nested/ClientFactory.5aefa85525b1fef70adc71cb80a931f1.php\] => ' |
176
|
|
|
. '7c53b164b4c17990ef697a38cd9dabbb%s' |
177
|
|
|
); |
178
|
|
|
$this->overrider->applyOverrides(); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|