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

TestCase::assertException()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
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
        try {
24
            $lambda();
25
            $this->fail(get_class($expected) . ' was not thrown');
26
        } catch (Throwable | Exception $actual) {
27
            $this->assertThat($actual, new PHPUnit_Framework_Constraint_Exception(get_class($expected)));
28
            $this->assertThat($actual, new PHPUnit_Framework_Constraint_ExceptionMessage($expected->getMessage()));
29
        }
30
    }
31
}
32