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 id. By default, the test case's class and |
32
|
|
|
* method names are used. |
33
|
|
|
* |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
|
|
protected function getSnapshotId(): string |
37
|
|
|
{ |
38
|
|
|
return (new ReflectionClass($this))->getShortName().'__'.$this->getName(); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Determines the directory where snapshots are stored. By default a |
43
|
|
|
* `__snapshots__` directory is created at the same level as the test |
44
|
|
|
* class. |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
protected function getSnapshotDirectory(): string |
49
|
|
|
{ |
50
|
|
|
return dirname((new ReflectionClass($this))->getFileName()). |
51
|
|
|
DIRECTORY_SEPARATOR. |
52
|
|
|
'__snapshots__'; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Determines whether or not the snapshot should be updated instead of |
57
|
|
|
* matched. |
58
|
|
|
* |
59
|
|
|
* Override this method it you want to use a different flag or mechanism |
60
|
|
|
* than `-d --update-snapshots`. |
61
|
|
|
* |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
|
|
protected function shouldUpdateSnapshots(): bool |
65
|
|
|
{ |
66
|
|
|
return in_array('--update-snapshots', $_SERVER['argv'], true); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function createSnapshotWithDriver(Driver $driver): Snapshot |
70
|
|
|
{ |
71
|
|
|
return Snapshot::forTestCase( |
72
|
|
|
$this->getSnapshotId(), |
73
|
|
|
$this->getSnapshotDirectory(), |
74
|
|
|
$driver |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected function doSnapShotAssertion(Snapshot $snapshot, $actual) |
79
|
|
|
{ |
80
|
|
|
if (! $snapshot->exists()) { |
81
|
|
|
$snapshot->create($actual); |
82
|
|
|
|
83
|
|
|
return $this->markTestIncomplete("Snapshot created for {$snapshot->id()}"); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ($this->shouldUpdateSnapshots()) { |
87
|
|
|
try { |
88
|
|
|
// We only want to update snapshots which need updating. If the snapshot doesn't |
89
|
|
|
// match the expected output, we'll catch the failure, create a new snapshot and |
90
|
|
|
// mark the test as incomplete. |
91
|
|
|
$snapshot->assertMatches($actual); |
92
|
|
|
} catch (ExpectationFailedException $exception) { |
93
|
|
|
$snapshot->create($actual); |
94
|
|
|
|
95
|
|
|
return $this->markTestIncomplete("Snapshot updated for {$snapshot->id()}"); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$snapshot->assertMatches($actual); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
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.