Completed
Push — 2.x ( 359f6e )
by Sullivan
16:25 queued 14:10
created

AddAuditEntityCompilerPassTest::testProcess()   C

Complexity

Conditions 9
Paths 3

Size

Total Lines 75
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 75
rs 5.875
cc 9
eloc 43
nc 3
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the Sonata project.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\DoctrineORMAdminBundle\Tests\DependencyInjection\Compiler;
13
14
use Sonata\DoctrineORMAdminBundle\DependencyInjection\Compiler\AddAuditEntityCompilerPass;
15
use Symfony\Component\DependencyInjection\Definition;
16
17
class AddAuditEntityCompilerPassTest extends \PHPUnit_Framework_TestCase
18
{
19
    public function processDataProvider()
20
    {
21
        return array(
22
            array(true, array(
23
                'admin1' => array('audit' => null,  'audited' => true),
24
                'admin2' => array('audit' => true,  'audited' => true),
25
                'admin3' => array('audit' => false, 'audited' => false),
26
            )),
27
            array(false, array(
28
                'admin1' => array('audit' => null,  'audited' => false),
29
                'admin2' => array('audit' => true,  'audited' => true),
30
                'admin3' => array('audit' => false, 'audited' => false),
31
            )),
32
        );
33
    }
34
35
    /**
36
     * @dataProvider processDataProvider
37
     */
38
    public function testProcess($force, array $services)
39
    {
40
        $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder');
41
42
        $container
43
            ->expects($this->any())
44
            ->method('hasDefinition')
45
            ->will($this->returnCallback(function ($id) {
46
                if ('simplethings_entityaudit.config' === $id) {
47
                    return true;
48
                }
49
            }))
50
        ;
51
52
        $container
53
            ->expects($this->any())
54
            ->method('getParameter')
55
            ->will($this->returnCallback(function ($id) use ($force) {
56
                if ('sonata_doctrine_orm_admin.audit.force' === $id) {
57
                    return $force;
58
                }
59
60
                if ('simplethings.entityaudit.audited_entities' === $id) {
61
                    return array();
62
                }
63
            }))
64
        ;
65
66
        $container
67
            ->expects($this->any())
68
            ->method('findTaggedServiceIds')
69
            ->will($this->returnCallback(function ($id) use ($services) {
70
                if ('sonata.admin' === $id) {
71
                    $tags = array();
72
73
                    foreach ($services as $id => $service) {
74
                        $attributes = array('manager_type' => 'orm');
75
76
                        if (null !== $audit = $service['audit']) {
77
                            $attributes['audit'] = $audit;
78
                        }
79
80
                        $tags[$id] = array(0 => $attributes);
81
                    }
82
83
                    return $tags;
84
                }
85
            }))
86
        ;
87
88
        $container
89
            ->expects($this->any())
90
            ->method('getDefinition')
91
            ->will($this->returnCallback(function ($id) {
92
                return new Definition(null, array(null, $id));
93
            }))
94
        ;
95
96
        $expectedAuditedEntities = array();
97
98
        foreach ($services as $id => $service) {
99
            if ($service['audited']) {
100
                $expectedAuditedEntities[] = $id;
101
            }
102
        }
103
104
        $container
105
            ->expects($this->once())
106
            ->method('setParameter')
107
            ->with('simplethings.entityaudit.audited_entities', $expectedAuditedEntities)
108
        ;
109
110
        $compilerPass = new AddAuditEntityCompilerPass();
111
        $compilerPass->process($container);
112
    }
113
}
114