|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace SmartWeb\ModuleTesting\Assertions; |
|
5
|
|
|
|
|
6
|
|
|
use Exception; |
|
7
|
|
|
use PHPUnit\Framework\AssertionFailedError; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Trait AssertThrows |
|
13
|
|
|
* |
|
14
|
|
|
* @link https://github.com/Codeception/AssertThrows |
|
15
|
|
|
* |
|
16
|
|
|
* @package SmartWeb\Testing\Assertions |
|
17
|
|
|
*/ |
|
18
|
|
|
trait AssertThrows |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Asserts that callback throws an exception |
|
23
|
|
|
* |
|
24
|
|
|
* @param $throws |
|
25
|
|
|
* @param callable $fn |
|
26
|
|
|
* |
|
27
|
|
|
* @throws \Exception |
|
28
|
|
|
*/ |
|
29
|
|
|
public function assertThrows($throws, callable $fn) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->assertThrowsWithMessage($throws, false, $fn); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Asserts that callback throws an exception with a message |
|
36
|
|
|
* |
|
37
|
|
|
* @param $throws |
|
38
|
|
|
* @param $message |
|
39
|
|
|
* @param callable $fn |
|
40
|
|
|
* |
|
41
|
|
|
* @throws Exception |
|
42
|
|
|
*/ |
|
43
|
|
|
public function assertThrowsWithMessage($throws, $message, callable $fn) |
|
44
|
|
|
{ |
|
45
|
|
|
/** @var $this TestCase * */ |
|
46
|
|
|
$result = $this->getTestResultObject(); |
|
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
$this->parseThrows($throws, $message); |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
try { |
|
51
|
|
|
call_user_func($fn); |
|
52
|
|
|
} catch (AssertionFailedError $e) { |
|
53
|
|
|
if ($throws !== get_class($e)) { |
|
54
|
|
|
throw $e; |
|
55
|
|
|
} |
|
56
|
|
|
if ($this->exceptionMessageDoesNotMatchExpected($e, $message)) { |
|
|
|
|
|
|
57
|
|
|
throw new AssertionFailedError("exception message '$message' was expected, but '" . $e->getMessage() . "' was received"); |
|
58
|
|
|
} |
|
59
|
|
|
} catch (\Exception $e) { |
|
60
|
|
|
if ($throws) { |
|
61
|
|
|
if ($throws !== get_class($e)) { |
|
62
|
|
|
throw new AssertionFailedError("exception '$throws' was expected, but " . get_class($e) . ' was thrown'); |
|
63
|
|
|
} |
|
64
|
|
|
if ($this->exceptionMessageDoesNotMatchExpected($e, $message)) { |
|
|
|
|
|
|
65
|
|
|
throw new AssertionFailedError("exception message '$message' was expected, but '" . $e->getMessage() . "' was received"); |
|
66
|
|
|
} |
|
67
|
|
|
} else { |
|
68
|
|
|
throw $e; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if ($throws) { |
|
73
|
|
|
if (isset($e)) { |
|
74
|
|
|
$this->assertTrue(true, 'exception handled'); |
|
75
|
|
|
} else { |
|
76
|
|
|
throw new AssertionFailedError("exception '$throws' was not thrown as expected"); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param Exception $exception |
|
83
|
|
|
* @param string $expectedMsg |
|
84
|
|
|
* |
|
85
|
|
|
* @return bool |
|
86
|
|
|
*/ |
|
87
|
|
|
private function exceptionMessageDoesNotMatchExpected(Exception $exception, string $expectedMsg) : bool |
|
88
|
|
|
{ |
|
89
|
|
|
return $expectedMsg !== false && $expectedMsg !== strtolower($exception->getMessage()); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param $throws |
|
94
|
|
|
* @param $message |
|
95
|
|
|
*/ |
|
96
|
|
|
private function parseThrows(&$throws, &$message) |
|
97
|
|
|
{ |
|
98
|
|
|
if (is_array($throws)) { |
|
99
|
|
|
$message = ($throws[1]) |
|
100
|
|
|
? strtolower($throws[1]) |
|
101
|
|
|
: false; |
|
102
|
|
|
$throws = $throws[0]; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.