Drupal investigation

PathTest.php 56KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  1. <?php
  2. /*
  3. * This file is part of the webmozart/path-util package.
  4. *
  5. * (c) Bernhard Schussek <bschussek@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Webmozart\PathUtil\Tests;
  11. use Webmozart\PathUtil\Path;
  12. /**
  13. * @since 1.0
  14. *
  15. * @author Bernhard Schussek <bschussek@gmail.com>
  16. * @author Thomas Schulz <mail@king2500.net>
  17. */
  18. class PathTest extends \PHPUnit_Framework_TestCase
  19. {
  20. protected $storedEnv = array();
  21. public function setUp()
  22. {
  23. $this->storedEnv['HOME'] = getenv('HOME');
  24. $this->storedEnv['HOMEDRIVE'] = getenv('HOMEDRIVE');
  25. $this->storedEnv['HOMEPATH'] = getenv('HOMEPATH');
  26. putenv('HOME=/home/webmozart');
  27. putenv('HOMEDRIVE=');
  28. putenv('HOMEPATH=');
  29. }
  30. public function tearDown()
  31. {
  32. putenv('HOME='.$this->storedEnv['HOME']);
  33. putenv('HOMEDRIVE='.$this->storedEnv['HOMEDRIVE']);
  34. putenv('HOMEPATH='.$this->storedEnv['HOMEPATH']);
  35. }
  36. public function provideCanonicalizationTests()
  37. {
  38. return array(
  39. // relative paths (forward slash)
  40. array('css/./style.css', 'css/style.css'),
  41. array('css/../style.css', 'style.css'),
  42. array('css/./../style.css', 'style.css'),
  43. array('css/.././style.css', 'style.css'),
  44. array('css/../../style.css', '../style.css'),
  45. array('./css/style.css', 'css/style.css'),
  46. array('../css/style.css', '../css/style.css'),
  47. array('./../css/style.css', '../css/style.css'),
  48. array('.././css/style.css', '../css/style.css'),
  49. array('../../css/style.css', '../../css/style.css'),
  50. array('', ''),
  51. array('.', ''),
  52. array('..', '..'),
  53. array('./..', '..'),
  54. array('../.', '..'),
  55. array('../..', '../..'),
  56. // relative paths (backslash)
  57. array('css\\.\\style.css', 'css/style.css'),
  58. array('css\\..\\style.css', 'style.css'),
  59. array('css\\.\\..\\style.css', 'style.css'),
  60. array('css\\..\\.\\style.css', 'style.css'),
  61. array('css\\..\\..\\style.css', '../style.css'),
  62. array('.\\css\\style.css', 'css/style.css'),
  63. array('..\\css\\style.css', '../css/style.css'),
  64. array('.\\..\\css\\style.css', '../css/style.css'),
  65. array('..\\.\\css\\style.css', '../css/style.css'),
  66. array('..\\..\\css\\style.css', '../../css/style.css'),
  67. // absolute paths (forward slash, UNIX)
  68. array('/css/style.css', '/css/style.css'),
  69. array('/css/./style.css', '/css/style.css'),
  70. array('/css/../style.css', '/style.css'),
  71. array('/css/./../style.css', '/style.css'),
  72. array('/css/.././style.css', '/style.css'),
  73. array('/./css/style.css', '/css/style.css'),
  74. array('/../css/style.css', '/css/style.css'),
  75. array('/./../css/style.css', '/css/style.css'),
  76. array('/.././css/style.css', '/css/style.css'),
  77. array('/../../css/style.css', '/css/style.css'),
  78. // absolute paths (backslash, UNIX)
  79. array('\\css\\style.css', '/css/style.css'),
  80. array('\\css\\.\\style.css', '/css/style.css'),
  81. array('\\css\\..\\style.css', '/style.css'),
  82. array('\\css\\.\\..\\style.css', '/style.css'),
  83. array('\\css\\..\\.\\style.css', '/style.css'),
  84. array('\\.\\css\\style.css', '/css/style.css'),
  85. array('\\..\\css\\style.css', '/css/style.css'),
  86. array('\\.\\..\\css\\style.css', '/css/style.css'),
  87. array('\\..\\.\\css\\style.css', '/css/style.css'),
  88. array('\\..\\..\\css\\style.css', '/css/style.css'),
  89. // absolute paths (forward slash, Windows)
  90. array('C:/css/style.css', 'C:/css/style.css'),
  91. array('C:/css/./style.css', 'C:/css/style.css'),
  92. array('C:/css/../style.css', 'C:/style.css'),
  93. array('C:/css/./../style.css', 'C:/style.css'),
  94. array('C:/css/.././style.css', 'C:/style.css'),
  95. array('C:/./css/style.css', 'C:/css/style.css'),
  96. array('C:/../css/style.css', 'C:/css/style.css'),
  97. array('C:/./../css/style.css', 'C:/css/style.css'),
  98. array('C:/.././css/style.css', 'C:/css/style.css'),
  99. array('C:/../../css/style.css', 'C:/css/style.css'),
  100. // absolute paths (backslash, Windows)
  101. array('C:\\css\\style.css', 'C:/css/style.css'),
  102. array('C:\\css\\.\\style.css', 'C:/css/style.css'),
  103. array('C:\\css\\..\\style.css', 'C:/style.css'),
  104. array('C:\\css\\.\\..\\style.css', 'C:/style.css'),
  105. array('C:\\css\\..\\.\\style.css', 'C:/style.css'),
  106. array('C:\\.\\css\\style.css', 'C:/css/style.css'),
  107. array('C:\\..\\css\\style.css', 'C:/css/style.css'),
  108. array('C:\\.\\..\\css\\style.css', 'C:/css/style.css'),
  109. array('C:\\..\\.\\css\\style.css', 'C:/css/style.css'),
  110. array('C:\\..\\..\\css\\style.css', 'C:/css/style.css'),
  111. // Windows special case
  112. array('C:', 'C:/'),
  113. // Don't change malformed path
  114. array('C:css/style.css', 'C:css/style.css'),
  115. // absolute paths (stream, UNIX)
  116. array('phar:///css/style.css', 'phar:///css/style.css'),
  117. array('phar:///css/./style.css', 'phar:///css/style.css'),
  118. array('phar:///css/../style.css', 'phar:///style.css'),
  119. array('phar:///css/./../style.css', 'phar:///style.css'),
  120. array('phar:///css/.././style.css', 'phar:///style.css'),
  121. array('phar:///./css/style.css', 'phar:///css/style.css'),
  122. array('phar:///../css/style.css', 'phar:///css/style.css'),
  123. array('phar:///./../css/style.css', 'phar:///css/style.css'),
  124. array('phar:///.././css/style.css', 'phar:///css/style.css'),
  125. array('phar:///../../css/style.css', 'phar:///css/style.css'),
  126. // absolute paths (stream, Windows)
  127. array('phar://C:/css/style.css', 'phar://C:/css/style.css'),
  128. array('phar://C:/css/./style.css', 'phar://C:/css/style.css'),
  129. array('phar://C:/css/../style.css', 'phar://C:/style.css'),
  130. array('phar://C:/css/./../style.css', 'phar://C:/style.css'),
  131. array('phar://C:/css/.././style.css', 'phar://C:/style.css'),
  132. array('phar://C:/./css/style.css', 'phar://C:/css/style.css'),
  133. array('phar://C:/../css/style.css', 'phar://C:/css/style.css'),
  134. array('phar://C:/./../css/style.css', 'phar://C:/css/style.css'),
  135. array('phar://C:/.././css/style.css', 'phar://C:/css/style.css'),
  136. array('phar://C:/../../css/style.css', 'phar://C:/css/style.css'),
  137. // paths with "~" UNIX
  138. array('~/css/style.css', '/home/webmozart/css/style.css'),
  139. array('~/css/./style.css', '/home/webmozart/css/style.css'),
  140. array('~/css/../style.css', '/home/webmozart/style.css'),
  141. array('~/css/./../style.css', '/home/webmozart/style.css'),
  142. array('~/css/.././style.css', '/home/webmozart/style.css'),
  143. array('~/./css/style.css', '/home/webmozart/css/style.css'),
  144. array('~/../css/style.css', '/home/css/style.css'),
  145. array('~/./../css/style.css', '/home/css/style.css'),
  146. array('~/.././css/style.css', '/home/css/style.css'),
  147. array('~/../../css/style.css', '/css/style.css'),
  148. );
  149. }
  150. /**
  151. * @dataProvider provideCanonicalizationTests
  152. */
  153. public function testCanonicalize($path, $canonicalized)
  154. {
  155. $this->assertSame($canonicalized, Path::canonicalize($path));
  156. }
  157. /**
  158. * @expectedException \InvalidArgumentException
  159. * @expectedExceptionMessage The path must be a string. Got: array
  160. */
  161. public function testCanonicalizeFailsIfInvalidPath()
  162. {
  163. Path::canonicalize(array());
  164. }
  165. public function provideGetDirectoryTests()
  166. {
  167. return array(
  168. array('/webmozart/puli/style.css', '/webmozart/puli'),
  169. array('/webmozart/puli', '/webmozart'),
  170. array('/webmozart', '/'),
  171. array('/', '/'),
  172. array('', ''),
  173. array('\\webmozart\\puli\\style.css', '/webmozart/puli'),
  174. array('\\webmozart\\puli', '/webmozart'),
  175. array('\\webmozart', '/'),
  176. array('\\', '/'),
  177. array('C:/webmozart/puli/style.css', 'C:/webmozart/puli'),
  178. array('C:/webmozart/puli', 'C:/webmozart'),
  179. array('C:/webmozart', 'C:/'),
  180. array('C:/', 'C:/'),
  181. array('C:', 'C:/'),
  182. array('C:\\webmozart\\puli\\style.css', 'C:/webmozart/puli'),
  183. array('C:\\webmozart\\puli', 'C:/webmozart'),
  184. array('C:\\webmozart', 'C:/'),
  185. array('C:\\', 'C:/'),
  186. array('phar:///webmozart/puli/style.css', 'phar:///webmozart/puli'),
  187. array('phar:///webmozart/puli', 'phar:///webmozart'),
  188. array('phar:///webmozart', 'phar:///'),
  189. array('phar:///', 'phar:///'),
  190. array('phar://C:/webmozart/puli/style.css', 'phar://C:/webmozart/puli'),
  191. array('phar://C:/webmozart/puli', 'phar://C:/webmozart'),
  192. array('phar://C:/webmozart', 'phar://C:/'),
  193. array('phar://C:/', 'phar://C:/'),
  194. array('webmozart/puli/style.css', 'webmozart/puli'),
  195. array('webmozart/puli', 'webmozart'),
  196. array('webmozart', ''),
  197. array('webmozart\\puli\\style.css', 'webmozart/puli'),
  198. array('webmozart\\puli', 'webmozart'),
  199. array('webmozart', ''),
  200. array('/webmozart/./puli/style.css', '/webmozart/puli'),
  201. array('/webmozart/../puli/style.css', '/puli'),
  202. array('/webmozart/./../puli/style.css', '/puli'),
  203. array('/webmozart/.././puli/style.css', '/puli'),
  204. array('/webmozart/../../puli/style.css', '/puli'),
  205. array('/.', '/'),
  206. array('/..', '/'),
  207. array('C:webmozart', ''),
  208. );
  209. }
  210. /**
  211. * @dataProvider provideGetDirectoryTests
  212. */
  213. public function testGetDirectory($path, $directory)
  214. {
  215. $this->assertSame($directory, Path::getDirectory($path));
  216. }
  217. /**
  218. * @expectedException \InvalidArgumentException
  219. * @expectedExceptionMessage The path must be a string. Got: array
  220. */
  221. public function testGetDirectoryFailsIfInvalidPath()
  222. {
  223. Path::getDirectory(array());
  224. }
  225. public function provideGetFilenameTests()
  226. {
  227. return array(
  228. array('/webmozart/puli/style.css', 'style.css'),
  229. array('/webmozart/puli/STYLE.CSS', 'STYLE.CSS'),
  230. array('/webmozart/puli/style.css/', 'style.css'),
  231. array('/webmozart/puli/', 'puli'),
  232. array('/webmozart/puli', 'puli'),
  233. array('/', ''),
  234. array('', ''),
  235. );
  236. }
  237. /**
  238. * @dataProvider provideGetFilenameTests
  239. */
  240. public function testGetFilename($path, $filename)
  241. {
  242. $this->assertSame($filename, Path::getFilename($path));
  243. }
  244. /**
  245. * @expectedException \InvalidArgumentException
  246. * @expectedExceptionMessage The path must be a string. Got: array
  247. */
  248. public function testGetFilenameFailsIfInvalidPath()
  249. {
  250. Path::getFilename(array());
  251. }
  252. public function provideGetFilenameWithoutExtensionTests()
  253. {
  254. return array(
  255. array('/webmozart/puli/style.css.twig', null, 'style.css'),
  256. array('/webmozart/puli/style.css.', null, 'style.css'),
  257. array('/webmozart/puli/style.css', null, 'style'),
  258. array('/webmozart/puli/.style.css', null, '.style'),
  259. array('/webmozart/puli/', null, 'puli'),
  260. array('/webmozart/puli', null, 'puli'),
  261. array('/', null, ''),
  262. array('', null, ''),
  263. array('/webmozart/puli/style.css', 'css', 'style'),
  264. array('/webmozart/puli/style.css', '.css', 'style'),
  265. array('/webmozart/puli/style.css', 'twig', 'style.css'),
  266. array('/webmozart/puli/style.css', '.twig', 'style.css'),
  267. array('/webmozart/puli/style.css', '', 'style.css'),
  268. array('/webmozart/puli/style.css.', '', 'style.css'),
  269. array('/webmozart/puli/style.css.', '.', 'style.css'),
  270. array('/webmozart/puli/style.css.', '.css', 'style.css'),
  271. array('/webmozart/puli/.style.css', 'css', '.style'),
  272. array('/webmozart/puli/.style.css', '.css', '.style'),
  273. );
  274. }
  275. /**
  276. * @dataProvider provideGetFilenameWithoutExtensionTests
  277. */
  278. public function testGetFilenameWithoutExtension($path, $extension, $filename)
  279. {
  280. $this->assertSame($filename, Path::getFilenameWithoutExtension($path, $extension));
  281. }
  282. /**
  283. * @expectedException \InvalidArgumentException
  284. * @expectedExceptionMessage The path must be a string. Got: array
  285. */
  286. public function testGetFilenameWithoutExtensionFailsIfInvalidPath()
  287. {
  288. Path::getFilenameWithoutExtension(array(), '.css');
  289. }
  290. /**
  291. * @expectedException \InvalidArgumentException
  292. * @expectedExceptionMessage The extension must be a string or null. Got: array
  293. */
  294. public function testGetFilenameWithoutExtensionFailsIfInvalidExtension()
  295. {
  296. Path::getFilenameWithoutExtension('/style.css', array());
  297. }
  298. public function provideGetExtensionTests()
  299. {
  300. $tests = array(
  301. array('/webmozart/puli/style.css.twig', false, 'twig'),
  302. array('/webmozart/puli/style.css', false, 'css'),
  303. array('/webmozart/puli/style.css.', false, ''),
  304. array('/webmozart/puli/', false, ''),
  305. array('/webmozart/puli', false, ''),
  306. array('/', false, ''),
  307. array('', false, ''),
  308. array('/webmozart/puli/style.CSS', false, 'CSS'),
  309. array('/webmozart/puli/style.CSS', true, 'css'),
  310. array('/webmozart/puli/style.ÄÖÜ', false, 'ÄÖÜ'),
  311. );
  312. if (extension_loaded('mbstring')) {
  313. // This can only be tested, when mbstring is installed
  314. $tests[] = array('/webmozart/puli/style.ÄÖÜ', true, 'äöü');
  315. }
  316. return $tests;
  317. }
  318. /**
  319. * @dataProvider provideGetExtensionTests
  320. */
  321. public function testGetExtension($path, $forceLowerCase, $extension)
  322. {
  323. $this->assertSame($extension, Path::getExtension($path, $forceLowerCase));
  324. }
  325. /**
  326. * @expectedException \InvalidArgumentException
  327. * @expectedExceptionMessage The path must be a string. Got: array
  328. */
  329. public function testGetExtensionFailsIfInvalidPath()
  330. {
  331. Path::getExtension(array());
  332. }
  333. public function provideHasExtensionTests()
  334. {
  335. $tests = array(
  336. array(true, '/webmozart/puli/style.css.twig', null, false),
  337. array(true, '/webmozart/puli/style.css', null, false),
  338. array(false, '/webmozart/puli/style.css.', null, false),
  339. array(false, '/webmozart/puli/', null, false),
  340. array(false, '/webmozart/puli', null, false),
  341. array(false, '/', null, false),
  342. array(false, '', null, false),
  343. array(true, '/webmozart/puli/style.css.twig', 'twig', false),
  344. array(false, '/webmozart/puli/style.css.twig', 'css', false),
  345. array(true, '/webmozart/puli/style.css', 'css', false),
  346. array(true, '/webmozart/puli/style.css', '.css', false),
  347. array(true, '/webmozart/puli/style.css.', '', false),
  348. array(false, '/webmozart/puli/', 'ext', false),
  349. array(false, '/webmozart/puli', 'ext', false),
  350. array(false, '/', 'ext', false),
  351. array(false, '', 'ext', false),
  352. array(false, '/webmozart/puli/style.css', 'CSS', false),
  353. array(true, '/webmozart/puli/style.css', 'CSS', true),
  354. array(false, '/webmozart/puli/style.CSS', 'css', false),
  355. array(true, '/webmozart/puli/style.CSS', 'css', true),
  356. array(true, '/webmozart/puli/style.ÄÖÜ', 'ÄÖÜ', false),
  357. array(true, '/webmozart/puli/style.css', array('ext', 'css'), false),
  358. array(true, '/webmozart/puli/style.css', array('.ext', '.css'), false),
  359. array(true, '/webmozart/puli/style.css.', array('ext', ''), false),
  360. array(false, '/webmozart/puli/style.css', array('foo', 'bar', ''), false),
  361. array(false, '/webmozart/puli/style.css', array('.foo', '.bar', ''), false),
  362. );
  363. if (extension_loaded('mbstring')) {
  364. // This can only be tested, when mbstring is installed
  365. $tests[] = array(true, '/webmozart/puli/style.ÄÖÜ', 'äöü', true);
  366. $tests[] = array(true, '/webmozart/puli/style.ÄÖÜ', array('äöü'), true);
  367. }
  368. return $tests;
  369. }
  370. /**
  371. * @dataProvider provideHasExtensionTests
  372. */
  373. public function testHasExtension($hasExtension, $path, $extension, $ignoreCase)
  374. {
  375. $this->assertSame($hasExtension, Path::hasExtension($path, $extension, $ignoreCase));
  376. }
  377. /**
  378. * @expectedException \InvalidArgumentException
  379. * @expectedExceptionMessage The path must be a string. Got: array
  380. */
  381. public function testHasExtensionFailsIfInvalidPath()
  382. {
  383. Path::hasExtension(array());
  384. }
  385. /**
  386. * @expectedException \InvalidArgumentException
  387. * @expectedExceptionMessage The extensions must be strings. Got: stdClass
  388. */
  389. public function testHasExtensionFailsIfInvalidExtension()
  390. {
  391. Path::hasExtension('/style.css', (object) array());
  392. }
  393. public function provideChangeExtensionTests()
  394. {
  395. return array(
  396. array('/webmozart/puli/style.css.twig', 'html', '/webmozart/puli/style.css.html'),
  397. array('/webmozart/puli/style.css', 'sass', '/webmozart/puli/style.sass'),
  398. array('/webmozart/puli/style.css', '.sass', '/webmozart/puli/style.sass'),
  399. array('/webmozart/puli/style.css', '', '/webmozart/puli/style.'),
  400. array('/webmozart/puli/style.css.', 'twig', '/webmozart/puli/style.css.twig'),
  401. array('/webmozart/puli/style.css.', '', '/webmozart/puli/style.css.'),
  402. array('/webmozart/puli/style.css', 'äöü', '/webmozart/puli/style.äöü'),
  403. array('/webmozart/puli/style.äöü', 'css', '/webmozart/puli/style.css'),
  404. array('/webmozart/puli/', 'css', '/webmozart/puli/'),
  405. array('/webmozart/puli', 'css', '/webmozart/puli.css'),
  406. array('/', 'css', '/'),
  407. array('', 'css', ''),
  408. );
  409. }
  410. /**
  411. * @dataProvider provideChangeExtensionTests
  412. */
  413. public function testChangeExtension($path, $extension, $pathExpected)
  414. {
  415. static $call = 0;
  416. $this->assertSame($pathExpected, Path::changeExtension($path, $extension));
  417. ++$call;
  418. }
  419. /**
  420. * @expectedException \InvalidArgumentException
  421. * @expectedExceptionMessage The path must be a string. Got: array
  422. */
  423. public function testChangeExtensionFailsIfInvalidPath()
  424. {
  425. Path::changeExtension(array(), '.sass');
  426. }
  427. /**
  428. * @expectedException \InvalidArgumentException
  429. * @expectedExceptionMessage The extension must be a string. Got: array
  430. */
  431. public function testChangeExtensionFailsIfInvalidExtension()
  432. {
  433. Path::changeExtension('/style.css', array());
  434. }
  435. public function provideIsAbsolutePathTests()
  436. {
  437. return array(
  438. array('/css/style.css', true),
  439. array('/', true),
  440. array('css/style.css', false),
  441. array('', false),
  442. array('\\css\\style.css', true),
  443. array('\\', true),
  444. array('css\\style.css', false),
  445. array('C:/css/style.css', true),
  446. array('D:/', true),
  447. array('E:\\css\\style.css', true),
  448. array('F:\\', true),
  449. array('phar:///css/style.css', true),
  450. array('phar:///', true),
  451. // Windows special case
  452. array('C:', true),
  453. // Not considered absolute
  454. array('C:css/style.css', false),
  455. );
  456. }
  457. /**
  458. * @dataProvider provideIsAbsolutePathTests
  459. */
  460. public function testIsAbsolute($path, $isAbsolute)
  461. {
  462. $this->assertSame($isAbsolute, Path::isAbsolute($path));
  463. }
  464. /**
  465. * @expectedException \InvalidArgumentException
  466. * @expectedExceptionMessage The path must be a string. Got: array
  467. */
  468. public function testIsAbsoluteFailsIfInvalidPath()
  469. {
  470. Path::isAbsolute(array());
  471. }
  472. /**
  473. * @dataProvider provideIsAbsolutePathTests
  474. */
  475. public function testIsRelative($path, $isAbsolute)
  476. {
  477. $this->assertSame(!$isAbsolute, Path::isRelative($path));
  478. }
  479. /**
  480. * @expectedException \InvalidArgumentException
  481. * @expectedExceptionMessage The path must be a string. Got: array
  482. */
  483. public function testIsRelativeFailsIfInvalidPath()
  484. {
  485. Path::isRelative(array());
  486. }
  487. public function provideGetRootTests()
  488. {
  489. return array(
  490. array('/css/style.css', '/'),
  491. array('/', '/'),
  492. array('css/style.css', ''),
  493. array('', ''),
  494. array('\\css\\style.css', '/'),
  495. array('\\', '/'),
  496. array('css\\style.css', ''),
  497. array('C:/css/style.css', 'C:/'),
  498. array('C:/', 'C:/'),
  499. array('C:', 'C:/'),
  500. array('D:\\css\\style.css', 'D:/'),
  501. array('D:\\', 'D:/'),
  502. array('phar:///css/style.css', 'phar:///'),
  503. array('phar:///', 'phar:///'),
  504. array('phar://C:/css/style.css', 'phar://C:/'),
  505. array('phar://C:/', 'phar://C:/'),
  506. array('phar://C:', 'phar://C:/'),
  507. );
  508. }
  509. /**
  510. * @dataProvider provideGetRootTests
  511. */
  512. public function testGetRoot($path, $root)
  513. {
  514. $this->assertSame($root, Path::getRoot($path));
  515. }
  516. /**
  517. * @expectedException \InvalidArgumentException
  518. * @expectedExceptionMessage The path must be a string. Got: array
  519. */
  520. public function testGetRootFailsIfInvalidPath()
  521. {
  522. Path::getRoot(array());
  523. }
  524. public function providePathTests()
  525. {
  526. return array(
  527. // relative to absolute path
  528. array('css/style.css', '/webmozart/puli', '/webmozart/puli/css/style.css'),
  529. array('../css/style.css', '/webmozart/puli', '/webmozart/css/style.css'),
  530. array('../../css/style.css', '/webmozart/puli', '/css/style.css'),
  531. // relative to root
  532. array('css/style.css', '/', '/css/style.css'),
  533. array('css/style.css', 'C:', 'C:/css/style.css'),
  534. array('css/style.css', 'C:/', 'C:/css/style.css'),
  535. // same sub directories in different base directories
  536. array('../../puli/css/style.css', '/webmozart/css', '/puli/css/style.css'),
  537. array('', '/webmozart/puli', '/webmozart/puli'),
  538. array('..', '/webmozart/puli', '/webmozart'),
  539. );
  540. }
  541. public function provideMakeAbsoluteTests()
  542. {
  543. return array_merge($this->providePathTests(), array(
  544. // collapse dots
  545. array('css/./style.css', '/webmozart/puli', '/webmozart/puli/css/style.css'),
  546. array('css/../style.css', '/webmozart/puli', '/webmozart/puli/style.css'),
  547. array('css/./../style.css', '/webmozart/puli', '/webmozart/puli/style.css'),
  548. array('css/.././style.css', '/webmozart/puli', '/webmozart/puli/style.css'),
  549. array('./css/style.css', '/webmozart/puli', '/webmozart/puli/css/style.css'),
  550. array('css\\.\\style.css', '\\webmozart\\puli', '/webmozart/puli/css/style.css'),
  551. array('css\\..\\style.css', '\\webmozart\\puli', '/webmozart/puli/style.css'),
  552. array('css\\.\\..\\style.css', '\\webmozart\\puli', '/webmozart/puli/style.css'),
  553. array('css\\..\\.\\style.css', '\\webmozart\\puli', '/webmozart/puli/style.css'),
  554. array('.\\css\\style.css', '\\webmozart\\puli', '/webmozart/puli/css/style.css'),
  555. // collapse dots on root
  556. array('./css/style.css', '/', '/css/style.css'),
  557. array('../css/style.css', '/', '/css/style.css'),
  558. array('../css/./style.css', '/', '/css/style.css'),
  559. array('../css/../style.css', '/', '/style.css'),
  560. array('../css/./../style.css', '/', '/style.css'),
  561. array('../css/.././style.css', '/', '/style.css'),
  562. array('.\\css\\style.css', '\\', '/css/style.css'),
  563. array('..\\css\\style.css', '\\', '/css/style.css'),
  564. array('..\\css\\.\\style.css', '\\', '/css/style.css'),
  565. array('..\\css\\..\\style.css', '\\', '/style.css'),
  566. array('..\\css\\.\\..\\style.css', '\\', '/style.css'),
  567. array('..\\css\\..\\.\\style.css', '\\', '/style.css'),
  568. array('./css/style.css', 'C:/', 'C:/css/style.css'),
  569. array('../css/style.css', 'C:/', 'C:/css/style.css'),
  570. array('../css/./style.css', 'C:/', 'C:/css/style.css'),
  571. array('../css/../style.css', 'C:/', 'C:/style.css'),
  572. array('../css/./../style.css', 'C:/', 'C:/style.css'),
  573. array('../css/.././style.css', 'C:/', 'C:/style.css'),
  574. array('.\\css\\style.css', 'C:\\', 'C:/css/style.css'),
  575. array('..\\css\\style.css', 'C:\\', 'C:/css/style.css'),
  576. array('..\\css\\.\\style.css', 'C:\\', 'C:/css/style.css'),
  577. array('..\\css\\..\\style.css', 'C:\\', 'C:/style.css'),
  578. array('..\\css\\.\\..\\style.css', 'C:\\', 'C:/style.css'),
  579. array('..\\css\\..\\.\\style.css', 'C:\\', 'C:/style.css'),
  580. array('./css/style.css', 'phar:///', 'phar:///css/style.css'),
  581. array('../css/style.css', 'phar:///', 'phar:///css/style.css'),
  582. array('../css/./style.css', 'phar:///', 'phar:///css/style.css'),
  583. array('../css/../style.css', 'phar:///', 'phar:///style.css'),
  584. array('../css/./../style.css', 'phar:///', 'phar:///style.css'),
  585. array('../css/.././style.css', 'phar:///', 'phar:///style.css'),
  586. array('./css/style.css', 'phar://C:/', 'phar://C:/css/style.css'),
  587. array('../css/style.css', 'phar://C:/', 'phar://C:/css/style.css'),
  588. array('../css/./style.css', 'phar://C:/', 'phar://C:/css/style.css'),
  589. array('../css/../style.css', 'phar://C:/', 'phar://C:/style.css'),
  590. array('../css/./../style.css', 'phar://C:/', 'phar://C:/style.css'),
  591. array('../css/.././style.css', 'phar://C:/', 'phar://C:/style.css'),
  592. // absolute paths
  593. array('/css/style.css', '/webmozart/puli', '/css/style.css'),
  594. array('\\css\\style.css', '/webmozart/puli', '/css/style.css'),
  595. array('C:/css/style.css', 'C:/webmozart/puli', 'C:/css/style.css'),
  596. array('D:\\css\\style.css', 'D:/webmozart/puli', 'D:/css/style.css'),
  597. ));
  598. }
  599. /**
  600. * @dataProvider provideMakeAbsoluteTests
  601. */
  602. public function testMakeAbsolute($relativePath, $basePath, $absolutePath)
  603. {
  604. $this->assertSame($absolutePath, Path::makeAbsolute($relativePath, $basePath));
  605. }
  606. /**
  607. * @expectedException \InvalidArgumentException
  608. * @expectedExceptionMessage The path must be a string. Got: array
  609. */
  610. public function testMakeAbsoluteFailsIfInvalidPath()
  611. {
  612. Path::makeAbsolute(array(), '/webmozart/puli');
  613. }
  614. /**
  615. * @expectedException \InvalidArgumentException
  616. * @expectedExceptionMessage The base path must be a non-empty string. Got: array
  617. */
  618. public function testMakeAbsoluteFailsIfInvalidBasePath()
  619. {
  620. Path::makeAbsolute('css/style.css', array());
  621. }
  622. /**
  623. * @expectedException \InvalidArgumentException
  624. * @expectedExceptionMessage The base path "webmozart/puli" is not an absolute path.
  625. */
  626. public function testMakeAbsoluteFailsIfBasePathNotAbsolute()
  627. {
  628. Path::makeAbsolute('css/style.css', 'webmozart/puli');
  629. }
  630. /**
  631. * @expectedException \InvalidArgumentException
  632. * @expectedExceptionMessage The base path must be a non-empty string. Got: ""
  633. */
  634. public function testMakeAbsoluteFailsIfBasePathEmpty()
  635. {
  636. Path::makeAbsolute('css/style.css', '');
  637. }
  638. /**
  639. * @expectedException \InvalidArgumentException
  640. * @expectedExceptionMessage The base path must be a non-empty string. Got: NULL
  641. */
  642. public function testMakeAbsoluteFailsIfBasePathNull()
  643. {
  644. Path::makeAbsolute('css/style.css', null);
  645. }
  646. public function provideAbsolutePathsWithDifferentRoots()
  647. {
  648. return array(
  649. array('C:/css/style.css', '/webmozart/puli'),
  650. array('C:/css/style.css', '\\webmozart\\puli'),
  651. array('C:\\css\\style.css', '/webmozart/puli'),
  652. array('C:\\css\\style.css', '\\webmozart\\puli'),
  653. array('/css/style.css', 'C:/webmozart/puli'),
  654. array('/css/style.css', 'C:\\webmozart\\puli'),
  655. array('\\css\\style.css', 'C:/webmozart/puli'),
  656. array('\\css\\style.css', 'C:\\webmozart\\puli'),
  657. array('D:/css/style.css', 'C:/webmozart/puli'),
  658. array('D:/css/style.css', 'C:\\webmozart\\puli'),
  659. array('D:\\css\\style.css', 'C:/webmozart/puli'),
  660. array('D:\\css\\style.css', 'C:\\webmozart\\puli'),
  661. array('phar:///css/style.css', '/webmozart/puli'),
  662. array('/css/style.css', 'phar:///webmozart/puli'),
  663. array('phar://C:/css/style.css', 'C:/webmozart/puli'),
  664. array('phar://C:/css/style.css', 'C:\\webmozart\\puli'),
  665. array('phar://C:\\css\\style.css', 'C:/webmozart/puli'),
  666. array('phar://C:\\css\\style.css', 'C:\\webmozart\\puli'),
  667. );
  668. }
  669. /**
  670. * @dataProvider provideAbsolutePathsWithDifferentRoots
  671. */
  672. public function testMakeAbsoluteDoesNotFailIfDifferentRoot($basePath, $absolutePath)
  673. {
  674. // If a path in partition D: is passed, but $basePath is in partition
  675. // C:, the path should be returned unchanged
  676. $this->assertSame(Path::canonicalize($absolutePath), Path::makeAbsolute($absolutePath, $basePath));
  677. }
  678. public function provideMakeRelativeTests()
  679. {
  680. $paths = array_map(function (array $arguments) {
  681. return array($arguments[2], $arguments[1], $arguments[0]);
  682. }, $this->providePathTests());
  683. return array_merge($paths, array(
  684. array('/webmozart/puli/./css/style.css', '/webmozart/puli', 'css/style.css'),
  685. array('/webmozart/puli/../css/style.css', '/webmozart/puli', '../css/style.css'),
  686. array('/webmozart/puli/.././css/style.css', '/webmozart/puli', '../css/style.css'),
  687. array('/webmozart/puli/./../css/style.css', '/webmozart/puli', '../css/style.css'),
  688. array('/webmozart/puli/../../css/style.css', '/webmozart/puli', '../../css/style.css'),
  689. array('/webmozart/puli/css/style.css', '/webmozart/./puli', 'css/style.css'),
  690. array('/webmozart/puli/css/style.css', '/webmozart/../puli', '../webmozart/puli/css/style.css'),
  691. array('/webmozart/puli/css/style.css', '/webmozart/./../puli', '../webmozart/puli/css/style.css'),
  692. array('/webmozart/puli/css/style.css', '/webmozart/.././puli', '../webmozart/puli/css/style.css'),
  693. array('/webmozart/puli/css/style.css', '/webmozart/../../puli', '../webmozart/puli/css/style.css'),
  694. // first argument shorter than second
  695. array('/css', '/webmozart/puli', '../../css'),
  696. // second argument shorter than first
  697. array('/webmozart/puli', '/css', '../webmozart/puli'),
  698. array('\\webmozart\\puli\\css\\style.css', '\\webmozart\\puli', 'css/style.css'),
  699. array('\\webmozart\\css\\style.css', '\\webmozart\\puli', '../css/style.css'),
  700. array('\\css\\style.css', '\\webmozart\\puli', '../../css/style.css'),
  701. array('C:/webmozart/puli/css/style.css', 'C:/webmozart/puli', 'css/style.css'),
  702. array('C:/webmozart/css/style.css', 'C:/webmozart/puli', '../css/style.css'),
  703. array('C:/css/style.css', 'C:/webmozart/puli', '../../css/style.css'),
  704. array('C:\\webmozart\\puli\\css\\style.css', 'C:\\webmozart\\puli', 'css/style.css'),
  705. array('C:\\webmozart\\css\\style.css', 'C:\\webmozart\\puli', '../css/style.css'),
  706. array('C:\\css\\style.css', 'C:\\webmozart\\puli', '../../css/style.css'),
  707. array('phar:///webmozart/puli/css/style.css', 'phar:///webmozart/puli', 'css/style.css'),
  708. array('phar:///webmozart/css/style.css', 'phar:///webmozart/puli', '../css/style.css'),
  709. array('phar:///css/style.css', 'phar:///webmozart/puli', '../../css/style.css'),
  710. array('phar://C:/webmozart/puli/css/style.css', 'phar://C:/webmozart/puli', 'css/style.css'),
  711. array('phar://C:/webmozart/css/style.css', 'phar://C:/webmozart/puli', '../css/style.css'),
  712. array('phar://C:/css/style.css', 'phar://C:/webmozart/puli', '../../css/style.css'),
  713. // already relative + already in root basepath
  714. array('../style.css', '/', 'style.css'),
  715. array('./style.css', '/', 'style.css'),
  716. array('../../style.css', '/', 'style.css'),
  717. array('..\\style.css', 'C:\\', 'style.css'),
  718. array('.\\style.css', 'C:\\', 'style.css'),
  719. array('..\\..\\style.css', 'C:\\', 'style.css'),
  720. array('../style.css', 'C:/', 'style.css'),
  721. array('./style.css', 'C:/', 'style.css'),
  722. array('../../style.css', 'C:/', 'style.css'),
  723. array('..\\style.css', '\\', 'style.css'),
  724. array('.\\style.css', '\\', 'style.css'),
  725. array('..\\..\\style.css', '\\', 'style.css'),
  726. array('../style.css', 'phar:///', 'style.css'),
  727. array('./style.css', 'phar:///', 'style.css'),
  728. array('../../style.css', 'phar:///', 'style.css'),
  729. array('..\\style.css', 'phar://C:\\', 'style.css'),
  730. array('.\\style.css', 'phar://C:\\', 'style.css'),
  731. array('..\\..\\style.css', 'phar://C:\\', 'style.css'),
  732. array('css/../style.css', '/', 'style.css'),
  733. array('css/./style.css', '/', 'css/style.css'),
  734. array('css\\..\\style.css', 'C:\\', 'style.css'),
  735. array('css\\.\\style.css', 'C:\\', 'css/style.css'),
  736. array('css/../style.css', 'C:/', 'style.css'),
  737. array('css/./style.css', 'C:/', 'css/style.css'),
  738. array('css\\..\\style.css', '\\', 'style.css'),
  739. array('css\\.\\style.css', '\\', 'css/style.css'),
  740. array('css/../style.css', 'phar:///', 'style.css'),
  741. array('css/./style.css', 'phar:///', 'css/style.css'),
  742. array('css\\..\\style.css', 'phar://C:\\', 'style.css'),
  743. array('css\\.\\style.css', 'phar://C:\\', 'css/style.css'),
  744. // already relative
  745. array('css/style.css', '/webmozart/puli', 'css/style.css'),
  746. array('css\\style.css', '\\webmozart\\puli', 'css/style.css'),
  747. // both relative
  748. array('css/style.css', 'webmozart/puli', '../../css/style.css'),
  749. array('css\\style.css', 'webmozart\\puli', '../../css/style.css'),
  750. // relative to empty
  751. array('css/style.css', '', 'css/style.css'),
  752. array('css\\style.css', '', 'css/style.css'),
  753. // different slashes in path and base path
  754. array('/webmozart/puli/css/style.css', '\\webmozart\\puli', 'css/style.css'),
  755. array('\\webmozart\\puli\\css\\style.css', '/webmozart/puli', 'css/style.css'),
  756. ));
  757. }
  758. /**
  759. * @dataProvider provideMakeRelativeTests
  760. */
  761. public function testMakeRelative($absolutePath, $basePath, $relativePath)
  762. {
  763. $this->assertSame($relativePath, Path::makeRelative($absolutePath, $basePath));
  764. }
  765. /**
  766. * @expectedException \InvalidArgumentException
  767. * @expectedExceptionMessage The path must be a string. Got: array
  768. */
  769. public function testMakeRelativeFailsIfInvalidPath()
  770. {
  771. Path::makeRelative(array(), '/webmozart/puli');
  772. }
  773. /**
  774. * @expectedException \InvalidArgumentException
  775. * @expectedExceptionMessage The base path must be a string. Got: array
  776. */
  777. public function testMakeRelativeFailsIfInvalidBasePath()
  778. {
  779. Path::makeRelative('/webmozart/puli/css/style.css', array());
  780. }
  781. /**
  782. * @expectedException \InvalidArgumentException
  783. * @expectedExceptionMessage The absolute path "/webmozart/puli/css/style.css" cannot be made relative to the relative path "webmozart/puli". You should provide an absolute base path instead.
  784. */
  785. public function testMakeRelativeFailsIfAbsolutePathAndBasePathNotAbsolute()
  786. {
  787. Path::makeRelative('/webmozart/puli/css/style.css', 'webmozart/puli');
  788. }
  789. /**
  790. * @expectedException \InvalidArgumentException
  791. * @expectedExceptionMessage The absolute path "/webmozart/puli/css/style.css" cannot be made relative to the relative path "". You should provide an absolute base path instead.
  792. */
  793. public function testMakeRelativeFailsIfAbsolutePathAndBasePathEmpty()
  794. {
  795. Path::makeRelative('/webmozart/puli/css/style.css', '');
  796. }
  797. /**
  798. * @expectedException \InvalidArgumentException
  799. * @expectedExceptionMessage The base path must be a string. Got: NULL
  800. */
  801. public function testMakeRelativeFailsIfBasePathNull()
  802. {
  803. Path::makeRelative('/webmozart/puli/css/style.css', null);
  804. }
  805. /**
  806. * @dataProvider provideAbsolutePathsWithDifferentRoots
  807. * @expectedException \InvalidArgumentException
  808. */
  809. public function testMakeRelativeFailsIfDifferentRoot($absolutePath, $basePath)
  810. {
  811. Path::makeRelative($absolutePath, $basePath);
  812. }
  813. public function provideIsLocalTests()
  814. {
  815. return array(
  816. array('/bg.png', true),
  817. array('bg.png', true),
  818. array('http://example.com/bg.png', false),
  819. array('http://example.com', false),
  820. array('', false),
  821. );
  822. }
  823. /**
  824. * @dataProvider provideIsLocalTests
  825. */
  826. public function testIsLocal($path, $isLocal)
  827. {
  828. $this->assertSame($isLocal, Path::isLocal($path));
  829. }
  830. /**
  831. * @expectedException \InvalidArgumentException
  832. * @expectedExceptionMessage The path must be a string. Got: array
  833. */
  834. public function testIsLocalFailsIfInvalidPath()
  835. {
  836. Path::isLocal(array());
  837. }
  838. public function provideGetLongestCommonBasePathTests()
  839. {
  840. return array(
  841. // same paths
  842. array(array('/base/path', '/base/path'), '/base/path'),
  843. array(array('C:/base/path', 'C:/base/path'), 'C:/base/path'),
  844. array(array('C:\\base\\path', 'C:\\base\\path'), 'C:/base/path'),
  845. array(array('C:/base/path', 'C:\\base\\path'), 'C:/base/path'),
  846. array(array('phar:///base/path', 'phar:///base/path'), 'phar:///base/path'),
  847. array(array('phar://C:/base/path', 'phar://C:/base/path'), 'phar://C:/base/path'),
  848. // trailing slash
  849. array(array('/base/path/', '/base/path'), '/base/path'),
  850. array(array('C:/base/path/', 'C:/base/path'), 'C:/base/path'),
  851. array(array('C:\\base\\path\\', 'C:\\base\\path'), 'C:/base/path'),
  852. array(array('C:/base/path/', 'C:\\base\\path'), 'C:/base/path'),
  853. array(array('phar:///base/path/', 'phar:///base/path'), 'phar:///base/path'),
  854. array(array('phar://C:/base/path/', 'phar://C:/base/path'), 'phar://C:/base/path'),
  855. array(array('/base/path', '/base/path/'), '/base/path'),
  856. array(array('C:/base/path', 'C:/base/path/'), 'C:/base/path'),
  857. array(array('C:\\base\\path', 'C:\\base\\path\\'), 'C:/base/path'),
  858. array(array('C:/base/path', 'C:\\base\\path\\'), 'C:/base/path'),
  859. array(array('phar:///base/path', 'phar:///base/path/'), 'phar:///base/path'),
  860. array(array('phar://C:/base/path', 'phar://C:/base/path/'), 'phar://C:/base/path'),
  861. // first in second
  862. array(array('/base/path/sub', '/base/path'), '/base/path'),
  863. array(array('C:/base/path/sub', 'C:/base/path'), 'C:/base/path'),
  864. array(array('C:\\base\\path\\sub', 'C:\\base\\path'), 'C:/base/path'),
  865. array(array('C:/base/path/sub', 'C:\\base\\path'), 'C:/base/path'),
  866. array(array('phar:///base/path/sub', 'phar:///base/path'), 'phar:///base/path'),
  867. array(array('phar://C:/base/path/sub', 'phar://C:/base/path'), 'phar://C:/base/path'),
  868. // second in first
  869. array(array('/base/path', '/base/path/sub'), '/base/path'),
  870. array(array('C:/base/path', 'C:/base/path/sub'), 'C:/base/path'),
  871. array(array('C:\\base\\path', 'C:\\base\\path\\sub'), 'C:/base/path'),
  872. array(array('C:/base/path', 'C:\\base\\path\\sub'), 'C:/base/path'),
  873. array(array('phar:///base/path', 'phar:///base/path/sub'), 'phar:///base/path'),
  874. array(array('phar://C:/base/path', 'phar://C:/base/path/sub'), 'phar://C:/base/path'),
  875. // first is prefix
  876. array(array('/base/path/di', '/base/path/dir'), '/base/path'),
  877. array(array('C:/base/path/di', 'C:/base/path/dir'), 'C:/base/path'),
  878. array(array('C:\\base\\path\\di', 'C:\\base\\path\\dir'), 'C:/base/path'),
  879. array(array('C:/base/path/di', 'C:\\base\\path\\dir'), 'C:/base/path'),
  880. array(array('phar:///base/path/di', 'phar:///base/path/dir'), 'phar:///base/path'),
  881. array(array('phar://C:/base/path/di', 'phar://C:/base/path/dir'), 'phar://C:/base/path'),
  882. // second is prefix
  883. array(array('/base/path/dir', '/base/path/di'), '/base/path'),
  884. array(array('C:/base/path/dir', 'C:/base/path/di'), 'C:/base/path'),
  885. array(array('C:\\base\\path\\dir', 'C:\\base\\path\\di'), 'C:/base/path'),
  886. array(array('C:/base/path/dir', 'C:\\base\\path\\di'), 'C:/base/path'),
  887. array(array('phar:///base/path/dir', 'phar:///base/path/di'), 'phar:///base/path'),
  888. array(array('phar://C:/base/path/dir', 'phar://C:/base/path/di'), 'phar://C:/base/path'),
  889. // root is common base path
  890. array(array('/first', '/second'), '/'),
  891. array(array('C:/first', 'C:/second'), 'C:/'),
  892. array(array('C:\\first', 'C:\\second'), 'C:/'),
  893. array(array('C:/first', 'C:\\second'), 'C:/'),
  894. array(array('phar:///first', 'phar:///second'), 'phar:///'),
  895. array(array('phar://C:/first', 'phar://C:/second'), 'phar://C:/'),
  896. // windows vs unix
  897. array(array('/base/path', 'C:/base/path'), null),
  898. array(array('C:/base/path', '/base/path'), null),
  899. array(array('/base/path', 'C:\\base\\path'), null),
  900. array(array('phar:///base/path', 'phar://C:/base/path'), null),
  901. // different partitions
  902. array(array('C:/base/path', 'D:/base/path'), null),
  903. array(array('C:/base/path', 'D:\\base\\path'), null),
  904. array(array('C:\\base\\path', 'D:\\base\\path'), null),
  905. array(array('phar://C:/base/path', 'phar://D:/base/path'), null),
  906. // three paths
  907. array(array('/base/path/foo', '/base/path', '/base/path/bar'), '/base/path'),
  908. array(array('C:/base/path/foo', 'C:/base/path', 'C:/base/path/bar'), 'C:/base/path'),
  909. array(array('C:\\base\\path\\foo', 'C:\\base\\path', 'C:\\base\\path\\bar'), 'C:/base/path'),
  910. array(array('C:/base/path//foo', 'C:/base/path', 'C:\\base\\path\\bar'), 'C:/base/path'),
  911. array(array('phar:///base/path/foo', 'phar:///base/path', 'phar:///base/path/bar'), 'phar:///base/path'),
  912. array(array('phar://C:/base/path/foo', 'phar://C:/base/path', 'phar://C:/base/path/bar'), 'phar://C:/base/path'),
  913. // three paths with root
  914. array(array('/base/path/foo', '/', '/base/path/bar'), '/'),
  915. array(array('C:/base/path/foo', 'C:/', 'C:/base/path/bar'), 'C:/'),
  916. array(array('C:\\base\\path\\foo', 'C:\\', 'C:\\base\\path\\bar'), 'C:/'),
  917. array(array('C:/base/path//foo', 'C:/', 'C:\\base\\path\\bar'), 'C:/'),
  918. array(array('phar:///base/path/foo', 'phar:///', 'phar:///base/path/bar'), 'phar:///'),
  919. array(array('phar://C:/base/path/foo', 'phar://C:/', 'phar://C:/base/path/bar'), 'phar://C:/'),
  920. // three paths, different roots
  921. array(array('/base/path/foo', 'C:/base/path', '/base/path/bar'), null),
  922. array(array('/base/path/foo', 'C:\\base\\path', '/base/path/bar'), null),
  923. array(array('C:/base/path/foo', 'D:/base/path', 'C:/base/path/bar'), null),
  924. array(array('C:\\base\\path\\foo', 'D:\\base\\path', 'C:\\base\\path\\bar'), null),
  925. array(array('C:/base/path//foo', 'D:/base/path', 'C:\\base\\path\\bar'), null),
  926. array(array('phar:///base/path/foo', 'phar://C:/base/path', 'phar:///base/path/bar'), null),
  927. array(array('phar://C:/base/path/foo', 'phar://D:/base/path', 'phar://C:/base/path/bar'), null),
  928. // only one path
  929. array(array('/base/path'), '/base/path'),
  930. array(array('C:/base/path'), 'C:/base/path'),
  931. array(array('C:\\base\\path'), 'C:/base/path'),
  932. array(array('phar:///base/path'), 'phar:///base/path'),
  933. array(array('phar://C:/base/path'), 'phar://C:/base/path'),
  934. );
  935. }
  936. /**
  937. * @dataProvider provideGetLongestCommonBasePathTests
  938. */
  939. public function testGetLongestCommonBasePath(array $paths, $basePath)
  940. {
  941. $this->assertSame($basePath, Path::getLongestCommonBasePath($paths));
  942. }
  943. /**
  944. * @expectedException \InvalidArgumentException
  945. * @expectedExceptionMessage The paths must be strings. Got: array
  946. */
  947. public function testGetLongestCommonBasePathFailsIfInvalidPath()
  948. {
  949. Path::getLongestCommonBasePath(array(array()));
  950. }
  951. public function provideIsBasePathTests()
  952. {
  953. return array(
  954. // same paths
  955. array('/base/path', '/base/path', true),
  956. array('C:/base/path', 'C:/base/path', true),
  957. array('C:\\base\\path', 'C:\\base\\path', true),
  958. array('C:/base/path', 'C:\\base\\path', true),
  959. array('phar:///base/path', 'phar:///base/path', true),
  960. array('phar://C:/base/path', 'phar://C:/base/path', true),
  961. // trailing slash
  962. array('/base/path/', '/base/path', true),
  963. array('C:/base/path/', 'C:/base/path', true),
  964. array('C:\\base\\path\\', 'C:\\base\\path', true),
  965. array('C:/base/path/', 'C:\\base\\path', true),
  966. array('phar:///base/path/', 'phar:///base/path', true),
  967. array('phar://C:/base/path/', 'phar://C:/base/path', true),
  968. array('/base/path', '/base/path/', true),
  969. array('C:/base/path', 'C:/base/path/', true),
  970. array('C:\\base\\path', 'C:\\base\\path\\', true),
  971. array('C:/base/path', 'C:\\base\\path\\', true),
  972. array('phar:///base/path', 'phar:///base/path/', true),
  973. array('phar://C:/base/path', 'phar://C:/base/path/', true),
  974. // first in second
  975. array('/base/path/sub', '/base/path', false),
  976. array('C:/base/path/sub', 'C:/base/path', false),
  977. array('C:\\base\\path\\sub', 'C:\\base\\path', false),
  978. array('C:/base/path/sub', 'C:\\base\\path', false),
  979. array('phar:///base/path/sub', 'phar:///base/path', false),
  980. array('phar://C:/base/path/sub', 'phar://C:/base/path', false),
  981. // second in first
  982. array('/base/path', '/base/path/sub', true),
  983. array('C:/base/path', 'C:/base/path/sub', true),
  984. array('C:\\base\\path', 'C:\\base\\path\\sub', true),
  985. array('C:/base/path', 'C:\\base\\path\\sub', true),
  986. array('phar:///base/path', 'phar:///base/path/sub', true),
  987. array('phar://C:/base/path', 'phar://C:/base/path/sub', true),
  988. // first is prefix
  989. array('/base/path/di', '/base/path/dir', false),
  990. array('C:/base/path/di', 'C:/base/path/dir', false),
  991. array('C:\\base\\path\\di', 'C:\\base\\path\\dir', false),
  992. array('C:/base/path/di', 'C:\\base\\path\\dir', false),
  993. array('phar:///base/path/di', 'phar:///base/path/dir', false),
  994. array('phar://C:/base/path/di', 'phar://C:/base/path/dir', false),
  995. // second is prefix
  996. array('/base/path/dir', '/base/path/di', false),
  997. array('C:/base/path/dir', 'C:/base/path/di', false),
  998. array('C:\\base\\path\\dir', 'C:\\base\\path\\di', false),
  999. array('C:/base/path/dir', 'C:\\base\\path\\di', false),
  1000. array('phar:///base/path/dir', 'phar:///base/path/di', false),
  1001. array('phar://C:/base/path/dir', 'phar://C:/base/path/di', false),
  1002. // root
  1003. array('/', '/second', true),
  1004. array('C:/', 'C:/second', true),
  1005. array('C:', 'C:/second', true),
  1006. array('C:\\', 'C:\\second', true),
  1007. array('C:/', 'C:\\second', true),
  1008. array('phar:///', 'phar:///second', true),
  1009. array('phar://C:/', 'phar://C:/second', true),
  1010. // windows vs unix
  1011. array('/base/path', 'C:/base/path', false),
  1012. array('C:/base/path', '/base/path', false),
  1013. array('/base/path', 'C:\\base\\path', false),
  1014. array('/base/path', 'phar:///base/path', false),
  1015. array('phar:///base/path', 'phar://C:/base/path', false),
  1016. // different partitions
  1017. array('C:/base/path', 'D:/base/path', false),
  1018. array('C:/base/path', 'D:\\base\\path', false),
  1019. array('C:\\base\\path', 'D:\\base\\path', false),
  1020. array('C:/base/path', 'phar://C:/base/path', false),
  1021. array('phar://C:/base/path', 'phar://D:/base/path', false),
  1022. );
  1023. }
  1024. /**
  1025. * @dataProvider provideIsBasePathTests
  1026. */
  1027. public function testIsBasePath($path, $ofPath, $result)
  1028. {
  1029. $this->assertSame($result, Path::isBasePath($path, $ofPath));
  1030. }
  1031. /**
  1032. * @expectedException \InvalidArgumentException
  1033. * @expectedExceptionMessage The base path must be a string. Got: array
  1034. */
  1035. public function testIsBasePathFailsIfInvalidBasePath()
  1036. {
  1037. Path::isBasePath(array(), '/base/path');
  1038. }
  1039. /**
  1040. * @expectedException \InvalidArgumentException
  1041. * @expectedExceptionMessage The path must be a string. Got: array
  1042. */
  1043. public function testIsBasePathFailsIfInvalidPath()
  1044. {
  1045. Path::isBasePath('/base/path', array());
  1046. }
  1047. public function provideJoinTests()
  1048. {
  1049. return array(
  1050. array('', '', ''),
  1051. array('/path/to/test', '', '/path/to/test'),
  1052. array('/path/to//test', '', '/path/to/test'),
  1053. array('', '/path/to/test', '/path/to/test'),
  1054. array('', '/path/to//test', '/path/to/test'),
  1055. array('/path/to/test', 'subdir', '/path/to/test/subdir'),
  1056. array('/path/to/test/', 'subdir', '/path/to/test/subdir'),
  1057. array('/path/to/test', '/subdir', '/path/to/test/subdir'),
  1058. array('/path/to/test/', '/subdir', '/path/to/test/subdir'),
  1059. array('/path/to/test', './subdir', '/path/to/test/subdir'),
  1060. array('/path/to/test/', './subdir', '/path/to/test/subdir'),
  1061. array('/path/to/test/', '../parentdir', '/path/to/parentdir'),
  1062. array('/path/to/test', '../parentdir', '/path/to/parentdir'),
  1063. array('path/to/test/', '/subdir', 'path/to/test/subdir'),
  1064. array('path/to/test', '/subdir', 'path/to/test/subdir'),
  1065. array('../path/to/test', '/subdir', '../path/to/test/subdir'),
  1066. array('path', '../../subdir', '../subdir'),
  1067. array('/path', '../../subdir', '/subdir'),
  1068. array('../path', '../../subdir', '../../subdir'),
  1069. array(array('/path/to/test', 'subdir'), '', '/path/to/test/subdir'),
  1070. array(array('/path/to/test', '/subdir'), '', '/path/to/test/subdir'),
  1071. array(array('/path/to/test/', 'subdir'), '', '/path/to/test/subdir'),
  1072. array(array('/path/to/test/', '/subdir'), '', '/path/to/test/subdir'),
  1073. array(array('/path'), '', '/path'),
  1074. array(array('/path', 'to', '/test'), '', '/path/to/test'),
  1075. array(array('/path', '', '/test'), '', '/path/test'),
  1076. array(array('path', 'to', 'test'), '', 'path/to/test'),
  1077. array(array(), '', ''),
  1078. array('base/path', 'to/test', 'base/path/to/test'),
  1079. array('C:\\path\\to\\test', 'subdir', 'C:/path/to/test/subdir'),
  1080. array('C:\\path\\to\\test\\', 'subdir', 'C:/path/to/test/subdir'),
  1081. array('C:\\path\\to\\test', '/subdir', 'C:/path/to/test/subdir'),
  1082. array('C:\\path\\to\\test\\', '/subdir', 'C:/path/to/test/subdir'),
  1083. array('/', 'subdir', '/subdir'),
  1084. array('/', '/subdir', '/subdir'),
  1085. array('C:/', 'subdir', 'C:/subdir'),
  1086. array('C:/', '/subdir', 'C:/subdir'),
  1087. array('C:\\', 'subdir', 'C:/subdir'),
  1088. array('C:\\', '/subdir', 'C:/subdir'),
  1089. array('C:', 'subdir', 'C:/subdir'),
  1090. array('C:', '/subdir', 'C:/subdir'),
  1091. array('phar://', '/path/to/test', 'phar:///path/to/test'),
  1092. array('phar:///', '/path/to/test', 'phar:///path/to/test'),
  1093. array('phar:///path/to/test', 'subdir', 'phar:///path/to/test/subdir'),
  1094. array('phar:///path/to/test', 'subdir/', 'phar:///path/to/test/subdir'),
  1095. array('phar:///path/to/test', '/subdir', 'phar:///path/to/test/subdir'),
  1096. array('phar:///path/to/test/', 'subdir', 'phar:///path/to/test/subdir'),
  1097. array('phar:///path/to/test/', '/subdir', 'phar:///path/to/test/subdir'),
  1098. array('phar://', 'C:/path/to/test', 'phar://C:/path/to/test'),
  1099. array('phar://', 'C:\\path\\to\\test', 'phar://C:/path/to/test'),
  1100. array('phar://C:/path/to/test', 'subdir', 'phar://C:/path/to/test/subdir'),
  1101. array('phar://C:/path/to/test', 'subdir/', 'phar://C:/path/to/test/subdir'),
  1102. array('phar://C:/path/to/test', '/subdir', 'phar://C:/path/to/test/subdir'),
  1103. array('phar://C:/path/to/test/', 'subdir', 'phar://C:/path/to/test/subdir'),
  1104. array('phar://C:/path/to/test/', '/subdir', 'phar://C:/path/to/test/subdir'),
  1105. array('phar://C:', 'path/to/test', 'phar://C:/path/to/test'),
  1106. array('phar://C:', '/path/to/test', 'phar://C:/path/to/test'),
  1107. array('phar://C:/', 'path/to/test', 'phar://C:/path/to/test'),
  1108. array('phar://C:/', '/path/to/test', 'phar://C:/path/to/test'),
  1109. );
  1110. }
  1111. /**
  1112. * @dataProvider provideJoinTests
  1113. */
  1114. public function testJoin($path1, $path2, $result)
  1115. {
  1116. $this->assertSame($result, Path::join($path1, $path2));
  1117. }
  1118. public function testJoinVarArgs()
  1119. {
  1120. $this->assertSame('/path', Path::join('/path'));
  1121. $this->assertSame('/path/to', Path::join('/path', 'to'));
  1122. $this->assertSame('/path/to/test', Path::join('/path', 'to', '/test'));
  1123. $this->assertSame('/path/to/test/subdir', Path::join('/path', 'to', '/test', 'subdir/'));
  1124. }
  1125. /**
  1126. * @expectedException \InvalidArgumentException
  1127. * @expectedExceptionMessage The paths must be strings. Got: array
  1128. */
  1129. public function testJoinFailsIfInvalidPath()
  1130. {
  1131. Path::join('/path', array());
  1132. }
  1133. /**
  1134. * @expectedException \RuntimeException
  1135. * @expectedExceptionMessage Your environment or operation system isn't supported
  1136. */
  1137. public function testGetHomeDirectoryFailsIfNotSupportedOperationSystem()
  1138. {
  1139. putenv('HOME=');
  1140. Path::getHomeDirectory();
  1141. }
  1142. public function testGetHomeDirectoryForUnix()
  1143. {
  1144. $this->assertEquals('/home/webmozart', Path::getHomeDirectory());
  1145. }
  1146. public function testGetHomeDirectoryForWindows()
  1147. {
  1148. putenv('HOME=');
  1149. putenv('HOMEDRIVE=C:');
  1150. putenv('HOMEPATH=/users/webmozart');
  1151. $this->assertEquals('C:/users/webmozart', Path::getHomeDirectory());
  1152. }
  1153. public function testNormalize()
  1154. {
  1155. $this->assertSame('C:/Foo/Bar/test', Path::normalize('C:\\Foo\\Bar/test'));
  1156. }
  1157. /**
  1158. * @expectedException \InvalidArgumentException
  1159. */
  1160. public function testNormalizeFailsIfNoString()
  1161. {
  1162. Path::normalize(true);
  1163. }
  1164. }