AbstractLugAdminExtensionTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 5
dl 0
loc 107
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 19 1
A testController() 0 6 1
A testMenus() 0 20 1
loadConfiguration() 0 1 ?
A compileContainer() 0 8 2
A createMenuFactoryMock() 0 4 1
A createEventDispatcherMock() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the Lug package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lug\Bundle\AdminBundle\Tests\DependencyInjection;
13
14
use Knp\Menu\FactoryInterface;
15
use Lug\Bundle\AdminBundle\Controller\DashboardController;
16
use Lug\Bundle\AdminBundle\DependencyInjection\LugAdminExtension;
17
use Lug\Bundle\AdminBundle\Menu\SidebarMenuBuilder;
18
use Lug\Bundle\AdminBundle\Menu\ToolbarMenuBuilder;
19
use Lug\Bundle\GridBundle\LugGridBundle;
20
use Lug\Bundle\UiBundle\Menu\AbstractMenuBuilder;
21
use Symfony\Component\DependencyInjection\ContainerBuilder;
22
use Symfony\Component\DependencyInjection\Definition;
23
use Symfony\Component\DependencyInjection\Reference;
24
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
25
26
/**
27
 * @author GeLo <[email protected]>
28
 */
29
abstract class AbstractLugAdminExtensionTest extends \PHPUnit_Framework_TestCase
30
{
31
    /**
32
     * @var LugAdminExtension
33
     */
34
    private $extension;
35
36
    /**
37
     * @var ContainerBuilder
38
     */
39
    private $container;
40
41
    /**
42
     * @var \PHPUnit_Framework_MockObject_MockObject|FactoryInterface
43
     */
44
    private $menuFactory;
45
46
    /**
47
     * @var \PHPUnit_Framework_MockObject_MockObject|EventDispatcherInterface
48
     */
49
    private $eventDispatcher;
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    protected function setUp()
55
    {
56
        $this->menuFactory = $this->createMenuFactoryMock();
57
        $this->eventDispatcher = $this->createEventDispatcherMock();
58
        $this->extension = new LugAdminExtension();
59
60
        $this->container = new ContainerBuilder();
61
        $this->container->setParameter('kernel.bundles', ['LugGridBundle' => LugGridBundle::class]);
62
        $this->container->set($menuFactoryName = 'knp_menu.factory', $this->menuFactory);
63
        $this->container->set($eventDispatcherName = 'event_dispatcher', $this->eventDispatcher);
64
65
        $this->container->setDefinition('lug.ui.menu.builder', (new Definition(AbstractMenuBuilder::class, [
66
            new Reference($menuFactoryName),
67
            new Reference($eventDispatcherName),
68
        ]))->setAbstract(true));
69
70
        $this->container->registerExtension($this->extension);
71
        $this->container->loadFromExtension($this->extension->getAlias());
72
    }
73
74
    public function testController()
75
    {
76
        $this->compileContainer();
77
78
        $this->assertInstanceOf(DashboardController::class, $this->container->get('lug.admin.controller.dashboard'));
79
    }
80
81
    public function testMenus()
82
    {
83
        $this->compileContainer();
84
85
        $this->assertTrue($this->container->hasDefinition($sidebarMenuBuilderName = 'lug.admin.menu.builder.sidebar'));
86
        $this->assertTrue($this->container->hasDefinition($toolbarMenuBuilderName = 'lug.admin.menu.builder.toolbar'));
87
88
        $this->assertSame(
89
            [['alias' => 'lug.admin.sidebar']],
90
            $this->container->getDefinition($sidebarMenuBuilderName)->getTag($tag = 'lug.menu.builder')
91
        );
92
93
        $this->assertSame(
94
            [['alias' => 'lug.admin.toolbar']],
95
            $this->container->getDefinition($toolbarMenuBuilderName)->getTag($tag)
96
        );
97
98
        $this->assertInstanceOf(SidebarMenuBuilder::class, $this->container->get($sidebarMenuBuilderName));
99
        $this->assertInstanceOf(ToolbarMenuBuilder::class, $this->container->get($toolbarMenuBuilderName));
100
    }
101
102
    /**
103
     * @param ContainerBuilder $container
104
     * @param string           $configuration
105
     */
106
    abstract protected function loadConfiguration(ContainerBuilder $container, $configuration);
107
108
    /**
109
     * @param string|null $configuration
110
     */
111
    private function compileContainer($configuration = null)
112
    {
113
        if ($configuration !== null) {
114
            $this->loadConfiguration($this->container, $configuration);
115
        }
116
117
        $this->container->compile();
118
    }
119
120
    /**
121
     * @return \PHPUnit_Framework_MockObject_MockObject|FactoryInterface
122
     */
123
    private function createMenuFactoryMock()
124
    {
125
        return $this->createMock(FactoryInterface::class);
126
    }
127
128
    /**
129
     * @return \PHPUnit_Framework_MockObject_MockObject|EventDispatcherInterface
130
     */
131
    private function createEventDispatcherMock()
132
    {
133
        return $this->createMock(EventDispatcherInterface::class);
134
    }
135
}
136