Passed
Push — master ( 6344e6...ea3537 )
by Simon
01:26
created

GoogleAnalyticsReportServiceTest::testGetPages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
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
        $this->client = new GoogleClientService();
29
        $this->service = new GoogleAnalyticsReportService($this->client);
30
        $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...
31
        $siteConfig->Viewid = static::VIEWID;
32
        $siteConfig->write();
33
    }
34
35
    /**
36
     * Check if we can set and get the client
37
     */
38
    public function testSetClient()
39
    {
40
        $this->service->setClient($this->client);
41
        $this->assertInstanceOf(GoogleClientService::class, $this->service->getClient());
42
    }
43
44
    /**
45
     * See if we have a new Service registered
46
     */
47
    public function testGetAnalytics()
48
    {
49
        $this->assertInstanceOf(Google_Service_AnalyticsReporting::class, $this->service->getAnalytics());
50
    }
51
52
    /**
53
     * Check if the Metric is set
54
     */
55
    public function testGetMetrics()
56
    {
57
        $this->assertInstanceOf(Google_Service_AnalyticsReporting_Metric::class, $this->service->getMetrics());
58
    }
59
60
    /**
61
     * Check if setting the daterange updates the Analytics_DateRange
62
     */
63
    public function testDateRange()
64
    {
65
        /* Set the daterange to 60 days */
66
        $this->service->setDateRange(60);
67
        /** @var Google_Service_AnalyticsReporting_DateRange $dateRange */
68
        $dateRange = $this->service->getDateRange();
69
70
        $this->assertInstanceOf(Google_Service_AnalyticsReporting_DateRange::class, $dateRange);
71
72
        $this->assertEquals('60daysAgo', $dateRange->getStartDate());
73
    }
74
75
    /**
76
     * Confirm the report request is correctly build
77
     */
78
    public function testGetReportRequest()
79
    {
80
        $reportRequest = $this->service->getReportRequest();
81
82
        $metrics = $reportRequest->getMetrics();
83
        $dimensions = $reportRequest->getDimensions();
84
85
        $this->assertInstanceOf(Google_Service_AnalyticsReporting_ReportRequest::class, $reportRequest);
86
        $this->assertTrue(is_array($metrics));
87
        $this->assertInstanceOf(Google_Service_AnalyticsReporting_Metric::class, $metrics[0]);
88
        $this->assertTrue(is_array($dimensions));
89
        $this->assertEquals('ga:pagePath', $dimensions[0]->getName());
0 ignored issues
show
Bug introduced by
The method getName() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

89
        $this->assertEquals('ga:pagePath', $dimensions[0]->/** @scrutinizer ignore-call */ getName());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
90
        $this->assertInstanceOf(Google_Service_AnalyticsReporting_DimensionFilterClause::class, $reportRequest->getDimensionFilterClauses());
91
        $this->assertInstanceOf(Google_Service_AnalyticsReporting_DateRange::class, $reportRequest->getDateRanges());
92
        $this->assertEquals(static::VIEWID, $reportRequest->getViewId());
93
    }
94
95
    public function testGetDimension()
96
    {
97
        $dimensions = $this->service->getDimensions();
98
        $this->assertTrue(is_array($dimensions));
99
        foreach ($dimensions as $dimension) {
100
            $this->assertEquals('ga:pagePath', $dimension->getName());
101
        }
102
    }
103
104
    /**
105
     * Validate start_with filter
106
     */
107 View Code Duplication
    public function testGetStartDimensionFilters()
108
    {
109
        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...
110
111
        $filters = $this->service->getDimensionFilters();
112
113
        $this->assertTrue(is_array($filters));
114
        /** @var  $filter */
115
        foreach ($filters as $filter) {
116
            $this->assertInstanceOf(Google_Service_AnalyticsReporting_DimensionFilter::class, $filter);
117
            $expressions = $filter->getExpressions();
118
            $this->assertEquals('/', $expressions[0]);
119
            $this->assertEquals('BEGINS_WITH', $filter->getOperator());
120
        }
121
    }
122
123
    /**
124
     * validate end_with filter
125
     */
126 View Code Duplication
    public function testGetEndDimensionFilters()
127
    {
128
        Config::inst()->update(GoogleAnalyticsReportService::class, 'ends_with', '/');
129
130
        $filters = $this->service->getDimensionFilters();
131
132
        $this->assertTrue(is_array($filters));
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('ENDS_WITH', $filter->getOperator());
138
        }
139
    }
140
141
    /**
142
     * Test page filters are created
143
     */
144
    public function testGetPageDimensionFilters()
145
    {
146
        $filters = $this->service->getDimensionFilters();
147
148
        $this->assertTrue(is_array($filters));
149
        foreach ($filters as $filter) {
150
            $this->assertInstanceOf(Google_Service_AnalyticsReporting_DimensionFilter::class, $filter);
151
            $this->assertTrue(in_array($filter->getOperator(), ['ENDS_WITH', 'EXACT'], true));
152
        }
153
    }
154
155
    /**
156
     * Validate our DimensionClause is set with OR
157
     */
158
    public function testGetDimensionFilterClauses()
159
    {
160
        $dimensionsClauses = $this->service->getDimensionFilterClauses();
161
        $this->assertInstanceOf(Google_Service_AnalyticsReporting_DimensionFilterClause::class, $dimensionsClauses);
162
        $this->assertEquals('OR', $dimensionsClauses->getOperator());
163
    }
164
165
    /**
166
     * Make sure the filter-list does not exceed 20
167
     */
168
    public function testGetPages()
169
    {
170
        $pages = $this->service->getPages();
171
        $this->assertTrue(is_array($pages));
172
        $this->assertLessThanOrEqual(20, count($pages));
173
    }
174
175
    /**
176
     * Make sure we're not adding more than 20 filters, despite more pages being available
177
     */
178
    public function testFilterLength()
179
    {
180
        for ($i = 1; $i <= 30; $i++) {
181
            /** @var Page` $page */
182
            $page = Page::create(['Title' => $i . ' test']);
183
            $page->write();
184
            $page->doPublish();
185
        }
186
        $pages = $this->service->getPages();
187
        $this->assertTrue(is_array($pages));
188
        $this->assertCount(20, $pages);
189
        $this->assertTrue($this->service->batched);
190
    }
191
192
    /**
193
     * Validate blacklisted pages don't show up
194
     */
195 View Code Duplication
    public function testGetBlacklistedPages()
196
    {
197
        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...
198
        $pages = $this->service->getPages();
199
        $this->assertTrue(is_array($pages));
200
        $this->assertNotContains('page-not-found', $pages);
201
    }
202
203
    /**
204
     * Make sure we only get whitelisted pages
205
     */
206 View Code Duplication
    public function testGetWhitelistedPages()
207
    {
208
        Config::inst()->update(GoogleAnalyticsReportService::class, 'whitelist', [Page::class]);
209
        $pages = $this->service->getPages();
210
        $this->assertTrue(is_array($pages));
211
        // There seems to be some chaining in effect, causing the array to be empty
212
        $this->assertEquals(20, count($pages));
213
        // Error Pages are children of Page, so they should be included
214
        $this->assertContains('page-not-found', $pages);
215
    }
216
}
217