1
|
|
|
<?php |
|
|
|
|
2
|
|
|
namespace WebStream\Exception\Test; |
3
|
|
|
|
4
|
|
|
require_once dirname(__FILE__) . '/../Modules/DI/Injector.php'; |
5
|
|
|
require_once dirname(__FILE__) . '/../Modules/Container/Container.php'; |
6
|
|
|
require_once dirname(__FILE__) . '/../Test/Fixtures/InjectedClass.php'; |
7
|
|
|
require_once dirname(__FILE__) . '/../Test/Providers/ExceptionDelegatorProvider.php'; |
8
|
|
|
require_once dirname(__FILE__) . '/../Delegate/ExceptionDelegator.php'; |
9
|
|
|
require_once dirname(__FILE__) . '/../ApplicationException.php'; |
10
|
|
|
require_once dirname(__FILE__) . '/../SystemException.php'; |
11
|
|
|
require_once dirname(__FILE__) . '/../DelegateException.php'; |
12
|
|
|
|
13
|
|
|
use WebStream\Exception\ApplicationException; |
14
|
|
|
use WebStream\Exception\SystemException; |
15
|
|
|
use WebStream\Exception\DelegateException; |
16
|
|
|
use WebStream\Exception\Delegate\ExceptionDelegator; |
17
|
|
|
use WebStream\Container\Container; |
18
|
|
|
use WebStream\Exception\Test\Fixtures\InjectedClass; |
19
|
|
|
use WebStream\Exception\Test\Providers\ExceptionDelegatorProvider; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* ExceptionDelegatorTest |
23
|
|
|
* @author Ryuichi TANAKA. |
24
|
|
|
* @since 2017/01/07 |
25
|
|
|
* @version 0.7 |
26
|
|
|
*/ |
27
|
|
|
class ExceptionDelegatorTest extends \PHPUnit\Framework\TestCase |
28
|
|
|
{ |
29
|
|
|
use ExceptionDelegatorProvider; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* 正常系 |
33
|
|
|
* 例外オブジェクトを保持でき、任意のタイミングでスローできること |
34
|
|
|
* @test |
35
|
|
|
* @dataProvider exceptionProvider |
36
|
|
|
* @expectedException \Exception |
37
|
|
|
*/ |
38
|
|
|
public function okDelegatableExceptionTest($handledException, $exceptionObject) |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
$instance = new InjectedClass(); |
41
|
|
|
$delegator = new ExceptionDelegator($instance, $exceptionObject); |
42
|
|
|
$delegator->raise(); |
43
|
|
|
$this->assertTrue(false); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* 正常系 |
48
|
|
|
* ハンドリング可能な例外がスローされた場合の例外オブジェクト構造が正しいこと |
49
|
|
|
* さらに例外を指定クラスで捕捉できること |
50
|
|
|
* @test |
51
|
|
|
* @dataProvider exceptionProvider |
52
|
|
|
*/ |
53
|
|
|
public function okDelegateAndHandleTest($handledException, $exceptionObject) |
54
|
|
|
{ |
55
|
|
|
$instance = new InjectedClass(); |
56
|
|
|
$container = new Container(); |
57
|
|
|
$container->exceptions = [$handledException]; |
|
|
|
|
58
|
|
|
$container->method = new \ReflectionMethod($instance, "handled1"); |
|
|
|
|
59
|
|
|
$delegator = new ExceptionDelegator($instance, $exceptionObject); |
60
|
|
|
$delegator->setExceptionHandler([$container]); |
61
|
|
|
|
62
|
|
|
$isAsserted = false; |
63
|
|
|
try { |
64
|
|
|
$delegator->raise(); |
65
|
|
|
} catch (\Exception $e) { |
66
|
|
|
$this->assertInstanceOf(DelegateException::class, $e); |
67
|
|
|
$this->expectOutputString("handled"); |
68
|
|
|
$isAsserted = true; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if (!$isAsserted) { |
72
|
|
|
$this->assertTrue($false); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.