File size: 8,587 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php

declare(strict_types=1);

namespace Mautic\ReportBundle\Tests\Crate;

use Mautic\CoreBundle\Twig\Helper\FormatterHelper;
use Mautic\ReportBundle\Crate\ReportDataResult;
use Mautic\ReportBundle\Tests\Fixtures;

class ReportDataResultTest extends \PHPUnit\Framework\TestCase
{
    public function testValidData(): void
    {
        $reportDataResult = new ReportDataResult(Fixtures::getValidReportResult());

        $this->assertSame(Fixtures::getValidReportData(), $reportDataResult->getData());
        $this->assertSame(Fixtures::getValidReportHeaders(), $reportDataResult->getHeaders());
        $this->assertSame(Fixtures::getValidReportTotalResult(), $reportDataResult->getTotalResults());
        $this->assertSame(Fixtures::getStringType(), $reportDataResult->getType('city'));
        $this->assertSame(Fixtures::getDateType(), $reportDataResult->getType('date_identified'));
        $this->assertSame(Fixtures::getEmailType(), $reportDataResult->getType('email'));
    }

    public function testValidDataWithAggregatedColumns(): void
    {
        $reportDataResult = new ReportDataResult(Fixtures::getValidReportResultWithAggregatedColumns());

        $this->assertSame(Fixtures::getValidReportDataAggregatedColumns(), $reportDataResult->getData());
        $this->assertSame(Fixtures::getValidReportWithAggregatedColumnsHeaders(), $reportDataResult->getHeaders());
        $this->assertSame(Fixtures::getValidReportWithAggregatedColumnsTotalResult(), $reportDataResult->getTotalResults());
        $this->assertSame(Fixtures::getIntegerType(), $reportDataResult->getType('SUM es.is_read'));
        $this->assertSame(Fixtures::getFloatType(), $reportDataResult->getType('AVG es.is_read'));
    }

    public function testNoDataProvided(): void
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage("Keys 'data', 'dataColumns' and 'columns' have to be provided");

