ObjectExceptionMatcher::withMessage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PHPKitchen\CodeSpecsCore\Expectation\Internal;
4
5
use PHPKitchen\CodeSpecsCore\Expectation\Matcher\Base\Matcher;
6
7
/**
8
 * ExceptionMatcher is designed to check that object throws valid exception.
9
 * This matcher being used in pair with {@link ObjectMatcher}.
10
 *
11
 * @property \Exception $actual
12
 *
13
 * @package PHPKitchen\CodeSpecsCore\Expectation
14
 * @author Dmitry Kolodko <[email protected]>
15
 */
16
class ObjectExceptionMatcher extends Matcher {
17
    public function withMessage($message) {
18
        $this->startStep('has message "' . $message . '"')->expectExceptionMessage($message);
19
        return $this;
20
    }
21
22
    public function withMessageMatchesPattern($messagePattern) {
23
        $this->startStep('has message matching pattern "' . $messagePattern . '"')
24
            ->expectExceptionMessage($messagePattern);
25
        return $this;
26
    }
27
28
    public function withCode($code) {
29
        $this->startStep('has code "' . $code . '"')->expectExceptionCode($code);
30
        return $this;
31
    }
32
33
    /**
34
     * Identifies a situation when exception should be raised.
35
     * Supposed to executes method of the object.
36
     * This methods should to be used after {@link throwsException} and "with*" methods to gain expression like:
37
     * <pre>
38
     * $I->seeObject($a)->throwsException(Exception::class)->when(function($object) {$object->doParty();});
39
     * // or
40
     * $I->seeObject($a)->throwsException(IronyException::class)->withCode(500)->when(function($object) {$object->doParty();});
41
     * <pre/>
42
     * and finish scenario.
43
     */
44
    public function when(callable $callback) {
45
        call_user_func_array($callback, [$this->getActualValue()]);
46
    }
47
}