Completed
Push — master ( 459e1d...afca2b )
by Eric
8s
created

AbstractLugUiExtensionTest::testMenu()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 1
eloc 9
nc 1
nop 0
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\UiBundle\Tests\DependencyInjection;
13
14
use Knp\Menu\FactoryInterface;
15
use Lug\Bundle\UiBundle\DependencyInjection\LugUiExtension;
16
use Lug\Bundle\UiBundle\Form\Extension\IconButtonExtension;
17
use Lug\Bundle\UiBundle\Form\Extension\IconFormExtension;
18
use Lug\Bundle\UiBundle\Menu\MenuBuilderInterface;
19
use Symfony\Component\DependencyInjection\ContainerBuilder;
20
use Symfony\Component\DependencyInjection\DefinitionDecorator;
21
use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher;
22
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
23
use Symfony\Component\Form\Extension\Core\Type\ButtonType;
24
use Symfony\Component\Form\Extension\Core\Type\FormType;
25
26
/**
27
 * @author GeLo <[email protected]>
28
 */
29
abstract class AbstractLugUiExtensionTest extends \PHPUnit_Framework_TestCase
30
{
31
    /**
32
     * @var LugUiExtension
33
     */
34
    private $extension;
35
36
    /**
37
     * @var ContainerBuilder
38
     */
39
    private $container;
40
41
    /**
42
     * @var FactoryInterface
43
     */
44
    private $menuFactory;
45
46
    /**
47
     * @var EventDispatcherInterface
48
     */
49
    private $eventDispatcher;
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    protected function setUp()
55
    {
56
        $this->menuFactory = $this->createMenuFactoryMock();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMenuFactoryMock() can also be of type object<PHPUnit_Framework_MockObject_MockObject>. However, the property $menuFactory is declared as type object<Knp\Menu\FactoryInterface>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
57
        $this->eventDispatcher = $this->createEventDispatcherMock();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createEventDispatcherMock() can also be of type object<PHPUnit_Framework_MockObject_MockObject>. However, the property $eventDispatcher is declared as type object<Symfony\Component...entDispatcherInterface>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
58
        $this->extension = new LugUiExtension();
59
60
        $this->container = new ContainerBuilder();
61
        $this->container->set('knp_menu.factory', $this->menuFactory);
62
        $this->container->set('event_dispatcher', $this->eventDispatcher);
63
        $this->container->registerExtension($this->extension);
64
        $this->container->loadFromExtension($this->extension->getAlias());
65
    }
66
67
    public function testForm()
68
    {
69
        $this->compileContainer();
70
71
        $this->assertTrue($this->container->hasDefinition($iconFormName = 'lug.ui.form.extension.icon.form'));
72
        $this->assertTrue($this->container->hasDefinition($iconButtonName = 'lug.ui.form.extension.icon.button'));
73
74
        $iconFormDefinition = $this->container->getDefinition($iconFormName);
75
        $iconButtonDefinition = $this->container->getDefinition($iconButtonName);
76
77
        $this->assertTrue($iconFormDefinition->hasTag($formTypeExtensionTag = 'form.type_extension'));
78
        $this->assertSame([[
79
            'extended_type' => FormType::class,
80
            'extended-type' => FormType::class,
81
        ]], $iconFormDefinition->getTag($formTypeExtensionTag));
82
83
        $this->assertTrue($iconButtonDefinition->hasTag($formTypeExtensionTag));
84
        $this->assertSame([[
85
            'extended_type' => ButtonType::class,
86
            'extended-type' => ButtonType::class,
87
        ]], $iconButtonDefinition->getTag($formTypeExtensionTag));
88
89
        $this->assertInstanceOf(IconFormExtension::class, $this->container->get($iconFormName));
90
        $this->assertInstanceOf(IconButtonExtension::class, $this->container->get($iconButtonName));
91
    }
92
93
    public function testMenu()
94
    {
95
        $definition = new DefinitionDecorator('lug.ui.menu.builder');
96
        $definition->setClass($class = $this->createMenuBuilderClassMock());
97
        $this->container->setDefinition($menuName = 'lug.ui.menu.test', $definition);
98
99
        $this->compileContainer();
100
101
        $this->assertInstanceOf(
102
            ContainerAwareEventDispatcher::class,
103
            $this->container->get('lug.ui.menu.event_dispatcher')
104
        );
105
106
        $this->assertInstanceOf($class, $this->container->get($menuName));
107
    }
108
109
    /**
110
     * @param ContainerBuilder $container
111
     * @param string           $configuration
112
     */
113
    abstract protected function loadConfiguration(ContainerBuilder $container, $configuration);
114
115
    /**
116
     * @param string|null $configuration
117
     */
118
    private function compileContainer($configuration = null)
119
    {
120
        if ($configuration !== null) {
121
            $this->loadConfiguration($this->container, $configuration);
122
        }
123
124
        $this->container->compile();
125
    }
126
127
    /**
128
     * @return \PHPUnit_Framework_MockObject_MockObject|FactoryInterface
129
     */
130
    private function createMenuFactoryMock()
131
    {
132
        return $this->getMock(FactoryInterface::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
133
    }
134
135
    /**
136
     * @return \PHPUnit_Framework_MockObject_MockObject|EventDispatcherInterface
137
     */
138
    private function createEventDispatcherMock()
139
    {
140
        return $this->getMock(EventDispatcherInterface::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
141
    }
142
143
    /**
144
     * @return string
145
     */
146
    private function createMenuBuilderClassMock()
147
    {
148
        return $this->getMockClass(MenuBuilderInterface::class);
149
    }
150
}
151