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

ExtractTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 75
Duplicated Lines 66.67 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testEmptyEvents() 0 5 1
A testEvents() 0 11 1
B testDateSwap() 26 26 1
B testFillGaps() 24 24 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Spiral\Tests\Statistics;
4
5
use Spiral\Statistics\Database\Statistics;
6
use Spiral\Statistics\Extract;
7
use Spiral\Tests\BaseTest;
8
use Spiral\Tests\Statistics\Entities\UnknownRange;
9
10
/**
11
 * For \Spiral\Statistics\Extract::events tests see below:
12
 *
13
 * @see Spiral\Tests\Statistics\Extract\Intervals\AbstractInterval::testSamePeriodExtractEvents
14
 * @see Spiral\Tests\Statistics\Extract\Intervals\AbstractInterval::testAnotherPeriodExtractEvents
15
 */
16
class ExtractTest extends BaseTest
17
{
18
    /**
19
     * @expectedException \Spiral\Statistics\Exceptions\InvalidExtractException
20
     */
21
    public function testEmptyEvents()
22
    {
23
        $extract = $this->getExtract();
24
        $extract->events(new \DateTime(), new \DateTime(), new UnknownRange(), []);
25
    }
26
27
    public function testEvents()
28
    {
29
        $extract = $this->getExtract();
30
        $events = $extract->events(
31
            new \DateTime(),
32
            new \DateTime(),
33
            new Extract\Range\DailyRange(), ['event']
34
        );
35
36
        $this->assertInstanceOf(Extract\Events::class, $events);
37
    }
38
39 View Code Duplication
    public function testDateSwap()
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...
40
    {
41
        $extract = $this->getExtract();
42
        $track = $this->getTrack();
43
44
        $this->assertCount(0, $this->orm->source(Statistics::class));
45
46
        $datetime1 = new \DateTime('now');
47
        $datetime2 = new \DateTime('tomorrow');
48
49
        $track->event('event1', 1, $datetime1);
50
        $track->event('event2', 1, $datetime1);
51
        $track->event('event2', 2, $datetime2);
52
53
        $this->assertCount(3, $this->orm->source(Statistics::class));
54
55
        $start = new \DateTime('-2 days');
56
        $end = new \DateTime('+2 days');
57
58
        $range = new Extract\Range\DailyRange();
59
60
        $this->assertEquals(
61
            $extract->events($start, $end, $range, ['event1']),
62
            $extract->events($end, $start, $range, ['event1'])
63
        );
64
    }
65
66 View Code Duplication
    public function testFillGaps()
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...
67
    {
68
        $extract = $this->getExtract();
69
        $track = $this->getTrack();
70
71
        $this->assertCount(0, $this->orm->source(Statistics::class));
72
73
        $datetime1 = new \DateTime('now');
74
        $datetime2 = new \DateTime('tomorrow');
75
76
        $track->event('event1', 1, $datetime1);
77
        $track->event('event2', 1, $datetime1);
78
        $track->event('event2', 2, $datetime2);
79
80
        $this->assertCount(3, $this->orm->source(Statistics::class));
81
82
        $start = new \DateTime('-2 days');
83
        $end = new \DateTime('+2 days');
84
85
        $range = new Extract\Range\DailyRange();
86
        $events = $extract->events($start, $end, $range, ['event1']);
87
88
        $this->assertCount(5, $events->getRows());
89
    }
90
}