GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 3277d6...ddd2b8 )
by Sebastian
02:26
created

MatchesSnapshots::assertMatchesJsonSnapshot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
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().'__'.
0 ignored issues
show
Bug introduced by
It seems like getName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
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) {
0 ignored issues
show
Bug introduced by
The class PHPUnit_Framework_ExpectationFailedException does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
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) {
0 ignored issues
show
Bug introduced by
The class PHPUnit_Framework_ExpectationFailedException does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
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()}");
0 ignored issues
show
Bug introduced by
The method markTestIncomplete() does not exist on Spatie\Snapshots\MatchesSnapshots. Did you maybe mean createSnapshotAndMarkTestIncomplete()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
121
    }
122
123
    protected function updateSnapshotAndMarkTestIncomplete(Snapshot $snapshot, $actual)
124
    {
125
        $snapshot->create($actual);
126
127
        $this->markTestIncomplete("Snapshot updated for {$snapshot->id()}");
0 ignored issues
show
Bug introduced by
The method markTestIncomplete() does not exist on Spatie\Snapshots\MatchesSnapshots. Did you maybe mean createSnapshotAndMarkTestIncomplete()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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