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