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