1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule\Handler\Container\Tests; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Test\Support\Container\SimpleContainer; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use Yiisoft\Validator\Exception\RuleHandlerInterfaceNotImplementedException; |
10
|
|
|
use Yiisoft\Validator\Exception\RuleHandlerNotFoundException; |
11
|
|
|
use Yiisoft\Validator\RuleHandlerContainer; |
12
|
|
|
use Yiisoft\Validator\Tests\Support\Rule\PiHandler; |
13
|
|
|
use Yiisoft\Validator\RuleHandlerInterface; |
14
|
|
|
|
15
|
|
|
class RuleHandlerContainerTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
public function testCreate(): void |
18
|
|
|
{ |
19
|
|
|
$handlersContainer = new RuleHandlerContainer(new SimpleContainer([ |
20
|
|
|
PiHandler::class => new PiHandler(), |
21
|
|
|
])); |
22
|
|
|
|
23
|
|
|
$handler = $handlersContainer->resolve(PiHandler::class); |
24
|
|
|
|
25
|
|
|
$this->assertInstanceOf(PiHandler::class, $handler); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testNotFound(): void |
29
|
|
|
{ |
30
|
|
|
$handlersContainer = new RuleHandlerContainer(new SimpleContainer()); |
31
|
|
|
|
32
|
|
|
$this->expectException(RuleHandlerNotFoundException::class); |
33
|
|
|
$this->expectExceptionMessage( |
34
|
|
|
'Handler was not found for "not-exists-handler" rule or unresolved "not-exists-handler" class.' |
35
|
|
|
); |
36
|
|
|
$this->expectExceptionCode(0); |
37
|
|
|
$handlersContainer->resolve('not-exists-handler'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testNotRuleInterface(): void |
41
|
|
|
{ |
42
|
|
|
$handlersContainer = new RuleHandlerContainer(new SimpleContainer(['handler' => new \stdClass()])); |
43
|
|
|
|
44
|
|
|
$this->expectException(RuleHandlerInterfaceNotImplementedException::class); |
45
|
|
|
$this->expectExceptionMessage('Handler "handler" should implement "' . RuleHandlerInterface::class . '".'); |
46
|
|
|
$this->expectExceptionCode(0); |
47
|
|
|
$handlersContainer->resolve('handler'); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|