Completed
Push — master ( 081637...206e0a )
by Eric
06:26
created

MenuBuilderTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 86
rs 10
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\Menu;
13
14
use Knp\Menu\FactoryInterface;
15
use Knp\Menu\ItemInterface;
16
use Lug\Bundle\UiBundle\Menu\AbstractMenuBuilder;
17
use Lug\Bundle\UiBundle\Menu\MenuBuilderEvent;
18
use Lug\Bundle\UiBundle\Menu\MenuBuilderEvents;
19
use Lug\Bundle\UiBundle\Menu\MenuBuilderInterface;
20
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
21
22
/**
23
 * @author GeLo <[email protected]>
24
 */
25
class MenuBuilderTest extends \PHPUnit_Framework_TestCase
26
{
27
    /**
28
     * @var \PHPUnit_Framework_MockObject_MockObject|AbstractMenuBuilder
29
     */
30
    private $builder;
31
32
    /**
33
     * @var \PHPUnit_Framework_MockObject_MockObject|FactoryInterface
34
     */
35
    private $factory;
36
37
    /**
38
     * @var \PHPUnit_Framework_MockObject_MockObject|EventDispatcherInterface
39
     */
40
    private $eventDispatcher;
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    protected function setUp()
46
    {
47
        $this->factory = $this->createFactoryMock();
48
        $this->eventDispatcher = $this->createEventDispatcherMock();
49
50
        $this->builder = $this->getMockBuilder(AbstractMenuBuilder::class)
51
            ->setConstructorArgs([$this->factory, $this->eventDispatcher])
52
            ->getMockForAbstractClass();
53
    }
54
55
    public function testInheritance()
56
    {
57
        $this->assertInstanceOf(MenuBuilderInterface::class, $this->builder);
58
    }
59
60
    public function testCreate()
61
    {
62
        $this->builder
63
            ->expects($this->once())
64
            ->method('getName')
65
            ->will($this->returnValue($name = 'name'));
66
67
        $this->factory
68
            ->expects($this->once())
69
            ->method('createItem')
70
            ->with($this->identicalTo($name))
71
            ->will($this->returnValue($item = $this->createItemMock()));
72
73
        $this->eventDispatcher
74
            ->expects($this->once())
75
            ->method('dispatch')
76
            ->with(
77
                $this->identicalTo(MenuBuilderEvents::BUILD),
78
                $this->callback(function (MenuBuilderEvent $event) use ($item) {
79
                    return $event->getFactory() === $this->factory
80
                        && $event->getItem() === $item;
81
                })
82
            );
83
84
        $this->assertSame($item, $this->builder->create());
85
    }
86
87
    /**
88
     * @return \PHPUnit_Framework_MockObject_MockObject|FactoryInterface
89
     */
90
    private function createFactoryMock()
91
    {
92
        return $this->getMock(FactoryInterface::class);
93
    }
94
95
    /**
96
     * @return \PHPUnit_Framework_MockObject_MockObject|EventDispatcherInterface
97
     */
98
    private function createEventDispatcherMock()
99
    {
100
        return $this->getMock(EventDispatcherInterface::class);
101
    }
102
103
    /**
104
     * @return \PHPUnit_Framework_MockObject_MockObject|ItemInterface
105
     */
106
    private function createItemMock()
107
    {
108
        return $this->getMock(ItemInterface::class);
109
    }
110
}
111