Completed
Push — master ( b6038c...004e03 )
by Daniel
01:58
created

AddonBuilderTest::testGetGitHubContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
/**
3
 * Tests for the AddonBuilder
4
 *
5
 * @package mysite
6
 */
7
class AddonBuilderTest extends SapphireTest
8
{
9
    /**
10
     * @var AddonBuilder
11
     */
12
    protected $builder;
13
14
    /**
15
     * Get the test subject
16
     */
17
    public function setUp()
18
    {
19
        parent::setUp();
20
21
        // Partially mocked as we don't care about the PackagistService at this point
22
        $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...
23
            ->setMethods(null)
24
            ->disableOriginalConstructor()
25
            ->getMock();
26
    }
27
28
    /**
29
     * Test that a GitHub repository can be identified, and have its context returned if it matches
30
     *
31
     * @param string $input
32
     * @param string|false $expected
33
     * @dataProvider repositoryContextProvider
34
     */
35
    public function testGetGitHubContext($input, $expected)
36
    {
37
        $addon = new Addon(array('Repository' => $input));
38
        $builder = new AddonBuilder(new PackagistService);
39
        $result = $builder->getGitHubContext($addon);
40
        $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...
41
    }
42
43
    public function repositoryContextProvider()
44
    {
45
        return array(
46
            array('https://github.com/silverstripe/addons.org.git', 'silverstripe/addons.org'),
47
            array('http://github.com/silverstripe/addons.org.git', 'silverstripe/addons.org'),
48
            array('https://github.com/silverstripe/sspak.git', 'silverstripe/sspak'),
49
            array('http://github.com/silverstripe/sspak.git', 'silverstripe/sspak')
50
        );
51
    }
52
53
    /**
54
     * Test that the GitHub-ness of an addon's repository can be correctly established
55
     *
56
     * @param string $repository
57
     * @param bool   $expected
58
     * @covers ::hasGitHubRepository
59
     * @dataProvider hasGitHubProvider
60
     */
61
    public function testHasGitHubRepository($repository, $expected)
62
    {
63
        $addon = Addon::create();
64
        $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...
65
66
        $result = $this->builder->hasGitHubRepository($addon);
67
        $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...
68
    }
69
70
    /**
71
     * @return array
72
     */
73
    public function hasGitHubProvider()
74
    {
75
        return array(
76
            array(
77
                'https://github.com/silverstripe/silverstripe-framework',
78
                true
79
            ),
80
            array(
81
                'https://bitbucket.org/some/otherrepo',
82
                false
83
            )
84
        );
85
    }
86
87
    /**
88
     * Test that we can determine the differece between a relative-ish URI and one that isn't, so we know
89
     * when to insert the GitHub repository URL into the mix.
90
     *
91
     * @param string $uri
92
     * @param bool   $expected
93
     * @covers ::isRelativeUri
94
     * @dataProvider uriProvider()
95
     */
96
    public function testIsRelativeUri($uri, $expected)
97
    {
98
        $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...
99
    }
100
101
    /**
102
     * @return array
103
     */
104
    public function uriProvider()
105
    {
106
        return array(
107
            array('/add-ons/silverstripe/sapphire#-preview', false),
108
            array('#installation-with-composer', false),
109
            array('resources/example.png?raw=true', true),
110
            array('add-ons/silverstripe/sapphire#usage', true),
111
            array('//add-ons/silverstripe/sapphire#usage', false),
112
            array('https://silverstripe.mit-license.org/', false),
113
            array('http://silverstripe.mit-license.org/', false)
114
        );
115
    }
116
117
    /**
118
     * Ensure that a HTML readme can have its relative links rewritten according to the Addon is belongs to
119
     *
120
     * @covers ::replaceRelativeLinks
121
     */
122
    public function testRewriteRelativeLinksAndImages()
123
    {
124
        $addon = Addon::create();
125
        $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...
126
127
        $input = <<<HTML
128
<h1>Heading</h1>
129
130
<p><a href="relative">Relative</a> and <a href="//absolute.com">absolute</a>.</p>
131
132
<p><img src="relative.png"><img src="https://www.whatever.com/image.png"></p>
133
HTML;
134
135
        $expected = <<<HTML
136
<h1>Heading</h1>
137
138
<p><a href="https://github.com/silverstripe/silverstripe-framework/blob/master/relative">Relative</a> and <a href="//absolute.com">absolute</a>.</p>
139
140
<p><img src="https://github.com/silverstripe/silverstripe-framework/raw/master/relative.png"><img src="https://www.whatever.com/image.png"></p>
141
HTML;
142
143
        $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...
144
    }
145
}
146