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

TestCase   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 16
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
dl 0
loc 16
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A assertException() 0 7 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