IncidentSourceTest::testFindSnapshotHistory()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 9.488
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Spiral\Tests\Snapshotter\AggregationHandler\Sources;
4
5
use Spiral\Snapshotter\AggregationHandler;
6
use Spiral\Snapshotter\AggregationHandler\Database\IncidentRecord;
7
use Spiral\Snapshotter\AggregationHandler\Database\Sources\IncidentSource;
8
use Spiral\Tests\BaseTest;
9
10
class IncidentSourceTest extends BaseTest
11
{
12
    /**
13
     * Test creation method
14
     */
15
    public function testCreateFromSnapshot()
16
    {
17
        /** @var IncidentSource $source */
18
        $source = $this->container->get(IncidentSource::class);
19
        $this->createIncident($source);
20
21
        $this->assertCount(1, $source->find());
22
    }
23
24
    /**
25
     * Test soft deletion method.
26
     */
27
    public function testDelete()
28
    {
29
        /** @var IncidentSource $source */
30
        $source = $this->container->get(IncidentSource::class);
31
        $incident = $this->createIncident($source);
32
33
        $incident->delete();
34
35
        /** @var IncidentRecord $incident */
36
        $incident = $source->findOne();
37
38
        $this->assertCount(1, $source->find());
39
        $this->assertEquals(true, $incident->status->isDeleted());
40
    }
41
42
    public function testFindStored()
43
    {
44
        $snapshot = $this->makeSnapshot('custom error', 777);
45
        $snapshotRecord = $this->handleSnapshot($snapshot, true);
46
47
        /** @var IncidentSource $source */
48
        $source = $this->container->get(IncidentSource::class);
49
50
        $this->assertCount(1, $source->find());
51
        $this->assertCount(1, $source->findBySnapshotWithSource());
52
        $this->assertCount(1, $source->findBySnapshotWithSource($snapshotRecord));
53
54
        $snapshotRecord = $this->handleSnapshot($snapshot, true);
55
56
        $this->assertCount(2, $source->find());
57
        $this->assertCount(2, $source->findBySnapshotWithSource());
58
        $this->assertCount(2, $source->findBySnapshotWithSource($snapshotRecord));
59
    }
60
61
    public function testFindSnapshotHistory()
62
    {
63
        $snapshot = $this->makeSnapshot('custom error', 777);
64
65
        /** @var IncidentSource $source */
66
        $source = $this->container->get(IncidentSource::class);
67
68
        $snapshotRecord = $this->handleSnapshot($snapshot, true);
69
        $this->assertCount(0, $source->findSnapshotHistory($snapshotRecord));
70
71
        $snapshotRecord = $this->handleSnapshot($snapshot, true);
72
        $this->assertCount(1, $source->findSnapshotHistory($snapshotRecord));
73
74
        /** @var IncidentRecord $incident */
75
        $incident = $source->find()->orderBy('time_created', 'ASC')->findOne();
76
        $this->assertNotEmpty($incident);
77
78
        $incident->status->setDeleted();
79
        $incident->save();
80
81
        $this->assertCount(0, $source->findSnapshotHistory($snapshotRecord));
82
83
        $incident->status->setSuppressed();
84
        $incident->save();
85
86
        $this->assertCount(1, $source->findSnapshotHistory($snapshotRecord));
87
    }
88
89
    public function testFinsStoredBySnapshotByPK()
90
    {
91
        $snapshot = $this->makeSnapshot('custom error', 777);
92
93
        /** @var IncidentSource $source */
94
        $source = $this->container->get(IncidentSource::class);
95
96
        $snapshotRecord = $this->handleSnapshot($snapshot, true);
0 ignored issues
show
Unused Code introduced by
$snapshotRecord is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
97
        $snapshotRecord = $this->handleSnapshot($snapshot, true);
98
99
        /** @var IncidentRecord $incident */
100
        $incident = $source->find()->orderBy('time_created', 'ASC')->findOne();
101
        $this->assertNotEmpty($incident);
102
103
        $this->assertNotEmpty(
104
            $source->findStoredBySnapshotByPK($snapshotRecord, $incident->primaryKey())
105
        );
106
107
        //Change status
108
        $incident->status->setSuppressed();
109
        $incident->save();
110
111
        $this->assertEmpty(
112
            $source->findStoredBySnapshotByPK($snapshotRecord, $incident->primaryKey())
113
        );
114
115
        //Correct id
116
        $this->assertEmpty(
117
            $source->findStoredBySnapshotByPK($snapshotRecord, $incident->primaryKey() + 20)
118
        );
119
120
        //Change hash and change status back
121
        $snapshotRecord->exception_hash = 'some hash';
122
        $snapshotRecord->save();
123
124
        $incident->status->setStored();
125
        $incident->save();
126
127
        $this->assertEmpty(
128
            $source->findStoredBySnapshotByPK($snapshotRecord, $incident->primaryKey())
129
        );
130
    }
131
132
    public function testFindBySnapshotByPK()
133
    {
134
        $snapshot = $this->makeSnapshot('custom error', 777);
135
136
        /** @var IncidentSource $source */
137
        $source = $this->container->get(IncidentSource::class);
138
139
        $snapshotRecord = $this->handleSnapshot($snapshot, true);
0 ignored issues
show
Unused Code introduced by
$snapshotRecord is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
140
        $snapshotRecord = $this->handleSnapshot($snapshot, true);
141
142
        /** @var IncidentRecord $incident */
143
        $incident = $source->find()->orderBy('time_created', 'ASC')->findOne();
144
        $this->assertNotEmpty($incident);
145
146
        $this->assertNotEmpty(
147
            $source->findBySnapshotByPK($snapshotRecord, $incident->primaryKey())
148
        );
149
150
        //Correct id
151
        $this->assertEmpty(
152
            $source->findBySnapshotByPK($snapshotRecord, $incident->primaryKey() + 20)
153
        );
154
155
        //Change hash
156
        $snapshotRecord->exception_hash = 'some hash';
157
        $snapshotRecord->save();
158
159
        $this->assertEmpty(
160
            $source->findBySnapshotByPK($snapshotRecord, $incident->primaryKey())
161
        );
162
    }
163
164
    /**
165
     * @param IncidentSource $source
166
     * @return IncidentRecord
167
     */
168 View Code Duplication
    private function createIncident(IncidentSource $source): IncidentRecord
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
169
    {
170
        $snapshot = $this->makeSnapshot('custom error', 123);
171
        $incident = $source->createFromSnapshot($snapshot);
172
173
        $incident->save();
174
175
        return $incident;
176
    }
177
}