AssertThrows::assertThrowsWithMessage()   D
last analyzed

Complexity

Conditions 10
Paths 14

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 14
dl 0
loc 34
c 0
b 0
f 0
cc 10
eloc 22
nop 3
rs 4.8196

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
4
namespace SmartWeb\ModuleTesting\Assertions;
5
6
use Exception;
7
use Framework\TestResult;
8
use PHPUnit\Framework\AssertionFailedError;
9
10
/**
11
 * Trait AssertThrows
12
 *
13
 * @link    https://github.com/Codeception/AssertThrows
14
 *
15
 * @package SmartWeb\ModuleTesting\Assertions
16
 */
17
trait AssertThrows
18
{
19
    
20
    /**
21
     * Asserts that callback throws an exception
22
     *
23
     * @param callable $callable
24
     * @param          $throws
25
     *
26
     * @throws Exception
27
     */
28
    public function assertThrows(callable $callable, $throws)
29
    {
30
        $this->assertThrowsWithMessage($callable, $throws, false);
31
    }
32
    
33
    /**
34
     * Asserts that callback throws an exception with a message
35
     *
36
     * @param callable $callback
37
     * @param          $throws
38
     * @param          $message
39
     *
40
     * @throws Exception
41
     */
42
    public function assertThrowsWithMessage(callable $callback, $throws, $message)
43
    {
44
        $this->parseThrows($throws, $message);
45
        
46
        try {
47
            call_user_func($callback);
48
        } catch (AssertionFailedError $e) {
49
            if ($throws !== get_class($e)) {
50
                throw $e;
51
            }
52
            if ($this->exceptionMessageDoesNotMatchExpected($e, $message)) {
53
                throw new AssertionFailedError("exception message '$message' was expected, but '" . $e->getMessage() . "' was received");
54
            }
55
        } catch (Exception $e) {
56
            if ($throws) {
57
                if ($throws !== get_class($e)) {
58
                    throw new AssertionFailedError("exception '$throws' was expected, but " . get_class($e) . ' was thrown');
59
                }
60
                if ($this->exceptionMessageDoesNotMatchExpected($e, $message)) {
61
                    throw new AssertionFailedError("exception message '$message' was expected, but '" . $e->getMessage() . "' was received");
62
                }
63
            } else {
64
                throw $e;
65
            }
66
        }
67
        
68
        if ($throws) {
69
            if (isset($e)) {
70
                static::assertTrue(true, 'exception handled');
71
            } else {
72
                throw new AssertionFailedError("exception '$throws' was not thrown as expected");
73
            }
74
        }
75
    }
76
    
77
    /**
78
     * @param Exception   $exception
79
     * @param string|bool $expectedMsg
80
     *
81
     * @return bool
82
     */
83
    private function exceptionMessageDoesNotMatchExpected(Exception $exception, $expectedMsg) : bool
84
    {
85
        return $expectedMsg !== false && $expectedMsg !== strtolower($exception->getMessage());
86
    }
87
    
88
    /**
89
     * @param $throws
90
     * @param $message
91
     */
92
    private function parseThrows(&$throws, &$message)
93
    {
94
        if (is_array($throws)) {
95
            $message = ($throws[1])
96
                ? strtolower($throws[1])
97
                : false;
98
            $throws = $throws[0];
99
        }
100
    }
101
    
102
    /**
103
     * @return TestResult
104
     */
105
    abstract public function getTestResultObject();
106
    
107
    /**
108
     * Asserts that a condition is true.
109
     *
110
     * @param bool   $condition
111
     * @param string $message
112
     *
113
     * @throws AssertionFailedError
114
     */
115
    abstract public static function assertTrue($condition, $message = '');
116
}
117