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
|
|
|
/** @var int */ |
14
|
|
|
protected $snapshotIncrementor; |
15
|
|
|
|
16
|
|
|
/** @before */ |
17
|
|
|
public function setUpSnapshotIncrementor() |
18
|
|
|
{ |
19
|
|
|
$this->snapshotIncrementor = 0; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function assertMatchesSnapshot($actual) |
23
|
|
|
{ |
24
|
|
|
$this->doSnapshotAssertion($actual, new VarDriver()); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function assertMatchesXmlSnapshot($actual) |
28
|
|
|
{ |
29
|
|
|
$this->doSnapshotAssertion($actual, new XmlDriver()); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function assertMatchesJsonSnapshot($actual) |
33
|
|
|
{ |
34
|
|
|
$this->doSnapshotAssertion($actual, new JsonDriver()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Determines the snapshot's id. By default, the test case's class and |
39
|
|
|
* method names are used. |
40
|
|
|
* |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
|
|
protected function getSnapshotId(): string |
44
|
|
|
{ |
45
|
|
|
return (new ReflectionClass($this))->getShortName().'__'. |
46
|
|
|
$this->getName().'__'. |
|
|
|
|
47
|
|
|
$this->snapshotIncrementor; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Determines the directory where snapshots are stored. By default a |
52
|
|
|
* `__snapshots__` directory is created at the same level as the test |
53
|
|
|
* class. |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
protected function getSnapshotDirectory(): string |
58
|
|
|
{ |
59
|
|
|
return dirname((new ReflectionClass($this))->getFileName()). |
60
|
|
|
DIRECTORY_SEPARATOR. |
61
|
|
|
'__snapshots__'; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Determines whether or not the snapshot should be updated instead of |
66
|
|
|
* matched. |
67
|
|
|
* |
68
|
|
|
* Override this method it you want to use a different flag or mechanism |
69
|
|
|
* than `-d --update-snapshots`. |
70
|
|
|
* |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
protected function shouldUpdateSnapshots(): bool |
74
|
|
|
{ |
75
|
|
|
return in_array('--update-snapshots', $_SERVER['argv'], true); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected function doSnapshotAssertion($actual, Driver $driver) |
79
|
|
|
{ |
80
|
|
|
$this->snapshotIncrementor++; |
81
|
|
|
|
82
|
|
|
$snapshot = Snapshot::forTestCase( |
83
|
|
|
$this->getSnapshotId(), |
84
|
|
|
$this->getSnapshotDirectory(), |
85
|
|
|
$driver |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
if (! $snapshot->exists()) { |
89
|
|
|
$snapshot->create($actual); |
90
|
|
|
|
91
|
|
|
return $this->markTestIncomplete("Snapshot created for {$snapshot->id()}"); |
|
|
|
|
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
|
|
|
$snapshot->create($actual); |
102
|
|
|
|
103
|
|
|
return $this->markTestIncomplete("Snapshot updated for {$snapshot->id()}"); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$snapshot->assertMatches($actual); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
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.