File size: 3,528 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php

namespace Mautic\StatsBundle\Tests\Aggregate\Collection\DAO;

use Mautic\StatsBundle\Aggregate\Collection\DAO\StatsDAO;
use Mautic\StatsBundle\Aggregate\Collection\Stats\DayStat;
use Mautic\StatsBundle\Aggregate\Collection\Stats\MonthStat;
use Mautic\StatsBundle\Aggregate\Collection\Stats\WeekStat;
use Mautic\StatsBundle\Aggregate\Collection\Stats\YearStat;
use PHPUnit\Framework\TestCase;

class StatsDAOTest extends TestCase
{
    public function testGetYearsReturnsYears(): void
    {
        $expected = [
            2018,
            2019,
        ];

        $stats = $this->getStats()->getYears();
        $this->assertEquals($expected, array_keys($stats));

        array_walk($stats, function ($stat): void {
            $this->assertInstanceOf(YearStat::class, $stat);
        });
    }

    public function testGetMonthsReturnsFlattenedMonths(): void
    {
        $expected = [
            '2018-12',
            '2019-11',
            '2019-12',
        ];

        $stats = $this->getStats()->getMonths();
        $this->assertEquals($expected, array_keys($stats));

        array_walk($stats, function ($stat): void {
            $this->assertInstanceOf(MonthStat::class, $stat);
        });
    }

    public function testGetWeekReturnsFlattenedMonths(): void
    {
        $expected = [
            '2018-49',
            '2019-45',
            '2019-49',
        ];

        $stats = $this->getStats()->getWeeks();
        $this->assertEquals($expected, array_keys($stats));

        array_walk($stats, function ($stat): void {
            $this->assertInstanceOf(WeekStat::class, $stat);
        });
    }

    public function testGetDaysReturnsFlattenedDays(): void
    {
        $expected = [
            '2018-12-07',
            '2019-11-07',
            '2019-11-08',
            '2019-12-07',
        ];

        $stats = $this->getStats()->getDays();
        $this->assertEquals($expected, array_keys($stats));

        array_walk($stats, function ($stat): void {
            $this->assertInstanceOf(DayStat::class, $stat);
        });
    }

    public function testGetHoursReturnsFlattenedHours(): void
    {
        $expected = [
            '2018-12-07 12',
            '2018-12-07 13',
            '2018-12-07 14',
            '2019-11-07 12',
            '2019-11-08 12',
            '2019-12-07 12',
        ];

        $stats = $this->getStats()->getHours();
        $this->assertEquals($expected, array_keys($stats));

        array_walk($stats, function ($stat): void {
            $this->assertTrue(is_int($stat->getCount()));
        });
    }

    private function getStats(): StatsDAO
    {
        $stats = new StatsDAO();

        $stats->getYear(2019)
            ->getMonth(11)
            ->getDay(8)
            ->getHour(12)
            ->setCount(100);

        $stats->getYear(2018)
            ->getMonth(12)
            ->getDay(7)
            ->getHour(12)
            ->setCount(100);

        $stats->getYear(2018)
            ->getMonth(12)
            ->getDay(7)
            ->getHour(14)
            ->setCount(300);

        $stats->getYear(2018)
            ->getMonth(12)
            ->getDay(7)
            ->getHour(13)
            ->setCount(200);

        $stats->getYear(2019)
            ->getMonth(12)
            ->getDay(7)
            ->getHour(12)
            ->setCount(100);

        $stats->getYear(2019)
            ->getMonth(11)
            ->getDay(7)
            ->getHour(12)
            ->setCount(100);

        return $stats;
    }
}