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()); |
|
|
|
|
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()); |
|
|
|
|
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() |
|
|
|
|
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()); |
|
|
|
|
81
|
|
|
$this->assertSame($url, $obj->getUrl()); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Data provider for constructWithValidParameters. |
86
|
|
|
* |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
|
View Code Duplication |
public function constructorGoodData() |
|
|
|
|
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()); |
|
|
|
|
110
|
|
|
$this->assertSame('a url', $url->getUrl()); |
|
|
|
|
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() |
|
|
|
|
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()); |
|
|
|
|
144
|
|
|
$this->assertSame('a url', $urls[0]->getUrl()); |
|
|
|
|
145
|
|
|
$this->assertSame('another type', $urls[1]->getType()); |
|
|
|
|
146
|
|
|
$this->assertSame('another url', $urls[1]->getUrl()); |
|
|
|
|
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
|
|
|
|
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: