PathTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace Path\Tests;
  3. use Path\Path;
  4. use PHPUnit\Framework\TestCase;
  5. class PathTest extends TestCase
  6. {
  7. protected Path $pathClass;
  8. protected function setUp(): void
  9. {
  10. }
  11. /**
  12. * Test 'join' method.
  13. */
  14. public function testJoin(): void
  15. {
  16. // One part
  17. $this->assertEquals(
  18. '/home/user',
  19. Path::join('/home', 'user')
  20. );
  21. // Multiple parts
  22. $this->assertEquals(
  23. '/home/user/documents',
  24. Path::join('/home', 'user', 'documents')
  25. );
  26. // Absolute path passed in $parts
  27. $this->assertEquals(
  28. '/user/documents',
  29. Path::join('home', '/user', 'documents')
  30. );
  31. }
  32. /**
  33. * Test `eq` method with equal paths.
  34. *
  35. * Check that the method returns the correct result when the paths are equal.
  36. */
  37. public function testEqWithEqualPaths(): void
  38. {
  39. $path = new Path('/foo/bar');
  40. $this->assertTrue($path->eq('/foo/bar'));
  41. }
  42. /**
  43. * Test `eq` method with different paths.
  44. *
  45. * Check that the method returns the correct result when the paths are different.
  46. */
  47. public function testEqWithDifferentPaths(): void
  48. {
  49. $path = new Path('/foo/bar');
  50. $this->assertFalse($path->eq('/foo/zzz'));
  51. }
  52. /**
  53. * Test `eq` method with empty path.
  54. *
  55. * Check that the method returns the correct result when the path is empty.
  56. */
  57. public function testEqWithEmptyPath(): void
  58. {
  59. $path = new Path('/foo/bar');
  60. $this->assertFalse($path->eq(''));
  61. }
  62. public function testAppend(): void
  63. {
  64. $path = new Path('/foo');
  65. $this->assertEquals(
  66. "/foo/bar",
  67. $path->append('bar')
  68. );
  69. // One part
  70. $this->assertTrue(
  71. (new Path('/home'))->append('user')->eq('/home/user')
  72. );
  73. // Multiple parts
  74. $this->assertTrue(
  75. (new Path('/home'))->append('user', 'documents')->eq('/home/user/documents')
  76. );
  77. // Absolute path passed in $parts
  78. $this->assertTrue(
  79. (new Path('/home'))->append('/user', 'documents')->eq('/user/documents')
  80. );
  81. }
  82. }