Completed
Push — master ( 7e46fd...825bf4 )
by Derek
65:21 queued 42:07
created

UriPartTestCase   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 62.5%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 70
ccs 15
cts 24
cp 0.625
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A assertSameAsToString() 0 9 2
A expectInvalidArgumentExceptionWhenConstructedWithNonString() 0 20 4
A compareExceptionMessage() 0 10 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
     * @param string $class_name    The name of the class to test
35
     * @param string $data          Data to be passed to the class's constructor
36
     * @param string|null $message  [optional] The expected \InvalidArgumentException message
37
     */
38 27
    public static function expectInvalidArgumentExceptionWhenConstructedWithNonString(
39
        $class_name,
40
        $data,
41
        $message = null
42
    ) {
43 27
        $exception_thrown = false;
44
45
        try {
46
            /** @noinspection PhpUnusedLocalVariableInspection */
47 27
            $non_string_instance = new $class_name($data);
0 ignored issues
show
Unused Code introduced by
$non_string_instance is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
48 27
        } catch (\InvalidArgumentException $exception) {
49 21
            $exception_thrown = true;
50
51 21
            self::compareExceptionMessage($exception, $message);
52
        }
53
54 6
        if (!is_string($data) && !$exception_thrown) {
55 1
            throw new \PHPUnit_Framework_ExpectationFailedException("Expected exception not thrown");
56
        }
57 5
    }
58
59
    /**
60
     * Compares a given exceptions message with a given message string.
61
     *
62
     * @throws \PHPUnit_Framework_AssertionFailedError when exception message does not match given message
63
     *
64
     * @param \Exception $exception     An exception for message comparison
65
     * @param string $message           The intended message
66
     */
67
    private function compareExceptionMessage(\Exception $exception, $message)
68
    {
69
        if (isset($message)) {
70
            self::assertSame(
71
                $message,
72
                $exception->getMessage(),
73
                "Failed asserting that {$exception->getMessage()} is the same as {$message}"
74
            );
75
        }
76
    }
77
}
78