UniformResourceLocatorTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
  3. class UniformResourceLocatorTest extends PHPUnit_Framework_TestCase
  4. {
  5. /**
  6. * @var UniformResourceLocator
  7. */
  8. static protected $locator;
  9. public static function setUpBeforeClass()
  10. {
  11. // Share locator in all tests.
  12. self::$locator = new UniformResourceLocator(__DIR__ . '/data');
  13. }
  14. public function testGetBase()
  15. {
  16. $this->assertEquals(__DIR__ . '/data', self::$locator->getBase());
  17. }
  18. /**
  19. * @param $scheme
  20. * @param $path
  21. * @param $lookup
  22. *
  23. * @dataProvider addPathProvider
  24. */
  25. public function testAddPath($scheme, $path, $lookup)
  26. {
  27. $locator = self::$locator;
  28. $this->assertFalse($locator->schemeExists($scheme));
  29. $locator->addPath($scheme, $path, $lookup);
  30. $this->assertTrue($locator->schemeExists($scheme));
  31. }
  32. public function addPathProvider() {
  33. return [
  34. ['base', '', 'base'],
  35. ['local', '', 'local'],
  36. ['override', '', 'override'],
  37. ['all', '', ['override://all', 'local://all', 'base://all']],
  38. ];
  39. }
  40. /**
  41. * @depends testAddPath
  42. */
  43. public function testGetSchemes()
  44. {
  45. $this->assertEquals(
  46. ['base', 'local', 'override', 'all'],
  47. self::$locator->getSchemes()
  48. );
  49. }
  50. /**
  51. * @depends testAddPath
  52. * @dataProvider getPathsProvider
  53. */
  54. public function testGetPaths($scheme, $expected)
  55. {
  56. $locator = self::$locator;
  57. $this->assertEquals($expected, $locator->getPaths($scheme));
  58. }
  59. public function getPathsProvider() {
  60. return [
  61. ['base', ['' => ['base']]],
  62. ['local', ['' => ['local']]],
  63. ['override', ['' => ['override']]],
  64. ['all', ['' => [['override', 'all'], ['local', 'all'], ['base', 'all']]]],
  65. ['fail', []]
  66. ];
  67. }
  68. /**
  69. * @depends testAddPath
  70. */
  71. public function testSchemeExists()
  72. {
  73. $locator = self::$locator;
  74. // Partially tested in addPath() tests.
  75. $this->assertFalse($locator->schemeExists('foo'));
  76. $this->assertFalse($locator->schemeExists('file'));
  77. }
  78. /**
  79. * @depends testAddPath
  80. */
  81. public function testGetIterator()
  82. {
  83. $locator = self::$locator;
  84. $this->assertInstanceOf(
  85. 'RocketTheme\Toolbox\ResourceLocator\UniformResourceIterator',
  86. $locator->getIterator('all://')
  87. );
  88. $this->setExpectedException('InvalidArgumentException', 'Invalid resource fail://');
  89. $locator->getIterator('fail://');
  90. }
  91. /**
  92. * @depends testAddPath
  93. */
  94. public function testGetRecursiveIterator()
  95. {
  96. $locator = self::$locator;
  97. $this->assertInstanceOf(
  98. 'RocketTheme\Toolbox\ResourceLocator\RecursiveUniformResourceIterator',
  99. $locator->getRecursiveIterator('all://')
  100. );
  101. $this->setExpectedException('InvalidArgumentException', 'Invalid resource fail://');
  102. $locator->getRecursiveIterator('fail://');
  103. }
  104. /**
  105. * @depends testAddPath
  106. */
  107. public function testIsStream($uri)
  108. {
  109. $locator = self::$locator;
  110. // Existing file.
  111. $this->assertEquals(true, $locator->isStream('all://base.txt'));
  112. // Non-existing file.
  113. $this->assertEquals(true, $locator->isStream('all://bar.txt'));
  114. // Unknown uri type.
  115. $this->assertEquals(false, $locator->isStream('fail://base.txt'));
  116. // Bad uri.
  117. $this->assertEquals(false, $locator->isStream('fail://../base.txt'));
  118. }
  119. /**
  120. * @dataProvider normalizeProvider
  121. */
  122. public function testNormalize($uri, $path)
  123. {
  124. $locator = self::$locator;
  125. $this->assertEquals($path, $locator->normalize($uri));
  126. }
  127. /**
  128. * @depends testAddPath
  129. * @dataProvider findResourcesProvider
  130. */
  131. public function testFindResource($uri, $paths)
  132. {
  133. $locator = self::$locator;
  134. $path = $paths ? reset($paths) : false;
  135. $fullPath = !$path ? false : __DIR__ . "/data/{$path}";
  136. $this->assertEquals($fullPath, $locator->findResource($uri));
  137. $this->assertEquals($path, $locator->findResource($uri, false));
  138. }
  139. /**
  140. * @depends testAddPath
  141. * @dataProvider findResourcesProvider
  142. */
  143. public function testFindResources($uri, $paths)
  144. {
  145. $locator = self::$locator;
  146. $this->assertEquals($paths, $locator->findResources($uri, false));
  147. }
  148. /**
  149. * @depends testFindResource
  150. * @dataProvider findResourcesProvider
  151. */
  152. public function testInvoke($uri, $paths)
  153. {
  154. $locator = self::$locator;
  155. $path = $paths ? reset($paths) : false;
  156. $fullPath = !$path ? false : __DIR__ . "/data/{$path}";
  157. $this->assertEquals($fullPath, $locator($uri));
  158. }
  159. public function normalizeProvider() {
  160. return [
  161. ['', ''],
  162. ['./', ''],
  163. ['././/./', ''],
  164. ['././/../', false],
  165. ['/', '/'],
  166. ['//', '/'],
  167. ['///', '/'],
  168. ['/././', '/'],
  169. ['foo', 'foo'],
  170. ['/foo', '/foo'],
  171. ['//foo', '/foo'],
  172. ['/foo/', '/foo/'],
  173. ['//foo//', '/foo/'],
  174. ['path/to/file.txt', 'path/to/file.txt'],
  175. ['path/to/../file.txt', 'path/file.txt'],
  176. ['path/to/../../file.txt', 'file.txt'],
  177. ['path/to/../../../file.txt', false],
  178. ['/path/to/file.txt', '/path/to/file.txt'],
  179. ['/path/to/../file.txt', '/path/file.txt'],
  180. ['/path/to/../../file.txt', '/file.txt'],
  181. ['/path/to/../../../file.txt', false],
  182. ['c:\\', 'c:/'],
  183. ['c:\\path\\to\file.txt', 'c:/path/to/file.txt'],
  184. ['c:\\path\\to\../file.txt', 'c:/path/file.txt'],
  185. ['c:\\path\\to\../../file.txt', 'c:/file.txt'],
  186. ['c:\\path\\to\../../../file.txt', false],
  187. ['stream://path/to/file.txt', 'stream://path/to/file.txt'],
  188. ['stream://path/to/../file.txt', 'stream://path/file.txt'],
  189. ['stream://path/to/../../file.txt', 'stream://file.txt'],
  190. ['stream://path/to/../../../file.txt', false],
  191. ];
  192. }
  193. public function findResourcesProvider() {
  194. return [
  195. ['all://base.txt', ['base/all/base.txt']],
  196. ['all://base_all.txt', ['override/all/base_all.txt', 'local/all/base_all.txt', 'base/all/base_all.txt']],
  197. ['all://base_local.txt', ['local/all/base_local.txt', 'base/all/base_local.txt']],
  198. ['all://base_override.txt', ['override/all/base_override.txt', 'base/all/base_override.txt']],
  199. ['all://local.txt', ['local/all/local.txt']],
  200. ['all://local_override.txt', ['override/all/local_override.txt', 'local/all/local_override.txt']],
  201. ['all://override.txt', ['override/all/override.txt']],
  202. ['all://missing.txt', []],
  203. ['all://asdf/../base.txt', ['base/all/base.txt']],
  204. ];
  205. }
  206. /**
  207. * @depends testAddPath
  208. */
  209. public function testMergeResources()
  210. {
  211. $locator = self::$locator;
  212. }
  213. public function testReset()
  214. {
  215. $locator = self::$locator;
  216. }
  217. public function testResetScheme()
  218. {
  219. $locator = self::$locator;
  220. }
  221. }