1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Framework\Grid\Action; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Framework\Html\Attribute; |
8
|
|
|
use AbterPhp\Framework\Html\Helper\Attributes; |
9
|
|
|
use AbterPhp\Framework\Html\INode; |
10
|
|
|
use AbterPhp\Framework\Html\Node; |
11
|
|
|
use AbterPhp\Framework\Html\Tag; |
12
|
|
|
use AbterPhp\Framework\TestDouble\Html\Component\StubAttributeFactory; |
13
|
|
|
use AbterPhp\Framework\TestDouble\I18n\MockTranslatorFactory; |
14
|
|
|
use LogicException; |
15
|
|
|
use Opulence\Orm\IEntity; |
16
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
17
|
|
|
use PHPUnit\Framework\TestCase; |
18
|
|
|
|
19
|
|
|
class ActionTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
public function testConstructThrowsExceptionIfAttributeDoesNotExistForAttributeCallback() |
22
|
|
|
{ |
23
|
|
|
$this->expectException(\InvalidArgumentException::class); |
24
|
|
|
|
25
|
|
|
new Action(null, [], null, ['foo' => fn () => true]); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return array[] |
30
|
|
|
*/ |
31
|
|
|
public function renderProvider(): array |
32
|
|
|
{ |
33
|
|
|
$attributes = StubAttributeFactory::createAttributes(); |
34
|
|
|
$str = Attributes::toString($attributes); |
35
|
|
|
|
36
|
|
|
$callbacks = [ |
37
|
|
|
StubAttributeFactory::ATTRIBUTE_FOO => fn() => [ |
38
|
|
|
StubAttributeFactory::VALUE_FOO, |
39
|
|
|
StubAttributeFactory::VALUE_BAZ, |
40
|
|
|
], |
41
|
|
|
StubAttributeFactory::ATTRIBUTE_BAR => fn() => StubAttributeFactory::VALUE_BAR_BAZ, |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
return [ |
45
|
|
|
'simple' => ['Button', null, [], null, null, "<button>Button</button>"], |
46
|
|
|
'with attributes' => ['Button', $attributes, [], null, null, "<button$str>Button</button>",], |
47
|
|
|
'missing translations' => ['Button', null, [], [], null, "<button>Button</button>"], |
48
|
|
|
'custom tag' => ['Button', null, [], null, 'mybutton', "<mybutton>Button</mybutton>"], |
49
|
|
|
'with translations' => ['Button', null, [], ['Button' => 'Gomb'], null, "<button>Gomb</button>"], |
50
|
|
|
'with callbacks' => ['Button', $attributes, $callbacks, null, null, "<button$str>Button</button>",], |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @dataProvider renderProvider |
56
|
|
|
* |
57
|
|
|
* @param INode[]|INode|string|null $content |
58
|
|
|
* @param Attribute[]|null $attributes |
59
|
|
|
* @param array $attributeCallbacks |
60
|
|
|
* @param string[]|null $translations |
61
|
|
|
* @param string|null $tag |
62
|
|
|
* @param string $expectedResult |
63
|
|
|
*/ |
64
|
|
|
public function testRender( |
65
|
|
|
$content, |
66
|
|
|
?array $attributes, |
67
|
|
|
array $attributeCallbacks, |
68
|
|
|
?array $translations, |
69
|
|
|
?string $tag, |
70
|
|
|
string $expectedResult |
71
|
|
|
): void { |
72
|
|
|
$sut = $this->createAction($content, $attributes, $attributeCallbacks, $translations, $tag); |
73
|
|
|
|
74
|
|
|
$actualResult1 = (string)$sut; |
75
|
|
|
$actualResult2 = (string)$sut; |
76
|
|
|
|
77
|
|
|
$this->assertSame($expectedResult, $actualResult1); |
78
|
|
|
$this->assertSame($expectedResult, $actualResult2); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testRenderCallsCallbackWithEntity(): void |
82
|
|
|
{ |
83
|
|
|
$expectedResult = "<button foo=\"bar\">Button</button>"; |
84
|
|
|
|
85
|
|
|
/** @var IEntity|MockObject $entityMock */ |
86
|
|
|
$entityMock = $this->getMockBuilder(IEntity::class)->getMock(); |
87
|
|
|
$entityMock->expects($this->atLeastOnce())->method('getId')->willReturn('bar'); |
88
|
|
|
|
89
|
|
|
$content = 'Button'; |
90
|
|
|
$attributes = Attributes::fromArray([ |
91
|
|
|
StubAttributeFactory::ATTRIBUTE_FOO => '', |
92
|
|
|
]); |
93
|
|
|
$attributeCallbacks = [ |
94
|
|
|
StubAttributeFactory::ATTRIBUTE_FOO => fn($value, IEntity $entity) => [$entity->getId()], |
95
|
|
|
]; |
96
|
|
|
|
97
|
|
|
$sut = $this->createAction($content, $attributes, $attributeCallbacks, null, null); |
98
|
|
|
|
99
|
|
|
$sut->setEntity($entityMock); |
100
|
|
|
|
101
|
|
|
$actualResult = (string)$sut; |
102
|
|
|
$this->assertSame($expectedResult, $actualResult); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function testClone(): void |
106
|
|
|
{ |
107
|
|
|
$attributes = StubAttributeFactory::createAttributes(); |
108
|
|
|
|
109
|
|
|
$sut = new Action([new Node('A'), new Tag('B')], [], $attributes); |
110
|
|
|
|
111
|
|
|
$clone = clone $sut; |
112
|
|
|
|
113
|
|
|
$this->assertNotSame($sut, $clone); |
114
|
|
|
$this->assertEquals($sut, $clone); |
115
|
|
|
$this->assertCount(2, $clone->getNodes()); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param INode[]|INode|string|null $content |
120
|
|
|
* @param array<string,Attribute>|null $attributes |
121
|
|
|
* @param array $attributeCallbacks |
122
|
|
|
* @param array|null $translations |
123
|
|
|
* @param string|null $tag |
124
|
|
|
* |
125
|
|
|
* @return Action |
126
|
|
|
*/ |
127
|
|
|
private function createAction( |
128
|
|
|
$content, |
129
|
|
|
?array $attributes, |
130
|
|
|
array $attributeCallbacks, |
131
|
|
|
?array $translations, |
132
|
|
|
?string $tag |
133
|
|
|
): Action { |
134
|
|
|
$action = new Action($content, [], $attributes, $attributeCallbacks, $tag); |
135
|
|
|
|
136
|
|
|
$action->setTranslator(MockTranslatorFactory::createSimpleTranslator($this, $translations)); |
137
|
|
|
|
138
|
|
|
return $action; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function testRemoveAttribute() |
142
|
|
|
{ |
143
|
|
|
$sut = new Action(null, [], ['foo' => new Attribute('foo')]); |
144
|
|
|
|
145
|
|
|
$sut->removeAttribute('foo'); |
146
|
|
|
|
147
|
|
|
$this->assertSame('<button></button>', (string)$sut); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function testRemoveAttributeThrowsAttributeWhenRemovingAttributeForAttributeCallback() |
151
|
|
|
{ |
152
|
|
|
$this->expectException(LogicException::class); |
153
|
|
|
|
154
|
|
|
$sut = new Action(null, [], ['foo' => new Attribute('foo')], ['foo' => fn () => true]); |
155
|
|
|
|
156
|
|
|
$sut->removeAttribute('foo'); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|