Passed
Pull Request — master (#16)
by Donald
01:47
created

TestCase::assertException()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 6
nc 3
nop 2
1
<?php namespace Chekote\PHPUnit\Framework;
2
3
use Exception;
4
use PHPUnit\Framework\TestCase as BaseTestCase;
5
use PHPUnit_Framework_Constraint_Exception;
6
use PHPUnit_Framework_Constraint_ExceptionMessage;
7
use PHPUnit_Framework_ExpectationFailedException;
8
use Throwable;
9
10
/**
11
 * Adds additional functionality to the PHPUnit TestCase class.
12
 */
13
class TestCase extends BaseTestCase
14
{
15
    /**
16
     * Asserts that the lambda throws the specified exception.
17
     *
18
     * @param  Exception                                    $expected the expected exception (class and message must match)
19
     * @param  callable                                     $lambda   the lambda to execute
20
     * @throws PHPUnit_Framework_ExpectationFailedException If the specified exception is not thrown.`
21
     */
22
    public function assertException(Exception $expected, callable $lambda): void
23
    {
24
        try {
25
            $lambda();
26
            $this->fail(get_class($expected) . ' was not thrown');
27
        } catch (Throwable | Exception $actual) {
28
            $this->assertThat($actual, new PHPUnit_Framework_Constraint_Exception(get_class($expected)));
29
            $this->assertThat($actual, new PHPUnit_Framework_Constraint_ExceptionMessage($expected->getMessage()));
30
        }
31
    }
32
}
33