-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionTest.php
More file actions
105 lines (86 loc) · 3.1 KB
/
ExceptionTest.php
File metadata and controls
105 lines (86 loc) · 3.1 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
namespace unit;
use Pdsinterop\Solid\Resources\Exception;
use PHPUnit\Framework\TestCase;
use TypeError;
/**
* @coversDefaultClass \Pdsinterop\Solid\Resources\Exception
* @covers ::create
*/
class ExceptionTest extends TestCase
{
const MOCK_CONTEXT = ['Test'];
const MOCK_MESSAGE = 'Error: %s';
/**
* @testdox Exception should complain when called without a message
*/
public function testCreateWithoutMessage(): void
{
$this->expectException(TypeError::class);
$this->expectExceptionMessageMatches('/Too few arguments .+ 0 passed/');
Exception::create();
}
/**
* @testdox Exception should complain when called without a context
*/
public function testCreateWithoutContext(): void
{
$this->expectException(TypeError::class);
$this->expectExceptionMessageMatches('/Too few arguments .+ 1 passed/');
Exception::create(self::MOCK_MESSAGE);
}
/**
* @testdox Exception should complain when called with invalid message
*/
public function testCreateWithInvalidMessage(): void
{
$this->expectException(TypeError::class);
$this->expectExceptionMessageMatches('/Argument #1 .+ must be of type string/');
Exception::create(null, self::MOCK_CONTEXT);
}
/**
* @testdox Exception should complain when called with invalid context
*/
public function testCreateWithInvalidContext(): void
{
$this->expectException(TypeError::class);
$this->expectExceptionMessageMatches('/Argument #2 .+ must be of type array/');
Exception::create(self::MOCK_MESSAGE, null);
}
/**
* @testdox Exception should complain when given context does not match provided message format
*/
public function testCreateWithIncorrectContext(): void
{
$this->expectException(\ValueError::class);
$this->expectExceptionMessageMatches('/The arguments array must contain 1 items?, 0 given/');
Exception::create(self::MOCK_MESSAGE, []);
}
/**
* @testdox Exception should be created when called with valid message and context
*/
public function testCreateWithMessageAndContext(): void
{
$expected = Exception::class;
$actual = Exception::create(self::MOCK_MESSAGE, self::MOCK_CONTEXT);
$this->assertInstanceOf($expected, $actual);
}
/**
* @testdox Created Exception should have the correct message when called with a message and context
*/
public function testCreateFormatsErrorMessage(): void
{
$expected = 'Error: Test';
$actual = Exception::create(self::MOCK_MESSAGE, self::MOCK_CONTEXT)->getMessage();
$this->assertSame($expected, $actual);
}
/**
* @testdox Exception should be created when called with a message, context and previous exception
*/
public function testCreateSetsPreviousException(): void
{
$expected = new \Exception('Previous exception');
$actual = Exception::create(self::MOCK_MESSAGE, self::MOCK_CONTEXT, $expected)->getPrevious();
$this->assertSame($expected, $actual);
}
}