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 (#35)
by
unknown
07:23
created

MatchesSnapshots::doFileSnapshotAssertion()   C

Complexity

Conditions 7
Paths 48

Size

Total Lines 44
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 22
nc 48
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
    public function assertMatchesFileHashSnapshot($filePath)
40
    {
41
        if (! file_exists($filePath)) {
42
            $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...
43
        }
44
45
        $actual = sha1_file($filePath);
46
47
        $this->assertMatchesSnapshot($actual);
48
    }
49
50
    public function assertMatchesFileSnapshot($file)
51
    {
52
        $this->doFileSnapshotAssertion($file);
53
    }
54
55
    /**
56
     * Determines the snapshot's id. By default, the test case's class and
57
     * method names are used.
58
     *
59
     * @return string
60
     */
61
    protected function getSnapshotId(): string
62
    {
63
        return (new ReflectionClass($this))->getShortName().'__'.
64
            $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...
65
            $this->snapshotIncrementor;
66
    }
67
68
    /**
69
     * Determines the directory where snapshots are stored. By default a
70
     * `__snapshots__` directory is created at the same level as the test
71
     * class.
72
     *
73
     * @return string
74
     */
75
    protected function getSnapshotDirectory(): string
76
    {
77
        return dirname((new ReflectionClass($this))->getFileName()).
78
            DIRECTORY_SEPARATOR.
79
            '__snapshots__';
80
    }
81
82
    /**
83
     * Determines the directory where file snapshots are stored. By default a
84
     * `__snapshots__/files` directory is created at the same level as the
85
     * test class.
86
     *
87
     * @return string
88
     */
89
    protected function getFileSnapshotDirectory(): string
90
    {
91
        return $this->getSnapshotDirectory().
92
            DIRECTORY_SEPARATOR.
93
            'files';
94
    }
95
96
    /**
97
     * Determines whether or not the snapshot should be updated instead of
98
     * matched.
99
     *
100
     * Override this method it you want to use a different flag or mechanism
101
     * than `-d --update-snapshots`.
102
     *
103
     * @return bool
104
     */
105
    protected function shouldUpdateSnapshots(): bool
106
    {
107
        return in_array('--update-snapshots', $_SERVER['argv'], true);
108
    }
109
110
    protected function doSnapshotAssertion($actual, Driver $driver)
111
    {
112
        $this->snapshotIncrementor++;
113
114
        $snapshot = Snapshot::forTestCase(
115
            $this->getSnapshotId(),
116
            $this->getSnapshotDirectory(),
117
            $driver
118
        );
119
120
        if (! $snapshot->exists()) {
121
            $this->createSnapshotAndMarkTestIncomplete($snapshot, $actual);
122
        }
123
124
        if ($this->shouldUpdateSnapshots()) {
125
            try {
126
                // We only want to update snapshots which need updating. If the snapshot doesn't
127
                // match the expected output, we'll catch the failure, create a new snapshot and
128
                // mark the test as incomplete.
129
                $snapshot->assertMatches($actual);
130
            } catch (ExpectationFailedException $exception) {
131
                $this->updateSnapshotAndMarkTestIncomplete($snapshot, $actual);
132
            } 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...
133
                $this->updateSnapshotAndMarkTestIncomplete($snapshot, $actual);
134
            }
135
        }
136
137
        try {
138
            $snapshot->assertMatches($actual);
139
        } catch (ExpectationFailedException $exception) {
140
            $this->rethrowExpectationFailedExceptionWithUpdateSnapshotsPrompt($exception);
141
        } 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...
142
            $this->rethrowExpectationFailedExceptionWithUpdateSnapshotsPrompt($exception);
143
        }
144
    }
145
146
    protected function doFileSnapshotAssertion(string $filePath)
147
    {
148
        if (! file_exists($filePath)) {
149
            $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...
150
        }
151
152
        $fileExtension = pathinfo($filePath, PATHINFO_EXTENSION);
153
154
        if (empty($fileExtension)) {
155
            $this->fail("Unable to make a file snapshot, file does not have a file extension ({$filePath})");
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...
156
        }
157
158
        $fileSystem = Filesystem::inDirectory($this->getFileSnapshotDirectory());
159
160
        $this->snapshotIncrementor++;
161
162
        $snapshotId = $this->getSnapshotId().'.'.$fileExtension;
163
164
        $failedSnapshotId = $snapshotId.'_failed.'.$fileExtension;
165
166
        if ($fileSystem->has($failedSnapshotId)) {
167
            $fileSystem->delete($failedSnapshotId);
168
        }
169
170
        if (! $fileSystem->has($snapshotId)) {
171
            $fileSystem->copy($filePath, $snapshotId);
172
173
            $this->markTestIncomplete("File snapshot created for {$snapshotId}");
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...
174
        }
175
176
        if (! $fileSystem->fileEquals($filePath, $snapshotId)) {
177
            if ($this->shouldUpdateSnapshots()) {
178
                $fileSystem->copy($filePath, $snapshotId);
179
180
                $this->markTestIncomplete("File snapshot updated for {$snapshotId}");
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...
181
            }
182
183
            $fileSystem->copy($filePath, $failedSnapshotId);
184
185
            $this->fail("File did not match snapshot ({$snapshotId})");
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...
186
        }
187
188
        $this->assertTrue(true);
0 ignored issues
show
Bug introduced by
It seems like assertTrue() 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...
189
    }
190
191
    protected function createSnapshotAndMarkTestIncomplete(Snapshot $snapshot, $actual)
192
    {
193
        $snapshot->create($actual);
194
195
        $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...
196
    }
197
198
    protected function updateSnapshotAndMarkTestIncomplete(Snapshot $snapshot, $actual)
199
    {
200
        $snapshot->create($actual);
201
202
        $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...
203
    }
204
205
    protected function rethrowExpectationFailedExceptionWithUpdateSnapshotsPrompt($exception)
206
    {
207
        $newMessage = $exception->getMessage()."\n\n".
208
            'Snapshots can be updated by passing '.
209
            '`-d --update-snapshots` through PHPUnit\'s CLI arguments.';
210
211
        $exceptionReflection = new ReflectionObject($exception);
212
213
        $messageReflection = $exceptionReflection->getProperty('message');
214
        $messageReflection->setAccessible(true);
215
        $messageReflection->setValue($exception, $newMessage);
216
217
        throw $exception;
218
    }
219
}
220