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 Sonata\AdminBundle\DependencyInjection\Compiler\ModelManagerCompilerPass; |
18
|
|
|
use Sonata\AdminBundle\Maker\AdminMaker; |
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 = new Definition(AdminMaker::class); |
33
|
|
|
$adminMaker->setArguments([ |
34
|
|
|
'', |
35
|
|
|
[], |
36
|
|
|
]); |
37
|
|
|
|
38
|
|
|
$containerBuilder = new ContainerBuilder(); |
39
|
|
|
$containerBuilder->setDefinition('sonata.admin.maker', $adminMaker); |
40
|
|
|
$containerBuilder->setParameter('kernel.bundles', ['MakerBundle' => 'MakerBundle']); |
41
|
|
|
|
42
|
|
|
$compilerPass = new ModelManagerCompilerPass(); |
43
|
|
|
$compilerPass->process($containerBuilder); |
44
|
|
|
|
45
|
|
|
$this->assertCount(0, $adminMaker->getArgument(1)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testProcessWithTaggedManagerDefinition(): void |
49
|
|
|
{ |
50
|
|
|
$adminMaker = new Definition(AdminMaker::class); |
51
|
|
|
$adminMaker->setArguments([ |
52
|
|
|
'', |
53
|
|
|
[], |
54
|
|
|
]); |
55
|
|
|
$containerBuilder = new ContainerBuilder(); |
56
|
|
|
$containerBuilder->setParameter('kernel.bundles', ['MakerBundle' => 'MakerBundle']); |
57
|
|
|
$containerBuilder->setDefinition('sonata.admin.maker', $adminMaker); |
58
|
|
|
|
59
|
|
|
$managerDefinition = new Definition(ModelManager::class); |
60
|
|
|
$managerDefinition->addTag(ModelManagerCompilerPass::MANAGER_TAG); |
61
|
|
|
|
62
|
|
|
$containerBuilder->setDefinition('sonata.admin.manager.test', $managerDefinition); |
63
|
|
|
|
64
|
|
|
$compilerPass = new ModelManagerCompilerPass(); |
65
|
|
|
$compilerPass->process($containerBuilder); |
66
|
|
|
|
67
|
|
|
$this->assertCount(1, $adminMaker->getArgument(1)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testProcessWithInvalidTaggedManagerDefinition(): void |
71
|
|
|
{ |
72
|
|
|
$adminMaker = new Definition(AdminMaker::class); |
73
|
|
|
$adminMaker->setArguments([ |
74
|
|
|
'', |
75
|
|
|
[], |
76
|
|
|
]); |
77
|
|
|
|
78
|
|
|
$containerBuilder = new ContainerBuilder(); |
79
|
|
|
$containerBuilder->setParameter('kernel.bundles', ['MakerBundle' => 'MakerBundle']); |
80
|
|
|
$containerBuilder->setDefinition('sonata.admin.maker', $adminMaker); |
81
|
|
|
|
82
|
|
|
$managerDefinition = new Definition(\stdClass::class); |
83
|
|
|
$managerDefinition->addTag(ModelManagerCompilerPass::MANAGER_TAG); |
84
|
|
|
|
85
|
|
|
$containerBuilder->setDefinition('sonata.admin.manager.test', $managerDefinition); |
86
|
|
|
|
87
|
|
|
$compilerPass = new ModelManagerCompilerPass(); |
88
|
|
|
|
89
|
|
|
$this->expectException(LogicException::class); |
90
|
|
|
$this->expectExceptionMessage(sprintf('Service "sonata.admin.manager.test" must implement `%s`.', ModelManagerInterface::class)); |
91
|
|
|
|
92
|
|
|
$compilerPass->process($containerBuilder); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|