Completed
Push — master ( a7db60...7e46fd )
by Derek
02:14
created

UriPartTestCase::assertSameAsToString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
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
     * @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
            if (isset($message)) {
52 20
                self::assertSame(
53 20
                    $message,
54 20
                    $exception->getMessage(),
55 20
                    "Failed asserting that {$exception->getMessage()} is equal to {$message}"
56 20
                );
57 19
            }
58
        }
59
60 26
        if (!is_string($data) && !$exception_thrown) {
61 1
            throw new \PHPUnit_Framework_ExpectationFailedException("Expected exception not thrown");
62
        }
63 25
    }
64
}
65