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
Pull Request — master (#30)
by
unknown
02:25
created

updateSnapshotAndMarkTestIncomplete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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