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 ( ee82d5...421663 )
by Sebastian
12s
created

rethrowExpectationFailedExceptionWithUpdateSnapshotsPrompt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
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\FileHashDriver;
10
use Spatie\Snapshots\Drivers\JsonDriver;
11
use Spatie\Snapshots\Drivers\VarDriver;
12
use Spatie\Snapshots\Drivers\XmlDriver;
13
14
trait MatchesSnapshots
15
{
16
    /** @var int */
17
    protected $snapshotIncrementor;
18
19
    /** @before */
20
    public function setUpSnapshotIncrementor()
21
    {
22
        $this->snapshotIncrementor = 0;
23
    }
24
25
    public function assertMatchesSnapshot($actual, Driver $driver = null)
26
    {
27
        $this->doSnapshotAssertion($actual, $driver ?? new VarDriver());
28
    }
29
30
    public function assertMatchesXmlSnapshot($actual)
31
    {
32
        $this->assertMatchesSnapshot($actual, new XmlDriver());
33
    }
34
35
    public function assertMatchesJsonSnapshot($actual)
36
    {
37
        $this->assertMatchesSnapshot($actual, new JsonDriver());
38
    }
39
40
    public function assertMatchesFileHashSnapshot($filePath)
41
    {
42
        if (! file_exists($filePath)) {
43
            $this->fail('File does not exist');
0 ignored issues
show
Bug introduced by
It seems like fail() 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...
44
        }
45
46
        $actual = sha1_file($filePath);
47
48
        $this->assertMatchesSnapshot($actual, new FileHashDriver());
49
    }
50
51
    /**
52
     * Determines the snapshot's id. By default, the test case's class and
53
     * method names are used.
54
     *
55
     * @return string
56
     */
57
    protected function getSnapshotId(): string
58
    {
59
        return (new ReflectionClass($this))->getShortName().'__'.
60
            $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...
61
            $this->snapshotIncrementor;
62
    }
63
64
    /**
65
     * Determines the directory where snapshots are stored. By default a
66
     * `__snapshots__` directory is created at the same level as the test
67
     * class.
68
     *
69
     * @return string
70
     */
71
    protected function getSnapshotDirectory(): string
72
    {
73
        return dirname((new ReflectionClass($this))->getFileName()).
74
            DIRECTORY_SEPARATOR.
75
            '__snapshots__';
76
    }
77
78
    /**
79
     * Determines whether or not the snapshot should be updated instead of
80
     * matched.
81
     *
82
     * Override this method it you want to use a different flag or mechanism
83
     * than `-d --update-snapshots`.
84
     *
85
     * @return bool
86
     */
87
    protected function shouldUpdateSnapshots(): bool
88
    {
89
        return in_array('--update-snapshots', $_SERVER['argv'], true);
90
    }
91
92
    protected function doSnapshotAssertion($actual, Driver $driver)
93
    {
94
        $this->snapshotIncrementor++;
95
96
        $snapshot = Snapshot::forTestCase(
97
            $this->getSnapshotId(),
98
            $this->getSnapshotDirectory(),
99
            $driver
100
        );
101
102
        if (! $snapshot->exists()) {
103
            $this->createSnapshotAndMarkTestIncomplete($snapshot, $actual);
104
        }
105
106
        if ($this->shouldUpdateSnapshots()) {
107
            try {
108
                // We only want to update snapshots which need updating. If the snapshot doesn't
109
                // match the expected output, we'll catch the failure, create a new snapshot and
110
                // mark the test as incomplete.
111
                $snapshot->assertMatches($actual);
112
            } catch (ExpectationFailedException $exception) {
113
                $this->updateSnapshotAndMarkTestIncomplete($snapshot, $actual);
114
            } 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...
115
                $this->updateSnapshotAndMarkTestIncomplete($snapshot, $actual);
116
            }
117
        }
118
119
        try {
120
            $snapshot->assertMatches($actual);
121
        } catch (ExpectationFailedException $exception) {
122
            $this->rethrowExpectationFailedExceptionWithUpdateSnapshotsPrompt($exception);
123
        } 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...
124
            $this->rethrowExpectationFailedExceptionWithUpdateSnapshotsPrompt($exception);
125
        }
126
    }
127
128
    protected function createSnapshotAndMarkTestIncomplete(Snapshot $snapshot, $actual)
129
    {
130
        $snapshot->create($actual);
131
132
        $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...
133
    }
134
135
    protected function updateSnapshotAndMarkTestIncomplete(Snapshot $snapshot, $actual)
136
    {
137
        $snapshot->create($actual);
138
139
        $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...
140
    }
141
142
    protected function rethrowExpectationFailedExceptionWithUpdateSnapshotsPrompt($exception)
143
    {
144
        $newMessage = $exception->getMessage()."\n\n".
145
            'Snapshots can be updated by passing '.
146
            '`-d --update-snapshots` through PHPUnit\'s CLI arguments.';
147
148
        $exceptionReflection = new ReflectionObject($exception);
149
150
        $messageReflection = $exceptionReflection->getProperty('message');
151
        $messageReflection->setAccessible(true);
152
        $messageReflection->setValue($exception, $newMessage);
153
154
        throw $exception;
155
    }
156
}
157