ExtensionMenuCollectorTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 82
rs 10
c 0
b 0
f 0
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testAdd() 0 8 1
A menuProvider() 0 8 1
A testHas() 0 6 1
A allMenusProvider() 0 7 1
A testGet() 0 4 2
A setUp() 0 15 1
A testGetAllByContext() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
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 Zikula\ThemeBundle\Tests\ExtensionMenu;
15
16
use Knp\Menu\MenuFactory;
0 ignored issues
show
Bug introduced by
The type Knp\Menu\MenuFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use PHPUnit\Framework\TestCase;
18
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
19
use Zikula\ThemeBundle\ExtensionMenu\ExtensionMenuCollector;
20
use Zikula\ThemeBundle\ExtensionMenu\ExtensionMenuEvent;
21
use Zikula\ThemeBundle\ExtensionMenu\ExtensionMenuInterface;
22
use Zikula\ThemeBundle\Tests\ExtensionMenu\Fixtures\BarExtensionMenu;
23
use Zikula\ThemeBundle\Tests\ExtensionMenu\Fixtures\FooExtensionMenu;
24
25
class ExtensionMenuCollectorTest extends TestCase
26
{
27
    private ExtensionMenuCollector $collector;
28
29
    protected function setUp(): void
30
    {
31
        $dispatcher = $this
32
            ->getMockBuilder(EventDispatcherInterface::class)
33
            ->disableOriginalConstructor()
34
            ->getMock();
35
        $dispatcher
36
            ->method('dispatch')
37
            ->with($this->isInstanceOf(ExtensionMenuEvent::class), $this->anything())
38
            ->will($this->returnArgument(0));
0 ignored issues
show
Bug introduced by
The method returnArgument() does not exist on Zikula\ThemeBundle\Tests...ensionMenuCollectorTest. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
            ->will($this->/** @scrutinizer ignore-call */ returnArgument(0));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
39
        $this->collector = new ExtensionMenuCollector($dispatcher, []);
40
41
        $factory = new MenuFactory();
42
        $this->collector->add(new FooExtensionMenu($factory));
43
        $this->collector->add(new BarExtensionMenu($factory));
44
    }
45
46
    /**
47
     * @covers ExtensionMenuCollector::add
48
     */
49
    public function testAdd(): void
50
    {
51
        $menu = $this->getMockBuilder(ExtensionMenuInterface::class)->getMock();
52
        $menu
53
            ->method('getBundleName')
54
            ->willReturn('MockExtension');
55
        $this->collector->add($menu);
56
        $this->assertTrue($this->collector->has('MockExtension'));
57
    }
58
59
    /**
60
     * @covers ExtensionMenuCollector::has
61
     */
62
    public function testHas(): void
63
    {
64
        $this->assertTrue($this->collector->has('ZikulaFooExtension'));
65
        $this->assertTrue($this->collector->has('ZikulaBarExtension'));
66
        $this->assertFalse($this->collector->has('ZikulaFazExtension'));
67
        $this->assertFalse($this->collector->has('ZikulaBazExtension'));
68
    }
69
70
    /**
71
     * @covers ExtensionMenuCollector::get
72
     * @dataProvider menuProvider
73
     */
74
    public function testGet(string $extension, string $context, int $count): void
75
    {
76
        $menu = $this->collector->get($extension, $context);
77
        $this->assertEquals($count, $menu ? $menu->count() : 0);
0 ignored issues
show
introduced by
$menu is of type iterable, thus it always evaluated to false.
Loading history...
78
    }
79
80
    public static function menuProvider(): array
81
    {
82
        return [
83
            ['Unknown Extension', ExtensionMenuInterface::CONTEXT_ADMIN, 0],
84
            ['ZikulaFooExtension', 'UnknownContext', 0],
85
            ['ZikulaFooExtension', ExtensionMenuInterface::CONTEXT_ADMIN, 3],
86
            ['ZikulaFooExtension', ExtensionMenuInterface::CONTEXT_USER, 1],
87
            ['ZikulaBarExtension', 'bar', 1],
88
        ];
89
    }
90
91
    /**
92
     * @covers ExtensionMenuCollector::getAllByContext
93
     * @dataProvider allMenusProvider
94
     */
95
    public function testGetAllByContext(string $context, array $expected = []): void
96
    {
97
        $this->assertEquals($expected, array_keys($this->collector->getAllByContext($context)));
98
    }
99
100
    public static function allMenusProvider(): array
101
    {
102
        return [
103
            [ExtensionMenuInterface::CONTEXT_ACCOUNT, ['ZikulaBarExtension']],
104
            [ExtensionMenuInterface::CONTEXT_USER, ['ZikulaFooExtension', 'ZikulaBarExtension']],
105
            [ExtensionMenuInterface::CONTEXT_ADMIN, ['ZikulaFooExtension']],
106
            ['bar', ['ZikulaBarExtension']],
107
        ];
108
    }
109
}
110