        $data = Fixtures::getValidReportResult();
        unset($data['data']);
        new ReportDataResult($data);
    }

    public function testNoDataColumnProvided(): void
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage("Keys 'data', 'dataColumns' and 'columns' have to be provided");

        $data = Fixtures::getValidReportResult();
        unset($data['dataColumns']);
        new ReportDataResult($data);
    }

    public function testNoColumnProvided(): void
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage("Keys 'data', 'dataColumns' and 'columns' have to be provided");

        $data = Fixtures::getValidReportResult();
        unset($data['columns']);
        new ReportDataResult($data);
    }

    public function testDataCount(): void
    {
        $fixture           = Fixtures::getValidReportResult();
        $reportDataResult  = new ReportDataResult($fixture);
        $expectedDataCount =  count($fixture['data']);
        $actualDataCount   = $reportDataResult->getDataCount();

        $fixture['data']       = [];
        $reportEmptyDataResult = new ReportDataResult($fixture);

        $this->assertSame($expectedDataCount, $actualDataCount);
        $this->assertSame(0, $reportEmptyDataResult->getDataCount());
    }

    public function testGetTotals(): void
    {
        $fixtureAggregatedColumns       = Fixtures::getValidReportResultWithAggregatedColumns();
        $fixtureAggregatedColumnsTotals = Fixtures::getValidReportDataAggregatedTotals();
        $reportDataResult               = new ReportDataResult($fixtureAggregatedColumns);

        $fixtureNoAggregatedColumns          = Fixtures::getValidReportResult();
        $reportDataResultNoAggregatedColumns = new ReportDataResult($fixtureNoAggregatedColumns);

        $this->assertSame($fixtureAggregatedColumnsTotals, $reportDataResult->getTotals());
        $this->assertEmpty($reportDataResultNoAggregatedColumns->getTotals());
    }

    public function testGetGraphs(): void
    {
        $reportDataResult          = new ReportDataResult(Fixtures::getValidReportResultWithNoGraphs());
        $reportDataResulWithGraphs = new ReportDataResult(Fixtures::getValidReportResultWithGraphs());
        $expectedGraphData         = [
            'mautic.email.graph.line.stats' => [
                'options'        => [],
                'dynamicFilters' => [],
                'paginate'       => true,
            ],
        ];

        $this->assertEmpty($reportDataResult->getGraphs());
        $this->assertEquals($expectedGraphData, $reportDataResulWithGraphs->getGraphs());
    }

    public function testGetDateFrom(): void
    {
        $reportDataResult = new ReportDataResult(Fixtures::getValidReportResult());
        $expectedDateFrom = Fixtures::getDateFrom();

        $this->assertEquals($expectedDateFrom, $reportDataResult->getDateFrom());
        $this->assertTrue($reportDataResult->getDateFrom() instanceof \DateTime);
    }

    public function testGetDateTo(): void
    {
        $reportDataResult = new ReportDataResult(Fixtures::getValidReportResult());
        $dateTo           = Fixtures::getDateTo();

        $this->assertEquals($dateTo, $reportDataResult->getDateTo());
        $this->assertTrue($reportDataResult->getDateTo() instanceof \DateTime);
    }

    public function testIsLastPage(): void
    {
        $reportDataResult                      = new ReportDataResult(Fixtures::getValidReportResult());
        $reportDataResultWithAggregatedColumns = new ReportDataResult(Fixtures::getValidReportResultWithAggregatedColumns());

        $this->assertTrue($reportDataResult->isLastPage());
        $this->assertTrue($reportDataResultWithAggregatedColumns->isLastPage());
    }

    public function testCalcTotal(): void
    {
        $reportDataResult = new ReportDataResult(Fixtures::getValidReportResult());
        $values           = ['1', '13', '4', '6', '20'];
        $valuesCount      = count($values);

        // Calc test COUNT
        $calc         = $reportDataResult->calcTotal('COUNT', $valuesCount, $values);
        $calcWithPrev = $reportDataResult->calcTotal('COUNT', $valuesCount, $values, 5);

        $this->assertEquals(array_sum($values), $calc);
        $this->assertEquals(array_sum($values) + 5, $calcWithPrev);

        // Calc test SUM
        $calc         = $reportDataResult->calcTotal('SUM', $valuesCount, $values);
        $calcWithPrev = $reportDataResult->calcTotal('SUM', $valuesCount, $values, 10);

        $this->assertEquals(array_sum($values), $calc);
        $this->assertEquals(array_sum($values) + 10, $calcWithPrev);

        // Calc test AVG
        $calc         = $reportDataResult->calcTotal('AVG', $valuesCount, $values);
        $calcWithPrev = $reportDataResult->calcTotal('AVG', $valuesCount + 2, $values, 10);

        $this->assertEquals(round(array_sum($values) / $valuesCount, FormatterHelper::FLOAT_PRECISION), $calc);
        $this->assertEquals(round((array_sum($values) + 10) / ($valuesCount + 2), FormatterHelper::FLOAT_PRECISION), $calcWithPrev);

        // Calc test MIN
        $calc         = $reportDataResult->calcTotal('MIN', $valuesCount, $values);
        $calcWithPrev = $reportDataResult->calcTotal('MIN', $valuesCount, $values, 0);

        $this->assertEquals(1, $calc);
        $this->assertEquals(0, $calcWithPrev);

        // Calc test MAX
        $calc         = $reportDataResult->calcTotal('MAX', $valuesCount, $values);
        $calcWithPrev = $reportDataResult->calcTotal('MAX', $valuesCount, $values, 25);

        $this->assertEquals(20, $calc);
        $this->assertEquals(25, $calcWithPrev);

        // Calc test 'default'
        $calc         = $reportDataResult->calcTotal('RANDOM', $valuesCount, $values);
        $calcWithPrev = $reportDataResult->calcTotal('RANDOM', $valuesCount, $values, 50);

        $this->assertNull($calc);
        $this->assertEquals(50, $calcWithPrev);
    }

    public function testGetColumnKeys(): void
    {
        $fixtures                              = Fixtures::getValidReportResultWithAggregatedColumns();
        $reportDataResultWithAggregatedColumns = new ReportDataResult($fixtures);

        unset($fixtures['data'][0]);
        $reportDataResultWithAggregatedColumnsInvalid = new ReportDataResult($fixtures);

        $this->assertEquals(Fixtures::getValidReportWithAggregatedColumnsKeys(), $reportDataResultWithAggregatedColumns->getColumnKeys());
        $this->assertEmpty($reportDataResultWithAggregatedColumnsInvalid->getColumnKeys());
    }
}