|
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
|
|
|
|