Completed
Pull Request — master (#70)
by Daniel
04:46
created

URLArrayObjectTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 82
Duplicated Lines 31.71 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 26
loc 82
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testExcludeFromCacheExcludesPageURLs() 13 20 1
A testExcludeFromCacheExcludesPageURLsWithQueryStrings() 13 20 1
A testExcludeFromCachePrefersObjectAnnotationOverUrls() 0 22 1
A getProtectedOrPrivateMethod() 0 7 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
namespace SilverStripe\StaticPublishQueue\Test;
4
5
use PHPUnit_Framework_Assert;
6
use ReflectionMethod;
7
use SilverStripe\CMS\Model\SiteTree;
8
use SilverStripe\Dev\SapphireTest;
9
use SilverStripe\StaticPublishQueue\Extension\Engine\SiteTreePublishingEngine;
10
use SilverStripe\StaticPublishQueue\Model\URLArrayObject;
11
use SilverStripe\StaticPublishQueue\Test\URLArrayObjectTest\Model\URLArrayObjectTestPage;
12
13
/**
14
 * @mixin PHPUnit_Framework_Assert
15
 */
16
class URLArrayObjectTest extends SapphireTest
17
{
18
19
    protected static $fixture_file = 'URLArrayObjectTestFixture.yml';
20
    protected static $extra_dataobjects = array(URLArrayObjectTestPage::class);
21
    protected static $required_extensions = array(
22
    SiteTree::class => array(SiteTreePublishingEngine::class),
23
    );
24
25 View Code Duplication
    public function testExcludeFromCacheExcludesPageURLs()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
26
    {
27
        /**
28
 * @var URLArrayObject $urlArrayObject
29
*/
30
        $urlArrayObject = singleton('SiteTree')->urlArrayObject;
31
        $method = $this->getProtectedOrPrivateMethod('excludeFromCache');
32
33
        /**
34
 * @var SiteTree $excluded
35
*/
36
        $excluded = $this->objFromFixture(URLArrayObjectTestPage::class, 'excluded');
37
        /**
38
 * @var SiteTree $included
39
*/
40
        $included = $this->objFromFixture(URLArrayObjectTestPage::class, 'included');
41
42
        $this->assertTrue($method->invoke($urlArrayObject, $excluded->Link()));
43
        $this->assertFalse($method->invoke($urlArrayObject, $included->Link()));
44
    }
45
46 View Code Duplication
    public function testExcludeFromCacheExcludesPageURLsWithQueryStrings()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48
        /**
49
 * @var URLArrayObject $urlArrayObject
50
*/
51
        $urlArrayObject = SiteTree::singleton()->urlArrayObject;
52
        $method = $this->getProtectedOrPrivateMethod('excludeFromCache');
53
54
        /**
55
 * @var SiteTree $excluded
56
*/
57
        $excluded = $this->objFromFixture(URLArrayObjectTestPage::class, 'excluded');
58
        /**
59
 * @var SiteTree $included
60
*/
61
        $included = $this->objFromFixture(URLArrayObjectTestPage::class, 'included');
62
63
        $this->assertTrue($method->invoke($urlArrayObject, $excluded->Link() . '?query=string'));
64
        $this->assertFalse($method->invoke($urlArrayObject, $included->Link() . '?query=string'));
65
    }
66
67
    public function testExcludeFromCachePrefersObjectAnnotationOverUrls()
68
    {
69
        /**
70
 * @var URLArrayObject $urlArrayObject
71
*/
72
        $urlArrayObject = singleton('SiteTree')->urlArrayObject;
73
        $method = $this->getProtectedOrPrivateMethod('excludeFromCache');
74
75
        /**
76
 * @var SiteTree $excluded
77
*/
78
        $excluded = $this->objFromFixture(URLArrayObjectTestPage::class, 'excluded');
79
        /**
80
 * @var SiteTree $included
81
*/
82
        $included = $this->objFromFixture(URLArrayObjectTestPage::class, 'included');
83
84
        $actuallyExcludedUrl = $included->RelativeLink . "?_ID=$excluded->ID&_ClassName=$excluded->ClassName";
85
        $actuallyIncludedUrl = $excluded->RelativeLink . "?_ID=$included->ID&_ClassName=$included->ClassName";
86
        $this->assertTrue($method->invoke($urlArrayObject, $actuallyExcludedUrl));
87
        $this->assertFalse($method->invoke($urlArrayObject, $actuallyIncludedUrl));
88
    }
89
90
    private function getProtectedOrPrivateMethod($name)
91
    {
92
        $method = new ReflectionMethod(URLArrayObject::class, $name);
93
        $method->setAccessible(true);
94
95
        return $method;
96
    }
97
}
98