Passed
Push — master ( f9d022...66a5c5 )
by Sergei
02:37
created

RuleHandlerContainerTest::testNotFound()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 10
rs 10
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