Skip to content

Commit 49218be

Browse files
committed
Add unit-tests for Exception class.
1 parent f1ed933 commit 49218be

1 file changed

Lines changed: 105 additions & 0 deletions

File tree

tests/Unit/ExceptionTest.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
namespace Unit;
4+
5+
use Pdsinterop\Solid\Resources\Exception;
6+
use PHPUnit\Framework\TestCase;
7+
use TypeError;
8+
9+
/**
10+
* @coversDefaultClass \Pdsinterop\Solid\Resources\Exception
11+
* @covers ::create
12+
*/
13+
class ExceptionTest extends TestCase
14+
{
15+
const MOCK_CONTEXT = ['Test'];
16+
const MOCK_MESSAGE = 'Error: %s';
17+
18+
/**
19+
* @testdox Exception should complain when called without a message
20+
*/
21+
public function testCreateWithoutMessage(): void
22+
{
23+
$this->expectException(TypeError::class);
24+
$this->expectExceptionMessageMatches('/Too few arguments .+ 0 passed/');
25+
26+
Exception::create();
27+
}
28+
29+
/**
30+
* @testdox Exception should complain when called without a context
31+
*/
32+
public function testCreateWithoutContext(): void
33+
{
34+
$this->expectException(TypeError::class);
35+
$this->expectExceptionMessageMatches('/Too few arguments .+ 1 passed/');
36+
37+
Exception::create(self::MOCK_MESSAGE);
38+
}
39+
40+
/**
41+
* @testdox Exception should complain when called with invalid message
42+
*/
43+
public function testCreateWithInvalidMessage(): void
44+
{
45+
$this->expectException(TypeError::class);
46+
$this->expectExceptionMessageMatches('/Argument #1 .+ must be of type string/');
47+
48+
Exception::create(null, self::MOCK_CONTEXT);
49+
}
50+
51+
/**
52+
* @testdox Exception should complain when called with invalid context
53+
*/
54+
public function testCreateWithInvalidContext(): void
55+
{
56+
$this->expectException(TypeError::class);
57+
$this->expectExceptionMessageMatches('/Argument #2 .+ must be of type array/');
58+
59+
Exception::create(self::MOCK_MESSAGE, null);
60+
}
61+
62+
/**
63+
* @testdox Exception should complain when given context does not match provided message format
64+
*/
65+
public function testCreateWithIncorrectContext(): void
66+
{
67+
$this->expectException(\ValueError::class);
68+
$this->expectExceptionMessageMatches('/The arguments array must contain 1 items?, 0 given/');
69+
70+
Exception::create(self::MOCK_MESSAGE, []);
71+
}
72+
73+
/**
74+
* @testdox Exception should be created when called with valid message and context
75+
*/
76+
public function testCreateWithMessageAndContext(): void
77+
{
78+
$expected = Exception::class;
79+
$actual = Exception::create(self::MOCK_MESSAGE, self::MOCK_CONTEXT);
80+
81+
$this->assertInstanceOf($expected, $actual);
82+
}
83+
84+
/**
85+
* @testdox Created Exception should have the correct message when called with a message and context
86+
*/
87+
public function testCreateFormatsErrorMessage(): void
88+
{
89+
$expected = 'Error: Test';
90+
$actual = Exception::create(self::MOCK_MESSAGE, self::MOCK_CONTEXT)->getMessage();
91+
92+
$this->assertSame($expected, $actual);
93+
}
94+
95+
/**
96+
* @testdox Exception should be created when called with a message, context and previous exception
97+
*/
98+
public function testCreateSetsPreviousException(): void
99+
{
100+
$expected = new \Exception('Previous exception');
101+
$actual = Exception::create(self::MOCK_MESSAGE, self::MOCK_CONTEXT, $expected)->getPrevious();
102+
103+
$this->assertSame($expected, $actual);
104+
}
105+
}

0 commit comments

Comments
 (0)