|
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
|
|
|
* @param string $expected The string expected to match |
|
15
|
|
|
* @param UriPartInterface $test_instance An instance of the object whose __toString method is under test |
|
16
|
|
|
*/ |
|
17
|
89 |
|
public static function assertSameAsToString($expected, UriPartInterface $test_instance) |
|
18
|
|
|
{ |
|
19
|
89 |
|
$actual = (string) $test_instance; |
|
20
|
89 |
|
self::assertSame($expected, $actual); |
|
21
|
88 |
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Asserts that the given expected string is not the same as the string returned by an object instance's __toString |
|
25
|
|
|
* magic method. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $expected The string expected not to match |
|
28
|
|
|
* @param UriPartInterface $test_instance An instance of the object whose __toString method is under test |
|
29
|
|
|
*/ |
|
30
|
2 |
|
public static function assertNotSameAsToString($expected, UriPartInterface $test_instance) |
|
31
|
|
|
{ |
|
32
|
2 |
|
$actual = (string) $test_instance; |
|
33
|
2 |
|
self::assertNotSame($expected, $actual); |
|
34
|
1 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Asserts that the given data is valid for the given class. |
|
38
|
|
|
* |
|
39
|
|
|
* @param mixed $data The data expected to be valid |
|
40
|
|
|
* @param string $class The class whose isValid method is under test |
|
41
|
|
|
*/ |
|
42
|
59 |
|
public static function assertIsValid($data, $class) |
|
43
|
|
|
{ |
|
44
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
45
|
59 |
|
$is_valid = $class::isValid($data); |
|
46
|
|
|
|
|
47
|
59 |
|
self::assertTrue($is_valid); |
|
48
|
58 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Asserts that the given data is not valid for the given class. |
|
52
|
|
|
* |
|
53
|
|
|
* @param mixed $data The data expected to be not valid |
|
54
|
|
|
* @param string $class The class whose isValid method is under test |
|
55
|
|
|
*/ |
|
56
|
32 |
|
public static function assertIsNotValid($data, $class) |
|
57
|
|
|
{ |
|
58
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
59
|
32 |
|
$is_valid = $class::isValid($data); |
|
60
|
|
|
|
|
61
|
32 |
|
self::assertFalse($is_valid); |
|
62
|
31 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Tests whether the constructor of a given class throws an \InvalidArgumentException when attempting to construct |
|
66
|
|
|
* an instance with invalid data. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $class_name The name of the class to test |
|
69
|
|
|
* @param mixed $data Data to be passed to the class's constructor |
|
70
|
|
|
* @param string|null $message [optional] The expected \InvalidArgumentException message |
|
71
|
|
|
*/ |
|
72
|
58 |
|
public static function expectInvalidArgumentExceptionWhenConstructedWithInvalidData( |
|
73
|
|
|
$class_name, |
|
74
|
|
|
$data, |
|
75
|
|
|
$message = null |
|
76
|
|
|
) { |
|
77
|
58 |
|
$exception_thrown = false; |
|
78
|
|
|
|
|
79
|
|
|
try { |
|
80
|
58 |
|
$non_string_instance = new $class_name($data); |
|
81
|
58 |
|
} catch (\InvalidArgumentException $exception) { |
|
82
|
56 |
|
self::compareExceptionMessage($exception, $message); |
|
83
|
|
|
|
|
84
|
54 |
|
$exception_thrown = true; |
|
85
|
56 |
|
} finally { |
|
86
|
58 |
|
unset($non_string_instance); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
56 |
|
if (!$exception_thrown) { |
|
90
|
2 |
|
throw new \PHPUnit_Framework_ExpectationFailedException("Expected InvalidArgumentException not thrown"); |
|
91
|
|
|
} |
|
92
|
54 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Compares a given exceptions message with a given message string. |
|
96
|
|
|
* |
|
97
|
|
|
* @throws \PHPUnit_Framework_AssertionFailedError when exception message does not match given message |
|
98
|
|
|
* |
|
99
|
|
|
* @param \Exception $exception An exception for message comparison |
|
100
|
|
|
* @param string $message The intended message |
|
101
|
|
|
*/ |
|
102
|
56 |
|
private static function compareExceptionMessage(\Exception $exception, $message) |
|
103
|
|
|
{ |
|
104
|
56 |
|
if (isset($message)) { |
|
105
|
52 |
|
self::assertSame( |
|
106
|
52 |
|
$message, |
|
107
|
52 |
|
$exception->getMessage(), |
|
108
|
52 |
|
"Failed asserting that {$exception->getMessage()} is the same as {$message}" |
|
109
|
52 |
|
); |
|
110
|
50 |
|
} |
|
111
|
54 |
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|