SnapshotServiceTest::testCountOccurred()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 69

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 69
rs 8.6763
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Spiral\Tests\Snapshotter\AggregationHandler\Services;
4
5
use Spiral\Snapshotter\AggregationHandler\Database\SnapshotRecord;
6
use Spiral\Snapshotter\AggregationHandler\Database\IncidentRecord;
7
use Spiral\Snapshotter\AggregationHandler\Database\Sources\IncidentSource;
8
use Spiral\Snapshotter\AggregationHandler\Database\Sources\SnapshotSource;
9
use Spiral\Snapshotter\AggregationHandler\Database\Types\IncidentStatus;
10
use Spiral\Snapshotter\AggregationHandler\Services\SnapshotService;
11
use Spiral\Tests\BaseTest;
12
13
class SnapshotServiceTest extends BaseTest
14
{
15
    /**
16
     * Source getter.
17
     */
18
    public function testSource()
19
    {
20
        /** @var SnapshotService $service */
21
        $service = $this->container->get(SnapshotService::class);
22
        $source = $service->getSource();
23
        $this->assertInstanceOf(SnapshotSource::class, $source);
24
    }
25
26
    /**
27
     * Test snapshot aggregation created by hash. Created only once.
28
     */
29
    public function testGetByHash()
30
    {
31
        /** @var SnapshotService $service */
32
        $service = $this->container->get(SnapshotService::class);
33
34
        $this->assertCount(0, $this->orm->source(SnapshotRecord::class)->find());
35
36
        $hash = 'some hash';
37
        $snapshot = $service->getByHash($hash);
38
        $snapshot->save();
39
40
        $this->assertCount(1, $this->orm->source(SnapshotRecord::class)->find());
41
42
        $hash2 = 'some hash2';
43
        $snapshot = $service->getByHash($hash2);
44
        $snapshot->save();
45
46
        $this->assertCount(2, $this->orm->source(SnapshotRecord::class)->find());
47
    }
48
49
    /**
50
     * Test snapshot aggregation created by hash. Created only once.
51
     */
52
    public function testCountOccurred()
53
    {
54
        /** @var SnapshotService $service */
55
        $service = $this->container->get(SnapshotService::class);
56
        /** @var IncidentSource $source */
57
        $source = $this->container->get(IncidentSource::class);
58
59
        $snapshot = $this->makeSnapshot('custom error', 777);
60
61
        //Create 2 incidents
62
        $snapshotRecord = $this->handleSnapshot($snapshot, true);
63
        $counters = $service->countOccurred($snapshotRecord, $source);
64
65
        $this->assertEquals(1, $counters['daily']);
66
        $this->assertEquals(1, $counters['weekly']);
67
        $this->assertEquals(1, $counters['monthly']);
68
        $this->assertEquals(1, $counters['yearly']);
69
70
        $this->handleSnapshot($snapshot, true);
71
        $counters = $service->countOccurred($snapshotRecord, $source);
72
73
        $this->assertEquals(2, $counters['daily']);
74
        $this->assertEquals(2, $counters['weekly']);
75
        $this->assertEquals(2, $counters['monthly']);
76
        $this->assertEquals(2, $counters['yearly']);
77
78
        /** @var IncidentRecord $incident */
79
        $incident = $snapshotRecord->getIncidentsHistory()->getIterator()->current();
80
81
        //One of them occurred not in this day
82
        $incident->time_created = (new \DateTime('now'))->sub(new \DateInterval('P2D'));
83
        $incident->save();
84
85
        $counters = $service->countOccurred($snapshotRecord, $source);
86
        $this->assertEquals(1, $counters['daily']);
87
        $this->assertEquals(2, $counters['weekly']);
88
        $this->assertEquals(2, $counters['monthly']);
89
        $this->assertEquals(2, $counters['yearly']);
90
91
        //One of them occurred not in this week
92
        $incident->time_created = (new \DateTime('now'))->sub(new \DateInterval('P8D'));
93
        $incident->save();
94
95
        $counters = $service->countOccurred($snapshotRecord, $source);
96
        $this->assertEquals(1, $counters['daily']);
97
        $this->assertEquals(1, $counters['weekly']);
98
        $this->assertEquals(2, $counters['monthly']);
99
        $this->assertEquals(2, $counters['yearly']);
100
101
        //One of them occurred not in this month
102
        $incident->time_created = (new \DateTime('now'))->sub(new \DateInterval('P2M'));
103
        $incident->save();
104
105
        $counters = $service->countOccurred($snapshotRecord, $source);
106
        $this->assertEquals(1, $counters['daily']);
107
        $this->assertEquals(1, $counters['weekly']);
108
        $this->assertEquals(1, $counters['monthly']);
109
        $this->assertEquals(2, $counters['yearly']);
110
111
        //One of them occurred not in this year
112
        $incident->time_created = (new \DateTime('now'))->sub(new \DateInterval('P2Y'));
113
        $incident->save();
114
115
        $counters = $service->countOccurred($snapshotRecord, $source);
116
        $this->assertEquals(1, $counters['daily']);
117
        $this->assertEquals(1, $counters['weekly']);
118
        $this->assertEquals(1, $counters['monthly']);
119
        $this->assertEquals(1, $counters['yearly']);
120
    }
121
122
    /**
123
     * Test delete operation.
124
     */
125
    public function testDelete()
126
    {
127
        /** @var SnapshotService $service */
128
        $service = $this->container->get(SnapshotService::class);
129
        /** @var SnapshotSource $source */
130
        $source = $this->container->get(SnapshotSource::class);
131
132
        /** @var IncidentSource $incidentSource */
133
        $incidentSource = $this->container->get(IncidentSource::class);
134
135
        $snapshot = $this->makeSnapshot('custom error', 777);
136
        $snapshotRecord = $this->handleSnapshot($snapshot, true);
137
138
        $this->assertCount(1, $source->findWithLast());
139
        $this->assertNotEmpty($snapshotRecord->getLastIncident());
140
        $this->assertCount(0, $snapshotRecord->getIncidentsHistory());
141
        $this->assertCount(1, $incidentSource);
142
        $this->assertCount(1, $incidentSource->find(['status' => IncidentStatus::LAST]));
143
144
        $service->delete($snapshotRecord);
145
146
        /** @var SnapshotRecord $snapshotRecord */
147
        $snapshotRecord = $source->findOne();
148
        $this->assertNotEmpty($snapshotRecord);
149
150
        $this->assertCount(0, $source->findWithLast());
151
//        $this->assertEmpty($snapshotRecord->getLastIncident());
152
        $this->assertCount(1, $snapshotRecord->getIncidentsHistory());
153
        $this->assertCount(1, $incidentSource);
154
        $this->assertCount(1, $incidentSource->find(['status' => IncidentStatus::DELETED]));
155
156
        /** @var IncidentRecord $incident */
157
        $incident = iterator_to_array($snapshotRecord->getIncidentsHistory())[0];
158
159
        $this->assertNotEmpty($incident);
160
        $this->assertEmpty($incident->getExceptionSource());
161
        $this->assertTrue($incident->status->isDeleted());
162
    }
163
}