1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
|
<?php
/**
* League.Uri (https://uri.thephpleague.com)
*
* (c) Ignace Nyamagana Butera <nyamsprod@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\Uri\Components;
use ArrayIterator;
use League\Uri\Contracts\UriInterface;
use League\Uri\Exceptions\OffsetOutOfBounds;
use League\Uri\Exceptions\SyntaxError;
use League\Uri\Http;
use League\Uri\Uri;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\UriInterface as Psr7UriInterface;
use function iterator_to_array;
#[CoversClass(HierarchicalPath::class)]
#[Group('path')]
#[Group('hierarchicalpath')]
final class HierarchicalPathTest extends TestCase
{
public function testIterator(): void
{
$path = HierarchicalPath::new('/5.0/components/path');
self::assertEquals(['5.0', 'components', 'path'], iterator_to_array($path));
}
#[DataProvider('validPathProvider')]
public function testValidPath(string $raw, string $expected): void
{
self::assertSame($expected, (string) HierarchicalPath::new($raw));
}
public static function validPathProvider(): array
{
$unreserved = 'a-zA-Z0-9.-_~!$&\'()*+,;=:@';
return [
'empty' => ['', ''],
'root path' => ['/', '/'],
'absolute path' => ['/path/to/my/file.csv', '/path/to/my/file.csv'],
'relative path' => ['you', 'you'],
'relative path with ending slash' => ['foo/bar/', 'foo/bar/'],
'path with a space' => ['/shop/rev iew/', '/shop/rev%20iew/'],
'path with an encoded char in lowercase' => ['/master/toto/a%c2%b1b', '/master/toto/a%C2%B1b'],
'path with an encoded char in uppercase' => ['/master/toto/%7Eetc', '/master/toto/%7Eetc'],
'path with character to encode' => ['/foo^bar', '/foo%5Ebar'],
'path with a reserved char encoded' => ['%2Ffoo^bar', '%2Ffoo%5Ebar'],
'Percent encode spaces' => ['/pa th', '/pa%20th'],
'Percent encode multibyte' => ['/€', '/%E2%82%AC'],
"Don't encode something that's already encoded" => ['/pa%20th', '/pa%20th'],
'Percent encode invalid percent encodings' => ['/pa%2-th', '/pa%252-th'],
"Don't encode path segments" => ['/pa/th//two', '/pa/th//two'],
"Don't encode unreserved chars or sub-delimiters" => ["/$unreserved", "/$unreserved"],
'Encoded unreserved chars are not decoded' => ['/p%61th', '/p%61th'],
];
}
#[DataProvider('isAbsoluteProvider')]
public function testIsAbsolute(string $raw, bool $expected): void
{
$path = HierarchicalPath::new($raw);
self::assertSame($expected, $path->isAbsolute());
}
public static function isAbsoluteProvider(): array
{
return [
['', false],
['/', true],
['../..', false],
['/a/b/c', true],
];
}
#[DataProvider('getProvider')]
public function testget(string $raw, int $key, ?string $expected): void
{
self::assertSame($expected, HierarchicalPath::new($raw)->get($key));
}
public static function getProvider(): array
{
return [
['/shop/rev iew/', 1, 'rev iew'],
['/shop/rev%20iew/', 1, 'rev iew'],
['/shop/rev iew/', -2, 'rev iew'],
['/shop/rev%20iew/', -2, 'rev iew'],
['/shop/rev%20iew/', 28, null],
];
}
/**
* Test Removing Dot Segment.
*/
#[DataProvider('normalizeProvider')]
public function testWithoutDotSegments(string $path, string $expected): void
{
self::assertSame($expected, (string) HierarchicalPath::new($path)->withoutDotSegments());
}
/**
* Provides different segment to be normalized.
*/
public static function normalizeProvider(): array
{
return [
['/a/b/c/./../../g', '/a/g'],
['mid/content=5/../6', 'mid/6'],
['a/b/c', 'a/b/c'],
['a/b/c/.', 'a/b/c/'],
['/a/b/c', '/a/b/c'],
];
}
#[DataProvider('withLeadingSlashProvider')]
public function testWithLeadingSlash(string $path, string $expected): void
{
self::assertSame($expected, (string) HierarchicalPath::new($path)->withLeadingSlash());
}
public static function withLeadingSlashProvider(): array
{
return [
'relative path without leading slash' => ['toto', '/toto'],
'absolute path' => ['/toto', '/toto'],
'root path' => ['/', '/'],
'empty path' => ['', '/'],
'relative path with ending slash' => ['toto/', '/toto/'],
'absolute path with ending slash' => ['/toto/', '/toto/'],
];
}
#[DataProvider('withoutLeadingSlashProvider')]
public function testWithoutLeadingSlash(string $path, string $expected): void
{
self::assertSame($expected, (string) HierarchicalPath::new($path)->withoutLeadingSlash());
}
public static function withoutLeadingSlashProvider(): array
{
return [
'relative path without ending slash' => ['toto', 'toto'],
'absolute path without ending slash' => ['/toto', 'toto'],
'root path' => ['/', ''],
'empty path' => ['', ''],
'absolute path with ending slash' => ['/toto/', 'toto/'],
];
}
#[DataProvider('createFromRelativeSegmentsValid')]
public function testCreateRelativeFromSegments(iterable $input, string $expected): void
{
self::assertSame($expected, (string) HierarchicalPath::fromRelative(...$input));
}
public static function createFromRelativeSegmentsValid(): array
{
return [
'array (1)' => [['www', 'example', 'com'], 'www/example/com'],
'ending delimiter' => [['foo/bar/baz', ''], 'foo/bar/baz/'],
'use reserved characters ?' => [['all', 'i%3fs', 'good'], 'all/i%3Fs/good'],
'enforce path status (1)' => [['', 'toto', 'yeah', ''], 'toto/yeah/'],
'enforce path status (3)' => [['', '', 'toto', 'yeah', ''], 'toto/yeah/'],
];
}
#[DataProvider('createFromAbsoluteSegmentsValid')]
public function testCreateAbsoluteFromSegments(iterable $input, string $expected): void
{
self::assertSame($expected, (string) HierarchicalPath::fromAbsolute(...$input));
}
public static function createFromAbsoluteSegmentsValid(): array
{
return [
'array (2)' => [['www', 'example', 'com'], '/www/example/com'],
'iterator' => [new ArrayIterator(['www', 'example', 'com']), '/www/example/com'],
'Path object' => [HierarchicalPath::new('/foo/bar/baz'), '/foo/bar/baz'],
'arbitrary cut 1' => [['foo', 'bar', 'baz'], '/foo/bar/baz'],
'arbitrary cut 2' => [['foo/bar', 'baz'], '/foo/bar/baz'],
'arbitrary cut 3' => [['foo/bar/baz'], '/foo/bar/baz'],
'use reserved characters #' => [['all', 'i%23s', 'good'], '/all/i%23s/good'],
'enforce path status (2)' => [['', 'toto', 'yeah', ''], '/toto/yeah/'],
'enforce path status (4)' => [['', '', 'toto', 'yeah', ''], '//toto/yeah/'],
];
}
#[DataProvider('prependData')]
public function testPrepend(string $source, string $prepend, string $res): void
{
self::assertSame($res, (string) HierarchicalPath::new($source)->prepend($prepend));
}
public static function prependData(): array
{
return [
['/test/query.php', '/master', '/master/test/query.php'],
['/test/query.php', '/master/', '/master/test/query.php'],
['/test/query.php', '', '/test/query.php'],
['/test/query.php', '/', '/test/query.php'],
['test', '/', '/test'],
['/', 'test', 'test/'],
];
}
#[DataProvider('appendData')]
public function testAppend(string $source, string $append, string $res): void
{
self::assertSame($res, (string) HierarchicalPath::new($source)->append($append));
}
public static function appendData(): array
{
return [
['/test/', '/master/', '/test/master/'],
['/test/', '/master', '/test/master'],
['/test', 'master', '/test/master'],
['test', 'master', 'test/master'],
['test', '/master', 'test/master'],
['test', 'master/', 'test/master/'],
['test', '/', 'test/'],
['/', 'test', '/test'],
];
}
public function testWithSegmentUseAppend(): void
{
$path = HierarchicalPath::new('foo/bar');
self::assertEquals($path->withSegment(2, 'baz'), $path->append('baz'));
}
#[DataProvider('withoutEmptySegmentsProvider')]
public function testWithoutEmptySegments(string $path, string $expected): void
{
self::assertSame($expected, (string) HierarchicalPath::new($path)->withoutEmptySegments());
}
public static function withoutEmptySegmentsProvider(): array
{
return [
['/a/b/c', '/a/b/c'],
['//a//b//c', '/a/b/c'],
['a//b/c//', 'a/b/c/'],
['/a/b/c//', '/a/b/c/'],
];
}
#[DataProvider('replaceValid')]
public function testReplace(string $raw, string $input, int $offset, string $expected): void
{
self::assertSame($expected, (string) HierarchicalPath::new($raw)->withSegment($offset, $input));
}
public static function replaceValid(): array
{
return [
['/path/to/the/sky', 'shop', 0, '/shop/to/the/sky'],
['', 'shoki', 0, 'shoki'],
['', 'shoki/', 0, 'shoki/'],
['', '/shoki/', 0, '/shoki/'],
['/path/to/paradise', 'path', 0, '/path/to/paradise'],
['/path/to/paradise', 'path', -1, '/path/to/path'],
['/path/to/paradise', 'path', -4, 'path/path/to/paradise'],
['/path/to/paradise', 'path', -3, '/path/to/paradise'],
['/foo', 'bar', -1, '/bar'],
['foo', 'bar', -1, 'bar'],
];
}
public function testWithSegmentThrowsException(): void
{
$this->expectException(OffsetOutOfBounds::class);
HierarchicalPath::new('/test/')->withSegment(23, 'bar');
}
/**
* Test AbstractSegment::without.
*
*
* @param int[] $without
*/
#[DataProvider('withoutProvider')]
public function testWithout(string $origin, array $without, string $result): void
{
self::assertSame($result, (string) HierarchicalPath::new($origin)->withoutSegment(...$without));
}
public static function withoutProvider(): array
{
return [
['/master/test/query.php', [2], '/master/test'],
['/master/test/query.php', [-1], '/master/test'],
['/toto/le/heros/masson', [0], '/le/heros/masson'],
['/toto', [-1], '/'],
['toto/le/heros/masson', [2, 3], 'toto/le'],
];
}
public function testWithoutSegmentThrowsException(): void
{
$this->expectException(OffsetOutOfBounds::class);
HierarchicalPath::new('/test/')->withoutSegment(23);
}
public function testWithoutSegmentVariadicArgument(): void
{
$path = HierarchicalPath::new('www/example/com');
self::assertSame($path, $path->withoutSegment());
}
public function testKeys(): void
{
$path = HierarchicalPath::new('/bar/3/troll/3');
self::assertCount(0, $path->keys('foo'));
self::assertSame([0], $path->keys('bar'));
self::assertCount(2, $path->keys('3'));
self::assertSame([1, 3], $path->keys('3'));
self::assertSame([0, 1, 2, 3], $path->keys());
}
public function testSegments(): void
{
$path = HierarchicalPath::new('/bar/3/troll/3');
self::assertSame(['bar', '3', 'troll', '3'], iterator_to_array($path));
self::assertSame([''], iterator_to_array(HierarchicalPath::new()));
self::assertSame([''], iterator_to_array(HierarchicalPath::new('/')));
}
#[DataProvider('arrayProvider')]
public function testCountable(string $input, array $gets, int $nbSegment): void
{
$path = HierarchicalPath::new($input);
self::assertCount($nbSegment, $path);
}
public static function arrayProvider(): array
{
return [
['/toto/le/heros/masson', ['toto', 'le', 'heros', 'masson'], 4],
['toto/le/heros/masson', ['toto', 'le', 'heros', 'masson'], 4],
['/toto/le/heros/masson/', ['toto', 'le', 'heros', 'masson', ''], 5],
];
}
#[DataProvider('provideBasenamePath')]
public function testGetBasemane(string $path, string $expected): void
{
self::assertSame($expected, HierarchicalPath::new($path)->getBasename());
}
public static function provideBasenamePath(): iterable
{
yield 'simple basename' => [
'path' => '/path/to/my/file.txt',
'expected' => 'file.txt',
];
yield 'path with trailing slash' => [
'path' => '/path/to/my/',
'expected' => '',
];
yield 'basename with path parameter' => [
'path' => '/path/to/my/file.txt;param1;param2',
'expected' => 'file.txt',
];
}
#[DataProvider('dirnameProvider')]
public function testGetDirmane(string $path, string $dirname): void
{
self::assertSame($dirname, HierarchicalPath::new($path)->getDirname());
}
public static function dirnameProvider(): array
{
return [
['/path/to/my/file.txt', '/path/to/my'],
['/path/to/my/file/', '/path/to/my'],
['/path/to/my\\file/', '/path/to'],
['.', '.'],
['/path/to/my//file/', '/path/to/my'],
['', ''],
['/', '/'],
['/path/to/my/../file.txt', '/path/to/my/..'],
];
}
#[DataProvider('extensionProvider')]
public function testGetExtension(string $raw, string $parsed): void
{
self::assertSame($parsed, HierarchicalPath::new($raw)->getExtension());
}
public static function extensionProvider(): array
{
return [
['/path/to/my/', ''],
['/path/to/my/file', ''],
['/path/to/my/file.txt', 'txt'],
['/path/to/my/file.csv.txt', 'txt'],
];
}
#[DataProvider('withExtensionProvider')]
public function testWithExtension(string $raw, string $raw_ext, string $new_path, string $parsed_ext): void
{
$newPath = HierarchicalPath::new($raw)->withExtension($raw_ext);
self::assertSame($new_path, (string) $newPath);
self::assertSame($parsed_ext, $newPath->getExtension());
}
public static function withExtensionProvider(): array
{
return [
['/path/to/my/file.txt', 'csv', '/path/to/my/file.csv', 'csv'],
['/path/to/my/file.txt;foo=bar', 'csv', '/path/to/my/file.csv;foo=bar', 'csv'],
['/path/to/my/file', 'csv', '/path/to/my/file.csv', 'csv'],
['/path/to/my/file;foo', 'csv', '/path/to/my/file.csv;foo', 'csv'],
['/path/to/my/file.csv', '', '/path/to/my/file', ''],
['/path/to/my/file.csv;foo=bar,baz', '', '/path/to/my/file;foo=bar,baz', ''],
['/path/to/my/file.tar.gz', 'bz2', '/path/to/my/file.tar.bz2', 'bz2'],
['/path/to/my/file.tar.gz;foo', 'bz2', '/path/to/my/file.tar.bz2;foo', 'bz2'],
['', 'csv', '', ''],
[';foo=bar', 'csv', ';foo=bar', ''],
['toto.', 'csv', 'toto.csv', 'csv'],
['toto.;foo', 'csv', 'toto.csv;foo', 'csv'],
['toto.csv;foo', 'csv', 'toto.csv;foo', 'csv'],
];
}
#[DataProvider('invalidExtension')]
public function testWithExtensionWithInvalidExtension(string $extension): void
{
$this->expectException(SyntaxError::class);
HierarchicalPath::new()->withExtension($extension);
}
public static function invalidExtension(): array
{
return [
'invalid format' => ['t/xt'],
'starting with a dot' => ['.csv'],
'invalid chars' => ["\0"],
];
}
#[DataProvider('withExtensionProvider2')]
public function testWithExtensionPreserveTypeCode(string $uri, string $extension, string $expected): void
{
self::assertSame(
$expected,
(string) HierarchicalPath::new($uri)->withExtension($extension)
);
}
public static function withExtensionProvider2(): array
{
return [
'no typecode' => ['/foo/bar.csv', 'txt', '/foo/bar.txt'],
'with typecode' => ['/foo/bar.csv;type=a', 'txt', '/foo/bar.txt;type=a'],
'remove extension with no typecode' => ['/foo/bar.csv', '', '/foo/bar'],
'remove extension with typecode' => ['/foo/bar.csv;type=a', '', '/foo/bar;type=a'],
];
}
#[DataProvider('getExtensionProvider')]
public function testGetExtensionPreserveTypeCode(string $uri, string $extension): void
{
self::assertSame($extension, HierarchicalPath::new($uri)->getExtension());
}
public static function getExtensionProvider(): array
{
return [
'no typecode' => ['/foo/bar.csv', 'csv'],
'with typecode' => ['/foo/bar.csv;type=a', 'csv'],
];
}
public static function geValueProvider(): array
{
return [
['', ''],
['0', '0'],
['azAZ0-9/%3F-._~!$&\'()*+,;=:@%^/[]{}\"<>\\', 'azAZ0-9/?-._~!$&\'()*+,;=:@%^/[]{}\"<>\\'],
['€', '€'],
['%E2%82%AC', '€'],
['frag ment', 'frag ment'],
['frag%20ment', 'frag ment'],
['frag%2-ment', 'frag%2-ment'],
['fr%61gment', 'fr%61gment'],
];
}
#[DataProvider('getDirnameProvider')]
public function testWithDirname(string $path, string $dirname, string $expected): void
{
self::assertSame($expected, (string) HierarchicalPath::new($path)->withDirname($dirname));
}
public static function getDirnameProvider(): array
{
return [
'path with basename and absolute dirname' => [
'path' => '/foo/bar/baz',
'dirname' => '/bar',
'expected' => '/bar/baz',
],
'path with basename and rootless dirname' => [
'path' => '/foo/bar/baz',
'dirname' => 'bar',
'expected' => 'bar/baz',
],
'path with basename and empty dirname' => [
'path' => '/foo/bar/baz',
'dirname' => '',
'expected' => '/baz',
],
'empty path and empty dirname' => [
'path' => '',
'dirname' => '',
'expected' => '',
],
'empty path and non empty dirname' => [
'path' => '',
'dirname' => '/foo/bar',
'expected' => '/foo/bar/',
],
'dirname with trailing slash' => [
'path' => '',
'dirname' => 'bar/baz/',
'expected' => 'bar/baz/',
],
];
}
#[DataProvider('getBasenameProvider')]
public function testWithBasename(string $path, string $basename, string $expected): void
{
self::assertSame($expected, (string) HierarchicalPath::new($path)->withBasename($basename));
}
public static function getBasenameProvider(): array
{
return [
[
'path' => '/foo/bar/baz',
'basename' => 'bar',
'expected' => '/foo/bar/bar',
],
[
'path' => 'foo/bar/baz',
'basename' => 'bar',
'expected' => 'foo/bar/bar',
],
[
'path' => '/foo/bar/',
'basename' => '',
'expected' => '/foo/bar/',
],
[
'path' => '',
'basename' => '',
'expected' => '',
],
[
'path' => '',
'basename' => 'bar',
'expected' => 'bar',
],
];
}
#[DataProvider('basenameInvalidProvider')]
public function testWithBasenameThrowException(string $path): void
{
$this->expectException(SyntaxError::class);
HierarchicalPath::new('foo/bar')->withBasename($path);
}
public static function basenameInvalidProvider(): array
{
return [
['foo/bar'],
];
}
#[DataProvider('getURIProvider')]
public function testCreateFromUri(Psr7UriInterface|UriInterface $uri, ?string $expected): void
{
$path = HierarchicalPath::fromUri($uri);
self::assertSame($expected, $path->value());
}
public static function getURIProvider(): iterable
{
return [
'PSR-7 URI object' => [
'uri' => Http::new('http://example.com/path'),
'expected' => '/path',
],
'PSR-7 URI object with no path' => [
'uri' => Http::new('toto://example.com'),
'expected' => '',
],
'PSR-7 URI object with no authority' => [
'uri' => Http::new('path/to/sky?toto'),
'expected' => 'path/to/sky',
],
'League URI object' => [
'uri' => Uri::new('http://example.com/path'),
'expected' => '/path',
],
'League URI object with no path' => [
'uri' => Uri::new('toto://example.com'),
'expected' => '',
],
'League URI object with no authority' => [
'uri' => Uri::new('path/to/sky?toto'),
'expected' => 'path/to/sky',
],
];
}
public function testCreateFromUriWithPSR7Implementation(): void
{
$uri = Uri::new('http://example.com')
->withPath('/path');
self::assertSame('/path', $uri->getPath());
self::assertSame('/path', HierarchicalPath::fromUri($uri)->toString());
}
#[DataProvider('trailingSlashProvider')]
public function testHasTrailingSlash(string $path, bool $expected): void
{
self::assertSame($expected, HierarchicalPath::new($path)->hasTrailingSlash());
}
public static function trailingSlashProvider(): array
{
return [
['/path/to/my/', true],
['/path/to/my', false],
['path/to/my', false],
['path/to/my/', true],
['/', true],
['', false],
];
}
#[DataProvider('withTrailingSlashProvider')]
public function testWithTrailingSlash(string $path, string $expected): void
{
self::assertSame($expected, (string) HierarchicalPath::new($path)->withTrailingSlash());
}
public static function withTrailingSlashProvider(): array
{
return [
'relative path without ending slash' => ['toto', 'toto/'],
'absolute path without ending slash' => ['/toto', '/toto/'],
'root path' => ['/', '/'],
'empty path' => ['', '/'],
'relative path with ending slash' => ['toto/', 'toto/'],
'absolute path with ending slash' => ['/toto/', '/toto/'],
];
}
#[DataProvider('withoutTrailingSlashProvider')]
public function testWithoutTrailingSlash(string $path, string $expected): void
{
self::assertSame($expected, (string) HierarchicalPath::new($path)->withoutTrailingSlash());
}
public static function withoutTrailingSlashProvider(): array
{
return [
'relative path without ending slash' => ['toto', 'toto'],
'absolute path without ending slash' => ['/toto', '/toto'],
'root path' => ['/', ''],
'empty path' => ['', ''],
'relative path with ending slash' => ['toto/', 'toto'],
'absolute path with ending slash' => ['/toto/', '/toto'],
];
}
#[DataProvider('validPathEncoding')]
public function testGetUriComponent(string $decoded, string $encoded): void
{
$path = HierarchicalPath::new($decoded);
self::assertSame($decoded, $path->decoded());
self::assertSame($encoded, $path->value());
self::assertSame($encoded, $path->getUriComponent());
}
public static function validPathEncoding(): array
{
return [
[
'toto',
'toto',
],
[
'bar---',
'bar---',
],
[
'',
'',
],
[
'"bad"',
'%22bad%22',
],
[
'<not good>',
'%3Cnot%20good%3E',
],
[
'{broken}',
'%7Bbroken%7D',
],
[
'`oops`',
'%60oops%60',
],
[
'\\slashy',
'%5Cslashy',
],
[
'foo^bar',
'foo%5Ebar',
],
[
'foo^bar/baz',
'foo%5Ebar/baz',
],
[
'foo%2Fbar',
'foo%2Fbar',
],
[
'/v1/people/%7E:(first-name,last-name,email-address,picture-url)',
'/v1/people/%7E:(first-name,last-name,email-address,picture-url)',
],
[
'/v1/people/~:(first-name,last-name,email-address,picture-url)',
'/v1/people/~:(first-name,last-name,email-address,picture-url)',
],
[
'foo%2520bar',
'foo%2520bar',
],
];
}
public function testSlice(): void
{
$domain = HierarchicalPath::new('/ulb/ac/be');
self::assertSame($domain->value(), $domain->slice(-3)->value());
self::assertSame($domain->value(), $domain->slice(0)->value());
self::assertSame('/ac/be', $domain->slice(1)->value());
self::assertSame('/be', $domain->slice(-1)->value());
self::assertSame('/ulb', $domain->slice(-3, 1)->value());
}
public function testSliceThrowsOnOverFlow(): void
{
$this->expectException(OffsetOutOfBounds::class);
HierarchicalPath::new('/ulb/ac/be')->slice(5);
}
}
|