FilesystemTest.php 9.3 KB

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