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