Passed
Push — master ( 0e5cc9...6344e6 )
by Simon
01:22
created

GoogleAnalyticsReportServiceTest   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 195
Duplicated Lines 23.08 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 45
loc 195
rs 10
wmc 18

14 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetReportRequest() 0 12 1
A setUp() 0 8 1
A testGetAnalytics() 0 3 1
A testGetPages() 0 5 1
A testGetWhitelistedPages() 9 9 1
A testGetBlacklistedPages() 6 6 1
A testGetMetrics() 0 3 1
A testSetClient() 0 4 1
A testGetDimensionFilterClauses() 0 5 1
A testGetEndDimensionFilters() 12 12 2
A testGetPageDimensionFilters() 0 8 2
A testFilterLength() 0 12 2
A testGetStartDimensionFilters() 13 13 2
A testDateRange() 0 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
84
        $this->assertInstanceOf(Google_Service_AnalyticsReporting_ReportRequest::class, $reportRequest);
85
        $this->assertInstanceOf(Google_Service_AnalyticsReporting_Metric::class, $metrics[0]);
86
        $this->assertContains('ga:pagePath', array_values($reportRequest->getDimensions()));
0 ignored issues
show
Bug introduced by
$reportRequest->getDimensions() of type Google_Service_AnalyticsReporting_Dimension is incompatible with the type array expected by parameter $input of array_values(). ( Ignorable by Annotation )

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

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