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 ( 10ba63...efbfef )
by Sebastian
02:12
created

MatchesSnapshots::setUpSnapshotIncrementor()   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 0
1
<?php
2
3
namespace Spatie\Snapshots;
4
5
use PHPUnit\Framework\ExpectationFailedException;
6
use ReflectionClass;
7
use Spatie\Snapshots\Drivers\JsonDriver;
8
use Spatie\Snapshots\Drivers\VarDriver;
9
use Spatie\Snapshots\Drivers\XmlDriver;
10
11
trait MatchesSnapshots
12
{
13
    /** @var int */
14
    protected $snapshotIncrementor;
15
16
    /** @before */
17
    public function setUpSnapshotIncrementor()
18
    {
19
        $this->snapshotIncrementor = 0;
20
    }
21
22
    public function assertMatchesSnapshot($actual)
23
    {
24
        $this->doSnapshotAssertion($actual, new VarDriver());
25
    }
26
27
    public function assertMatchesXmlSnapshot($actual)
28
    {
29
        $this->doSnapshotAssertion($actual, new XmlDriver());
30
    }
31
32
    public function assertMatchesJsonSnapshot($actual)
33
    {
34
        $this->doSnapshotAssertion($actual, new JsonDriver());
35
    }
36
37
    /**
38
     * Determines the snapshot's id. By default, the test case's class and
39
     * method names are used.
40
     *
41
     * @return string
42
     */
43
    protected function getSnapshotId(): string
44
    {
45
        return (new ReflectionClass($this))->getShortName().'__'.
46
            $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...
47
            $this->snapshotIncrementor;
48
    }
49
50
    /**
51
     * Determines the directory where snapshots are stored. By default a
52
     * `__snapshots__` directory is created at the same level as the test
53
     * class.
54
     *
55
     * @return string
56
     */
57
    protected function getSnapshotDirectory(): string
58
    {
59
        return dirname((new ReflectionClass($this))->getFileName()).
60
            DIRECTORY_SEPARATOR.
61
            '__snapshots__';
62
    }
63
64
    /**
65
     * Determines whether or not the snapshot should be updated instead of
66
     * matched.
67
     *
68
     * Override this method it you want to use a different flag or mechanism
69
     * than `-d --update-snapshots`.
70
     *
71
     * @return bool
72
     */
73
    protected function shouldUpdateSnapshots(): bool
74
    {
75
        return in_array('--update-snapshots', $_SERVER['argv'], true);
76
    }
77
78
    protected function doSnapshotAssertion($actual, Driver $driver)
79
    {
80
        $this->snapshotIncrementor++;
81
82
        $snapshot = Snapshot::forTestCase(
83
            $this->getSnapshotId(),
84
            $this->getSnapshotDirectory(),
85
            $driver
86
        );
87
88
        if (! $snapshot->exists()) {
89
            $snapshot->create($actual);
90
91
            return $this->markTestIncomplete("Snapshot created for {$snapshot->id()}");
0 ignored issues
show
Bug introduced by
It seems like markTestIncomplete() 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...
92
        }
93
94
        if ($this->shouldUpdateSnapshots()) {
95
            try {
96
                // We only want to update snapshots which need updating. If the snapshot doesn't
97
                // match the expected output, we'll catch the failure, create a new snapshot and
98
                // mark the test as incomplete.
99
                $snapshot->assertMatches($actual);
100
            } catch (ExpectationFailedException $exception) {
101
                $snapshot->create($actual);
102
103
                return $this->markTestIncomplete("Snapshot updated for {$snapshot->id()}");
0 ignored issues
show
Bug introduced by
It seems like markTestIncomplete() 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...
104
            }
105
        }
106
107
        $snapshot->assertMatches($actual);
108
    }
109
}
110