Completed
Push — refonte ( 64e01a...7173e3 )
by Arnaud
03:31
created

FieldCompilerPassTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 97
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testProcess() 0 38 1
A testProcessWrongConfiguration() 0 26 1
A testProcessWithoutAdminFactory() 0 8 1
A testProcessWithoutTaggedServices() 0 10 1
1
<?php
2
3
namespace LAG\AdminBundle\Tests\AdminBundle\DependencyInjection;
4
5
use LAG\AdminBundle\DependencyInjection\CompilerPass\FieldCompilerPass;
6
use LAG\AdminBundle\Tests\AdminTestBase;
7
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
8
use Symfony\Component\DependencyInjection\ContainerBuilder;
9
use Symfony\Component\DependencyInjection\Definition;
10
use Symfony\Component\DependencyInjection\Reference;
11
12
class FieldCompilerPassTest extends AdminTestBase
13
{
14
    /**
15
     * Process method should add the definition of the tagged services to the data providers factory.
16
     */
17
    public function testProcess()
18
    {
19
        $compilerPass = new FieldCompilerPass();
20
        $containerBuilder = new ContainerBuilder();
21
22
        // create a tagged service definition
23
        $taggedServiceDefinition = new Definition();
24
        $taggedServiceDefinition->addTag('lag.field', [
25
            'type' => 'string'
26
        ]);
27
28
        // create the data providers factory definition
29
        $factoryDefinition = new Definition();
30
31
        // add them to the container builder
32
        $containerBuilder->addDefinitions([
33
            'one_field' => $taggedServiceDefinition,
34
            'lag.admin.field_factory' => $factoryDefinition
35
        ]);
36
37
        // process the compiler pass
38
        $compilerPass->process($containerBuilder);
39
40
        $calls = $containerBuilder->getDefinition('lag.admin.field_factory')->getMethodCalls();
41
42
        $this->assertCount(1, $calls);
43
        $this->assertEquals('addFieldMapping', $calls[0][0]);
44
        $this->assertEquals('string', $calls[0][1][0]);
45
        $this->assertEquals('one_field', $calls[0][1][1]);
46
47
        $fieldCalls = $taggedServiceDefinition->getMethodCalls();
48
        $this->assertCount(1, $fieldCalls);
49
        $this->assertEquals('setConfiguration', $fieldCalls[0][0]);
50
51
        $this->assertInstanceOf(Reference::class, $fieldCalls[0][1][0]);
52
        $this->assertEquals('lag.admin.application', (string)$fieldCalls[0][1][0]);
53
54
    }
55
56
    public function testProcessWrongConfiguration()
57
    {
58
        $compilerPass = new FieldCompilerPass();
59
        $containerBuilder = new ContainerBuilder();
60
61
        // create a tagged service definition
62
        $taggedServiceDefinition = new Definition();
63
        $taggedServiceDefinition->addTag('lag.field');
64
65
        // create the data providers factory definition
66
        $factoryDefinition = new Definition();
67
68
        // add them to the container builder
69
        $containerBuilder->addDefinitions([
70
            'my_custom_provider' => $taggedServiceDefinition,
71
            'lag.admin.field_factory' => $factoryDefinition
72
        ]);
73
74
        $this->assertExceptionRaised(
75
            InvalidConfigurationException::class,
76
            function () use ($compilerPass, $containerBuilder) {
77
                // process the compiler pass
78
                $compilerPass->process($containerBuilder);
79
            }
80
        );
81
    }
82
83
    /**
84
     * Process method should not change the container builder if the admin factory definition does not exists.
85
     */
86
    public function testProcessWithoutAdminFactory()
87
    {
88
        $containerBuilder = new ContainerBuilder();
89
        $compilerPass = new FieldCompilerPass();
90
        $compilerPass->process($containerBuilder);
91
        
92
        $this->assertFalse($containerBuilder->has('lag.admin.data_providers_factory'));
93
    }
94
95
    /**
96
     * Process method should not change the container builder if no tagged services were found.
97
     */
98
    public function testProcessWithoutTaggedServices()
99
    {
100
        $containerBuilder = new ContainerBuilder();
101
        $containerBuilder->setDefinition('lag.admin.field_factory', new Definition());
102
103
        $compilerPass = new FieldCompilerPass();
104
        $compilerPass->process($containerBuilder);
105
        
106
        $this->assertTrue($containerBuilder->hasDefinition('lag.admin.field_factory'));
107
    }
108
}
109