OccurrenceSourceTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 7
dl 0
loc 81
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testFindByTimestamp() 0 17 1
A testGetByTimestamp() 0 17 1
A testCreateFromTimestamp() 0 14 1
B testDataIntegrity() 0 27 1
1
<?php
2
3
namespace Spiral\Tests\Statistics\Sources;
4
5
use Spiral\Statistics\Database\Occurrence;
6
use Spiral\Statistics\Database\Sources\OccurrenceSource;
7
use Spiral\Statistics\DatetimeConverter;
8
use Spiral\Statistics\Extract;
9
use Spiral\Statistics\Track;
10
use Spiral\Tests\BaseTest;
11
12
/**
13
 * For \Spiral\Statistics\Database\Sources\OccurrenceSource::findByGroupedInterval tests see below:
14
 *
15
 * @see Spiral\Tests\Statistics\Extract\Intervals\AbstractInterval::testSamePeriodOccurrenceSourceFindByGroupedInterval
16
 * @see Spiral\Tests\Statistics\Extract\Intervals\AbstractInterval::testAnotherPeriodOccurrenceSourceFindByGroupedInterval
17
 */
18
class OccurrenceSourceTest extends BaseTest
19
{
20
    public function testFindByTimestamp()
21
    {
22
        $track = $this->getTrack();
23
        $source = $this->getSource();
24
25
        $datetime = new \DateTime('now');
26
        $occurrence = $source->findByTimestamp($datetime);
27
28
        $this->assertEmpty($occurrence);
29
        $this->assertCount(0, $this->orm->source(Occurrence::class));
30
31
        $track->event('some-event', 1.23, $datetime);
32
        $this->assertCount(1, $this->orm->source(Occurrence::class));
33
34
        $occurrence = $source->findByTimestamp($datetime);
35
        $this->assertNotEmpty($occurrence);
36
    }
37
38
    public function testGetByTimestamp()
39
    {
40
        $source = $this->getSource();
41
42
        $datetime = new \DateTime('now');
43
        $occurrence = $source->findByTimestamp($datetime);
44
45
        $this->assertEmpty($occurrence);
46
        $this->assertCount(0, $this->orm->source(Occurrence::class));
47
48
        $occurrence = $source->getByTimestamp($datetime);
49
        $this->assertEmpty($occurrence->primaryKey());
50
        $this->assertCount(0, $this->orm->source(Occurrence::class));
51
52
        $occurrence->save();
53
        $this->assertCount(1, $this->orm->source(Occurrence::class));
54
    }
55
56
    public function testCreateFromTimestamp()
57
    {
58
        $source = $this->getSource();
59
60
        $this->assertCount(0, $this->orm->source(Occurrence::class));
61
62
        $datetime = new \DateTime('now');
63
        $occurrence = $source->createFromTimestamp($datetime);
64
        $this->assertEmpty($occurrence->primaryKey());
65
        $this->assertCount(0, $this->orm->source(Occurrence::class));
66
67
        $occurrence->save();
68
        $this->assertCount(1, $this->orm->source(Occurrence::class));
69
    }
70
71
    public function testDataIntegrity()
72
    {
73
        $source = $this->getSource();
74
        $converter = $this->getConverter();
75
76
        $datetime = new \DateTime('now');
77
        $occurrence = $source->createFromTimestamp($datetime);
78
        $this->assertNotEmpty($occurrence);
79
80
        $this->assertEquals($occurrence->timestamp->getTimestamp(), $datetime->getTimestamp());
81
        $this->assertEquals(
82
            $occurrence->day_mark,
83
            $converter->convert($datetime, 'day')
84
        );
85
        $this->assertEquals(
86
            $occurrence->week_mark,
87
            $converter->convert($datetime, 'week')
88
        );
89
        $this->assertEquals(
90
            $occurrence->month_mark,
91
            $converter->convert($datetime, 'month')
92
        );
93
        $this->assertEquals(
94
            $occurrence->year_mark,
95
            $converter->convert($datetime, 'year')
96
        );
97
    }
98
}