Passed
Push — master ( c81a5e...499d21 )
by Simon
01:29
created

GoogleAnalyticsReportServiceTest   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 173
Duplicated Lines 26.01 %

Importance

Changes 0
Metric Value
dl 45
loc 173
rs 10
c 0
b 0
f 0
wmc 17

13 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testGetPages() 0 5 1
A testGetAnalytics() 0 3 1
A testGetMetrics() 0 3 1
A testSetClient() 0 4 1
A testGetDimensionFilterClauses() 0 5 1
A testGetPageDimensionFilters() 0 8 2
A testGetEndDimensionFilters() 12 12 2
A testFilterLength() 0 12 2
A testDateRange() 0 10 1
A testGetStartDimensionFilters() 13 13 2
A testGetWhitelistedPages() 9 9 1
A testGetBlacklistedPages() 6 6 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
2 ignored issues
show
Bug introduced by
The type SapphireTest 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...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
9
{
10
    /**
11
     * @var GoogleClientService
12
     */
13
    protected $client;
14
15
    /**
16
     * @var GoogleAnalyticsReportService
17
     */
18
    protected $service;
19
20
    /**
21
     * Create the Services
22
     */
23
    public function setUp()
24
    {
25
        parent::setUp();
26
        $this->client = new GoogleClientService();
27
        $this->service = new GoogleAnalyticsReportService($this->client);
28
    }
29
30
    /**
31
     * Check if we can set and get the client
32
     */
33
    public function testSetClient()
34
    {
35
        $this->service->setClient($this->client);
36
        $this->assertInstanceOf(GoogleClientService::class, $this->service->getClient());
37
    }
38
39
    /**
40
     * See if we have a new Service registered
41
     */
42
    public function testGetAnalytics()
43
    {
44
        $this->assertInstanceOf(Google_Service_AnalyticsReporting::class, $this->service->getAnalytics());
45
    }
46
47
    /**
48
     * Check if the Metric is set
49
     */
50
    public function testGetMetrics()
51
    {
52
        $this->assertInstanceOf(Google_Service_AnalyticsReporting_Metric::class, $this->service->getMetrics());
53
    }
54
55
    /**
56
     * Check if setting the daterange updates the Analytics_DateRange
57
     */
58
    public function testDateRange()
59
    {
60
        /* Set the daterange to 60 days */
61
        $this->service->setDateRange(60);
62
        /** @var Google_Service_AnalyticsReporting_DateRange $dateRange */
63
        $dateRange = $this->service->getDateRange();
64
65
        $this->assertInstanceOf(Google_Service_AnalyticsReporting_DateRange::class, $dateRange);
66
67
        $this->assertEquals('60daysAgo', $dateRange->getStartDate());
68
    }
69
70
    /**
71
     * Validate start_with filter
72
     */
73 View Code Duplication
    public function testGetStartDimensionFilters()
74
    {
75
        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...
76
77
        $filters = $this->service->getDimensionFilters();
78
79
        $this->assertTrue(is_array($filters));
80
        /** @var  $filter */
81
        foreach ($filters as $filter) {
82
            $this->assertInstanceOf(Google_Service_AnalyticsReporting_DimensionFilter::class, $filter);
83
            $expressions = $filter->getExpressions();
84
            $this->assertEquals('/', $expressions[0]);
85
            $this->assertEquals('BEGINS_WITH', $filter->getOperator());
86
        }
87
    }
88
89
    /**
90
     * validate end_with filter
91
     */
92 View Code Duplication
    public function testGetEndDimensionFilters()
93
    {
94
        Config::inst()->update(GoogleAnalyticsReportService::class, 'ends_with', '/');
95
96
        $filters = $this->service->getDimensionFilters();
97
98
        $this->assertTrue(is_array($filters));
99
        foreach ($filters as $filter) {
100
            $this->assertInstanceOf(Google_Service_AnalyticsReporting_DimensionFilter::class, $filter);
101
            $expressions = $filter->getExpressions();
102
            $this->assertEquals('/', $expressions[0]);
103
            $this->assertEquals('ENDS_WITH', $filter->getOperator());
104
        }
105
    }
106
107
    /**
108
     * Test page filters are created
109
     */
110
    public function testGetPageDimensionFilters()
111
    {
112
        $filters = $this->service->getDimensionFilters();
113
114
        $this->assertTrue(is_array($filters));
115
        foreach ($filters as $filter) {
116
            $this->assertInstanceOf(Google_Service_AnalyticsReporting_DimensionFilter::class, $filter);
117
            $this->assertTrue(in_array($filter->getOperator(), ['ENDS_WITH', 'EXACT'], true));
118
        }
119
    }
120
121
    /**
122
     * Validate our DimensionClause is set with OR
123
     */
124
    public function testGetDimensionFilterClauses()
125
    {
126
        $dimensionsClauses = $this->service->getDimensionFilterClauses();
127
        $this->assertInstanceOf(Google_Service_AnalyticsReporting_DimensionFilterClause::class, $dimensionsClauses);
128
        $this->assertEquals('OR', $dimensionsClauses->getOperator());
129
    }
130
131
    /**
132
     * Make sure the filter-list does not exceed 20
133
     */
134
    public function testGetPages()
135
    {
136
        $pages = $this->service->getPages();
137
        $this->assertTrue(is_array($pages));
138
        $this->assertLessThanOrEqual(20, count($pages));
139
    }
140
141
    /**
142
     * Make sure we're not adding more than 20 filters, despite more pages being available
143
     */
144
    public function testFilterLength()
145
    {
146
        for ($i = 1; $i <= 30; $i++) {
147
            /** @var Page` $page */
148
            $page = Page::create(['Title' => $i . ' test']);
149
            $page->write();
150
            $page->doPublish();
151
        }
152
        $pages = $this->service->getPages();
153
        $this->assertTrue(is_array($pages));
154
        $this->assertCount(20, $pages);
155
        $this->assertTrue($this->service->batched);
156
    }
157
158
    /**
159
     * Validate blacklisted pages don't show up
160
     */
161 View Code Duplication
    public function testGetBlacklistedPages()
162
    {
163
        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...
164
        $pages = $this->service->getPages();
165
        $this->assertTrue(is_array($pages));
166
        $this->assertNotContains('page-not-found', $pages);
167
    }
168
169
    /**
170
     * Make sure we only get whitelisted pages
171
     */
172 View Code Duplication
    public function testGetWhitelistedPages()
173
    {
174
        Config::inst()->update(GoogleAnalyticsReportService::class, 'whitelist', [Page::class]);
175
        $pages = $this->service->getPages();
176
        $this->assertTrue(is_array($pages));
177
        // There seems to be some chaining in effect, causing the array to be empty
178
        $this->assertEquals(20, count($pages));
179
        // Error Pages are children of Page, so they should be included
180
        $this->assertContains('page-not-found', $pages);
181
    }
182
}
183