Completed
Push — master ( 6333bd...a7db60 )
by Derek
04:40
created

expectInvalidArgumentExceptionGivenNonString()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4285
cc 3
eloc 6
nc 3
nop 3
crap 3
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 21
    public function expectInvalidArgumentExceptionGivenNonString($class_name, $data, $message = null)
39
    {
40 21
        if (!is_string($data)) {
41 18
            $this->expectException(\InvalidArgumentException::class);
42
43 18
            if (isset($message)) {
44 18
                $this->expectExceptionMessage($message);
45 18
            }
46 18
        }
47
48
        /** @noinspection PhpUnusedLocalVariableInspection */
49 21
        $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...
50 3
    }
51
}
52