Passed
Pull Request — main (#4)
by Peter
06:44 queued 03:43
created

testCreateFromNameCanCreateComplexButtonWithIcon()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 20
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 29
rs 9.6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Html\Factory;
6
7
use AbterPhp\Framework\Constant\Html5;
8
use AbterPhp\Framework\Html\Attribute;
9
use AbterPhp\Framework\Html\Component\Button as ButtonComponent;
10
use AbterPhp\Framework\Html\Component\ButtonWithIcon;
11
use AbterPhp\Framework\Html\Helper\Attributes;
12
use AbterPhp\Framework\TestDouble\Html\Component\StubAttributeFactory;
13
use Opulence\Routing\Urls\UrlGenerator;
14
use PHPUnit\Framework\MockObject\MockObject;
15
use PHPUnit\Framework\TestCase;
16
17
class ButtonTest extends TestCase
18
{
19
    /** @var Button - System Under Test */
20
    protected Button $sut;
21
22
    /** @var UrlGenerator|MockObject */
23
    protected $urlGeneratorMock;
24
25
    /** @var array<string,Attribute> */
26
    protected array $iconAttributes;
27
28
    /** @var array<string,Attribute> */
29
    protected array $textAttributes;
30
31
    public function setUp(): void
32
    {
33
        parent::setUp();
34
35
        $this->urlGeneratorMock = $this->createMock(UrlGenerator::class);
36
37
        $this->iconAttributes = StubAttributeFactory::createAttributes(['icon' => ['asd']]);
38
        $this->textAttributes = StubAttributeFactory::createAttributes(['text' => ['qwe']]);
39
40
        $this->sut = new Button($this->urlGeneratorMock, $this->textAttributes, $this->iconAttributes);
41
    }
42
43
    public function testCreateFromUrlCreatesSimpleButtonByDefault(): void
44
    {
45
        $url  = '/best/url/ever';
46
        $text = 'button text';
47
48
        $actualResult = $this->sut->createFromUrl($text, $url);
49
50
        $this->assertInstanceOf(ButtonComponent::class, $actualResult);
51
        $this->assertNotInstanceOf(ButtonWithIcon::class, $actualResult);
52
    }
53
54
    public function testCreateFromUrlCanCreateButtonWithIcon(): void
55
    {
56
        $url  = '/best/url/ever';
57
        $text = 'button text';
58
        $icon = 'hello';
59
60
        $actualResult = $this->sut->createFromUrl($text, $url, $icon);
61
62
        $this->assertInstanceOf(ButtonComponent::class, $actualResult);
63
        $this->assertInstanceOf(ButtonWithIcon::class, $actualResult);
64
    }
65
66
    public function testCreateFromUrlCanCreateComplexButtonWithIcon(): void
67
    {
68
        $url         = '/best/url/ever';
69
        $text        = 'button text';
70
        $icon        = 'hello';
71
        $textAttribs = Attributes::fromArray(['attr5' => ['val7', 'val8'], 'attr6' => ['val9']]);
72
        $iconAttribs = Attributes::fromArray(['attr3' => ['val4', 'val5'], 'attr4' => ['val6']]);
73
        $attribs     = Attributes::fromArray(['attr1' => ['val1', 'val2'], 'attr2' => ['val3']]);
74
75
        $actualResult = $this->sut->createFromUrl(
76
            $text,
77
            $url,
78
            $icon,
79
            $textAttribs,
80
            $iconAttribs,
81
            [],
82
            $attribs,
83
            Html5::TAG_STRONG
84
        );
85
86
        $expectedResult = '<strong attr1="val1 val2" attr2="val3" href="/best/url/ever"><i attr3="val4 val5" attr4="val6" foo="foo baz" bar="bar baz" icon="asd">hello</i> <span attr5="val7 val8" attr6="val9" foo="foo baz" bar="bar baz" text="qwe">button text</span></strong>'; // phpcs:ignore
87
88
        $this->assertSame($expectedResult, (string)$actualResult);
89
    }
90
91
    public function testCreateFromNameCreatesSimpleButtonByDefault(): void
92
    {
93
        $name = 'best-route-ever';
94
        $text = 'button text';
95
96
        $this->urlGeneratorMock
97
            ->expects($this->atLeastOnce())
98
            ->method('createFromName')
99
            ->willReturn('/best/url/ever');
100
101
        $actualResult = $this->sut->createFromName($text, $name, []);
102
103
        $this->assertInstanceOf(ButtonComponent::class, $actualResult);
104
        $this->assertNotInstanceOf(ButtonWithIcon::class, $actualResult);
105
    }
106
107
    public function testCreateFromNameCanCreateButtonWithIcon(): void
108
    {
109
        $name = 'best-route-ever';
110
        $text = 'button text';
111
        $icon = 'hello';
112
113
        $this->urlGeneratorMock
114
            ->expects($this->atLeastOnce())
115
            ->method('createFromName')
116
            ->willReturn('/best/url/ever');
117
118
        $actualResult = $this->sut->createFromName($text, $name, [], $icon);
119
120
        $this->assertInstanceOf(ButtonComponent::class, $actualResult);
121
        $this->assertInstanceOf(ButtonWithIcon::class, $actualResult);
122
    }
123
124
    public function testCreateFromNameCanCreateComplexButtonWithIcon(): void
125
    {
126
        $name        = 'best-route-ever';
127
        $text        = 'button text';
128
        $icon        = 'hello';
129
        $textAttribs = Attributes::fromArray(['attr5' => ['val7', 'val8'], 'attr6' => ['val9']]);
130
        $iconAttribs = Attributes::fromArray(['attr3' => ['val4', 'val5'], 'attr4' => ['val6']]);
131
        $attribs     = Attributes::fromArray(['attr1' => ['val1', 'val2'], 'attr2' => ['val3']]);
132
133
        $this->urlGeneratorMock
134
            ->expects($this->atLeastOnce())
135
            ->method('createFromName')
136
            ->willReturn('/best/url/ever');
137
138
        $actualResult = $this->sut->createFromName(
139
            $text,
140
            $name,
141
            [],
142
            $icon,
143
            $textAttribs,
144
            $iconAttribs,
145
            [],
146
            $attribs,
147
            Html5::TAG_STRONG
148
        );
149
150
        $expectedResult = '<strong attr1="val1 val2" attr2="val3" href="/best/url/ever"><i attr3="val4 val5" attr4="val6" foo="foo baz" bar="bar baz" icon="asd">hello</i> <span attr5="val7 val8" attr6="val9" foo="foo baz" bar="bar baz" text="qwe">button text</span></strong>'; // phpcs:ignore
151
152
        $this->assertSame($expectedResult, (string)$actualResult);
153
    }
154
}
155