Completed
Push — master ( a9105f...b51c2c )
by Derek
02:40
created

expectInvalidArgumentExceptionWhenConstructedWithNonString()   A

Complexity

Conditions 4
Paths 9

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
ccs 11
cts 11
cp 1
rs 9.0534
cc 4
eloc 14
nc 9
nop 3
crap 4
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          The string expected to match
18
     * @param object $test_instance     An instance of the object whose __toString method is under test
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
     * Asserts that the given expected string is not the same as the string returned by an object instance's __toString
32
     * magic method.
33
     *
34
     * @throws \PHPUnit_Framework_ExpectationFailedException provided an instance of an object with no __toString
35
     * method.
36
     *
37
     * @param string $expected          The string expected not to match
38
     * @param object $test_instance     An instance of the object whose __toString method is under test
39
     */
40 3
    public static function assertNotSameAsToString($expected, $test_instance)
41
    {
42
        try {
43 3
            $actual = (string) $test_instance;
44 2
            self::assertNotSame($expected, $actual);
45 3
        } catch (\PHPUnit_Framework_Error $error) {
46 1
            throw new \PHPUnit_Framework_ExpectationFailedException("Cannot convert object to string");
47
        }
48 1
    }
49
50
    /**
51
     * Tests whether the constructor of a given class throws an \InvalidArgumentException when attempting to construct
52
     * an instance with a non-string.
53
     *
54
     * @throws \PHPUnit_Framework_ExpectationFailedException when a non-string does not throw an exception
55
     *
56
     * @param string $class_name    The name of the class to test
57
     * @param string $data          Data to be passed to the class's constructor
58
     * @param string|null $message  [optional] The expected \InvalidArgumentException message
59
     */
60 27
    public static function expectInvalidArgumentExceptionWhenConstructedWithNonString(
61
        $class_name,
62
        $data,
63
        $message = null
64
    ) {
65 27
        $exception_thrown = false;
66
67
        try {
68 27
            $non_string_instance = new $class_name($data);
69 27
        } catch (\InvalidArgumentException $exception) {
70 21
            $exception_thrown = true;
71
72 21
            self::compareExceptionMessage($exception, $message);
73 26
        } finally {
74 27
            unset($non_string_instance);
75
        }
76
77 26
        if (!is_string($data) && !$exception_thrown) {
78 1
            throw new \PHPUnit_Framework_ExpectationFailedException("Expected exception not thrown");
79
        }
80 25
    }
81
82
    /**
83
     * Compares a given exceptions message with a given message string.
84
     *
85
     * @throws \PHPUnit_Framework_AssertionFailedError when exception message does not match given message
86
     *
87
     * @param \Exception $exception     An exception for message comparison
88
     * @param string $message           The intended message
89
     */
90 21
    private static function compareExceptionMessage(\Exception $exception, $message)
91
    {
92 21
        if (isset($message)) {
93 20
            self::assertSame(
94 20
                $message,
95 20
                $exception->getMessage(),
96 20
                "Failed asserting that {$exception->getMessage()} is the same as {$message}"
97 20
            );
98 19
        }
99 20
    }
100
}
101