1
0

PathTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. namespace fphammerle\helpers\tests;
  3. use \fphammerle\helpers\Path;
  4. class PathTest extends \PHPUnit_Framework_TestCase
  5. {
  6. /**
  7. * @dataProvider pathProvider
  8. */
  9. public function testConstruct($path, $expected_dir_path, $expected_filename, $expected_extension)
  10. {
  11. $p = new Path($path);
  12. if(isset($expected_dir_path)) {
  13. $this->assertSame($expected_dir_path, $p->dirPath->path);
  14. } else {
  15. $this->assertNull($p->dirPath);
  16. }
  17. $this->assertSame($expected_filename, $p->filename);
  18. $this->assertSame($expected_extension, $p->extension);
  19. }
  20. /**
  21. * @dataProvider pathProvider
  22. */
  23. public function testToString($expected_path, $dir_path, $filename, $extension)
  24. {
  25. $p = new Path;
  26. $p->dirPath = $dir_path;
  27. $p->filename = $filename;
  28. $p->extension = $extension;
  29. $this->assertSame($expected_path ?: '', (string)$p);
  30. }
  31. public function getBasenameProvider()
  32. {
  33. return [
  34. ['.', null, '.'],
  35. ['..', null, '..'],
  36. ['file', 'php', 'file.php'],
  37. ['file', null, 'file'],
  38. [null, 'php', '.php'],
  39. [null, null, null],
  40. ];
  41. }
  42. /**
  43. * @dataProvider getBasenameProvider
  44. */
  45. public function testGetBasename($filename, $extension, $expected_basename)
  46. {
  47. $p = new Path;
  48. $p->filename = $filename;
  49. $p->extension = $extension;
  50. $this->assertSame($expected_basename, $p->basename);
  51. }
  52. public function setBasenameProvider()
  53. {
  54. return [
  55. ['.', '.', null],
  56. ['..', '..', null],
  57. ['..txt', '.', 'txt'],
  58. ['.php', null, 'php'],
  59. ['file', 'file', null],
  60. ['file.php', 'file', 'php'],
  61. [null, null, null],
  62. ];
  63. }
  64. /**
  65. * @dataProvider setBasenameProvider
  66. */
  67. public function testSetBasename($basename, $expected_filename, $expected_extension)
  68. {
  69. $p = new Path;
  70. $p->basename = $basename;
  71. $this->assertSame($expected_filename, $p->filename);
  72. $this->assertSame($expected_extension, $p->extension);
  73. }
  74. public function dirPathProvider()
  75. {
  76. return [
  77. ['/', '/'],
  78. ['/dir', '/dir'],
  79. ['/dir/subdir', '/dir/subdir'],
  80. ['dir', 'dir'],
  81. ['dir/subdir', 'dir/subdir'],
  82. [null, null],
  83. ];
  84. }
  85. /**
  86. * @dataProvider dirPathProvider
  87. */
  88. public function testDirPath($input_dir_path, $expected_dir_path)
  89. {
  90. $p = new Path;
  91. $p->dirPath = $input_dir_path;
  92. if(isset($input_dir_path)) {
  93. $this->assertSame($expected_dir_path, $p->dirPath->path);
  94. } else {
  95. $this->assertNull($p->dirPath);
  96. }
  97. }
  98. public function extensionProvider()
  99. {
  100. return [
  101. ['.', '.'],
  102. ['.php', '.php'],
  103. ['file', 'file'],
  104. ['file.php', 'file.php'],
  105. [123, '123'],
  106. [null, null],
  107. ];
  108. }
  109. /**
  110. * @dataProvider extensionProvider
  111. */
  112. public function testExtension($input_extension, $expected_extension)
  113. {
  114. $p = new Path;
  115. $p->extension = $input_extension;
  116. $this->assertSame($expected_extension, $p->extension);
  117. }
  118. public function filenameProvider()
  119. {
  120. return [
  121. ['.', '.'],
  122. ['.php', '.php'],
  123. ['file', 'file'],
  124. ['file.php', 'file.php'],
  125. [123, '123'],
  126. [null, null],
  127. ];
  128. }
  129. /**
  130. * @dataProvider filenameProvider
  131. */
  132. public function testFilename($input_filename, $expected_filename)
  133. {
  134. $p = new Path;
  135. $p->filename = $input_filename;
  136. $this->assertSame($expected_filename, $p->filename);
  137. }
  138. public function pathProvider()
  139. {
  140. return [
  141. ['.', null, '.', null],
  142. ['..', null, '..', null],
  143. ['../../file.php', '../..', 'file', 'php'],
  144. ['../file.php', '..', 'file', 'php'],
  145. ['./file.php', '.', 'file', 'php'],
  146. ['dir/../file.php', 'dir/..', 'file', 'php'],
  147. ['dir/file.php', 'dir', 'file', 'php'],
  148. ['dir/subdir/file.php', 'dir/subdir', 'file', 'php'],
  149. ['/file.php', '/', 'file', 'php'],
  150. ['/dir/file.php', '/dir', 'file', 'php'],
  151. ['/', '/', null, null],
  152. ['file.php', null, 'file', 'php'],
  153. [null, null, null, null],
  154. ];
  155. }
  156. /**
  157. * @dataProvider pathProvider
  158. */
  159. public function testGetPath($expected_path, $dir_path, $filename, $extension)
  160. {
  161. $p = new Path;
  162. $p->dirPath = $dir_path;
  163. $p->filename = $filename;
  164. $p->extension = $extension;
  165. $this->assertSame($expected_path, $p->path);
  166. }
  167. /**
  168. * @dataProvider pathProvider
  169. */
  170. public function testSetPath($path, $expected_dir_path, $expected_filename, $expected_extension)
  171. {
  172. $p = new Path;
  173. $p->path = $path;
  174. if(isset($expected_dir_path)) {
  175. $this->assertSame($expected_dir_path, $p->dirPath->path);
  176. } else {
  177. $this->assertNull($p->dirPath);
  178. }
  179. $this->assertSame($expected_filename, $p->filename);
  180. $this->assertSame($expected_extension, $p->extension);
  181. }
  182. /**
  183. * @dataProvider pathProvider
  184. */
  185. public function testSetEqualsGet($path)
  186. {
  187. $p = new Path;
  188. $p->path = $path;
  189. $this->assertSame($path, $p->path);
  190. }
  191. public function isRootProvider()
  192. {
  193. return [
  194. ['.', false],
  195. ['..', false],
  196. ['../../file.php', false],
  197. ['../file.php', false],
  198. ['./file.php', false],
  199. ['/', true],
  200. ['//', false],
  201. ['/dir/file.php', false],
  202. ['/file.php', false],
  203. ['dir/../file.php', false],
  204. ['dir/file.php', false],
  205. ['dir/subdir/file.php', false],
  206. ['file.php', false],
  207. [null, false],
  208. ];
  209. }
  210. /**
  211. * @dataProvider isRootProvider
  212. */
  213. public function testIsRoot($path, $expected_result)
  214. {
  215. $p = new Path;
  216. $p->path = $path;
  217. $this->assertSame($expected_result, $p->isRoot());
  218. }
  219. public function reverseNameProvider()
  220. {
  221. return [
  222. ['.', '.'],
  223. ['..', '..'],
  224. ['../../file.php', '../../elif.php'],
  225. ['dir/subdir/file.html', 'dir/subdir/elif.lmth'],
  226. ['file.png', 'elif.gnp'],
  227. [null, null],
  228. ];
  229. }
  230. /**
  231. * @dataProvider reverseNameProvider
  232. */
  233. public function testReverseName($path, $expected_result)
  234. {
  235. $p = new Path($path);
  236. if(isset($p->filename)) {
  237. $p->filename = strrev($p->filename);
  238. }
  239. if(isset($p->extension)) {
  240. $p->extension = strrev($p->extension);
  241. }
  242. $this->assertSame($expected_result, $p->path);
  243. }
  244. }