|
1
|
|
|
<?php |
|
2
|
|
|
namespace Subreality\Dilmun\Anshar\Http\UriParts; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Class Assertions |
|
6
|
|
|
* @package Subreality\Dilmun\Anshar\PhpUnit |
|
7
|
|
|
*/ |
|
8
|
|
|
class UriPartTestCase extends \PHPUnit_Framework_TestCase |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Asserts that the given expected string is the same as the string returned by an object instance's __toString |
|
12
|
|
|
* magic method. |
|
13
|
|
|
* |
|
14
|
|
|
* @throws \PHPUnit_Framework_ExpectationFailedException provided an instance of an object with no __toString |
|
15
|
|
|
* method. |
|
16
|
|
|
* |
|
17
|
|
|
* @param string $expected |
|
18
|
|
|
* @param object $test_instance |
|
19
|
|
|
*/ |
|
20
|
10 |
|
public static function assertSameAsToString($expected, $test_instance) |
|
21
|
|
|
{ |
|
22
|
|
|
try { |
|
23
|
10 |
|
$actual = (string) $test_instance; |
|
24
|
9 |
|
self::assertSame($expected, $actual); |
|
25
|
10 |
|
} catch (\PHPUnit_Framework_Error $error) { |
|
26
|
1 |
|
throw new \PHPUnit_Framework_ExpectationFailedException("Cannot convert object to string"); |
|
27
|
|
|
} |
|
28
|
8 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Tests whether the constructor of a given class throws an \InvalidArgumentException when attempting to construct |
|
32
|
|
|
* an instance with a non-string. |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $class_name The name of the class to test |
|
35
|
|
|
* @param string $data Data to be passed to the class's constructor |
|
36
|
|
|
* @param string|null $message [optional] The expected \InvalidArgumentException message |
|
37
|
|
|
*/ |
|
38
|
27 |
|
public static function expectInvalidArgumentExceptionWhenConstructedWithNonString( |
|
39
|
|
|
$class_name, |
|
40
|
|
|
$data, |
|
41
|
|
|
$message = null |
|
42
|
|
|
) { |
|
43
|
27 |
|
$exception_thrown = false; |
|
44
|
|
|
|
|
45
|
|
|
try { |
|
46
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */ |
|
47
|
27 |
|
$non_string_instance = new $class_name($data); |
|
|
|
|
|
|
48
|
27 |
|
} catch (\InvalidArgumentException $exception) { |
|
49
|
21 |
|
$exception_thrown = true; |
|
50
|
|
|
|
|
51
|
21 |
|
if (isset($message)) { |
|
52
|
20 |
|
self::assertSame( |
|
53
|
20 |
|
$message, |
|
54
|
20 |
|
$exception->getMessage(), |
|
55
|
20 |
|
"Failed asserting that {$exception->getMessage()} is equal to {$message}" |
|
56
|
20 |
|
); |
|
57
|
19 |
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
26 |
|
if (!is_string($data) && !$exception_thrown) { |
|
61
|
1 |
|
throw new \PHPUnit_Framework_ExpectationFailedException("Expected exception not thrown"); |
|
62
|
|
|
} |
|
63
|
25 |
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.