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