|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\Snapshots; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\ExpectationFailedException; |
|
6
|
|
|
use ReflectionClass; |
|
7
|
|
|
use Spatie\Snapshots\Drivers\JsonDriver; |
|
8
|
|
|
use Spatie\Snapshots\Drivers\VarDriver; |
|
9
|
|
|
use Spatie\Snapshots\Drivers\XmlDriver; |
|
10
|
|
|
|
|
11
|
|
|
trait MatchesSnapshots |
|
12
|
|
|
{ |
|
13
|
|
|
public function assertMatchesSnapshot($actual, Driver $driver = null) |
|
14
|
|
|
{ |
|
15
|
|
|
$snapshot = $this->createSnapshotWithDriver($driver ?? new VarDriver()); |
|
16
|
|
|
|
|
17
|
|
|
$this->doSnapShotAssertion($snapshot, $actual); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function assertMatchesXmlSnapshot($actual) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->assertMatchesSnapshot($actual, new XmlDriver()); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function assertMatchesJsonSnapshot($actual) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->assertMatchesSnapshot($actual, new JsonDriver()); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Determines the snapshot's test, which is the first part of the ID. |
|
32
|
|
|
* By default, the test class' name is used. |
|
33
|
|
|
* |
|
34
|
|
|
* @return string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected function getSnapshotTestName(): string |
|
37
|
|
|
{ |
|
38
|
|
|
return (new ReflectionClass($this))->getShortName(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Determines the snapshot's test case, which is the second part of the ID. |
|
43
|
|
|
* By default, the test case's method name is used. |
|
44
|
|
|
* |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function getSnapshotTestCaseName(): string |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->getName(); |
|
|
|
|
|
|
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 shouldUpdateSnapshot(): bool |
|
76
|
|
|
{ |
|
77
|
|
|
return in_array('--update-snapshots', $_SERVER['argv'], true); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
protected function createSnapshotWithDriver(Driver $driver): Snapshot |
|
81
|
|
|
{ |
|
82
|
|
|
return Snapshot::forTestCase( |
|
83
|
|
|
$this->getSnapshotTestName(), |
|
84
|
|
|
$this->getSnapshotTestCaseName(), |
|
85
|
|
|
$this->getSnapshotDirectory(), |
|
86
|
|
|
$driver |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
protected function doSnapShotAssertion(Snapshot $snapshot, $actual) |
|
91
|
|
|
{ |
|
92
|
|
|
if (! $snapshot->exists()) { |
|
93
|
|
|
$snapshot->create($actual); |
|
94
|
|
|
|
|
95
|
|
|
return $this->markTestIncomplete("Snapshot created for {$snapshot->id()}"); |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
if ($this->shouldUpdateSnapshot()) { |
|
99
|
|
|
try { |
|
100
|
|
|
// We only want to update snapshots which need updating. If the snapshot doesn't |
|
101
|
|
|
// match the expected output, we'll catch the failure, create a new snapshot and |
|
102
|
|
|
// mark the test as incomplete. |
|
103
|
|
|
$snapshot->assertMatches($actual); |
|
104
|
|
|
} catch (ExpectationFailedException $exception) { |
|
105
|
|
|
$snapshot->create($actual); |
|
106
|
|
|
|
|
107
|
|
|
return $this->markTestIncomplete("Snapshot updated for {$snapshot->id()}"); |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$snapshot->assertMatches($actual); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
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.