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 (#40)
by Jakub
02:21
created

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