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