1 | <?php |
||
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 |
||
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 |
||
79 | |||
80 | protected function doSnapshotAssertion($actual, Driver $driver) |
||
115 | |||
116 | protected function createSnapshotAndMarkTestIncomplete(Snapshot $snapshot, $actual) |
||
122 | |||
123 | protected function updateSnapshotAndMarkTestIncomplete(Snapshot $snapshot, $actual) |
||
129 | |||
130 | protected function rethrowExpectationFailedExceptionWithUpdateSnapshotsPrompt($exception) |
||
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.