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