|
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\FileHashDriver; |
|
10
|
|
|
use Spatie\Snapshots\Drivers\JsonDriver; |
|
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 assertMatchesFileHashSnapshot($filePath) |
|
41
|
|
|
{ |
|
42
|
|
|
if (! file_exists($filePath)) { |
|
43
|
|
|
$this->fail('File does not exist'); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$actual = sha1_file($filePath); |
|
47
|
|
|
|
|
48
|
|
|
$this->assertMatchesSnapshot($actual, new FileHashDriver()); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Determines the snapshot's id. By default, the test case's class and |
|
53
|
|
|
* method names are used. |
|
54
|
|
|
* |
|
55
|
|
|
* @return string |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function getSnapshotId(): string |
|
58
|
|
|
{ |
|
59
|
|
|
return (new ReflectionClass($this))->getShortName().'__'. |
|
60
|
|
|
$this->getName().'__'. |
|
|
|
|
|
|
61
|
|
|
$this->snapshotIncrementor; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Determines the directory where snapshots are stored. By default a |
|
66
|
|
|
* `__snapshots__` directory is created at the same level as the test |
|
67
|
|
|
* class. |
|
68
|
|
|
* |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function getSnapshotDirectory(): string |
|
72
|
|
|
{ |
|
73
|
|
|
return dirname((new ReflectionClass($this))->getFileName()). |
|
74
|
|
|
DIRECTORY_SEPARATOR. |
|
75
|
|
|
'__snapshots__'; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Determines whether or not the snapshot should be updated instead of |
|
80
|
|
|
* matched. |
|
81
|
|
|
* |
|
82
|
|
|
* Override this method it you want to use a different flag or mechanism |
|
83
|
|
|
* than `-d --update-snapshots`. |
|
84
|
|
|
* |
|
85
|
|
|
* @return bool |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function shouldUpdateSnapshots(): bool |
|
88
|
|
|
{ |
|
89
|
|
|
return in_array('--update-snapshots', $_SERVER['argv'], true); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
protected function doSnapshotAssertion($actual, Driver $driver) |
|
93
|
|
|
{ |
|
94
|
|
|
$this->snapshotIncrementor++; |
|
95
|
|
|
|
|
96
|
|
|
$snapshot = Snapshot::forTestCase( |
|
97
|
|
|
$this->getSnapshotId(), |
|
98
|
|
|
$this->getSnapshotDirectory(), |
|
99
|
|
|
$driver |
|
100
|
|
|
); |
|
101
|
|
|
|
|
102
|
|
|
if (! $snapshot->exists()) { |
|
103
|
|
|
$this->createSnapshotAndMarkTestIncomplete($snapshot, $actual); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
if ($this->shouldUpdateSnapshots()) { |
|
107
|
|
|
try { |
|
108
|
|
|
// We only want to update snapshots which need updating. If the snapshot doesn't |
|
109
|
|
|
// match the expected output, we'll catch the failure, create a new snapshot and |
|
110
|
|
|
// mark the test as incomplete. |
|
111
|
|
|
$snapshot->assertMatches($actual); |
|
112
|
|
|
} catch (ExpectationFailedException $exception) { |
|
113
|
|
|
$this->updateSnapshotAndMarkTestIncomplete($snapshot, $actual); |
|
114
|
|
|
} catch (PHPUnit_Framework_ExpectationFailedException $exception) { |
|
|
|
|
|
|
115
|
|
|
$this->updateSnapshotAndMarkTestIncomplete($snapshot, $actual); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
try { |
|
120
|
|
|
$snapshot->assertMatches($actual); |
|
121
|
|
|
} catch (ExpectationFailedException $exception) { |
|
122
|
|
|
$this->rethrowExpectationFailedExceptionWithUpdateSnapshotsPrompt($exception); |
|
123
|
|
|
} catch (PHPUnit_Framework_ExpectationFailedException $exception) { |
|
|
|
|
|
|
124
|
|
|
$this->rethrowExpectationFailedExceptionWithUpdateSnapshotsPrompt($exception); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
protected function createSnapshotAndMarkTestIncomplete(Snapshot $snapshot, $actual) |
|
129
|
|
|
{ |
|
130
|
|
|
$snapshot->create($actual); |
|
131
|
|
|
|
|
132
|
|
|
$this->markTestIncomplete("Snapshot created for {$snapshot->id()}"); |
|
|
|
|
|
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
protected function updateSnapshotAndMarkTestIncomplete(Snapshot $snapshot, $actual) |
|
136
|
|
|
{ |
|
137
|
|
|
$snapshot->create($actual); |
|
138
|
|
|
|
|
139
|
|
|
$this->markTestIncomplete("Snapshot updated for {$snapshot->id()}"); |
|
|
|
|
|
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
protected function rethrowExpectationFailedExceptionWithUpdateSnapshotsPrompt($exception) |
|
143
|
|
|
{ |
|
144
|
|
|
$newMessage = $exception->getMessage()."\n\n". |
|
145
|
|
|
'Snapshots can be updated by passing '. |
|
146
|
|
|
'`-d --update-snapshots` through PHPUnit\'s CLI arguments.'; |
|
147
|
|
|
|
|
148
|
|
|
$exceptionReflection = new ReflectionObject($exception); |
|
149
|
|
|
|
|
150
|
|
|
$messageReflection = $exceptionReflection->getProperty('message'); |
|
151
|
|
|
$messageReflection->setAccessible(true); |
|
152
|
|
|
$messageReflection->setValue($exception, $newMessage); |
|
153
|
|
|
|
|
154
|
|
|
throw $exception; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
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
Idableprovides a methodequalsIdthat 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.