Passed
Branch feature/v2 (61dd95)
by Valentin
05:53
created

testFindByEventNameAndDatetime()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 17
nc 1
nop 0
1
<?php
2
3
namespace Spiral\Tests\Statistics\Sources;
4
5
use Spiral\Statistics\Database\Statistics;
6
use Spiral\Statistics\Extract;
7
use Spiral\Tests\BaseTest;
8
9
/**
10
 * @see Spiral\Tests\Statistics\Extract\Intervals\AbstractInterval::testSamePeriodSourceFindExtract
11
 * @see Spiral\Tests\Statistics\Extract\Intervals\AbstractInterval::testAnotherPeriodSourceFindExtract
12
 */
13
class StatisticsSourceTest extends BaseTest
14
{
15 View Code Duplication
    public function testFindExtractWithEvents()
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...
16
    {
17
        $track = $this->getTrack();
18
19
        $this->assertCount(0, $this->orm->source(Statistics::class));
20
21
        $datetime = new \DateTime();
22
        $track->events(['some-event' => 1.23, 'some-event2' => 2.34], $datetime);
23
24
        $this->assertCount(2, $this->orm->source(Statistics::class));
25
26
        $this->assertCount(2, $this->getSource()
27
            ->findExtract(new \DateTime('-1 day'), new \DateTime('+1 day'), []));
28
29
        $this->assertCount(1, $this->getSource()
30
            ->findExtract(new \DateTime('-1 day'), new \DateTime('+1 day'), ['some-event']));
31
32
        $this->assertCount(0, $this->getSource()
33
            ->findExtract(new \DateTime('-1 day'), new \DateTime('+1 day'), ['some-event3']));
34
    }
35
36 View Code Duplication
    public function testFindExtractWithDateRange()
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...
37
    {
38
        $track = $this->getTrack();
39
40
        $this->assertCount(0, $this->orm->source(Statistics::class));
41
42
        $datetime = new \DateTime();
43
        $track->events(['some-event' => 1.23, 'some-event2' => 2.34], $datetime);
44
45
        $this->assertCount(2, $this->orm->source(Statistics::class));
46
47
        $this->assertCount(0, $this->getSource()
48
            ->findExtract(new \DateTime('-3 day'), new \DateTime('-1 day'), []));
49
50
        $this->assertCount(0, $this->getSource()
51
            ->findExtract(new \DateTime('-3 day'), new \DateTime('-1 day'), ['some-event']));
52
53
        $this->assertCount(0, $this->getSource()
54
            ->findExtract(new \DateTime('-3 day'), new \DateTime('-1 day'), ['some-event3']));
55
    }
56
57
    public function testFindByEventNameAndDatetime()
58
    {
59
        $track = $this->getTrack();
60
61
        $this->assertCount(0, $this->orm->source(Statistics::class));
62
63
        $datetime = new \DateTime();
64
        $track->events(['some-event' => 1.23, 'some-event2' => 2.34], $datetime);
65
66
        $this->assertCount(2, $this->orm->source(Statistics::class));
67
68
        $this->assertEmpty(
69
            $this->getSource()->findByEventNameAndDatetime('some-event3', $datetime));
70
71
        $this->assertEmpty(
72
            $this->getSource()->findByEventNameAndDatetime('some-event', new \DateTime('+1 day')));
73
74
        $record = $this->getSource()->findByEventNameAndDatetime('some-event', $datetime);
75
76
        $this->assertNotEmpty($record);
77
78
        $this->assertEquals($record->name, 'some-event');
0 ignored issues
show
Bug introduced by
Accessing name on the interface Spiral\ORM\RecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
79
        $this->assertEquals($record->value, 1.23);
0 ignored issues
show
Bug introduced by
Accessing value on the interface Spiral\ORM\RecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
80
        $this->assertEquals(
81
            $record->timestamp->getTimestamp(),
0 ignored issues
show
Bug introduced by
Accessing timestamp on the interface Spiral\ORM\RecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
82
            $datetime->getTimestamp()
83
        );
84
    }
85
86
    public function testDataIntegrity()
87
    {
88
        $track = $this->getTrack();
89
90
        $this->assertCount(0, $this->orm->source(Statistics::class));
91
92
        $datetime = new \DateTime();
93
        $track->event('some-event', 1.23, $datetime);
94
95
        $this->assertCount(1, $this->orm->source(Statistics::class));
96
97
        /** @var Statistics $record */
98
        $record = $this->orm->source(Statistics::class)->findOne();
99
100
        $this->assertNotEmpty($record);
101
102
        $this->assertEquals($record->name, 'some-event');
103
        $this->assertEquals($record->value, 1.23);
104
        $this->assertEquals(
105
            $record->timestamp->getTimestamp(),
106
            $datetime->getTimestamp()
107
        );
108
    }
109
}