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 ( df2f3e...7fce51 )
by Freek
8s
created

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