| Total Complexity | 51 |
| Total Lines | 786 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 1 |
Complex classes like NodeTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use NodeTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class NodeTest extends TestCase |
||
| 14 | { |
||
| 15 | |||
| 16 | /** @var Node - System Under Test */ |
||
| 17 | protected Node $sut; |
||
| 18 | |||
| 19 | public function setUp(): void |
||
| 20 | { |
||
| 21 | $this->sut = new Node(); |
||
| 22 | } |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @return array[] |
||
| 26 | */ |
||
| 27 | public function setContentProvider(): array |
||
| 28 | { |
||
| 29 | $fooNode = new Node('foo'); |
||
| 30 | $fooBarNode = new Node(['foo', 'bar']); |
||
| 31 | $fooNodeNode = new Node($fooNode, 'baz'); |
||
| 32 | |||
| 33 | return [ |
||
| 34 | 'null' => [null, '', []], |
||
| 35 | 'false' => [false, '', []], |
||
| 36 | 'true' => [true, '1', []], |
||
| 37 | '12.43' => [12.43, '12.43', []], |
||
| 38 | 'string' => ['foo', 'foo', []], |
||
| 39 | 'array' => [['foo'], 'foo', []], |
||
| 40 | 'strings' => [['foo', 'bar'], 'foobar', []], |
||
| 41 | 'node' => [$fooNode, 'foo', [$fooNode]], |
||
| 42 | 'node-with-array' => [$fooBarNode, 'foobar', [$fooBarNode]], |
||
| 43 | 'node-node' => [$fooNodeNode, 'foo', [$fooNodeNode]], |
||
| 44 | 'node-nodes' => [[$fooNode, $fooNodeNode], 'foofoo', [$fooNode, $fooNodeNode]], |
||
| 45 | 'mixed' => [[$fooNode, 'bar', $fooNodeNode], 'foobarfoo', [$fooNode, $fooNodeNode]], |
||
| 46 | ]; |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @dataProvider setContentProvider |
||
| 51 | * |
||
| 52 | * @param $content |
||
| 53 | * @param string $expectedAsString |
||
| 54 | * @param array $expectedNodes |
||
| 55 | */ |
||
| 56 | public function testSetContentNode($content, string $expectedAsString, array $expectedNodes) |
||
| 57 | { |
||
| 58 | $node = new Node($content); |
||
| 59 | |||
| 60 | $actualString = (string)$node; |
||
| 61 | $this->assertEquals($expectedAsString, $actualString); |
||
| 62 | |||
| 63 | $actualNodes = $node->getExtendedNodes(); |
||
| 64 | $this->assertEquals($expectedNodes, $actualNodes); |
||
| 65 | } |
||
| 66 | |||
| 67 | public function testSetContentWillThrowExceptionOnNonStringLikeContent() |
||
| 72 | } |
||
| 73 | |||
| 74 | public function testGetNodes() |
||
| 75 | { |
||
| 76 | $nodeStub = $this->createMock(Node::class); |
||
| 77 | |||
| 78 | $expectedResult = [$nodeStub]; |
||
| 79 | |||
| 80 | $sut = new Node(['foo', $nodeStub]); |
||
| 81 | |||
| 82 | $actualResult = $sut->getNodes(); |
||
| 83 | |||
| 84 | $this->assertSame($expectedResult, $actualResult); |
||
| 85 | } |
||
| 86 | |||
| 87 | public function testGetExtendedNodes() |
||
| 88 | { |
||
| 89 | $nodeStub = $this->createMock(Node::class); |
||
| 90 | |||
| 91 | $expectedResult = [$nodeStub]; |
||
| 92 | |||
| 93 | $sut = new Node(['foo', $nodeStub]); |
||
| 94 | |||
| 95 | $actualResult = $sut->getExtendedNodes(); |
||
| 96 | |||
| 97 | $this->assertSame($expectedResult, $actualResult); |
||
| 98 | } |
||
| 99 | |||
| 100 | public function testForceGetNodes() |
||
| 101 | { |
||
| 102 | $nodeContent = 'foo'; |
||
| 103 | |||
| 104 | $nodeStub = $this->createMock(Node::class); |
||
| 105 | $nodeStub2 = new Node($nodeContent); |
||
| 106 | |||
| 107 | $expectedResult = [$nodeStub2, $nodeStub]; |
||
| 108 | |||
| 109 | $sut = new Node([$nodeContent, $nodeStub]); |
||
| 110 | |||
| 111 | $actualResult = $sut->forceGetNodes(); |
||
| 112 | |||
| 113 | $this->assertNotSame($expectedResult, $actualResult); |
||
| 114 | $this->assertEquals($expectedResult, $actualResult); |
||
| 115 | } |
||
| 116 | |||
| 117 | public function testConstructAddsIntents() |
||
| 118 | { |
||
| 119 | $expectedIntents = ['foo', 'bar']; |
||
| 120 | |||
| 121 | $node = new Node(null, ...$expectedIntents); |
||
| 122 | $this->assertEquals($expectedIntents, $node->getIntents()); |
||
| 123 | } |
||
| 124 | |||
| 125 | public function testSetIntent() |
||
| 126 | { |
||
| 127 | $node = new Node(null, 'foo', 'bar'); |
||
| 128 | $this->assertTrue($node->hasIntent('foo')); |
||
| 129 | $this->assertTrue($node->hasIntent('bar')); |
||
| 130 | |||
| 131 | $node->setIntent('Foo', 'Bar'); |
||
| 132 | $this->assertFalse($node->hasIntent('foo')); |
||
| 133 | $this->assertFalse($node->hasIntent('bar')); |
||
| 134 | $this->assertTrue($node->hasIntent('Foo')); |
||
| 135 | $this->assertTrue($node->hasIntent('Bar')); |
||
| 136 | } |
||
| 137 | |||
| 138 | public function testAddIntent() |
||
| 139 | { |
||
| 140 | $sut = new Node(null, 'foo', 'bar'); |
||
| 141 | $this->assertTrue($sut->hasIntent('foo')); |
||
| 142 | $this->assertTrue($sut->hasIntent('bar')); |
||
| 143 | |||
| 144 | $sut->addIntent('baz'); |
||
| 145 | $this->assertTrue($sut->hasIntent('foo')); |
||
| 146 | $this->assertTrue($sut->hasIntent('bar')); |
||
| 147 | $this->assertTrue($sut->hasIntent('baz')); |
||
| 148 | } |
||
| 149 | |||
| 150 | public function testSetTranslatorSetTranslatorOnNodes() |
||
| 151 | { |
||
| 152 | $embedded = new Node(); |
||
| 153 | $wrapper = new Node($embedded); |
||
| 154 | |||
| 155 | $translatorMock = $this->getMockBuilder(ITranslator::class)->getMock(); |
||
| 156 | |||
| 157 | $wrapper->setTranslator($translatorMock); |
||
| 158 | |||
| 159 | $this->assertSame($translatorMock, $embedded->getTranslator()); |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @return array[] |
||
| 164 | */ |
||
| 165 | public function isMatchProvider(): array |
||
| 166 | { |
||
| 167 | $f1 = fn(INode $node) => $node->hasIntent('foo'); |
||
| 168 | $f2 = fn() => false; |
||
| 169 | |||
| 170 | return [ |
||
| 171 | 'empty' => [null, null, [], true], |
||
| 172 | 'inode' => [INode::class, null, [], true], |
||
| 173 | 'node' => [Node::class, null, [], true], |
||
| 174 | 'nodetest' => [NodeTest::class, null, [], false], |
||
| 175 | 'foo' => [null, null, ['foo'], true], |
||
| 176 | 'bar' => [null, null, ['bar'], true], |
||
| 177 | 'baz' => [null, null, ['baz'], false], |
||
| 178 | 'foobar' => [null, null, ['foo', 'bar'], true], |
||
| 179 | 'node-foobar' => [Node::class, null, ['foo', 'bar'], true], |
||
| 180 | 'node-foobarbaz' => [Node::class, null, ['foo', 'bar', 'baz'], false], |
||
| 181 | 'f1' => [null, $f1, [], true], |
||
| 182 | 'f2' => [null, $f2, [], false], |
||
| 183 | ]; |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @dataProvider isMatchProvider |
||
| 188 | * |
||
| 189 | * @param string|null $className |
||
| 190 | * @param \Closure|null $matcher |
||
| 191 | * @param string[] $intents |
||
| 192 | * @param bool $expectedResult |
||
| 193 | */ |
||
| 194 | public function testIsMatch(?string $className, ?Closure $matcher, array $intents, bool $expectedResult) |
||
| 195 | { |
||
| 196 | $sut = new Node('foo', 'foo', 'bar'); |
||
| 197 | |||
| 198 | $actualResult = $sut->isMatch($className, $matcher, ...$intents); |
||
| 199 | |||
| 200 | $this->assertSame($expectedResult, $actualResult); |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @dataProvider isMatchProvider |
||
| 205 | * |
||
| 206 | * @param string|null $className |
||
| 207 | * @param \Closure|null $matcher |
||
| 208 | * @param string[] $intents |
||
| 209 | * @param bool $expectedResult |
||
| 210 | */ |
||
| 211 | public function testFindFindsItself(?string $className, ?Closure $matcher, array $intents, bool $expectedResult) |
||
| 212 | { |
||
| 213 | $sut = new Node('foo', 'foo', 'bar'); |
||
| 214 | |||
| 215 | $actualResult = $sut->find($className, $matcher, ...$intents); |
||
| 216 | |||
| 217 | if ($expectedResult) { |
||
| 218 | $this->assertSame($sut, $actualResult); |
||
| 219 | } else { |
||
| 220 | $this->assertNull($actualResult); |
||
| 221 | } |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @return array[] |
||
| 226 | */ |
||
| 227 | public function findTraversesNodesProvider(): array |
||
| 241 | ]; |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @dataProvider findTraversesNodesProvider |
||
| 246 | * |
||
| 247 | * @param string|null $className |
||
| 248 | * @param \Closure|null $matcher |
||
| 249 | * @param string[] $intents |
||
| 250 | * @param bool $expectedResult |
||
| 251 | */ |
||
| 252 | public function testFindTraversesNodes(?string $className, ?Closure $matcher, array $intents, bool $expectedResult) |
||
| 253 | { |
||
| 254 | $grandChild = new Node('foo', 'foo', 'bar'); |
||
| 255 | $child = new Node([new Node(), '', $grandChild]); |
||
| 256 | $sut = new Node($child); |
||
| 257 | |||
| 258 | $actualResult = $sut->find($className, $matcher, ...$intents); |
||
| 259 | |||
| 260 | if ($expectedResult) { |
||
| 261 | $this->assertSame($grandChild, $actualResult); |
||
| 262 | } else { |
||
| 263 | $this->assertNull($actualResult); |
||
| 264 | } |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @return array[] |
||
| 269 | */ |
||
| 270 | public function findAllProvider(): array |
||
| 271 | { |
||
| 272 | $f1 = fn(INode $node) => $node->hasIntent('foo'); |
||
| 273 | $f2 = fn() => false; |
||
| 274 | |||
| 275 | return [ |
||
| 276 | 'empty' => [null, null, [], 4], |
||
| 277 | 'inode' => [INode::class, null, [], 4], |
||
| 278 | 'node' => [Node::class, null, [], 4], |
||
| 279 | 'nodetest' => [NodeTest::class, null, [], 0], |
||
| 280 | 'foo' => [null, null, ['foo'], 1], |
||
| 281 | 'bar' => [null, null, ['bar'], 1], |
||
| 282 | 'baz' => [null, null, ['baz'], 0], |
||
| 283 | 'foobar' => [null, null, ['foo', 'bar'], 1], |
||
| 284 | 'node-foobar' => [Node::class, null, ['foo', 'bar'], 1], |
||
| 285 | 'node-foobarbaz' => [Node::class, null, ['foo', 'bar', 'baz'], 0], |
||
| 286 | 'f1' => [null, $f1, [], 1], |
||
| 287 | 'f2' => [null, $f2, [], 0], |
||
| 288 | ]; |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @dataProvider findAllProvider |
||
| 293 | * |
||
| 294 | * @param string|null $className |
||
| 295 | * @param \Closure|null $matcher |
||
| 296 | * @param string[] $intents |
||
| 297 | * @param int $expectedLength |
||
| 298 | */ |
||
| 299 | public function testFindAll(?string $className, ?Closure $matcher, array $intents, int $expectedLength) |
||
| 300 | { |
||
| 301 | $grandChild = new Node('foo', 'foo', 'bar'); |
||
| 302 | $child = new Node([new Node(), '', $grandChild]); |
||
| 303 | $sut = new Node($child); |
||
| 304 | |||
| 305 | $actualResult = $sut->findAll($className, $matcher, ...$intents); |
||
| 306 | |||
| 307 | $this->assertSame($expectedLength, count($actualResult)); |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @return array[] |
||
| 312 | */ |
||
| 313 | public function findAllShallowProvider(): array |
||
| 314 | { |
||
| 315 | return [ |
||
| 316 | 'empty-3' => [3, null, null, [], 4], |
||
| 317 | 'empty-2' => [2, null, null, [], 4], |
||
| 318 | 'empty-1' => [1, null, null, [], 2], |
||
| 319 | 'empty-0' => [0, null, null, [], 1], |
||
| 320 | 'empty--1' => [-1, null, null, [], 4], |
||
| 321 | 'foo-2' => [2, null, null, ['foo'], 1], |
||
| 322 | 'foo-1' => [1, null, null, ['foo'], 0], |
||
| 323 | ]; |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @dataProvider findAllShallowProvider |
||
| 328 | * |
||
| 329 | * @param int $maxDepth |
||
| 330 | * @param string|null $className |
||
| 331 | * @param \Closure|null $matcher |
||
| 332 | * @param string[] $intents |
||
| 333 | * @param int $expectedLength |
||
| 334 | */ |
||
| 335 | public function testFindAllShallow( |
||
| 336 | int $maxDepth, |
||
| 337 | ?string $className, |
||
| 338 | ?Closure $matcher, |
||
| 339 | array $intents, |
||
| 340 | int $expectedLength |
||
| 341 | ) { |
||
| 342 | $grandChild = new Node('foo', 'foo', 'bar'); |
||
| 343 | $child = new Node([new Node(), '', $grandChild]); |
||
| 344 | $sut = new Node($child); |
||
| 345 | |||
| 346 | $actualResult = $sut->findAllShallow($maxDepth, $className, $matcher, ...$intents); |
||
| 347 | |||
| 348 | $this->assertSame($expectedLength, count($actualResult)); |
||
| 349 | } |
||
| 350 | |||
| 351 | public function testToString() |
||
| 352 | { |
||
| 353 | $grandChild = new Node('foo', 'foo', 'bar'); |
||
| 354 | $child = new Node([new Node(), '', $grandChild]); |
||
| 355 | $sut = new Node($child); |
||
| 356 | |||
| 357 | $expectedResult = 'bar'; |
||
| 358 | |||
| 359 | $translatorMock = MockTranslatorFactory::createSimpleTranslator($this, ['foo' => 'bar']); |
||
| 360 | |||
| 361 | $sut->setTranslator($translatorMock); |
||
| 362 | |||
| 363 | $actualResult = (string)$sut; |
||
| 364 | |||
| 365 | $this->assertSame($expectedResult, $actualResult); |
||
| 366 | } |
||
| 367 | public function toStringReturnsRawContentByDefaultProvider(): array |
||
| 368 | { |
||
| 369 | return [ |
||
| 370 | 'INode[]' => [[new Node('foo')], 'foo'], |
||
| 371 | ]; |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @dataProvider toStringReturnsRawContentByDefaultProvider |
||
| 376 | * |
||
| 377 | * @param mixed $rawContent |
||
| 378 | * @param string $expectedResult |
||
| 379 | */ |
||
| 380 | public function testToStringReturnsRawContentByDefault($rawContent, string $expectedResult): void |
||
| 381 | { |
||
| 382 | $sut = $this->createCollectionStub($rawContent); |
||
| 383 | |||
| 384 | $this->assertStringContainsString($expectedResult, (string)$sut); |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * @return array |
||
| 389 | */ |
||
| 390 | public function toStringCanReturnTranslatedContentProvider(): array |
||
| 391 | { |
||
| 392 | $translations = ['foo' => 'bar']; |
||
| 393 | |||
| 394 | return [ |
||
| 395 | 'INode[]' => [[new Node('foo')], $translations, 'bar'], |
||
| 396 | ]; |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @dataProvider toStringCanReturnTranslatedContentProvider |
||
| 401 | * |
||
| 402 | * @param mixed $rawContent |
||
| 403 | * @param array $translations |
||
| 404 | * @param string $expectedResult |
||
| 405 | */ |
||
| 406 | public function testToStringCanReturnTranslatedContent( |
||
| 407 | $rawContent, |
||
| 408 | array $translations, |
||
| 409 | string $expectedResult |
||
| 410 | ): void { |
||
| 411 | $translatorMock = MockTranslatorFactory::createSimpleTranslator($this, $translations); |
||
| 412 | |||
| 413 | $sut = $this->createCollectionStub($rawContent); |
||
| 414 | |||
| 415 | $sut->setTranslator($translatorMock); |
||
| 416 | |||
| 417 | $this->assertStringContainsString($expectedResult, (string)$sut); |
||
| 418 | } |
||
| 419 | |||
| 420 | public function testCountWithoutOffset(): void |
||
| 421 | { |
||
| 422 | $expectedResult = 2; |
||
| 423 | |||
| 424 | $node1 = new Node('1'); |
||
| 425 | $node2 = new Node('2'); |
||
| 426 | |||
| 427 | $sut = $this->createCollectionStub(); |
||
| 428 | |||
| 429 | $sut[] = $node1; |
||
| 430 | $sut[] = $node2; |
||
| 431 | |||
| 432 | $this->assertSame($expectedResult, count($sut)); |
||
| 433 | } |
||
| 434 | |||
| 435 | public function testCountWithExplicitOffset(): void |
||
| 436 | { |
||
| 437 | $expectedResult = 2; |
||
| 438 | |||
| 439 | $node1 = new Node('1'); |
||
| 440 | $node2 = new Node('2'); |
||
| 441 | |||
| 442 | $sut = $this->createCollectionStub(); |
||
| 443 | |||
| 444 | $sut[0] = $node1; |
||
| 445 | $sut[1] = $node2; |
||
| 446 | |||
| 447 | $this->assertSame($expectedResult, count($sut)); |
||
| 448 | } |
||
| 449 | |||
| 450 | public function testCountWithMixedOffset(): void |
||
| 451 | { |
||
| 452 | $node1 = new Node('1'); |
||
| 453 | $node2 = new Node('2'); |
||
| 454 | |||
| 455 | $expectedCount = 5; |
||
| 456 | |||
| 457 | $sut = $this->createCollectionStub(); |
||
| 458 | |||
| 459 | $sut[] = $node1; |
||
| 460 | $sut[] = $node1; |
||
| 461 | $sut[1] = $node2; |
||
| 462 | $sut[2] = $node1; |
||
| 463 | $sut[3] = $node1; |
||
| 464 | $sut[] = $node1; |
||
| 465 | |||
| 466 | $this->assertSame($expectedCount, count($sut)); |
||
| 467 | } |
||
| 468 | |||
| 469 | public function testArrayAccessWithoutOffset(): void |
||
| 470 | { |
||
| 471 | $node1 = new Node('1'); |
||
| 472 | $node2 = new Node('2'); |
||
| 473 | |||
| 474 | $sut = $this->createCollectionStub(); |
||
| 475 | |||
| 476 | $sut[] = $node1; |
||
| 477 | $sut[] = $node2; |
||
| 478 | |||
| 479 | $this->assertSame($node1, $sut[0]); |
||
| 480 | $this->assertSame($node2, $sut[1]); |
||
| 481 | } |
||
| 482 | |||
| 483 | public function testArrayAccessWithExplicitOffset(): void |
||
| 484 | { |
||
| 485 | $node1 = new Node('1'); |
||
| 486 | $node2 = new Node('2'); |
||
| 487 | |||
| 488 | $sut = $this->createCollectionStub(); |
||
| 489 | |||
| 490 | $sut[0] = $node1; |
||
| 491 | $sut[1] = $node2; |
||
| 492 | |||
| 493 | $this->assertSame($node1, $sut[0]); |
||
| 494 | $this->assertSame($node2, $sut[1]); |
||
| 495 | } |
||
| 496 | |||
| 497 | public function testArrayAccessThrowExceptionWhenMadeDirty(): void |
||
| 498 | { |
||
| 499 | $this->expectException(\InvalidArgumentException::class); |
||
| 500 | |||
| 501 | $node1 = new Node('1'); |
||
| 502 | |||
| 503 | $sut = $this->createCollectionStub(); |
||
| 504 | |||
| 505 | $sut[1] = $node1; |
||
| 506 | } |
||
| 507 | |||
| 508 | public function testArrayAccessWithMixedOffset(): void |
||
| 509 | { |
||
| 510 | $node1 = new Node('1'); |
||
| 511 | $node2 = new Node('2'); |
||
| 512 | |||
| 513 | $expectedNodes = [0 => $node1, 1 => $node2, 2 => $node1, 3 => $node1, 4 => $node1]; |
||
| 514 | |||
| 515 | $sut = $this->createCollectionStub(); |
||
| 516 | |||
| 517 | $sut[] = $node1; |
||
| 518 | $sut[] = $node1; |
||
| 519 | $sut[1] = $node2; |
||
| 520 | $sut[2] = $node1; |
||
| 521 | $sut[3] = $node1; |
||
| 522 | $sut[] = $node1; |
||
| 523 | |||
| 524 | $this->assertEquals($expectedNodes, $sut->getExtendedNodes()); |
||
| 525 | } |
||
| 526 | |||
| 527 | /** |
||
| 528 | * @return array[] |
||
| 529 | */ |
||
| 530 | public function contentFailureProvider(): array |
||
| 531 | { |
||
| 532 | return [ |
||
| 533 | 'string wrapped' => [['']], |
||
| 534 | 'non-node object wrapped' => [[new \StdClass()]], |
||
| 535 | 'node double wrapped' => [[[new Node()]]], |
||
| 536 | ]; |
||
| 537 | } |
||
| 538 | |||
| 539 | /** |
||
| 540 | * @dataProvider contentFailureProvider |
||
| 541 | */ |
||
| 542 | public function testConstructFailure($item): void |
||
| 543 | { |
||
| 544 | $this->expectException(\AssertionError::class); |
||
| 545 | |||
| 546 | $this->createCollectionStub($item); |
||
| 547 | } |
||
| 548 | |||
| 549 | /** |
||
| 550 | * @dataProvider contentFailureProvider |
||
| 551 | */ |
||
| 552 | public function testSetContentFailure($item): void |
||
| 553 | { |
||
| 554 | $this->expectException(\AssertionError::class); |
||
| 555 | |||
| 556 | $sut = $this->createCollectionStub(); |
||
| 557 | |||
| 558 | $sut->setContent($item); |
||
| 559 | } |
||
| 560 | |||
| 561 | /** |
||
| 562 | * @return array |
||
| 563 | */ |
||
| 564 | public function offsetSetFailureProvider(): array |
||
| 565 | { |
||
| 566 | $contentFailure = $this->contentFailureProvider(); |
||
| 567 | |||
| 568 | $offsetFailure = [ |
||
| 569 | 'string' => ['foo'], |
||
| 570 | 'node wrapped' => [[new Node()]], |
||
| 571 | ]; |
||
| 572 | |||
| 573 | return array_merge($contentFailure, $offsetFailure); |
||
| 574 | } |
||
| 575 | |||
| 576 | /** |
||
| 577 | * @dataProvider offsetSetFailureProvider |
||
| 578 | */ |
||
| 579 | public function testArrayAccessFailureWithoutOffset($item): void |
||
| 580 | { |
||
| 581 | $this->expectException(\AssertionError::class); |
||
| 582 | |||
| 583 | $sut = $this->createCollectionStub(); |
||
| 584 | |||
| 585 | $sut[] = $item; |
||
| 586 | } |
||
| 587 | |||
| 588 | /** |
||
| 589 | * @dataProvider offsetSetFailureProvider |
||
| 590 | */ |
||
| 591 | public function testArrayAccessFailureWithExplicitOffset($item): void |
||
| 592 | { |
||
| 593 | $this->expectException(\AssertionError::class); |
||
| 594 | |||
| 595 | $sut = $this->createCollectionStub(); |
||
| 596 | |||
| 597 | $sut[] = $item; |
||
| 598 | } |
||
| 599 | |||
| 600 | public function testArrayAccessUnset(): void |
||
| 601 | { |
||
| 602 | $node1 = new Node('1'); |
||
| 603 | |||
| 604 | $sut = $this->createCollectionStub(); |
||
| 605 | |||
| 606 | $sut[] = $node1; |
||
| 607 | |||
| 608 | $this->assertTrue($sut->offsetExists(0)); |
||
| 609 | |||
| 610 | unset($sut[0]); |
||
| 611 | |||
| 612 | $this->assertfalse($sut->offsetExists(0)); |
||
| 613 | } |
||
| 614 | |||
| 615 | public function testSetContentNull(): void |
||
| 616 | { |
||
| 617 | $expectedNodes = []; |
||
| 618 | |||
| 619 | $sut = $this->createCollectionStub(); |
||
| 620 | |||
| 621 | $sut->setContent(null); |
||
| 622 | |||
| 623 | $actualResult = $sut->getNodes(); |
||
| 624 | |||
| 625 | $this->assertEquals($expectedNodes, $actualResult); |
||
| 626 | } |
||
| 627 | |||
| 628 | public function testSetContentCollection(): void |
||
| 649 | } |
||
| 650 | |||
| 651 | public function testIterator(): void |
||
| 652 | { |
||
| 653 | $node1 = new Node('1'); |
||
| 654 | $node2 = new Node('2'); |
||
| 655 | |||
| 656 | $expectedKeys = [0, 1, 2, 3, 4]; |
||
| 657 | $expectedNodes = [$node1, $node2, $node1, $node1, $node1]; |
||
| 658 | |||
| 659 | $sut = $this->createCollectionStub(); |
||
| 660 | |||
| 661 | $sut[] = $node1; |
||
| 662 | $sut[] = $node1; |
||
| 663 | $sut[1] = $node2; |
||
| 664 | $sut[2] = $node1; |
||
| 665 | $sut[3] = $node1; |
||
| 666 | $sut[] = $node1; |
||
| 667 | |||
| 668 | $pos = 0; |
||
| 669 | foreach ($sut as $key => $node) { |
||
| 670 | $this->assertSame($expectedKeys[$pos], $key); |
||
| 671 | $this->assertSame($expectedNodes[$pos], $node); |
||
| 672 | $pos++; |
||
| 673 | } |
||
| 674 | } |
||
| 675 | |||
| 676 | public function testGetRawContentReturnsNonTranslatedContent(): void |
||
| 677 | { |
||
| 678 | $this->assertTrue(true, 'No need to test getRawContent'); |
||
| 679 | } |
||
| 680 | |||
| 681 | /** |
||
| 682 | * @return array |
||
| 683 | */ |
||
| 684 | public function replaceProvider(): array |
||
| 685 | { |
||
| 686 | $needle = new Node('1'); |
||
| 687 | $node2 = new Node('2'); |
||
| 688 | $node3 = new Node('3'); |
||
| 689 | |||
| 690 | return [ |
||
| 691 | 'empty-content' => [ |
||
| 692 | [], |
||
| 693 | $needle, |
||
| 694 | [$node2], |
||
| 695 | [], |
||
| 696 | ], |
||
| 697 | 'only-non-matching-content' => [ |
||
| 698 | [$node2, $node3], |
||
| 699 | $needle, |
||
| 700 | [$needle, $node3], |
||
| 701 | [$node2, $node3], |
||
| 702 | ], |
||
| 703 | 'only-non-matching-content-deep' => [ |
||
| 704 | [$node2, $node3, new CollectionStub([$node2, $node3])], |
||
| 705 | $needle, |
||
| 706 | [$needle, $node3], |
||
| 707 | [$node2, $node3, new CollectionStub([$node2, $node3])], |
||
| 708 | ], |
||
| 709 | 'only-matching-content' => [ |
||
| 710 | [$needle], |
||
| 711 | $needle, |
||
| 712 | [$node2, $node3], |
||
| 713 | [$node2, $node3], |
||
| 714 | ], |
||
| 715 | 'non-first-matching-content' => [ |
||
| 716 | [$node2, $needle], |
||
| 717 | $needle, |
||
| 718 | [$node3, $node3], |
||
| 719 | [$node2, $node3, $node3], |
||
| 720 | ], |
||
| 721 | 'non-last-matching-content' => [ |
||
| 722 | [$needle, $node2], |
||
| 723 | $needle, |
||
| 724 | [$node3, $node3], |
||
| 725 | [$node3, $node3, $node2], |
||
| 726 | ], |
||
| 727 | 'deep-first-matching-content' => [ |
||
| 728 | [$node2, new CollectionStub([$node2, $needle])], |
||
| 729 | $needle, |
||
| 730 | [$node3, $node3], |
||
| 731 | [$node2, new CollectionStub([$node2, $node3, $node3])], |
||
| 732 | ], |
||
| 733 | ]; |
||
| 734 | } |
||
| 735 | |||
| 736 | /** |
||
| 737 | * @dataProvider replaceProvider |
||
| 738 | * |
||
| 739 | * @param INode[] $content |
||
| 740 | * @param INode $nodeToFind |
||
| 741 | * @param INode[] $nodesToInsert |
||
| 742 | * @param INode[] $expectedNodes |
||
| 743 | */ |
||
| 744 | public function testReplace( |
||
| 755 | } |
||
| 756 | |||
| 757 | /** |
||
| 758 | * @return array |
||
| 759 | */ |
||
| 760 | public function hasIntentProvider(): array |
||
| 761 | { |
||
| 762 | return [ |
||
| 763 | [[], 'foo', false], |
||
| 764 | [['foo'], 'foo', true], |
||
| 765 | [['bar'], 'foo', false], |
||
| 766 | [['foo', 'bar', 'baz'], 'bar', true], |
||
| 767 | ]; |
||
| 768 | } |
||
| 769 | |||
| 770 | /** |
||
| 771 | * @dataProvider hasIntentProvider |
||
| 772 | * |
||
| 773 | * @param array $intents |
||
| 774 | * @param string $intentToCheck |
||
| 775 | * @param bool $expectedResult |
||
| 776 | */ |
||
| 777 | public function testHasIntentChecksIfAGivenIntentHasBeenSet( |
||
| 778 | array $intents, |
||
| 779 | string $intentToCheck, |
||
| 780 | bool $expectedResult |
||
| 781 | ): void { |
||
| 782 | $sut = $this->createCollectionStub(); |
||
| 783 | |||
| 784 | $sut->setIntent(...$intents); |
||
| 785 | |||
| 786 | $actualResult = $sut->hasIntent($intentToCheck); |
||
| 787 | |||
| 788 | $this->assertSame($expectedResult, $actualResult); |
||
| 789 | } |
||
| 790 | |||
| 791 | /** |
||
| 792 | * @param INode[]|INode|null $content |
||
| 793 | * |
||
| 794 | * @return CollectionStub |
||
| 795 | */ |
||
| 796 | private function createCollectionStub($content = null): CollectionStub |
||
| 799 | } |
||
| 800 | } |
||
| 801 |