PathTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace Path\Tests;
  3. use Path\Path;
  4. use PHPUnit\Framework\TestCase;
  5. class PathTest extends TestCase
  6. {
  7. const TEMP_TEST_DIR = __DIR__ . "/temp";
  8. protected Path $pathClass;
  9. public function setUp(): void
  10. {
  11. mkdir(self::TEMP_TEST_DIR);
  12. }
  13. private function rmDirs(string $dir): void {
  14. // Remove and replace by a proper tempdir method
  15. foreach(scandir($dir) as $file) {
  16. if ('.' === $file || '..' === $file) continue;
  17. if (is_dir($dir . DIRECTORY_SEPARATOR . $file)) $this->rmDirs($dir . DIRECTORY_SEPARATOR . $file);
  18. else unlink($dir . DIRECTORY_SEPARATOR . $file);
  19. }
  20. rmdir($dir);
  21. }
  22. public function tearDown(): void
  23. {
  24. $this->rmDirs(self::TEMP_TEST_DIR);
  25. chdir(__DIR__);
  26. }
  27. public function testToString(): void
  28. {
  29. $path = new Path('/foo/bar');
  30. $this->assertEquals('/foo/bar', $path->__toString());
  31. }
  32. /**
  33. * Test 'join' method.
  34. */
  35. public function testJoin(): void
  36. {
  37. // One part
  38. $this->assertEquals(
  39. '/home/user',
  40. Path::join('/home', 'user')
  41. );
  42. // Multiple parts
  43. $this->assertEquals(
  44. '/home/user/documents',
  45. Path::join('/home', 'user', 'documents')
  46. );
  47. // Absolute path passed in $parts
  48. $this->assertEquals(
  49. '/user/documents',
  50. Path::join('home', '/user', 'documents')
  51. );
  52. }
  53. /**
  54. * Test `eq` method with equal paths.
  55. *
  56. * Check that the method returns the correct result when the paths are equal.
  57. */
  58. public function testEqWithEqualPaths(): void
  59. {
  60. $path = new Path('/foo/bar');
  61. $this->assertTrue($path->eq('/foo/bar'));
  62. }
  63. /**
  64. * Test `eq` method with different paths.
  65. *
  66. * Check that the method returns the correct result when the paths are different.
  67. */
  68. public function testEqWithDifferentPaths(): void
  69. {
  70. $path = new Path('/foo/bar');
  71. $this->assertFalse($path->eq('/foo/zzz'));
  72. }
  73. /**
  74. * Test `eq` method with empty path.
  75. *
  76. * Check that the method returns the correct result when the path is empty.
  77. */
  78. public function testEqWithEmptyPath(): void
  79. {
  80. $path = new Path('/foo/bar');
  81. $this->assertFalse($path->eq(''));
  82. }
  83. /**
  84. * Test the append method of the Path class.
  85. *
  86. * @return void
  87. */
  88. public function testAppend(): void
  89. {
  90. $path = new Path('/foo');
  91. $this->assertEquals(
  92. "/foo/bar",
  93. $path->append('bar')
  94. );
  95. // One part
  96. $this->assertTrue(
  97. (new Path('/home'))->append('user')->eq('/home/user')
  98. );
  99. // Multiple parts
  100. $this->assertTrue(
  101. (new Path('/home'))->append('user', 'documents')->eq('/home/user/documents')
  102. );
  103. // Absolute path passed in $parts
  104. $this->assertTrue(
  105. (new Path('/home'))->append('/user', 'documents')->eq('/user/documents')
  106. );
  107. }
  108. /**
  109. * Test the abspath method of the Path class.
  110. *
  111. * @return void
  112. */
  113. public function testAbsPath(): void
  114. {
  115. touch(self::TEMP_TEST_DIR . "/foo");
  116. chdir(self::TEMP_TEST_DIR);
  117. $this->assertEquals(
  118. self::TEMP_TEST_DIR . "/foo",
  119. (new Path('foo'))->abspath()
  120. );
  121. }
  122. /**
  123. * Test the abspath method of the Path class with a relative path.
  124. *
  125. * @return void
  126. */
  127. public function testAbsPathWithRelative(): void
  128. {
  129. mkdir(self::TEMP_TEST_DIR . "/foo");
  130. touch(self::TEMP_TEST_DIR . "/bar");
  131. chdir(self::TEMP_TEST_DIR . "/foo");
  132. $this->assertEquals(
  133. self::TEMP_TEST_DIR . "/bar",
  134. (new Path('../bar'))->abspath()
  135. );
  136. }
  137. }