Completed
Push — 3.x ( 8467a0...771509 )
by Grégoire
02:53
created

testProcessWithUntaggedManagerDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 9.312
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\AdminBundle\Tests\DependencyInjection\Compiler;
15
16
use PHPUnit\Framework\TestCase;
17
use Prophecy\Argument;
18
use Sonata\AdminBundle\DependencyInjection\Compiler\ModelManagerCompilerPass;
19
use Sonata\AdminBundle\Model\ModelManagerInterface;
20
use Sonata\AdminBundle\Tests\App\Model\ModelManager;
21
use Symfony\Component\DependencyInjection\ContainerBuilder;
22
use Symfony\Component\DependencyInjection\Definition;
23
use Symfony\Component\DependencyInjection\Exception\LogicException;
24
25
/**
26
 * @author Gaurav Singh Faujdar <[email protected]>
27
 */
28
final class ModelManagerCompilerPassTest extends TestCase
29
{
30
    public function testProcess(): void
31
    {
32
        $adminMaker = $this->prophesize(Definition::class);
33
        $adminMaker->replaceArgument(Argument::type('integer'), Argument::any())->shouldNotBeCalled();
34
        $containerBuilderMock = $this->prophesize(ContainerBuilder::class);
35
36
        $containerBuilderMock->getServiceIds()
37
            ->willReturn([]);
38
39
        $containerBuilderMock->findTaggedServiceIds(Argument::exact(ModelManagerCompilerPass::MANAGER_TAG))
40
            ->willReturn([]);
41
42
        $containerBuilderMock->getParameter(Argument::exact('kernel.bundles'))
43
            ->willReturn(['MakerBundle' => 'MakerBundle']);
44
45
        $containerBuilderMock->getDefinition(Argument::exact('sonata.admin.maker'))
46
            ->willReturn($adminMaker->reveal());
47
        $containerBuilderMock->getParameter(Argument::containingString('kernel.project_dir'))
48
            ->willReturn(null);
49
50
        $compilerPass = new ModelManagerCompilerPass();
51
        $compilerPass->process($containerBuilderMock->reveal());
52
    }
53
54
    /**
55
     * NEXT_MAJOR: Remove this test.
56
     *
57
     * @group legacy
58
     *
59
     * @expectedDeprecation Not setting the "sonata.admin.manager" tag on the "sonata.admin.manager.test" service is deprecated since sonata-project/admin-bundle 3.x.
60
     */
61
    public function testProcessWithUntaggedManagerDefinition(): void
62
    {
63
        $adminMaker = $this->prophesize(Definition::class);
64
        $adminMaker->replaceArgument(Argument::type('integer'), Argument::any())->shouldBeCalledTimes(1);
65
        $containerBuilderMock = $this->prophesize(ContainerBuilder::class);
66
67
        $containerBuilderMock->getServiceIds()
68
            ->willReturn(['sonata.admin.manager.test']);
69
70
        $definitionMock = $this->prophesize(Definition::class);
71
72
        $definitionMock->getClass()
73
            ->willReturn(ModelManager::class);
74
75
        $definitionMock->hasTag(Argument::exact(ModelManagerCompilerPass::MANAGER_TAG))
76
            ->willReturn(false);
77
78
        $definitionMock->addTag(Argument::exact(ModelManagerCompilerPass::MANAGER_TAG))
79
            ->willReturn(null);
80
81
        $containerBuilderMock->findTaggedServiceIds(Argument::exact(ModelManagerCompilerPass::MANAGER_TAG))
82
            ->willReturn(['sonata.admin.manager.test' => ['other.tag']]);
83
84
        $containerBuilderMock->getParameter(Argument::exact('kernel.bundles'))
85
            ->willReturn(['MakerBundle' => 'MakerBundle']);
86
87
        $containerBuilderMock->findDefinition(Argument::exact('sonata.admin.manager.test'))
88
            ->willReturn($definitionMock->reveal());
89
90
        $containerBuilderMock->getDefinition(Argument::exact('sonata.admin.maker'))
91
            ->willReturn($adminMaker->reveal());
92
93
        $containerBuilderMock->getParameter(Argument::containingString('kernel.project_dir'))
94
            ->willReturn(null);
95
96
        $compilerPass = new ModelManagerCompilerPass();
97
        $compilerPass->process($containerBuilderMock->reveal());
98
    }
99
100
    public function testProcessWithTaggedManagerDefinition(): void
101
    {
102
        $adminMaker = $this->prophesize(Definition::class);
103
        $adminMaker->replaceArgument(Argument::type('integer'), Argument::any())->shouldBeCalledTimes(1);
104
        $containerBuilderMock = $this->prophesize(ContainerBuilder::class);
105
106
        $containerBuilderMock->getServiceIds()
107
            ->willReturn(['sonata.admin.manager.test']);
108
109
        $definitionMock = $this->prophesize(Definition::class);
110
111
        $definitionMock->getClass()
112
            ->willReturn(ModelManager::class);
113
114
        $definitionMock->hasTag(Argument::exact(ModelManagerCompilerPass::MANAGER_TAG))
115
            ->willReturn(true);
116
117
        $containerBuilderMock->findTaggedServiceIds(Argument::exact(ModelManagerCompilerPass::MANAGER_TAG))
118
            ->willReturn(['sonata.admin.manager.test' => [ModelManagerCompilerPass::MANAGER_TAG, 'other.tag']]);
119
120
        $containerBuilderMock->getParameter(Argument::exact('kernel.bundles'))
121
            ->willReturn(['MakerBundle' => 'MakerBundle']);
122
123
        $containerBuilderMock->findDefinition(Argument::exact('sonata.admin.manager.test'))
124
            ->willReturn($definitionMock->reveal());
125
126
        $containerBuilderMock->getDefinition(Argument::exact('sonata.admin.maker'))
127
            ->willReturn($adminMaker->reveal());
128
129
        $containerBuilderMock->getParameter(Argument::containingString('kernel.project_dir'))
130
            ->willReturn(null);
131
132
        $compilerPass = new ModelManagerCompilerPass();
133
        $compilerPass->process($containerBuilderMock->reveal());
134
    }
135
136
    public function testProcessWithInvalidTaggedManagerDefinition(): void
137
    {
138
        $adminMaker = $this->prophesize(Definition::class);
139
        $adminMaker->replaceArgument(Argument::type('integer'), Argument::any())->shouldNotBeCalled();
140
        $containerBuilderMock = $this->prophesize(ContainerBuilder::class);
141
142
        $containerBuilderMock->getServiceIds()
143
            ->willReturn(['sonata.admin.manager.test']);
144
145
        $definitionMock = $this->prophesize(Definition::class);
146
147
        $definitionMock->getClass()
148
            ->willReturn(\stdClass::class);
149
150
        $definitionMock->hasTag(Argument::exact(ModelManagerCompilerPass::MANAGER_TAG))
151
            ->willReturn(true);
152
153
        $containerBuilderMock->findTaggedServiceIds(Argument::exact(ModelManagerCompilerPass::MANAGER_TAG))
154
            ->willReturn(['sonata.admin.manager.test' => [ModelManagerCompilerPass::MANAGER_TAG, 'other.tag']]);
155
156
        $containerBuilderMock->getParameter(Argument::exact('kernel.bundles'))
157
            ->willReturn(['MakerBundle' => 'MakerBundle']);
158
159
        $containerBuilderMock->findDefinition(Argument::exact('sonata.admin.manager.test'))
160
            ->willReturn($definitionMock->reveal());
161
162
        $containerBuilderMock->getDefinition(Argument::exact('sonata.admin.maker'))
163
            ->willReturn($adminMaker->reveal());
164
165
        $containerBuilderMock->getParameter(Argument::containingString('kernel.project_dir'))
166
            ->willReturn(null);
167
168
        $compilerPass = new ModelManagerCompilerPass();
169
170
        $this->expectException(LogicException::class);
171
        $this->expectExceptionMessage(sprintf('Service "sonata.admin.manager.test" must implement `%s`.', ModelManagerInterface::class));
172
173
        $compilerPass->process($containerBuilderMock->reveal());
174
    }
175
}
176