testDecoratePrimaryNavigationRemovesBtnClasses()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 19
rs 9.8333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\PropellerAdmin\Decorator\Navigation;
6
7
use AbterPhp\Framework\Constant\Html5;
8
use AbterPhp\Framework\Constant\Session;
9
use AbterPhp\Framework\Html\Collection;
10
use AbterPhp\Framework\Html\Component;
11
use AbterPhp\Framework\Html\Component\Button;
12
use AbterPhp\Framework\Navigation\Dropdown;
13
use AbterPhp\Framework\Navigation\Item;
14
use AbterPhp\Framework\Navigation\Navigation;
15
use AbterPhp\Framework\Navigation\UserBlock;
16
use AbterPhp\Framework\TestDouble\Session\MockSessionFactory;
17
use Opulence\Routing\Urls\UrlGenerator;
18
use Opulence\Sessions\ISession;
19
use PHPUnit\Framework\MockObject\MockObject;
20
use PHPUnit\Framework\TestCase;
21
22
class PrimaryTest extends TestCase
23
{
24
    /** @var Primary - System Under Test */
25
    protected $sut;
26
27
    /** @var UrlGenerator|MockObject */
28
    protected $urlGeneratorMock;
29
30
    public function setUp(): void
31
    {
32
        $this->urlGeneratorMock = $this->getMockBuilder(UrlGenerator::class)
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated: https://github.com/sebastianbergmann/phpunit/pull/3687 ( Ignorable by Annotation )

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

32
        $this->urlGeneratorMock = /** @scrutinizer ignore-deprecated */ $this->getMockBuilder(UrlGenerator::class)

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

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

Loading history...
33
            ->disableOriginalConstructor()
34
            ->setMethods(['createFromName'])
35
            ->getMock();
36
37
        $this->sut = (new Primary($this->urlGeneratorMock))->init();
38
39
        parent::setUp();
40
    }
41
42
    public function testDecorateWithEmptyComponents()
43
    {
44
        $this->sut->decorate([]);
45
46
        $this->assertTrue(true, 'No error was found.');
47
    }
48
49
    public function testDecorateNonMatchingComponents()
50
    {
51
        $this->sut->decorate([new Component()]);
52
53
        $this->assertTrue(true, 'No error was found.');
54
    }
55
56
    public function testDecorateWithDoubleInit()
57
    {
58
        $this->sut->init();
59
        $this->sut->decorate([new Component()]);
60
61
        $this->testDecorateNonMatchingComponents();
62
    }
63
64
    public function testDecorateEmptyPrimaryNavigation()
65
    {
66
        $navigation = new Navigation([Navigation::INTENT_PRIMARY]);
67
68
        $this->sut->decorate([$navigation]);
69
70
        $this->assertStringContainsString(Primary::PRIMARY_CLASS, $navigation->getAttribute(Html5::ATTR_CLASS));
0 ignored issues
show
Bug introduced by
It seems like $navigation->getAttribut...tant\Html5::ATTR_CLASS) can also be of type null; however, parameter $haystack of PHPUnit\Framework\Assert...tStringContainsString() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

70
        $this->assertStringContainsString(Primary::PRIMARY_CLASS, /** @scrutinizer ignore-type */ $navigation->getAttribute(Html5::ATTR_CLASS));
Loading history...
71
    }
72
73
    public function testDecoratePrimaryNavigationWithGeneralItem()
74
    {
75
        $item = new Item();
76
77
        $navigation = new Navigation([Navigation::INTENT_PRIMARY]);
78
        $navigation->addItem($item);
79
80
        $this->sut->decorate([$navigation]);
81
82
        $this->assertFalse($item->hasAttribute(Html5::ATTR_CLASS));
83
    }
84
85
    public function testDecoratePrimaryNavigationWithUserGroup()
86
    {
87
        $sessionData = [
88
            Session::USERNAME => 'foo',
89
        ];
90
91
        /** @var ISession|MockObject $sessionMock */
92
        $sessionMock = MockSessionFactory::create($this, $sessionData);
93
94
        $item = new Item(new UserBlock($sessionMock), [UserBlock::class]);
95
96
        $navigation = new Navigation([Navigation::INTENT_PRIMARY]);
97
        $navigation->addItem($item);
98
99
        $this->sut->decorate([$navigation]);
100
101
        $this->assertEquals(Primary::USER_BLOCK_ITEM_CLASS, $item->getAttribute(Html5::ATTR_CLASS));
102
    }
103
104
    public function testDecoratePrimaryNavigationWithDropdownItemWithoutWrapper()
105
    {
106
        $dropdown = new Dropdown();
107
        $item     = new Item($dropdown, [UserBlock::class]);
108
109
        $navigation = new Navigation([Navigation::INTENT_PRIMARY]);
110
        $navigation->addItem($item);
111
112
        $this->sut->decorate([$navigation]);
113
114
        $this->assertEquals(Primary::DROPDOWN_CLASS, $dropdown->getAttribute(Html5::ATTR_CLASS));
115
    }
116
117
    public function testDecoratePrimaryNavigationWithDropdownItemWithWrapper()
118
    {
119
        $wrapper = new Component();
120
121
        $dropdown = new Dropdown();
122
        $dropdown->setWrapper($wrapper);
123
124
        $item = new Item($dropdown, [UserBlock::class]);
125
126
        $navigation = new Navigation([Navigation::INTENT_PRIMARY]);
127
        $navigation->addItem($item);
128
129
        $this->sut->decorate([$navigation]);
130
131
        $this->assertEquals(Primary::DROPDOWN_CLASS, $dropdown->getAttribute(Html5::ATTR_CLASS));
132
        $this->assertEquals(Primary::DROPDOWN_WRAPPER_CLASS, $wrapper->getAttribute(Html5::ATTR_CLASS));
133
    }
134
135
    public function testDecoratePrimaryNavigationRemovesBtnClasses()
136
    {
137
        /** @var Button[] $collection */
138
        $collection   = new Collection();
139
        $collection[] = new Button('A', [], [Html5::ATTR_CLASS => 'btn']);
140
        $collection[] = new Button('B', [], [Html5::ATTR_CLASS => 'btn btn-1']);
141
        $collection[] = new Button('C', [], [Html5::ATTR_CLASS => 'btn btn-2']);
142
        $button       = new Button('D', [], [Html5::ATTR_CLASS => 'btn btn-3']);
143
144
        $navigation = new Navigation([Navigation::INTENT_PRIMARY]);
145
        $navigation->addItem(new Item($collection));
146
        $navigation->addItem(new Item($button));
147
148
        $this->sut->decorate([$navigation]);
149
150
        $this->assertEquals('', $collection[0]->getAttribute(Html5::ATTR_CLASS));
151
        $this->assertEquals('btn-1', $collection[1]->getAttribute(Html5::ATTR_CLASS));
152
        $this->assertEquals('btn-2', $collection[2]->getAttribute(Html5::ATTR_CLASS));
153
        $this->assertEquals('btn-3', $button->getAttribute(Html5::ATTR_CLASS));
154
    }
155
}
156