ControllerMetadataFactoryTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 13
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 *
5
 * (c) Yaroslav Honcharuk <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Yarhon\RouteGuardBundle\Tests\Controller;
12
13
use PHPUnit\Framework\TestCase;
14
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadataFactoryInterface;
15
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
16
use Yarhon\RouteGuardBundle\DependencyInjection\Container\ClassMap;
17
use Yarhon\RouteGuardBundle\Controller\ControllerMetadata;
18
use Yarhon\RouteGuardBundle\Controller\ControllerMetadataFactory;
19
use Yarhon\RouteGuardBundle\Exception\InvalidArgumentException;
20
21
/**
22
 * @author Yaroslav Honcharuk <[email protected]>
23
 */
24
class ControllerMetadataFactoryTest extends TestCase
25
{
26
    private $argumentMetadataFactory;
27
28
    private $factory;
29
30
    public function setUp()
31
    {
32
        $this->argumentMetadataFactory = $this->createMock(ArgumentMetadataFactoryInterface::class);
33
34
        $classMap = [
35
            'service1' => 'service1_class',
36
            'service2' => '\\service2_class',
37
            'service3' => null,
38
        ];
39
40
        $classMap = new ClassMap($classMap);
41
42
        $this->factory = new ControllerMetadataFactory($this->argumentMetadataFactory, $classMap);
43
    }
44
45
    /**
46
     * @dataProvider createMetadataDataProvider
47
     */
48
    public function testCreateMetadata($controllerName, $argumentMetadatas, $expected)
49
    {
50
        $this->argumentMetadataFactory->method('createArgumentMetadata')
51
            ->willReturn($argumentMetadatas);
52
53
        $metadata = $this->factory->createMetadata($controllerName);
54
55
        $this->assertEquals($expected, $metadata);
56
    }
57
58
    public function createMetadataDataProvider()
59
    {
60
        return [
61
            [
62
                'class::method',
63
                [
64
                    new ArgumentMetadata('arg1', 'int', false, false, null),
65
                    new ArgumentMetadata('arg2', 'string', false, false, null),
66
                ],
67
68
                new ControllerMetadata('class::method', 'class', 'method', [
69
                    new ArgumentMetadata('arg1', 'int', false, false, null),
70
                    new ArgumentMetadata('arg2', 'string', false, false, null),
71
                ], null),
72
            ],
73
            [
74
                'service1::method',
75
                [],
76
                new ControllerMetadata('service1::method', 'service1_class', 'method', [], 'service1'),
77
            ],
78
            [
79
                '\\class::method',
80
                [],
81
                new ControllerMetadata('\\class::method', 'class', 'method', [], null),
82
            ],
83
            [
84
                'service2::method',
85
                [],
86
                new ControllerMetadata('service2::method', 'service2_class', 'method', [], 'service2'),
87
            ],
88
        ];
89
    }
90
91
    public function testCreateMetadataForControllerAsServiceException()
92
    {
93
        $this->argumentMetadataFactory->method('createArgumentMetadata')
94
            ->willReturn([]);
95
96
        $this->expectException(InvalidArgumentException::class);
97
        $this->expectExceptionMessage('Unable to resolve class for service "service3".');
98
99
        $this->factory->createMetadata('service3::method');
100
    }
101
}
102