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