Completed
Push — master ( b51c2c...3c115e )
by Derek
02:17
created

UriPartTestCase::assertNotSameAsToString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
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 9
    public static function assertSameAsToString($expected, UriPartInterface $test_instance)
18
    {
19 9
        $actual = (string) $test_instance;
20 9
        self::assertSame($expected, $actual);
21 8
    }
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 2
    public static function assertIsValid($data, $class)
43
    {
44
        /** @noinspection PhpUndefinedMethodInspection */
45 2
        $is_valid = $class::isValid($data);
46
47 2
        self::assertTrue($is_valid);
48 1
    }
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 2
    public static function assertIsNotValid($data, $class)
57
    {
58
        /** @noinspection PhpUndefinedMethodInspection */
59 2
        $is_valid = $class::isValid($data);
60
61 2
        self::assertFalse($is_valid);
62 1
    }
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
     * @throws \PHPUnit_Framework_ExpectationFailedException when a non-string does not throw an exception
69
     *
70
     * @param string $class_name    The name of the class to test
71
     * @param string $data          Data to be passed to the class's constructor
72
     * @param string|null $message  [optional] The expected \InvalidArgumentException message
73
     */
74 27
    public static function expectInvalidArgumentExceptionWhenConstructedWithNonString(
75
        $class_name,
76
        $data,
77
        $message = null
78
    ) {
79 27
        $exception_thrown = false;
80
81
        try {
82 27
            $non_string_instance = new $class_name($data);
83 27
        } catch (\InvalidArgumentException $exception) {
84 21
            $exception_thrown = true;
85
86 21
            self::compareExceptionMessage($exception, $message);
87 26
        } finally {
88 27
            unset($non_string_instance);
89
        }
90
91 26
        if (!is_string($data) && !$exception_thrown) {
92 1
            throw new \PHPUnit_Framework_ExpectationFailedException("Expected exception not thrown");
93
        }
94 25
    }
95
96
    /**
97
     * Compares a given exceptions message with a given message string.
98
     *
99
     * @throws \PHPUnit_Framework_AssertionFailedError when exception message does not match given message
100
     *
101
     * @param \Exception $exception     An exception for message comparison
102
     * @param string $message           The intended message
103
     */
104 21
    private static function compareExceptionMessage(\Exception $exception, $message)
105
    {
106 21
        if (isset($message)) {
107 20
            self::assertSame(
108 20
                $message,
109 20
                $exception->getMessage(),
110 20
                "Failed asserting that {$exception->getMessage()} is the same as {$message}"
111 20
            );
112 19
        }
113 20
    }
114
}
115