Completed
Push — master ( c682a2...eb81a1 )
by Derek
02:06
created

expectInvalidArgumentExceptionWhenConstructedWithNonString()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 10
nc 3
nop 3
crap 2
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 19
    public static function assertSameAsToString($expected, UriPartInterface $test_instance)
18
    {
19 19
        $actual = (string) $test_instance;
20 19
        self::assertSame($expected, $actual);
21 18
    }
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 4
    public static function assertNotSameAsToString($expected, UriPartInterface $test_instance)
31
    {
32 4
        $actual = (string) $test_instance;
33 4
        self::assertNotSame($expected, $actual);
34 3
    }
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 20
    public static function assertIsValid($data, $class)
43
    {
44
        /** @noinspection PhpUndefinedMethodInspection */
45 20
        $is_valid = $class::isValid($data);
46
47 20
        self::assertTrue($is_valid);
48 19
    }
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 13
    public static function assertIsNotValid($data, $class)
57
    {
58
        /** @noinspection PhpUndefinedMethodInspection */
59 13
        $is_valid = $class::isValid($data);
60
61 13
        self::assertFalse($is_valid);
62 12
    }
63
64
    /**
65
     * Tests whether the constructor of a given class throws an \InvalidArgumentException when attempting to construct
66
     * an instance with a non-string.
67
     *
68
     * @param string $class_name    The name of the class to test
69
     * @param string $data          Data to be passed to the class's constructor
70
     * @param string|null $message  [optional] The expected \InvalidArgumentException message
71
     */
72 27
    public static function expectInvalidArgumentExceptionWhenConstructedWithNonString(
73
        $class_name,
74
        $data,
75
        $message = null
76
    ) {
77
        try {
78 27
            $non_string_instance = new $class_name($data);
79 27
        } catch (\InvalidArgumentException $exception) {
80 22
            self::compareExceptionMessage($exception, $message);
81 25
        } finally {
82 27
            unset($non_string_instance);
83
        }
84 25
    }
85
86
    /**
87
     * Compares a given exceptions message with a given message string.
88
     *
89
     * @throws \PHPUnit_Framework_AssertionFailedError when exception message does not match given message
90
     *
91
     * @param \Exception $exception     An exception for message comparison
92
     * @param string $message           The intended message
93
     */
94 22
    private static function compareExceptionMessage(\Exception $exception, $message)
95
    {
96 22
        if (isset($message)) {
97 21
            self::assertSame(
98 21
                $message,
99 21
                $exception->getMessage(),
100 21
                "Failed asserting that {$exception->getMessage()} is the same as {$message}"
101 21
            );
102 19
        }
103 20
    }
104
}
105