PageRenderTime 53ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/FinderTest.php

https://github.com/orchestral/extension
PHP | 277 lines | 195 code | 32 blank | 50 comment | 0 complexity | 8c8ae8f2a5c72a375a7e15ac53ead88f MD5 | raw file
  1. <?php namespace Orchestra\Extension\TestCase;
  2. use Mockery as m;
  3. use Illuminate\Container\Container;
  4. use Illuminate\Support\Collection;
  5. use Orchestra\Extension\Finder;
  6. class FinderTest extends \PHPUnit_Framework_TestCase
  7. {
  8. /**
  9. * Teardown the test environment.
  10. */
  11. public function tearDown()
  12. {
  13. m::close();
  14. }
  15. /**
  16. * Test constructing a new Orchestra\Extension\Finder.
  17. *
  18. * @test
  19. */
  20. public function testConstructMethod()
  21. {
  22. $config['path.app'] = '/foo/app';
  23. $config['path.base'] = '/foo/path';
  24. $stub = new Finder(m::mock('\Illuminate\Filesystem\Filesystem'), $config);
  25. $refl = new \ReflectionObject($stub);
  26. $paths = $refl->getProperty('paths');
  27. $paths->setAccessible(true);
  28. $this->assertEquals(
  29. array('/foo/app/', '/foo/path/vendor/*/*/', '/foo/path/workbench/*/*/'),
  30. $paths->getValue($stub)
  31. );
  32. $stub->addPath('/foo/public');
  33. $this->assertEquals(
  34. array('/foo/app/', '/foo/path/vendor/*/*/', '/foo/path/workbench/*/*/', '/foo/public'),
  35. $paths->getValue($stub)
  36. );
  37. }
  38. /**
  39. * Test Orchestra\Extension\Finder::detect() method.
  40. *
  41. * @test
  42. */
  43. public function testDetectMethod()
  44. {
  45. $config['path.app'] = '/foo/app';
  46. $config['path.base'] = '/foo/path';
  47. $files = m::mock('\Illuminate\Filesystem\Filesystem');
  48. $files->shouldReceive('glob')->once()
  49. ->with('/foo/app/orchestra.json')->andReturn(array('/foo/app/orchestra.json'))
  50. ->shouldReceive('get')->once()
  51. ->with('/foo/app/orchestra.json')->andReturn('{"name":"Application"}')
  52. ->shouldReceive('glob')->once()
  53. ->with('/foo/path/vendor/*/*/orchestra.json')
  54. ->andReturn(array('/foo/path/vendor/laravel/framework/orchestra.json', '/foo/orchestra.js'))
  55. ->shouldReceive('get')->once()
  56. ->with('/foo/path/vendor/laravel/framework/orchestra.json')
  57. ->andReturn('{"name":"Laravel Framework","path": "vendor::laravel/framework"}')
  58. ->shouldReceive('glob')->once()
  59. ->with('/foo/path/workbench/*/*/orchestra.json')->andReturn(array());
  60. $stub = new Finder($files, $config);
  61. $expected = new Collection(array(
  62. 'laravel/framework' => array(
  63. 'path' => 'vendor::laravel/framework',
  64. 'source-path' => 'vendor::laravel/framework',
  65. 'name' => 'Laravel Framework',
  66. 'description' => null,
  67. 'author' => null,
  68. 'url' => null,
  69. 'version' => '>0',
  70. 'config' => array(),
  71. 'autoload' => array(),
  72. 'provide' => array(),
  73. ),
  74. 'app' => array(
  75. 'path' => 'app::',
  76. 'source-path' => 'app::',
  77. 'name' => 'Application',
  78. 'description' => null,
  79. 'author' => null,
  80. 'url' => null,
  81. 'version' => '>0',
  82. 'config' => array(),
  83. 'autoload' => array(),
  84. 'provide' => array(),
  85. ),
  86. ));
  87. $this->assertEquals($expected, $stub->detect());
  88. }
  89. /**
  90. * Test Orchestra\Extension\Finder::detect() method giveb reserved name
  91. * throws exception.
  92. *
  93. * @expectedException \RuntimeException
  94. */
  95. public function testDetectMethodGivenReservedNameThrowsException()
  96. {
  97. $config['path.app'] = '/foo/app';
  98. $config['path.base'] = '/foo/path';
  99. $files = m::mock('\Illuminate\Filesystem\Filesystem');
  100. $files->shouldReceive('glob')->once()
  101. ->with('/foo/app/orchestra.json')->andReturn(array('/foo/app/orchestra.json'))
  102. ->shouldReceive('get')->once()
  103. ->with('/foo/app/orchestra.json')->andReturn('{"name":"Application"}')
  104. ->shouldReceive('glob')->once()
  105. ->with('/foo/path/vendor/*/*/orchestra.json')
  106. ->andReturn(array('/foo/path/vendor/orchestra/foundation/orchestra.json'));
  107. $stub = new Finder($files, $config);
  108. $stub->detect();
  109. }
  110. /**
  111. * Test Orchestra\Extension\Finder::detect() method throws
  112. * exception when unable to parse json manifest file.
  113. *
  114. * @expectedException \Orchestra\Extension\ManifestRuntimeException
  115. */
  116. public function testDetectMethodThrowsException()
  117. {
  118. $config['path.app'] = '/foo/app';
  119. $config['path.base'] = '/foo/path';
  120. $files = m::mock('\Illuminate\Filesystem\Filesystem');
  121. $files->shouldReceive('glob')
  122. ->with('/foo/app/orchestra.json')->once()
  123. ->andReturn(array())
  124. ->shouldReceive('glob')
  125. ->with('/foo/path/vendor/*/*/orchestra.json')->once()
  126. ->andReturn(array('/foo/path/vendor/laravel/framework/orchestra.json'))
  127. ->shouldReceive('glob')
  128. ->with('/foo/path/workbench/*/*/orchestra.json')->never()
  129. ->andReturn(array())
  130. ->shouldReceive('get')
  131. ->with('/foo/path/vendor/laravel/framework/orchestra.json')->once()
  132. ->andReturn('{"name":"Laravel Framework}');
  133. with(new Finder($files, $config))->detect();
  134. }
  135. /**
  136. * Test Orchestra\Extension\Finder::guessExtensionPath() method.
  137. *
  138. * @test
  139. * @dataProvider extensionPathProvider
  140. */
  141. public function testGuessExtensionPathMethod($output, $expected)
  142. {
  143. $config['path.app'] = '/foo/path/app';
  144. $config['path.base'] = '/foo/path';
  145. $stub = new Finder(m::mock('\Illuminate\Filesystem\Filesystem'), $config);
  146. $this->assertEquals($expected, $stub->guessExtensionPath($output));
  147. }
  148. /**
  149. * Test Orchestra\Extension\Finder::resolveExtensionNamespace() method
  150. * with mixed directory separator.
  151. *
  152. * @test
  153. * @dataProvider extensionManifestProvider
  154. */
  155. public function testResolveExtensionNamespace($path, $expected, $output)
  156. {
  157. $config['path.app'] = '/foo/path/app';
  158. $config['path.base'] = '/foo/path';
  159. $stub = new Finder(m::mock('\Illuminate\Filesystem\Filesystem'), $config);
  160. $this->assertEquals($expected, $stub->resolveExtensionNamespace($output));
  161. }
  162. /**
  163. * Test Orchestra\Extension\Finder::resolveExtensionPath() method.
  164. *
  165. * @test
  166. * @dataProvider extensionPathProvider
  167. */
  168. public function testResolveExtensionPathMethod($expected, $output)
  169. {
  170. $config['path.app'] = '/foo/path/app';
  171. $config['path.base'] = '/foo/path';
  172. $stub = new Finder(m::mock('\Illuminate\Filesystem\Filesystem'), $config);
  173. $this->assertEquals($expected, $stub->resolveExtensionPath($output));
  174. }
  175. /**
  176. * Extension Path provider.
  177. */
  178. public function extensionManifestProvider()
  179. {
  180. $windowsPath['path.app'] = 'c:\www\laravel\app';
  181. $windowsPath['path.base'] = 'c:\www\laravel';
  182. $unixPath['path.app'] = '/var/www/laravel/app';
  183. $unixPath['path.base'] = '/var/www/laravel';
  184. return array(
  185. array(
  186. $windowsPath,
  187. array("laravel", "app"),
  188. "c:\www\laravel\app/orchestra.json",
  189. ),
  190. array(
  191. $windowsPath,
  192. array("orchestra", "control"),
  193. "c:\www\laravel\vendor/orchestra/control/orchestra.json",
  194. ),
  195. array(
  196. $windowsPath,
  197. array("orchestra", "story"),
  198. "c:\www\laravel\vendor/orchestra/story/orchestra.json",
  199. ),
  200. array(
  201. $windowsPath,
  202. array("laravel", "app"),
  203. "c:\www\laravel\app\orchestra.json",
  204. ),
  205. array(
  206. $windowsPath,
  207. array("orchestra", "control"),
  208. "c:\www\laravel\vendor\orchestra\control\orchestra.json",
  209. ),
  210. array(
  211. $windowsPath,
  212. array("orchestra", "story"),
  213. "c:\www\laravel\vendor\orchestra\story\orchestra.json",
  214. ),
  215. array(
  216. $unixPath,
  217. array("laravel", "app"),
  218. "/var/www/laravel/app/orchestra.json",
  219. ),
  220. array(
  221. $unixPath,
  222. array("orchestra", "control"),
  223. "/var/www/laravel/vendor/orchestra/control/orchestra.json",
  224. ),
  225. array(
  226. $unixPath,
  227. array("orchestra", "story"),
  228. "/var/www/laravel/vendor/orchestra/story/orchestra.json",
  229. ),
  230. );
  231. }
  232. /**
  233. * Extension Path provider.
  234. */
  235. public function extensionPathProvider()
  236. {
  237. return array(
  238. array("foobar", "foobar"),
  239. array("/foo/path/app/foobar", "app::foobar"),
  240. array("/foo/path/vendor/foobar", "vendor::foobar"),
  241. array("/foo/path/workbench/foobar", "workbench::foobar"),
  242. array("/foo/path/foobar", "base::foobar"),
  243. );
  244. }
  245. }