Completed
Push — master ( 825bf4...a9105f )
by Derek
98:19 queued 56:06
created

UriPartTestCase::compareExceptionMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 9
cts 9
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 2
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
     * @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
     * @throws \PHPUnit_Framework_ExpectationFailedException when a non-string does not throw an exception
35
     *
36
     * @param string $class_name    The name of the class to test
37
     * @param string $data          Data to be passed to the class's constructor
38
     * @param string|null $message  [optional] The expected \InvalidArgumentException message
39
     */
40 27
    public static function expectInvalidArgumentExceptionWhenConstructedWithNonString(
41
        $class_name,
42
        $data,
43
        $message = null
44
    ) {
45 27
        $exception_thrown = false;
46
47
        try {
48 27
            $non_string_instance = new $class_name($data);
49 27
        } catch (\InvalidArgumentException $exception) {
50 21
            $exception_thrown = true;
51
52 21
            self::compareExceptionMessage($exception, $message);
53 26
        } finally {
54 27
            unset($non_string_instance);
55
        }
56
57 26
        if (!is_string($data) && !$exception_thrown) {
58 1
            throw new \PHPUnit_Framework_ExpectationFailedException("Expected exception not thrown");
59
        }
60 25
    }
61
62
    /**
63
     * Compares a given exceptions message with a given message string.
64
     *
65
     * @throws \PHPUnit_Framework_AssertionFailedError when exception message does not match given message
66
     *
67
     * @param \Exception $exception     An exception for message comparison
68
     * @param string $message           The intended message
69
     */
70 21
    private static function compareExceptionMessage(\Exception $exception, $message)
71
    {
72 21
        if (isset($message)) {
73 20
            self::assertSame(
74 20
                $message,
75 20
                $exception->getMessage(),
76 20
                "Failed asserting that {$exception->getMessage()} is the same as {$message}"
77 20
            );
78 19
        }
79 20
    }
80
}
81