1 | <?php |
||
14 | trait MatchesSnapshots |
||
15 | { |
||
16 | protected int $snapshotIncrementor = 0; |
||
|
|||
17 | |||
18 | protected array $snapshotChanges = []; |
||
19 | |||
20 | /** @before */ |
||
21 | public function setUpSnapshotIncrementor() |
||
22 | { |
||
23 | $this->snapshotIncrementor = 0; |
||
24 | } |
||
25 | |||
26 | /** @after */ |
||
27 | public function markTestIncompleteIfSnapshotsHaveChanged() |
||
28 | { |
||
29 | if (empty($this->snapshotChanges)) { |
||
30 | return; |
||
31 | } |
||
32 | |||
33 | if (count($this->snapshotChanges) === 1) { |
||
34 | $this->markTestIncomplete($this->snapshotChanges[0]); |
||
35 | |||
36 | return; |
||
37 | } |
||
38 | |||
39 | $formattedMessages = implode(PHP_EOL, array_map(fn(string $message) => "- {$message}", $this->snapshotChanges)); |
||
40 | |||
41 | $this->markTestIncomplete($formattedMessages); |
||
42 | } |
||
43 | |||
44 | public function assertMatchesSnapshot($actual, Driver $driver = null) |
||
45 | { |
||
46 | if (is_null($driver) && is_array($actual)) { |
||
47 | $this->assertMatchesYamlSnapshot($actual); |
||
48 | |||
49 | return; |
||
50 | } |
||
51 | |||
52 | $this->doSnapshotAssertion($actual, $driver ?? new VarDriver()); |
||
53 | } |
||
54 | |||
55 | public function assertMatchesHtmlSnapshot($actual) |
||
56 | { |
||
57 | $this->assertMatchesSnapshot($actual, new HtmlDriver()); |
||
58 | } |
||
59 | |||
60 | public function assertMatchesXmlSnapshot($actual) |
||
61 | { |
||
62 | $this->assertMatchesSnapshot($actual, new XmlDriver()); |
||
63 | } |
||
64 | |||
65 | public function assertMatchesJsonSnapshot($actual) |
||
66 | { |
||
67 | $this->assertMatchesSnapshot($actual, new JsonDriver()); |
||
68 | } |
||
69 | |||
70 | public function assertMatchesYamlSnapshot($actual) |
||
71 | { |
||
72 | $this->assertMatchesSnapshot($actual, new YamlDriver()); |
||
73 | } |
||
74 | |||
75 | public function assertMatchesFileHashSnapshot($filePath) |
||
76 | { |
||
77 | if (! file_exists($filePath)) { |
||
78 | $this->fail('File does not exist'); |
||
79 | } |
||
80 | |||
81 | $actual = sha1_file($filePath); |
||
82 | |||
83 | $this->assertMatchesSnapshot($actual); |
||
84 | } |
||
85 | |||
86 | public function assertMatchesFileSnapshot($file) |
||
87 | { |
||
88 | $this->doFileSnapshotAssertion($file); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Determines the snapshot's id. By default, the test case's class and |
||
93 | * method names are used. |
||
94 | * |
||
95 | * @return string |
||
96 | */ |
||
97 | protected function getSnapshotId(): string |
||
98 | { |
||
99 | return (new ReflectionClass($this))->getShortName().'__'. |
||
100 | $this->getName().'__'. |
||
101 | $this->snapshotIncrementor; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Determines the directory where snapshots are stored. By default a |
||
106 | * `__snapshots__` directory is created at the same level as the test |
||
107 | * class. |
||
108 | * |
||
109 | * @return string |
||
110 | */ |
||
111 | protected function getSnapshotDirectory(): string |
||
112 | { |
||
113 | return dirname((new ReflectionClass($this))->getFileName()). |
||
114 | DIRECTORY_SEPARATOR. |
||
115 | '__snapshots__'; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Determines the directory where file snapshots are stored. By default a |
||
120 | * `__snapshots__/files` directory is created at the same level as the |
||
121 | * test class. |
||
122 | * |
||
123 | * @return string |
||
124 | */ |
||
125 | protected function getFileSnapshotDirectory(): string |
||
126 | { |
||
127 | return $this->getSnapshotDirectory(). |
||
128 | DIRECTORY_SEPARATOR. |
||
129 | 'files'; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Determines whether or not the snapshot should be updated instead of |
||
134 | * matched. |
||
135 | * |
||
136 | * Override this method it you want to use a different flag or mechanism |
||
137 | * than `-d --update-snapshots`. |
||
138 | * |
||
139 | * @return bool |
||
140 | */ |
||
141 | protected function shouldUpdateSnapshots(): bool |
||
142 | { |
||
143 | return in_array('--update-snapshots', $_SERVER['argv'], true); |
||
144 | } |
||
145 | |||
146 | protected function doSnapshotAssertion($actual, Driver $driver) |
||
147 | { |
||
148 | $this->snapshotIncrementor++; |
||
149 | |||
150 | $snapshot = Snapshot::forTestCase( |
||
151 | $this->getSnapshotId(), |
||
152 | $this->getSnapshotDirectory(), |
||
153 | $driver |
||
154 | ); |
||
155 | |||
156 | if (! $snapshot->exists()) { |
||
157 | $this->createSnapshotAndMarkTestIncomplete($snapshot, $actual); |
||
158 | } |
||
159 | |||
160 | if ($this->shouldUpdateSnapshots()) { |
||
161 | try { |
||
162 | // We only want to update snapshots which need updating. If the snapshot doesn't |
||
163 | // match the expected output, we'll catch the failure, create a new snapshot and |
||
164 | // mark the test as incomplete. |
||
165 | $snapshot->assertMatches($actual); |
||
166 | } catch (ExpectationFailedException $exception) { |
||
167 | $this->updateSnapshotAndMarkTestIncomplete($snapshot, $actual); |
||
168 | } |
||
169 | } |
||
170 | |||
171 | try { |
||
172 | $snapshot->assertMatches($actual); |
||
173 | } catch (ExpectationFailedException $exception) { |
||
174 | $this->rethrowExpectationFailedExceptionWithUpdateSnapshotsPrompt($exception); |
||
175 | } |
||
176 | } |
||
177 | |||
178 | protected function doFileSnapshotAssertion(string $filePath) |
||
179 | { |
||
180 | if (! file_exists($filePath)) { |
||
181 | $this->fail('File does not exist'); |
||
182 | } |
||
183 | |||
184 | $fileExtension = pathinfo($filePath, PATHINFO_EXTENSION); |
||
185 | |||
186 | if (empty($fileExtension)) { |
||
187 | $this->fail("Unable to make a file snapshot, file does not have a file extension ({$filePath})"); |
||
188 | } |
||
189 | |||
190 | $fileSystem = Filesystem::inDirectory($this->getFileSnapshotDirectory()); |
||
191 | |||
192 | $this->snapshotIncrementor++; |
||
193 | |||
194 | $snapshotId = $this->getSnapshotId().'.'.$fileExtension; |
||
195 | |||
196 | // If $filePath has a different file extension than the snapshot, the test should fail |
||
197 | if ($namesWithDifferentExtension = $fileSystem->getNamesWithDifferentExtension($snapshotId)) { |
||
198 | // There is always only one existing snapshot with a different extension |
||
199 | $existingSnapshotId = $namesWithDifferentExtension[0]; |
||
200 | |||
201 | if ($this->shouldUpdateSnapshots()) { |
||
202 | $fileSystem->delete($existingSnapshotId); |
||
203 | |||
204 | $fileSystem->copy($filePath, $snapshotId); |
||
205 | |||
206 | $this->registerSnapshotChange("File snapshot updated for {$snapshotId}"); |
||
207 | |||
208 | return; |
||
209 | } |
||
210 | |||
211 | $expectedExtension = pathinfo($existingSnapshotId, PATHINFO_EXTENSION); |
||
212 | |||
213 | return $this->fail("File did not match the snapshot file extension (expected: {$expectedExtension}, was: {$fileExtension})"); |
||
214 | } |
||
215 | |||
216 | $failedSnapshotId = $snapshotId.'_failed.'.$fileExtension; |
||
217 | |||
218 | if ($fileSystem->has($failedSnapshotId)) { |
||
219 | $fileSystem->delete($failedSnapshotId); |
||
220 | } |
||
221 | |||
222 | if (! $fileSystem->has($snapshotId)) { |
||
223 | $fileSystem->copy($filePath, $snapshotId); |
||
224 | |||
225 | $this->registerSnapshotChange("File snapshot created for {$snapshotId}"); |
||
226 | |||
227 | return; |
||
228 | } |
||
229 | |||
230 | if (! $fileSystem->fileEquals($filePath, $snapshotId)) { |
||
231 | if ($this->shouldUpdateSnapshots()) { |
||
232 | $fileSystem->copy($filePath, $snapshotId); |
||
233 | |||
234 | $this->registerSnapshotChange("File snapshot updated for {$snapshotId}"); |
||
235 | |||
236 | return; |
||
237 | } |
||
238 | |||
239 | $fileSystem->copy($filePath, $failedSnapshotId); |
||
240 | |||
241 | $this->fail("File did not match snapshot ({$snapshotId})"); |
||
242 | } |
||
243 | |||
244 | $this->assertTrue(true); |
||
245 | } |
||
246 | |||
247 | protected function createSnapshotAndMarkTestIncomplete(Snapshot $snapshot, $actual) |
||
248 | { |
||
249 | $snapshot->create($actual); |
||
250 | |||
251 | $this->registerSnapshotChange("Snapshot created for {$snapshot->id()}"); |
||
252 | } |
||
253 | |||
254 | protected function updateSnapshotAndMarkTestIncomplete(Snapshot $snapshot, $actual) |
||
255 | { |
||
256 | $snapshot->create($actual); |
||
257 | |||
258 | $this->registerSnapshotChange("Snapshot updated for {$snapshot->id()}"); |
||
259 | } |
||
260 | |||
261 | protected function rethrowExpectationFailedExceptionWithUpdateSnapshotsPrompt($exception) |
||
262 | { |
||
263 | $newMessage = $exception->getMessage()."\n\n". |
||
264 | 'Snapshots can be updated by passing '. |
||
265 | '`-d --update-snapshots` through PHPUnit\'s CLI arguments.'; |
||
266 | |||
267 | $exceptionReflection = new ReflectionObject($exception); |
||
268 | |||
269 | $messageReflection = $exceptionReflection->getProperty('message'); |
||
270 | $messageReflection->setAccessible(true); |
||
271 | $messageReflection->setValue($exception, $newMessage); |
||
272 | |||
273 | throw $exception; |
||
274 | } |
||
275 | |||
276 | protected function registerSnapshotChange(string $message) |
||
277 | { |
||
278 | $this->snapshotChanges[] = $message; |
||
279 | } |
||
280 | } |
||
281 |