Passed
Push — master ( d92bbf...19add9 )
by Simon
01:27
created

testGetGetReportRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 9
rs 9.6666
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * Class GoogleAnalyticsReportServiceTest
5
 *
6
 * Test the report service it's parts
7
 */
8
class GoogleAnalyticsReportServiceTest extends SapphireTest
9
{
10
    const VIEWID = 12345;
11
12
    /**
13
     * @var GoogleClientService
14
     */
15
    protected $client;
16
17
    /**
18
     * @var GoogleAnalyticsReportService
19
     */
20
    protected $service;
21
22
    /**
23
     * Create the Services
24
     */
25
    public function setUp()
26
    {
27
        parent::setUp();
28
        if (!defined('SS_ANALYTICS_KEY')) {
29
            define('SS_ANALYTICS_KEY', 'google-api/tests/fixtures/test.json');
30
        }
31
        $this->client = new GoogleClientService();
32
        $this->service = new GoogleAnalyticsReportService($this->client);
33
        $siteConfig = SiteConfig::current_site_config();
0 ignored issues
show
Bug introduced by
The type SiteConfig 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...
34
        $siteConfig->Viewid = static::VIEWID;
35
        $siteConfig->write();
36
    }
37
38
    /**
39
     * Check if we can set and get the client
40
     */
41
    public function testSetClient()
42
    {
43
        $this->service->setClient($this->client);
44
        $this->assertInstanceOf(GoogleClientService::class, $this->service->getClient());
45
    }
46
47
    /**
48
     * See if we have a new Service registered
49
     */
50
    public function testGetAnalytics()
51
    {
52
        $this->assertInstanceOf(Google_Service_AnalyticsReporting::class, $this->service->getAnalytics());
53
    }
54
55
    /**
56
     * Check if the Metric is set
57
     */
58
    public function testGetMetrics()
59
    {
60
        $this->assertInstanceOf(Google_Service_AnalyticsReporting_Metric::class, $this->service->getMetrics());
61
    }
62
63
    /**
64
     * Check if setting the daterange updates the Analytics_DateRange
65
     */
66
    public function testDateRange()
67
    {
68
        /* Set the daterange to 60 days */
69
        $this->service->setDateRange(60);
70
        /** @var Google_Service_AnalyticsReporting_DateRange $dateRange */
71
        $dateRange = $this->service->getDateRange();
72
73
        $this->assertInstanceOf(Google_Service_AnalyticsReporting_DateRange::class, $dateRange);
74
75
        $this->assertEquals('60daysAgo', $dateRange->getStartDate());
76
    }
77
78
    /**
79
     * Confirm the report request is correctly build
80
     */
81
    public function testGetReportRequest()
82
    {
83
        $reportRequest = $this->service->getReportRequest();
84
85
        /** @var array|Google_Service_AnalyticsReporting_Metric[] $metrics */
86
        $metrics = $reportRequest->getMetrics();
87
        /** @var array|Google_Service_AnalyticsReporting_Dimension[] $dimensions */
88
        $dimensions = $reportRequest->getDimensions();
89
90
        $this->assertInstanceOf(Google_Service_AnalyticsReporting_ReportRequest::class, $reportRequest);
91
        $this->assertTrue(is_array($metrics));
92
        $this->assertInstanceOf(Google_Service_AnalyticsReporting_Metric::class, $metrics[0]);
93
        $this->assertTrue(is_array($dimensions));
94
        $this->assertInstanceOf(Google_Service_AnalyticsReporting_Dimension::class, $dimensions[0]);
95
        $this->assertEquals('ga:pagePath', $dimensions[0]->getName());
96
        $this->assertInstanceOf(Google_Service_AnalyticsReporting_DimensionFilterClause::class, $reportRequest->getDimensionFilterClauses());
97
        $this->assertInstanceOf(Google_Service_AnalyticsReporting_DateRange::class, $reportRequest->getDateRanges());
98
        $this->assertEquals(static::VIEWID, $reportRequest->getViewId());
99
    }
100
101
    public function testGetGetReportRequest()
102
    {
103
        $reportRequest = $this->service->getReportRequest();
104
105
        $getReportRequest = $this->service->getGetReportRequest($reportRequest);
106
107
        $this->assertInstanceOf(Google_Service_AnalyticsReporting_GetReportsRequest::class, $getReportRequest);
108
        $reportRequest2 = $getReportRequest->getReportRequests();
109
        $this->assertEquals($reportRequest, $reportRequest2[0]);
110
    }
111
112
    public function testGetDimension()
113
    {
114
        /** @var array|Google_Service_AnalyticsReporting_Dimension[] $dimensions */
115
        $dimensions = $this->service->getDimensions();
116
        $this->assertTrue(is_array($dimensions));
117
        foreach ($dimensions as $dimension) {
118
            $this->assertEquals('ga:pagePath', $dimension->getName());
119
        }
120
    }
121
122
    /**
123
     * Validate start_with filter
124
     */
125 View Code Duplication
    public function testGetStartDimensionFilters()
126
    {
127
        Config::inst()->update(GoogleAnalyticsReportService::class, 'starts_with', '/');
0 ignored issues
show
Bug introduced by
The type Config 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...
128
129
        $filters = $this->service->getDimensionFilters();
130
131
        $this->assertTrue(is_array($filters));
132
        /** @var  $filter */
133
        foreach ($filters as $filter) {
134
            $this->assertInstanceOf(Google_Service_AnalyticsReporting_DimensionFilter::class, $filter);
135
            $expressions = $filter->getExpressions();
136
            $this->assertEquals('/', $expressions[0]);
137
            $this->assertEquals('BEGINS_WITH', $filter->getOperator());
138
        }
139
    }
140
141
    /**
142
     * validate end_with filter
143
     */
144 View Code Duplication
    public function testGetEndDimensionFilters()
145
    {
146
        Config::inst()->update(GoogleAnalyticsReportService::class, 'ends_with', '/');
147
148
        $filters = $this->service->getDimensionFilters();
149
150
        $this->assertTrue(is_array($filters));
151
        foreach ($filters as $filter) {
152
            $this->assertInstanceOf(Google_Service_AnalyticsReporting_DimensionFilter::class, $filter);
153
            $expressions = $filter->getExpressions();
154
            $this->assertEquals('/', $expressions[0]);
155
            $this->assertEquals('ENDS_WITH', $filter->getOperator());
156
        }
157
    }
158
159
    /**
160
     * Test page filters are created
161
     */
162
    public function testGetPageDimensionFilters()
163
    {
164
        $filters = $this->service->getDimensionFilters();
165
166
        $this->assertTrue(is_array($filters));
167
        foreach ($filters as $filter) {
168
            $this->assertInstanceOf(Google_Service_AnalyticsReporting_DimensionFilter::class, $filter);
169
            $this->assertTrue(in_array($filter->getOperator(), ['ENDS_WITH', 'EXACT'], true));
170
        }
171
    }
172
173
    /**
174
     * Validate our DimensionClause is set with OR
175
     */
176
    public function testGetDimensionFilterClauses()
177
    {
178
        $dimensionsClauses = $this->service->getDimensionFilterClauses();
179
        $this->assertInstanceOf(Google_Service_AnalyticsReporting_DimensionFilterClause::class, $dimensionsClauses);
180
        $this->assertEquals('OR', $dimensionsClauses->getOperator());
181
    }
182
183
    /**
184
     * Make sure the filter-list does not exceed 20
185
     */
186
    public function testGetPages()
187
    {
188
        $pages = $this->service->getPages();
189
        $this->assertTrue(is_array($pages));
190
        $this->assertLessThanOrEqual(20, count($pages));
191
    }
192
193
    /**
194
     * Make sure we're not adding more than 20 filters, despite more pages being available
195
     */
196
    public function testFilterLength()
197
    {
198
        for ($i = 1; $i <= 30; $i++) {
199
            /** @var Page` $page */
200
            $page = Page::create(['Title' => $i . ' test']);
201
            $page->write();
202
            $page->doPublish();
203
        }
204
        $pages = $this->service->getPages();
205
        $this->assertTrue(is_array($pages));
206
        $this->assertCount(20, $pages);
207
        $this->assertTrue($this->service->batched);
208
    }
209
210
    /**
211
     * Validate blacklisted pages don't show up
212
     */
213 View Code Duplication
    public function testGetBlacklistedPages()
214
    {
215
        Config::inst()->update(GoogleAnalyticsReportService::class, 'blacklist', [ErrorPage::class]);
0 ignored issues
show
Bug introduced by
The type ErrorPage 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...
216
        $pages = $this->service->getPages();
217
        $this->assertTrue(is_array($pages));
218
        $this->assertNotContains('page-not-found', $pages);
219
    }
220
221
    /**
222
     * Make sure we only get whitelisted pages
223
     */
224 View Code Duplication
    public function testGetWhitelistedPages()
225
    {
226
        Config::inst()->update(GoogleAnalyticsReportService::class, 'whitelist', [Page::class]);
227
        $pages = $this->service->getPages();
228
        $this->assertTrue(is_array($pages));
229
        // There seems to be some chaining in effect, causing the array to be empty
230
        $this->assertEquals(20, count($pages));
231
        // Error Pages are children of Page, so they should be included
232
        $this->assertContains('page-not-found', $pages);
233
    }
234
}
235