forked from kodus/mail
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionHelper.php
More file actions
50 lines (42 loc) · 1.28 KB
/
ExceptionHelper.php
File metadata and controls
50 lines (42 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
namespace Kodus\Mail\Test;
use Codeception\Module;
use Exception;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\Constraint\Exception as ConstraintException;
use PHPUnit\Framework\Constraint\ExceptionMessage as ConstraintExceptionMessage;
class ExceptionHelper extends Module
{
/**
* @param string $type
* @param string|callable $message_or_function
* @param callable|null $function
*/
public function assertException(string $type, $message_or_function, ?callable $function = null)
{
if (func_num_args() === 3) {
$message = $message_or_function;
} else { // 2 args
$message = null;
$function = $message_or_function;
}
$exception = null;
try {
call_user_func($function);
} catch (Exception $e) {
$exception = $e;
}
$exception_type = $exception ? get_class($exception) : 'null';
Assert::assertThat(
$exception,
new ConstraintException($type),
$exception_type . " NOT EQUAL TO " . $type
);
if ($message !== null) {
Assert::assertThat(
$exception,
new ConstraintExceptionMessage($message)
);
}
}
}