Completed
Push — master ( 7ca809...e10277 )
by GBProd
02:21
created

testDoNothingIfNoTaggedServices()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 14
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
namespace Tests\GBProd\DoctrineSpecificationBundle\DependencyInjection\Compiler;
4
5
use GBProd\DoctrineSpecification\Handler;
6
use GBProd\DoctrineSpecificationBundle\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->setExpectedException('\Exception');
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.doctrine_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.doctrine_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')
61
            ->addTag('doctrine.expression_builder')
62
        ;
63
        
64
        $this->setExpectedException('\Exception');
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('doctrine.expression_builder', ['specification' => 'Specification1'])
76
        ;
77
78
        $container
79
            ->register('builder2', 'Builder2')
80
            ->addTag('doctrine.expression_builder', ['specification' => 'Specification2'])
81
        ;
82
        
83
        $pass->process($container);
84
    
85
        $calls = $container
86
            ->getDefinition('gbprod.doctrine_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
}