FilesystemTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. use Grav\Framework\Filesystem\Filesystem;
  3. /**
  4. * Class FilesystemTest
  5. */
  6. class FilesystemTest extends \Codeception\TestCase\Test
  7. {
  8. protected $class;
  9. protected $tests = [
  10. '' => [
  11. 'parent' => '',
  12. 'normalize' => '',
  13. 'dirname' => '',
  14. 'pathinfo' => [
  15. 'basename' => '',
  16. 'filename' => '',
  17. ]
  18. ],
  19. '.' => [
  20. 'parent' => '',
  21. 'normalize' => '',
  22. 'dirname' => '.',
  23. 'pathinfo' => [
  24. 'dirname' => '.',
  25. 'basename' => '.',
  26. 'extension' => '',
  27. 'filename' => '',
  28. ]
  29. ],
  30. './' => [
  31. 'parent' => '',
  32. 'normalize' => '',
  33. 'dirname' => '.',
  34. 'pathinfo' => [
  35. 'dirname' => '.',
  36. 'basename' => '.',
  37. 'extension' => '',
  38. 'filename' => '',
  39. ]
  40. ],
  41. '././.' => [
  42. 'parent' => '',
  43. 'normalize' => '',
  44. 'dirname' => './.',
  45. 'pathinfo' => [
  46. 'dirname' => './.',
  47. 'basename' => '.',
  48. 'extension' => '',
  49. 'filename' => '',
  50. ]
  51. ],
  52. '.file' => [
  53. 'parent' => '.',
  54. 'normalize' => '.file',
  55. 'dirname' => '.',
  56. 'pathinfo' => [
  57. 'dirname' => '.',
  58. 'basename' => '.file',
  59. 'extension' => 'file',
  60. 'filename' => '',
  61. ]
  62. ],
  63. '/' => [
  64. 'parent' => '',
  65. 'normalize' => '/',
  66. 'dirname' => '/',
  67. 'pathinfo' => [
  68. 'dirname' => '/',
  69. 'basename' => '',
  70. 'filename' => '',
  71. ]
  72. ],
  73. '/absolute' => [
  74. 'parent' => '/',
  75. 'normalize' => '/absolute',
  76. 'dirname' => '/',
  77. 'pathinfo' => [
  78. 'dirname' => '/',
  79. 'basename' => 'absolute',
  80. 'filename' => 'absolute',
  81. ]
  82. ],
  83. '/absolute/' => [
  84. 'parent' => '/',
  85. 'normalize' => '/absolute',
  86. 'dirname' => '/',
  87. 'pathinfo' => [
  88. 'dirname' => '/',
  89. 'basename' => 'absolute',
  90. 'filename' => 'absolute',
  91. ]
  92. ],
  93. '/very/long/absolute/path' => [
  94. 'parent' => '/very/long/absolute',
  95. 'normalize' => '/very/long/absolute/path',
  96. 'dirname' => '/very/long/absolute',
  97. 'pathinfo' => [
  98. 'dirname' => '/very/long/absolute',
  99. 'basename' => 'path',
  100. 'filename' => 'path',
  101. ]
  102. ],
  103. '/very/long/absolute/../path' => [
  104. 'parent' => '/very/long',
  105. 'normalize' => '/very/long/path',
  106. 'dirname' => '/very/long/absolute/..',
  107. 'pathinfo' => [
  108. 'dirname' => '/very/long/absolute/..',
  109. 'basename' => 'path',
  110. 'filename' => 'path',
  111. ]
  112. ],
  113. 'relative' => [
  114. 'parent' => '.',
  115. 'normalize' => 'relative',
  116. 'dirname' => '.',
  117. 'pathinfo' => [
  118. 'dirname' => '.',
  119. 'basename' => 'relative',
  120. 'filename' => 'relative',
  121. ]
  122. ],
  123. 'very/long/relative/path' => [
  124. 'parent' => 'very/long/relative',
  125. 'normalize' => 'very/long/relative/path',
  126. 'dirname' => 'very/long/relative',
  127. 'pathinfo' => [
  128. 'dirname' => 'very/long/relative',
  129. 'basename' => 'path',
  130. 'filename' => 'path',
  131. ]
  132. ],
  133. 'path/to/file.jpg' => [
  134. 'parent' => 'path/to',
  135. 'normalize' => 'path/to/file.jpg',
  136. 'dirname' => 'path/to',
  137. 'pathinfo' => [
  138. 'dirname' => 'path/to',
  139. 'basename' => 'file.jpg',
  140. 'extension' => 'jpg',
  141. 'filename' => 'file',
  142. ]
  143. ],
  144. 'user://' => [
  145. 'parent' => '',
  146. 'normalize' => 'user://',
  147. 'dirname' => 'user://',
  148. 'pathinfo' => [
  149. 'dirname' => 'user://',
  150. 'basename' => '',
  151. 'filename' => '',
  152. 'scheme' => 'user',
  153. ]
  154. ],
  155. 'user://.' => [
  156. 'parent' => '',
  157. 'normalize' => 'user://',
  158. 'dirname' => 'user://',
  159. 'pathinfo' => [
  160. 'dirname' => 'user://',
  161. 'basename' => '',
  162. 'filename' => '',
  163. 'scheme' => 'user',
  164. ]
  165. ],
  166. 'user://././.' => [
  167. 'parent' => '',
  168. 'normalize' => 'user://',
  169. 'dirname' => 'user://',
  170. 'pathinfo' => [
  171. 'dirname' => 'user://',
  172. 'basename' => '',
  173. 'filename' => '',
  174. 'scheme' => 'user',
  175. ]
  176. ],
  177. 'user://./././file' => [
  178. 'parent' => 'user://',
  179. 'normalize' => 'user://file',
  180. 'dirname' => 'user://',
  181. 'pathinfo' => [
  182. 'dirname' => 'user://',
  183. 'basename' => 'file',
  184. 'filename' => 'file',
  185. 'scheme' => 'user',
  186. ]
  187. ],
  188. 'user://./././folder/file' => [
  189. 'parent' => 'user://folder',
  190. 'normalize' => 'user://folder/file',
  191. 'dirname' => 'user://folder',
  192. 'pathinfo' => [
  193. 'dirname' => 'user://folder',
  194. 'basename' => 'file',
  195. 'filename' => 'file',
  196. 'scheme' => 'user',
  197. ]
  198. ],
  199. 'user://.file' => [
  200. 'parent' => 'user://',
  201. 'normalize' => 'user://.file',
  202. 'dirname' => 'user://',
  203. 'pathinfo' => [
  204. 'dirname' => 'user://',
  205. 'basename' => '.file',
  206. 'extension' => 'file',
  207. 'filename' => '',
  208. 'scheme' => 'user',
  209. ]
  210. ],
  211. 'user:///' => [
  212. 'parent' => '',
  213. 'normalize' => 'user:///',
  214. 'dirname' => 'user:///',
  215. 'pathinfo' => [
  216. 'dirname' => 'user:///',
  217. 'basename' => '',
  218. 'filename' => '',
  219. 'scheme' => 'user',
  220. ]
  221. ],
  222. 'user:///absolute' => [
  223. 'parent' => 'user:///',
  224. 'normalize' => 'user:///absolute',
  225. 'dirname' => 'user:///',
  226. 'pathinfo' => [
  227. 'dirname' => 'user:///',
  228. 'basename' => 'absolute',
  229. 'filename' => 'absolute',
  230. 'scheme' => 'user',
  231. ]
  232. ],
  233. 'user:///very/long/absolute/path' => [
  234. 'parent' => 'user:///very/long/absolute',
  235. 'normalize' => 'user:///very/long/absolute/path',
  236. 'dirname' => 'user:///very/long/absolute',
  237. 'pathinfo' => [
  238. 'dirname' => 'user:///very/long/absolute',
  239. 'basename' => 'path',
  240. 'filename' => 'path',
  241. 'scheme' => 'user',
  242. ]
  243. ],
  244. 'user://relative' => [
  245. 'parent' => 'user://',
  246. 'normalize' => 'user://relative',
  247. 'dirname' => 'user://',
  248. 'pathinfo' => [
  249. 'dirname' => 'user://',
  250. 'basename' => 'relative',
  251. 'filename' => 'relative',
  252. 'scheme' => 'user',
  253. ]
  254. ],
  255. 'user://very/long/relative/path' => [
  256. 'parent' => 'user://very/long/relative',
  257. 'normalize' => 'user://very/long/relative/path',
  258. 'dirname' => 'user://very/long/relative',
  259. 'pathinfo' => [
  260. 'dirname' => 'user://very/long/relative',
  261. 'basename' => 'path',
  262. 'filename' => 'path',
  263. 'scheme' => 'user',
  264. ]
  265. ],
  266. 'user://path/to/file.jpg' => [
  267. 'parent' => 'user://path/to',
  268. 'normalize' => 'user://path/to/file.jpg',
  269. 'dirname' => 'user://path/to',
  270. 'pathinfo' => [
  271. 'dirname' => 'user://path/to',
  272. 'basename' => 'file.jpg',
  273. 'extension' => 'jpg',
  274. 'filename' => 'file',
  275. 'scheme' => 'user',
  276. ]
  277. ],
  278. ];
  279. protected function _before(): void
  280. {
  281. $this->class = Filesystem::getInstance();
  282. }
  283. protected function _after(): void
  284. {
  285. unset($this->class);
  286. }
  287. /**
  288. * @param array $tests
  289. * @param string $method
  290. */
  291. protected function runTestSet(array $tests, $method): void
  292. {
  293. $class = $this->class;
  294. foreach ($tests as $path => $candidates) {
  295. if (!array_key_exists($method, $candidates)) {
  296. continue;
  297. }
  298. $expected = $candidates[$method];
  299. $result = $class->{$method}($path);
  300. self::assertSame($expected, $result, "Test {$method}('{$path}')");
  301. if (function_exists($method) && !strpos($path, '://')) {
  302. $cmp_result = $method($path);
  303. self::assertSame($cmp_result, $result, "Compare to original {$method}('{$path}')");
  304. }
  305. }
  306. }
  307. public function testParent(): void
  308. {
  309. $this->runTestSet($this->tests, 'parent');
  310. }
  311. public function testNormalize(): void
  312. {
  313. $this->runTestSet($this->tests, 'normalize');
  314. }
  315. public function testDirname(): void
  316. {
  317. $this->runTestSet($this->tests, 'dirname');
  318. }
  319. public function testPathinfo(): void
  320. {
  321. $this->runTestSet($this->tests, 'pathinfo');
  322. }
  323. }