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