Completed
Pull Request — master (#143)
by Robbie
01:49
created

AddonBuilderTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 114
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 10 1
A testHasGitHubRepository() 0 8 1
A hasGitHubProvider() 0 13 1
A testIsRelativeUri() 0 4 1
A uriProvider() 0 12 1
A testRewriteRelativeLinksAndImages() 0 23 1
1
<?php
2
/**
3
 * Tests for the AddonBuilder
4
 *
5
 * @package mysite
6
 * @coversDefaultClass AddonBuilder
7
 */
8
class AddonBuilderTest extends SapphireTest
9
{
10
    /**
11
     * @var AddonBuilder
12
     */
13
    protected $builder;
14
15
    /**
16
     * Get the test subject
17
     */
18
    public function setUp()
19
    {
20
        parent::setUp();
21
22
        // Partially mocked as we don't care about the PackagistService at this point
23
        $this->builder = $this->getMockBuilder('AddonBuilder')
0 ignored issues
show
Bug introduced by
The method getMockBuilder() does not seem to exist on object<AddonBuilderTest>.

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...
24
            ->setMethods(null)
25
            ->disableOriginalConstructor()
26
            ->getMock();
27
    }
28
29
    /**
30
     * Test that the GitHub-ness of an addon's repository can be correctly established
31
     *
32
     * @param string $repository
33
     * @param bool   $expected
34
     * @covers ::hasGitHubRepository
35
     * @dataProvider hasGitHubProvider
36
     */
37
    public function testHasGitHubRepository($repository, $expected)
38
    {
39
        $addon = Addon::create();
40
        $addon->Repository = $repository;
0 ignored issues
show
Documentation introduced by
The property Repository does not exist on object<Addon>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
41
42
        $result = $this->builder->hasGitHubRepository($addon);
43
        $this->assertSame($expected, $result);
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<AddonBuilderTest>.

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...
44
    }
45
46
    /**
47
     * @return array
48
     */
49
    public function hasGitHubProvider()
50
    {
51
        return array(
52
            array(
53
                'https://github.com/silverstripe/silverstripe-framework',
54
                true
55
            ),
56
            array(
57
                'https://bitbucket.org/some/otherrepo',
58
                false
59
            )
60
        );
61
    }
62
63
    /**
64
     * Test that we can determine the differece between a relative-ish URI and one that isn't, so we know
65
     * when to insert the GitHub repository URL into the mix.
66
     *
67
     * @param string $uri
68
     * @param bool   $expected
69
     * @covers ::isRelativeUri
70
     * @dataProvider uriProvider()
71
     */
72
    public function testIsRelativeUri($uri, $expected)
73
    {
74
        $this->assertSame($expected, $this->builder->isRelativeUri($uri));
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<AddonBuilderTest>.

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...
75
    }
76
77
    /**
78
     * @return array
79
     */
80
    public function uriProvider()
81
    {
82
        return array(
83
            array('/add-ons/silverstripe/sapphire#-preview', false),
84
            array('#installation-with-composer', false),
85
            array('resources/example.png?raw=true', true),
86
            array('add-ons/silverstripe/sapphire#usage', true),
87
            array('//add-ons/silverstripe/sapphire#usage', false),
88
            array('https://silverstripe.mit-license.org/', false),
89
            array('http://silverstripe.mit-license.org/', false)
90
        );
91
    }
92
93
    /**
94
     * Ensure that a HTML readme can have its relative links rewritten according to the Addon is belongs to
95
     *
96
     * @covers ::replaceRelativeLinks
97
     */
98
    public function testRewriteRelativeLinksAndImages()
99
    {
100
        $addon = Addon::create();
101
        $addon->Repository = 'https://github.com/silverstripe/silverstripe-framework';
0 ignored issues
show
Documentation introduced by
The property Repository does not exist on object<Addon>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
102
103
        $input = <<<HTML
104
<h1>Heading</h1>
105
106
<p><a href="relative">Relative</a> and <a href="//absolute.com">absolute</a>.</p>
107
108
<p><img src="relative.png"><img src="https://www.whatever.com/image.png"></p>
109
HTML;
110
111
        $expected = <<<HTML
112
<h1>Heading</h1>
113
114
<p><a href="https://github.com/silverstripe/silverstripe-framework/blob/master/relative">Relative</a> and <a href="//absolute.com">absolute</a>.</p>
115
116
<p><img src="https://github.com/silverstripe/silverstripe-framework/raw/master/relative.png"><img src="https://www.whatever.com/image.png"></p>
117
HTML;
118
119
        $this->assertSame($expected, $this->builder->replaceRelativeLinks($addon, $input));
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<AddonBuilderTest>.

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...
120
    }
121
}
122