Completed
Push — master ( bf612a...7ad254 )
by Robbie
9s
created

testRewriteRelativeLinksAndImages()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 10

Duplication

Lines 23
Ratio 95.83 %

Importance

Changes 0
Metric Value
dl 23
loc 24
rs 8.9713
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
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 = Injector::inst()->create('AddonBuilder');
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 View Code Duplication
    public function testRewriteRelativeLinksAndImages()
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...
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
// phpcs:disable
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
// phpcs:enable
143
144
        $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...
145
    }
146
147
    /**
148
     * For non GitHub repositories, the readme input should simply be returned as is from the "replaceRelativeLinks"
149
     * method
150
     */
151 View Code Duplication
    public function testDoNotRewriteRelativeLinksForNonGitHubRepositories()
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...
152
    {
153
        $addon = Addon::create();
154
        $addon->Repository = 'https://gitlab.com/not-a/github-repo.git';
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...
155
        $readme = '<p>Please do not touch me.</p>';
156
        $this->assertSame($readme, $this->builder->replaceRelativeLinks($addon, $readme));
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...
157
    }
158
}
159