Issues (281)

Branch: master

Analytics/Tests/GoogleClient/ConnectorTest.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Backend\Modules\Analytics\Tests\GoogleClient;
4
5
use Backend\Modules\Analytics\GoogleClient\Connector;
6
use Common\ModulesSettings;
7
use Google_Client;
8
use Google_Service_Analytics;
9
use MatthiasMullie\Scrapbook\Adapters\MemoryStore;
10
use MatthiasMullie\Scrapbook\Psr6\Pool;
11
use PHPUnit\Framework\MockObject\MockObject;
0 ignored issues
show
The type PHPUnit\Framework\MockObject\MockObject was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use PHPUnit\Framework\TestCase;
13
14
class ConnectorTest extends TestCase
15
{
16
    public function testGetPageViews(): void
17
    {
18
        $connector = new Connector(
19
            $this->getAnalyticsServiceMock(),
20
            new Pool(new MemoryStore()),
21
            $this->getModulesSettingsMock()
22
        );
23
24
        self::assertEquals(
25
            1,
26
            $connector->getPageViews(
27
                strtotime('-1 day', mktime(0, 0, 0)),
28
                mktime(0, 0, 0)
29
            )
30
        );
31
    }
32
33
    public function testGetVisitors(): void
34
    {
35
        $connector = new Connector(
36
            $this->getAnalyticsServiceMock(),
37
            new Pool(new MemoryStore()),
38
            $this->getModulesSettingsMock()
39
        );
40
41
        self::assertEquals(
42
            2,
43
            $connector->getVisitors(
44
                strtotime('-1 day', mktime(0, 0, 0)),
45
                mktime(0, 0, 0)
46
            )
47
        );
48
    }
49
50
    public function testGetPagesPerVisit(): void
51
    {
52
        $connector = new Connector(
53
            $this->getAnalyticsServiceMock(),
54
            new Pool(new MemoryStore()),
55
            $this->getModulesSettingsMock()
56
        );
57
58
        self::assertEquals(
59
            3.14,
60
            $connector->getPagesPerVisit(
61
                strtotime('-1 day', mktime(0, 0, 0)),
62
                mktime(0, 0, 0)
63
            )
64
        );
65
    }
66
67
    public function testGetTimeOnSite(): void
68
    {
69
        $connector = new Connector(
70
            $this->getAnalyticsServiceMock(),
71
            new Pool(new MemoryStore()),
72
            $this->getModulesSettingsMock()
73
        );
74
75
        self::assertEquals(
76
            1.02,
77
            $connector->getTimeOnSite(
78
                strtotime('-1 day', mktime(0, 0, 0)),
79
                mktime(0, 0, 0)
80
            )
81
        );
82
    }
83
84
    public function testGetNewSessionsPercentage(): void
85
    {
86
        $connector = new Connector(
87
            $this->getAnalyticsServiceMock(),
88
            new Pool(new MemoryStore()),
89
            $this->getModulesSettingsMock()
90
        );
91
92
        self::assertEquals(
93
            78.23,
94
            $connector->getNewSessionsPercentage(
95
                strtotime('-1 day', mktime(0, 0, 0)),
96
                mktime(0, 0, 0)
97
            )
98
        );
99
    }
100
101
    public function testGetBounceRate(): void
102
    {
103
        $connector = new Connector(
104
            $this->getAnalyticsServiceMock(),
105
            new Pool(new MemoryStore()),
106
            $this->getModulesSettingsMock()
107
        );
108
109
        self::assertEquals(
110
            23.25,
111
            $connector->getBounceRate(
112
                strtotime('-1 day', mktime(0, 0, 0)),
113
                mktime(0, 0, 0)
114
            )
115
        );
116
    }
117
118
    public function testGetVisitorsGraphData(): void
119
    {
120
        ini_set('date.timezone', 'Europe/Brussels');
121
122
        $connector = new Connector(
123
            $this->getAnalyticsServiceMock(),
124
            new Pool(new MemoryStore()),
125
            $this->getModulesSettingsMock()
126
        );
127
128
        self::assertEquals(
129
            [
130
                [
131
                    'ga_date' => '1431295200',
132
                    'ga_pageviews' => '0',
133
                    'ga_users' => '0',
134
                ],
135
                [
136
                    'ga_date' => '1431381600',
137
                    'ga_pageviews' => '1',
138
                    'ga_users' => '1',
139
                ],
140
            ],
141
            $connector->getVisitorsGraphData(
142
                strtotime('-1 day', mktime(0, 0, 0)),
143
                mktime(0, 0, 0)
144
            )
145
        );
146
    }
147
148
    public function testGetSourceGraphData(): void
149
    {
150
        $connector = new Connector(
151
            $this->getAnalyticsServiceMock(),
152
            new Pool(new MemoryStore()),
153
            $this->getModulesSettingsMock()
154
        );
155
156
        self::assertEquals(
157
            [
158
                [
159
                    'ga_medium' => '(none)',
160
                    'ga_pageviews' => '8',
161
                ],
162
                [
163
                    'ga_medium' => 'organic',
164
                    'ga_pageviews' => '6',
165
                ],
166
            ],
167
            $connector->getSourceGraphData(
168
                strtotime('-1 day', mktime(0, 0, 0)),
169
                mktime(0, 0, 0)
170
            )
171
        );
172
    }
173
174
    public function testGetMostVisitedPagesData(): void
175
    {
176
        $connector = new Connector(
177
            $this->getAnalyticsServiceMock(),
178
            new Pool(new MemoryStore()),
179
            $this->getModulesSettingsMock()
180
        );
181
182
        self::assertEquals(
183
            [
184
                [
185
                    'ga_pagePath' => '/en',
186
                    'ga_pageviews' => '15',
187
                ],
188
                [
189
                    'ga_pagePath' => '/en/blog',
190
                    'ga_pageviews' => '8',
191
                ],
192
            ],
193
            $connector->getMostVisitedPagesData(
194
                strtotime('-1 day', mktime(0, 0, 0)),
195
                mktime(0, 0, 0)
196
            )
197
        );
198
    }
199
200
    private function getModulesSettingsMock(): MockObject
201
    {
202
        return $this->getMockBuilder(ModulesSettings::class)
203
            ->disableOriginalConstructor()
204
            ->getMock()
205
        ;
206
    }
207
208
    private function getAnalyticsServiceMock(): Google_Service_Analytics
209
    {
210
        $analyticsService = new Google_Service_Analytics(new Google_Client());
211
212
        $dataGateway = $this->getMockBuilder('Google_Service_Analytics_DataGa_Resource')
213
            ->disableOriginalConstructor()
214
            ->getMock()
215
        ;
216
217
        $metricsReturnMock = $this->getMockBuilder('Google_Service_Analytics_GaData')->getMock();
218
        $metricsReturnMock
219
            ->method('getTotalsForAllResults')
220
            ->willReturn([
221
                'ga:pageviews' => 1,
222
                'ga:users' => 2,
223
                'ga:pageviewsPerSession' => 3.14,
224
                'ga:avgSessionDuration' => 1.02,
225
                'ga:percentNewSessions' => 78.23,
226
                'ga:bounceRate' => 23.25,
227
            ])
228
        ;
229
230
        $visitGraphDataMock = $this->getMockBuilder('Google_Service_Analytics_GaData')->getMock();
231
        $visitGraphDataMock
232
            ->method('getRows')
233
            ->willReturn([
234
                ['20150511', '0', '0'],
235
                ['20150512', '1', '1'],
236
            ])
237
        ;
238
        $visitGraphDataMock
239
            ->method('getColumnHeaders')
240
            ->willReturn([
241
                ['name' => 'ga:date'],
242
                ['name' => 'ga:pageviews'],
243
                ['name' => 'ga:users'],
244
            ])
245
        ;
246
247
        $sourceGraphDataMock = $this->getMockBuilder('Google_Service_Analytics_GaData')->getMock();
248
        $sourceGraphDataMock
249
            ->method('getRows')
250
            ->willReturn([
251
                ['(none)', '8'],
252
                ['organic', '6'],
253
            ])
254
        ;
255
        $sourceGraphDataMock
256
            ->method('getColumnHeaders')
257
            ->willReturn([
258
                ['name' => 'ga:medium'],
259
                ['name' => 'ga:pageviews'],
260
            ])
261
        ;
262
263
        $pageViewsDataMock = $this->getMockBuilder('Google_Service_Analytics_GaData')->getMock();
264
        $pageViewsDataMock
265
            ->method('getRows')
266
            ->willReturn([
267
                ['/en', '15'],
268
                ['/en/blog', '8'],
269
            ])
270
        ;
271
        $pageViewsDataMock
272
            ->method('getColumnHeaders')
273
            ->willReturn([
274
                ['name' => 'ga:pagePath'],
275
                ['name' => 'ga:pageviews'],
276
            ])
277
        ;
278
279
        $dataGateway->method('get')
280
            ->will(self::onConsecutiveCalls(
281
                $metricsReturnMock,
282
                $visitGraphDataMock,
283
                $pageViewsDataMock,
284
                $sourceGraphDataMock
285
            ))
286
        ;
287
288
        $analyticsService->data_ga = $dataGateway;
289
290
        return $analyticsService;
291
    }
292
}
293