AddAuditEntityCompilerPassTest::testProcess()   C
last analyzed

Complexity

Conditions 9
Paths 3

Size

Total Lines 75

Duplication

Lines 0
Ratio 0 %

Importance

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