Completed
Push — master ( 0af75b...08e815 )
by GBProd
03:00
created

testThrowExceptionIfTagHasNoSpecification()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
namespace Tests\GBProd\ElasticaSpecificationBundle\DependencyInjection\Compiler;
4
5
use GBProd\ElasticaSpecification\Handler;
6
use GBProd\ElasticaSpecificationBundle\DependencyInjection\Compiler\ExpressionBuilderPass;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Definition;
9
use Symfony\Component\DependencyInjection\Reference;
10
11
/**
12
 * Tests for ExpressionBuilderPass
13
 *
14
 * @author gbprod <[email protected]>
15
 */
16
class ExpressionBuilderPassTest extends \PHPUnit_Framework_TestCase
17
{
18
    public function testThrowExceptionIfNoHandlerDefinition()
19
    {
20
        $pass = new ExpressionBuilderPass();
21
22
        $this->expectException(\Exception::class);
23
24
        $pass->process(new ContainerBuilder());
25
    }
26
27
    public function testDoNothingIfNoTaggedServices()
28
    {
29
        $pass = new ExpressionBuilderPass();
30
        $container = $this->createContainerWithHandler();
31
32
        $pass->process($container);
33
34
        $calls = $container
35
            ->getDefinition('gbprod.elastica_specification_handler')
36
            ->getMethodCalls()
37
        ;
38
39
        $this->assertEmpty($calls);
40
    }
41
42
    private function createContainerWithHandler()
43
    {
44
        $container = new ContainerBuilder();
45
46
        $container->setDefinition(
47
            'gbprod.elastica_specification_handler',
48
            new Definition(Handler::class)
49
        );
50
51
        return $container;
52
    }
53
54
    public function testThrowExceptionIfTagHasNoSpecification()
55
    {
56
        $pass = new ExpressionBuilderPass();
57
58
        $container = $this->createContainerWithHandler();
59
        $container
60
            ->register('builder', \stdClass::class)
61
            ->addTag('elastica.expression_builder')
62
        ;
63
64
        $this->expectException(\Exception::class);
65
        $pass->process($container);
66
    }
67
68
    public function testAddMethodCalls()
69
    {
70
        $pass = new ExpressionBuilderPass();
71
72
        $container = $this->createContainerWithHandler();
73
        $container
74
            ->register('builder1', 'Builder1')
75
            ->addTag('elastica.expression_builder', ['specification' => 'Specification1'])
76
        ;
77
78
        $container
79
            ->register('builder2', 'Builder2')
80
            ->addTag('elastica.expression_builder', ['specification' => 'Specification2'])
81
        ;
82
83
        $pass->process($container);
84
85
        $calls = $container
86
            ->getDefinition('gbprod.elastica_specification_handler')
87
            ->getMethodCalls()
88
        ;
89
90
        $this->assertCount(2, $calls);
91
92
        $this->assertEquals('registerBuilder', $calls[0][0]);
93
        $this->assertEquals('Specification1', $calls[0][1][0]);
94
        $this->assertInstanceOf(Reference::class, $calls[0][1][1]);
95
        $this->assertEquals('builder1', $calls[0][1][1]);
96
97
98
        $this->assertEquals('registerBuilder', $calls[1][0]);
99
        $this->assertEquals('Specification2', $calls[1][1][0]);
100
        $this->assertInstanceOf(Reference::class, $calls[1][1][1]);
101
        $this->assertEquals('builder2', $calls[1][1][1]);
102
    }
103
}
104