Failed Conditions
Pull Request — master (#25)
by Chad
02:50
created

UrlTest::fromArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
namespace Chadicus\Marvel\Api\Entities;
3
4
/**
5
 * Unit tests for the Url class.
6
 *
7
 * @coversDefaultClass \Chadicus\Marvel\Api\Entities\Url
8
 * @covers ::<protected>
9
 */
10
final class UrlTest extends \PHPUnit_Framework_TestCase
11
{
12
    /**
13
     * Verify basic behavior of getUrl.
14
     *
15
     * @test
16
     *
17
     * @return void
18
     */
19
    public function getUrl()
20
    {
21
        $this->assertSame('a url', (new Url(['url' => 'a url']))->getUrl());
0 ignored issues
show
Documentation Bug introduced by
The method getUrl does not exist on object<Chadicus\Marvel\Api\Entities\Url>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
22
    }
23
24
    /**
25
     * Verify basic behavior of getType.
26
     *
27
     * @test
28
     *
29
     * @return void
30
     */
31
    public function getType()
32
    {
33
        $this->assertSame('a type', (new Url(['type' => 'a type']))->getType());
0 ignored issues
show
Documentation Bug introduced by
The method getType does not exist on object<Chadicus\Marvel\Api\Entities\Url>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
34
    }
35
36
    /**
37
     * Verify invalid constructor parameters cause exceptions.
38
     *
39
     * @param mixed $type The text identifier for the URL.
40
     * @param mixed $url  The full URL (including scheme, domain, and path).
41
     *
42
     * @test
43
     * @dataProvider constructorBadData
44
     * @expectedException \InvalidArgumentException
45
     *
46
     * @return void
47
     */
48
    public function constructWithInvalidParameters($type, $url)
49
    {
50
        new Url(['type' => $type, 'url' => $url]);
51
    }
52
53
    /**
54
     * Data provider for constructWithInvalidParameters.
55
     *
56
     * @return array
57
     */
58 View Code Duplication
    public function constructorBadData()
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...
59
    {
60
        return [
61
            'type is not a string' => [true, 'a url'],
62
            'url is not a string' => ['a type', false],
63
        ];
64
    }
65
66
    /**
67
     * Verify invalid constructor parameters cause exceptions.
68
     *
69
     * @param mixed $type The text identifier for the URL.
70
     * @param mixed $url  The full URL (including scheme, domain, and path).
71
     *
72
     * @test
73
     * @dataProvider constructorGoodData
74
     *
75
     * @return void
76
     */
77
    public function constructWithValidParameters($type, $url)
78
    {
79
        $obj = new Url(['type' => $type, 'url' => $url]);
80
        $this->assertSame($type, $obj->getType());
0 ignored issues
show
Documentation Bug introduced by
The method getType does not exist on object<Chadicus\Marvel\Api\Entities\Url>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
81
        $this->assertSame($url, $obj->getUrl());
0 ignored issues
show
Documentation Bug introduced by
The method getUrl does not exist on object<Chadicus\Marvel\Api\Entities\Url>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
82
    }
83
84
    /**
85
     * Data provider for constructWithValidParameters.
86
     *
87
     * @return array
88
     */
89 View Code Duplication
    public function constructorGoodData()
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...
90
    {
91
        return [
92
            'type is null, url is null' => [null, null],
93
            'type is string, url is null' => ['a type', null],
94
            'type is null, url is string' => [null, 'a url'],
95
            'type is string, url is string' => ['a type', 'a url'],
96
        ];
97
    }
98
99
    /**
100
     * Verify basic behavior of fromArray().
101
     *
102
     * @test
103
     *
104
     * @return void
105
     */
106
    public function fromArray()
107
    {
108
        $url = Url::fromArray(['type' => 'a type', 'url' => 'a url']);
109
        $this->assertSame('a type', $url->getType());
0 ignored issues
show
Documentation Bug introduced by
The method getType does not exist on object<Chadicus\Marvel\A...ntities\AbstractEntity>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
110
        $this->assertSame('a url', $url->getUrl());
0 ignored issues
show
Documentation Bug introduced by
The method getUrl does not exist on object<Chadicus\Marvel\A...ntities\AbstractEntity>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
111
    }
112
113
    /**
114
     * Verify fromArray throws when input is invalid.
115
     *
116
     * @test
117
     * @expectedException \Exception
118
     *
119
     * @return void
120
     */
121
    public function fromArrayWithInvalidInput()
122
    {
123
        Url::fromArray(['type' => 'a type', 'url' => true]);
124
    }
125
126
    /**
127
     * Verify basic behavior of fromArrays().
128
     *
129
     * @test
130
     *
131
     * @return void
132
     */
133 View Code Duplication
    public function fromArrays()
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...
134
    {
135
        $urls = Url::fromArrays(
136
            [
137
                ['type' => 'a type', 'url' => 'a url'],
138
                ['type' => 'another type', 'url' => 'another url'],
139
            ]
140
        );
141
142
        $this->assertSame(2, count($urls));
143
        $this->assertSame('a type', $urls[0]->getType());
0 ignored issues
show
Documentation Bug introduced by
The method getType does not exist on object<Chadicus\Marvel\A...ntities\AbstractEntity>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
144
        $this->assertSame('a url', $urls[0]->getUrl());
0 ignored issues
show
Documentation Bug introduced by
The method getUrl does not exist on object<Chadicus\Marvel\A...ntities\AbstractEntity>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
145
        $this->assertSame('another type', $urls[1]->getType());
0 ignored issues
show
Documentation Bug introduced by
The method getType does not exist on object<Chadicus\Marvel\A...ntities\AbstractEntity>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
146
        $this->assertSame('another url', $urls[1]->getUrl());
0 ignored issues
show
Documentation Bug introduced by
The method getUrl does not exist on object<Chadicus\Marvel\A...ntities\AbstractEntity>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
147
    }
148
149
    /**
150
     * Verify fromArrays throws when input is invalid.
151
     *
152
     * @test
153
     * @expectedException \Exception
154
     *
155
     * @return void
156
     */
157
    public function fromArraysWithInvalidInput()
158
    {
159
        Url::fromArrays(
160
            [
161
                ['type' => 'a type', 'url' => 'a url'],
162
                ['type' => '', 'url' => 2],
163
            ]
164
        );
165
    }
166
}
167