Completed
Push — master ( 672a9c...7e2a8c )
by
unknown
13s queued 12s
created

UriImmutableTest::testGetQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * @copyright  Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
4
 * @license    GNU General Public License version 2 or later; see LICENSE
5
 */
6
7
namespace Joomla\Uri\Tests;
8
9
use Joomla\Uri\UriImmutable;
10
use PHPUnit\Framework\TestCase;
11
12
/**
13
 * Tests for the Joomla\Uri\UriImmutable class.
14
 *
15
 * @since  1.0
16
 */
17
class UriImmutableTest extends TestCase
18
{
19
	/**
20
	 * Object under test
21
	 *
22
	 * @var    UriImmutable
23
	 * @since  1.0
24
	 */
25
	protected $object;
26
27
	/**
28
	 * Sets up the fixture, for example, opens a network connection.
29
	 * This method is called before a test is executed.
30
	 *
31
	 * @return  void
32
	 *
33
	 * @since   1.0
34
	 */
35
	protected function setUp()
36
	{
37
		$this->object = new UriImmutable('http://someuser:[email protected]:80/path/file.html?var=value#fragment');
38
	}
39
40
	/**
41
	 * Tests the __set method. Immutable objects will throw
42
	 * an exception when you try to change a property.
43
	 *
44
	 * @return  void
45
	 *
46
	 * @since   1.2.0
47
	 * @covers  Joomla\Uri\UriImmutable::__set
48
	 * @expectedException \BadMethodCallException
49
	 */
50
	public function test__set()
51
	{
52
		$this->object->uri = 'http://someuser:[email protected]:80/path/file.html?var=value#fragment';
0 ignored issues
show
Documentation introduced by
The property $uri is declared protected in Joomla\Uri\AbstractUri. Since you implemented __set(), maybe consider adding a @property or @property-write annotation. This makes it easier for IDEs to provide auto-completion.

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...
53
	}
54
55
	/**
56
	 * Test the __toString method.
57
	 *
58
	 * @return  void
59
	 *
60
	 * @since   1.0
61
	 * @covers  Joomla\Uri\UriImmutable::__toString
62
	 */
63
	public function test__toString()
64
	{
65
		$this->assertThat(
66
			$this->object->__toString(),
67
			$this->equalTo('http://someuser:[email protected]:80/path/file.html?var=value#fragment')
68
		);
69
	}
70
71
	/**
72
	 * Test the toString method.
73
	 *
74
	 * @return  void
75
	 *
76
	 * @since   1.0
77
	 * @covers  Joomla\Uri\UriImmutable::toString
78
	 */
79
	public function testToString()
80
	{
81
		$classname = \get_class($this->object);
0 ignored issues
show
Unused Code introduced by
$classname is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
82
83
		// The next 2 tested functions should generate equivalent results
84
		$this->assertThat(
85
			$this->object->toString(),
86
			$this->equalTo('http://someuser:[email protected]:80/path/file.html?var=value#fragment')
87
		);
88
89
		$this->assertThat(
90
			$this->object->toString(array('scheme', 'user', 'pass', 'host', 'port', 'path', 'query', 'fragment')),
91
			$this->equalTo('http://someuser:[email protected]:80/path/file.html?var=value#fragment')
92
		);
93
94
		$this->assertThat(
95
			$this->object->toString(array('scheme')),
96
			$this->equalTo('http://')
97
		);
98
99
		$this->assertThat(
100
			$this->object->toString(array('host', 'port')),
101
			$this->equalTo('www.example.com:80')
102
		);
103
104
		$this->assertThat(
105
			$this->object->toString(array('path', 'query', 'fragment')),
106
			$this->equalTo('/path/file.html?var=value#fragment')
107
		);
108
109
		$this->assertThat(
110
			$this->object->toString(array('user', 'pass', 'host', 'port', 'path', 'query', 'fragment')),
111
			$this->equalTo('someuser:[email protected]:80/path/file.html?var=value#fragment')
112
		);
113
	}
114
115
	/**
116
	 * Test the render method.
117
	 *
118
	 * @return  void
119
	 *
120
	 * @since   1.2.0
121
	 * @covers  Joomla\Uri\UriImmutable::render
122
	 */
123
	public function testRender()
124
	{
125
		$classname = \get_class($this->object);
126
127
		$this->assertThat(
128
			$this->object->render($classname::ALL),
129
			$this->equalTo('http://someuser:[email protected]:80/path/file.html?var=value#fragment')
130
		);
131
132
		$this->assertThat(
133
			$this->object->render($classname::SCHEME),
134
			$this->equalTo('http://')
135
		);
136
137
		$this->assertThat(
138
			$this->object->render($classname::HOST | $classname::PORT),
139
			$this->equalTo('www.example.com:80')
140
		);
141
142
		$this->assertThat(
143
			$this->object->render($classname::PATH | $classname::QUERY | $classname::FRAGMENT),
144
			$this->equalTo('/path/file.html?var=value#fragment')
145
		);
146
147
		$this->assertThat(
148
			$this->object->render($classname::ALL & ~$classname::SCHEME),
149
			$this->equalTo('someuser:[email protected]:80/path/file.html?var=value#fragment')
150
		);
151
	}
152
153
	/**
154
	 * Test the hasVar method.
155
	 *
156
	 * @return  void
157
	 *
158
	 * @since   1.0
159
	 * @covers  Joomla\Uri\UriImmutable::hasVar
160
	 */
161 View Code Duplication
	public function testHasVar()
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...
162
	{
163
		$this->assertThat(
164
			$this->object->hasVar('somevar'),
165
			$this->equalTo(false)
166
		);
167
168
		$this->assertThat(
169
			$this->object->hasVar('var'),
170
			$this->equalTo(true)
171
		);
172
	}
173
174
	/**
175
	 * Test the getVar method.
176
	 *
177
	 * @return  void
178
	 *
179
	 * @since   1.0
180
	 * @covers  Joomla\Uri\UriImmutable::getVar
181
	 */
182 View Code Duplication
	public function testGetVar()
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...
183
	{
184
		$this->assertThat(
185
			$this->object->getVar('var'),
186
			$this->equalTo('value')
187
		);
188
189
		$this->assertThat(
190
			$this->object->getVar('var2'),
191
			$this->equalTo('')
192
		);
193
194
		$this->assertThat(
195
			$this->object->getVar('var2', 'default'),
196
			$this->equalTo('default')
197
		);
198
	}
199
200
	/**
201
	 * Test the getQuery method.
202
	 *
203
	 * @return  void
204
	 *
205
	 * @since   1.0
206
	 * @covers  Joomla\Uri\UriImmutable::getQuery
207
	 */
208
	public function testGetQuery()
209
	{
210
		$this->assertThat(
211
			$this->object->getQuery(),
212
			$this->equalTo('var=value')
213
		);
214
215
		$this->assertThat(
216
			$this->object->getQuery(true),
217
			$this->equalTo(array('var' => 'value'))
218
		);
219
	}
220
221
	/**
222
	 * Test the getScheme method.
223
	 *
224
	 * @return  void
225
	 *
226
	 * @since   1.0
227
	 * @covers  Joomla\Uri\UriImmutable::getScheme
228
	 */
229
	public function testGetScheme()
230
	{
231
		$this->assertThat(
232
			$this->object->getScheme(),
233
			$this->equalTo('http')
234
		);
235
	}
236
237
	/**
238
	 * Test the getUser method.
239
	 *
240
	 * @return  void
241
	 *
242
	 * @since   1.0
243
	 * @covers  Joomla\Uri\UriImmutable::getUser
244
	 */
245
	public function testGetUser()
246
	{
247
		$this->assertThat(
248
			$this->object->getUser(),
249
			$this->equalTo('someuser')
250
		);
251
	}
252
253
	/**
254
	 * Test the getPass method.
255
	 *
256
	 * @return  void
257
	 *
258
	 * @since   1.0
259
	 * @covers  Joomla\Uri\UriImmutable::getPass
260
	 */
261
	public function testGetPass()
262
	{
263
		$this->assertThat(
264
			$this->object->getPass(),
265
			$this->equalTo('somepass')
266
		);
267
	}
268
269
	/**
270
	 * Test the getHost method.
271
	 *
272
	 * @return  void
273
	 *
274
	 * @since   1.0
275
	 * @covers  Joomla\Uri\UriImmutable::getHost
276
	 */
277
	public function testGetHost()
278
	{
279
		$this->assertThat(
280
			$this->object->getHost(),
281
			$this->equalTo('www.example.com')
282
		);
283
	}
284
285
	/**
286
	 * Test the getPort method.
287
	 *
288
	 * @return  void
289
	 *
290
	 * @since   1.0
291
	 * @covers  Joomla\Uri\UriImmutable::getPort
292
	 */
293
	public function testGetPort()
294
	{
295
		$this->assertThat(
296
			$this->object->getPort(),
297
			$this->equalTo('80')
298
		);
299
	}
300
301
	/**
302
	 * Test the getPath method.
303
	 *
304
	 * @return  void
305
	 *
306
	 * @since   1.0
307
	 * @covers  Joomla\Uri\UriImmutable::getPath
308
	 */
309
	public function testGetPath()
310
	{
311
		$this->assertThat(
312
			$this->object->getPath(),
313
			$this->equalTo('/path/file.html')
314
		);
315
	}
316
317
	/**
318
	 * Test the getFragment method.
319
	 *
320
	 * @return  void
321
	 *
322
	 * @since   1.0
323
	 * @covers  Joomla\Uri\UriImmutable::getFragment
324
	 */
325
	public function testGetFragment()
326
	{
327
		$this->assertThat(
328
			$this->object->getFragment(),
329
			$this->equalTo('fragment')
330
		);
331
	}
332
333
	/**
334
	 * Test the isSsl method.
335
	 *
336
	 * @return  void
337
	 *
338
	 * @since   1.0
339
	 * @covers  Joomla\Uri\UriImmutable::isSsl
340
	 */
341
	public function testisSsl()
342
	{
343
		$this->object = new UriImmutable('https://someuser:[email protected]:80/path/file.html?var=value#fragment');
344
345
		$this->assertThat(
346
			$this->object->isSsl(),
347
			$this->equalTo(true)
348
		);
349
350
		$this->object = new UriImmutable('http://someuser:[email protected]:80/path/file.html?var=value#fragment');
351
352
		$this->assertThat(
353
			$this->object->isSsl(),
354
			$this->equalTo(false)
355
		);
356
	}
357
}
358