|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AbterPhp\Framework\TestCase\Html; |
|
6
|
|
|
|
|
7
|
|
|
use AbterPhp\Framework\Html\INode; |
|
8
|
|
|
use AbterPhp\Framework\Html\Node; |
|
9
|
|
|
use AbterPhp\Framework\TestDouble\I18n\MockTranslatorFactory; |
|
10
|
|
|
use PHPUnit\Framework\TestCase; |
|
11
|
|
|
|
|
12
|
|
|
abstract class NodeTestCase extends TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @return array |
|
16
|
|
|
*/ |
|
17
|
|
|
public function setContentFailureProvider(): array |
|
18
|
|
|
{ |
|
19
|
|
|
return [ |
|
20
|
|
|
[new \stdClass()], |
|
21
|
|
|
]; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @dataProvider setContentFailureProvider |
|
26
|
|
|
* |
|
27
|
|
|
* @param mixed $content |
|
28
|
|
|
*/ |
|
29
|
|
|
public function testCreateFailure($content) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
32
|
|
|
|
|
33
|
|
|
$this->createNode($content); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @dataProvider setContentFailureProvider |
|
38
|
|
|
* |
|
39
|
|
|
* @param mixed $content |
|
40
|
|
|
*/ |
|
41
|
|
|
public function testSetContentFailure($content) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
44
|
|
|
|
|
45
|
|
|
$sut = $this->createNode(); |
|
46
|
|
|
|
|
47
|
|
|
$sut->setContent($content); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testDefaultToString() |
|
51
|
|
|
{ |
|
52
|
|
|
$sut = $this->createNode(); |
|
53
|
|
|
|
|
54
|
|
|
$this->assertSame('', (string)$sut); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
|
|
public function toStringReturnsRawContentByDefaultProvider(): array |
|
61
|
|
|
{ |
|
62
|
|
|
return [ |
|
63
|
|
|
'string' => ['foo', 'foo'], |
|
64
|
|
|
]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @dataProvider toStringReturnsRawContentByDefaultProvider |
|
69
|
|
|
* |
|
70
|
|
|
* @param mixed $rawContent |
|
71
|
|
|
* @param string $expectedResult |
|
72
|
|
|
*/ |
|
73
|
|
|
public function testToStringReturnsRawContentByDefault($rawContent, string $expectedResult) |
|
74
|
|
|
{ |
|
75
|
|
|
$sut = $this->createNode($rawContent); |
|
76
|
|
|
|
|
77
|
|
|
$this->assertStringContainsString($expectedResult, (string)$sut); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return array |
|
82
|
|
|
*/ |
|
83
|
|
|
public function toStringCanReturnTranslatedContentProvider(): array |
|
84
|
|
|
{ |
|
85
|
|
|
$translations = ['foo' => 'bar']; |
|
86
|
|
|
|
|
87
|
|
|
return [ |
|
88
|
|
|
'string' => ['foo', $translations, 'bar'], |
|
89
|
|
|
]; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @dataProvider toStringCanReturnTranslatedContentProvider |
|
94
|
|
|
* |
|
95
|
|
|
* @param mixed $rawContent |
|
96
|
|
|
* @param string $expectedResult |
|
97
|
|
|
*/ |
|
98
|
|
|
public function testToStringCanReturnTranslatedContent($rawContent, array $translations, string $expectedResult) |
|
99
|
|
|
{ |
|
100
|
|
|
$translatorMock = MockTranslatorFactory::createSimpleTranslator($this, $translations); |
|
101
|
|
|
|
|
102
|
|
|
$sut = $this->createNode($rawContent); |
|
103
|
|
|
|
|
104
|
|
|
$sut->setTranslator($translatorMock); |
|
105
|
|
|
|
|
106
|
|
|
$this->assertStringContainsString($expectedResult, (string)$sut); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return array |
|
111
|
|
|
*/ |
|
112
|
|
|
abstract public function isMatchProvider(): array; |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @dataProvider isMatchProvider |
|
116
|
|
|
* |
|
117
|
|
|
* @param string|null $className |
|
118
|
|
|
* @param string[] $intents |
|
119
|
|
|
* @param int|null $expectedResult |
|
120
|
|
|
*/ |
|
121
|
|
|
public function testIsMatch(?string $className, array $intents, bool $expectedResult) |
|
122
|
|
|
{ |
|
123
|
|
|
$sut = $this->createNode(); |
|
124
|
|
|
$sut->setIntent('foo', 'bar'); |
|
125
|
|
|
|
|
126
|
|
|
$actualResult = $sut->isMatch($className, ...$intents); |
|
127
|
|
|
|
|
128
|
|
|
$this->assertSame($expectedResult, $actualResult); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function testAddIntent() |
|
132
|
|
|
{ |
|
133
|
|
|
$intent0 = 'foo'; |
|
134
|
|
|
$intent1 = 'bar'; |
|
135
|
|
|
|
|
136
|
|
|
$sut = $this->createNode(); |
|
137
|
|
|
|
|
138
|
|
|
$sut->addIntent($intent0); |
|
139
|
|
|
$sut->addIntent($intent1); |
|
140
|
|
|
|
|
141
|
|
|
$intents = $sut->getIntents(); |
|
142
|
|
|
|
|
143
|
|
|
$this->assertSame([$intent0, $intent1], $intents); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function testHasIntent() |
|
147
|
|
|
{ |
|
148
|
|
|
$intent0 = 'foo'; |
|
149
|
|
|
$intent1 = 'bar'; |
|
150
|
|
|
|
|
151
|
|
|
$sut = $this->createNode(); |
|
152
|
|
|
|
|
153
|
|
|
$sut->addIntent($intent0); |
|
154
|
|
|
$sut->addIntent($intent1); |
|
155
|
|
|
|
|
156
|
|
|
$this->assertTrue($sut->hasIntent($intent0)); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @param INode|string|null $content |
|
161
|
|
|
* |
|
162
|
|
|
* @return Node |
|
163
|
|
|
*/ |
|
164
|
|
|
private function createNode($content = null): INode |
|
165
|
|
|
{ |
|
166
|
|
|
return new Node($content); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|