Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

350 lines
11KB

  1. <?php
  2. /*
  3. * This file is part of the Fxp Composer Asset Plugin package.
  4. *
  5. * (c) François Pluchino <francois.pluchino@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 Fxp\Composer\AssetPlugin\Tests;
  11. use Composer\Composer;
  12. use Composer\Config;
  13. use Composer\Installer\InstallationManager;
  14. use Composer\Installer\InstallerEvent;
  15. use Composer\IO\IOInterface;
  16. use Composer\Plugin\CommandEvent;
  17. use Composer\Repository\RepositoryManager;
  18. use Composer\Util\Filesystem;
  19. use Fxp\Composer\AssetPlugin\AssetEvents;
  20. use Fxp\Composer\AssetPlugin\Event\VcsRepositoryEvent;
  21. use Fxp\Composer\AssetPlugin\FxpAssetPlugin;
  22. /**
  23. * Tests of asset plugin.
  24. *
  25. * @author François Pluchino <francois.pluchino@gmail.com>
  26. */
  27. class FxpAssetPluginTest extends \PHPUnit_Framework_TestCase
  28. {
  29. /**
  30. * @var FxpAssetPlugin
  31. */
  32. protected $plugin;
  33. /**
  34. * @var Composer
  35. */
  36. protected $composer;
  37. /**
  38. * @var IOInterface
  39. */
  40. protected $io;
  41. /**
  42. * @var \PHPUnit_Framework_MockObject_MockObject
  43. */
  44. protected $package;
  45. protected function setUp()
  46. {
  47. $io = $this->getMock('Composer\IO\IOInterface');
  48. $config = $this->getMock('Composer\Config');
  49. $config->expects($this->any())
  50. ->method('get')
  51. ->will($this->returnCallback(function ($key) {
  52. switch ($key) {
  53. case 'cache-repo-dir':
  54. return sys_get_temp_dir().'/composer-test-repo-cache';
  55. }
  56. return;
  57. }));
  58. $this->package = $this->getMock('Composer\Package\RootPackageInterface');
  59. $this->package->expects($this->any())
  60. ->method('getRequires')
  61. ->will($this->returnValue(array()));
  62. $this->package->expects($this->any())
  63. ->method('getDevRequires')
  64. ->will($this->returnValue(array()));
  65. /* @var IOInterface $io */
  66. /* @var Config $config */
  67. $rm = new RepositoryManager($io, $config);
  68. $im = new InstallationManager();
  69. $composer = $this->getMock('Composer\Composer');
  70. $composer->expects($this->any())
  71. ->method('getRepositoryManager')
  72. ->will($this->returnValue($rm));
  73. $composer->expects($this->any())
  74. ->method('getPackage')
  75. ->will($this->returnValue($this->package));
  76. $composer->expects($this->any())
  77. ->method('getConfig')
  78. ->will($this->returnValue($config));
  79. $composer->expects($this->any())
  80. ->method('getInstallationManager')
  81. ->will($this->returnValue($im));
  82. $this->plugin = new FxpAssetPlugin();
  83. $this->composer = $composer;
  84. $this->io = $io;
  85. }
  86. protected function tearDown()
  87. {
  88. $this->plugin = null;
  89. $this->composer = null;
  90. $this->io = null;
  91. $fs = new Filesystem();
  92. $fs->remove(sys_get_temp_dir().'/composer-test-repo-cache');
  93. }
  94. public function testAssetRepositories()
  95. {
  96. $this->package->expects($this->any())
  97. ->method('getExtra')
  98. ->will($this->returnValue(array()));
  99. $this->plugin->activate($this->composer, $this->io);
  100. $repos = $this->composer->getRepositoryManager()->getRepositories();
  101. $this->assertCount(2, $repos);
  102. foreach ($repos as $repo) {
  103. $this->assertInstanceOf('Composer\Repository\ComposerRepository', $repo);
  104. }
  105. }
  106. /**
  107. * @dataProvider getDataForAssetVcsRepositories
  108. */
  109. public function testAssetVcsRepositories($type)
  110. {
  111. $this->package->expects($this->any())
  112. ->method('getExtra')
  113. ->will($this->returnValue(array()));
  114. $this->plugin->activate($this->composer, $this->io);
  115. $rm = $this->composer->getRepositoryManager();
  116. $repo = $rm->createRepository($type, array(
  117. 'type' => $type,
  118. 'url' => 'http://foo.tld',
  119. ));
  120. $this->assertInstanceOf('Composer\Repository\VcsRepository', $repo);
  121. }
  122. public function getDataForAssetVcsRepositories()
  123. {
  124. return array(
  125. array('npm-vcs'),
  126. array('npm-git'),
  127. array('npm-github'),
  128. array('bower-vcs'),
  129. array('bower-git'),
  130. array('bower-github'),
  131. );
  132. }
  133. public function testAssetRepositoryWithValueIsNotArray()
  134. {
  135. $this->setExpectedException('UnexpectedValueException');
  136. $this->package->expects($this->any())
  137. ->method('getExtra')
  138. ->will($this->returnValue(array('asset-repositories' => array(
  139. 'invalid_repo',
  140. ))));
  141. $this->plugin->activate($this->composer, $this->io);
  142. }
  143. public function testAssetRepositoryWithInvalidType()
  144. {
  145. $this->setExpectedException('UnexpectedValueException');
  146. $this->package->expects($this->any())
  147. ->method('getExtra')
  148. ->will($this->returnValue(array('asset-repositories' => array(
  149. array(),
  150. ))));
  151. $this->plugin->activate($this->composer, $this->io);
  152. }
  153. public function testAssetRepositoryWithInvalidTypeFormat()
  154. {
  155. $this->setExpectedException('UnexpectedValueException');
  156. $this->package->expects($this->any())
  157. ->method('getExtra')
  158. ->will($this->returnValue(array('asset-repositories' => array(
  159. array('type' => 'invalid_type'),
  160. ))));
  161. $this->plugin->activate($this->composer, $this->io);
  162. }
  163. public function testAssetRepositoryWithInvalidUrl()
  164. {
  165. $this->setExpectedException('UnexpectedValueException');
  166. $this->package->expects($this->any())
  167. ->method('getExtra')
  168. ->will($this->returnValue(array('asset-repositories' => array(
  169. array('type' => 'npm-vcs'),
  170. ))));
  171. $this->plugin->activate($this->composer, $this->io);
  172. }
  173. public function testAssetRepository()
  174. {
  175. $this->package->expects($this->any())
  176. ->method('getExtra')
  177. ->will($this->returnValue(array('asset-repositories' => array(
  178. array('type' => 'npm-vcs', 'url' => 'http://foo.tld'),
  179. ))));
  180. $this->plugin->activate($this->composer, $this->io);
  181. $repos = $this->composer->getRepositoryManager()->getRepositories();
  182. $this->assertCount(3, $repos);
  183. $this->assertInstanceOf('Fxp\Composer\AssetPlugin\Repository\AssetVcsRepository', $repos[2]);
  184. }
  185. public function testAssetRepositoryWithAlreadyExistRepositoryName()
  186. {
  187. $this->package->expects($this->any())
  188. ->method('getExtra')
  189. ->will($this->returnValue(array('asset-repositories' => array(
  190. array('type' => 'npm-vcs', 'url' => 'http://foo.tld'),
  191. array('type' => 'npm-vcs', 'url' => 'http://foo.tld'),
  192. ))));
  193. $this->plugin->activate($this->composer, $this->io);
  194. $repos = $this->composer->getRepositoryManager()->getRepositories();
  195. $this->assertCount(3, $repos);
  196. $this->assertInstanceOf('Fxp\Composer\AssetPlugin\Repository\AssetVcsRepository', $repos[2]);
  197. }
  198. public function testAssetPackageWithoutPackage()
  199. {
  200. $this->setExpectedException('UnexpectedValueException');
  201. $this->package->expects($this->any())
  202. ->method('getExtra')
  203. ->will($this->returnValue(array('asset-repositories' => array(
  204. array('type' => 'package'),
  205. ))));
  206. $this->plugin->activate($this->composer, $this->io);
  207. }
  208. public function testAssetPackageWithInvalidPackage()
  209. {
  210. $this->setExpectedException('UnexpectedValueException');
  211. $this->package->expects($this->any())
  212. ->method('getExtra')
  213. ->will($this->returnValue(array('asset-repositories' => array(
  214. array('type' => 'package', 'package' => array('key' => 'value')),
  215. ))));
  216. $this->plugin->activate($this->composer, $this->io);
  217. }
  218. public function testAssetPackageRepositories()
  219. {
  220. $this->package->expects($this->any())
  221. ->method('getExtra')
  222. ->will($this->returnValue(array('asset-repositories' => array(
  223. array(
  224. 'type' => 'package',
  225. 'package' => array(
  226. 'name' => 'foo',
  227. 'type' => 'ASSET-asset-library',
  228. 'version' => '0.0.0.0',
  229. 'dist' => array(
  230. 'url' => 'foo.tld/bar',
  231. 'type' => 'file',
  232. ),
  233. ),
  234. ),
  235. ))));
  236. $rm = $this->composer->getRepositoryManager();
  237. $rm->setRepositoryClass('package', 'Composer\Repository\PackageRepository');
  238. $this->plugin->activate($this->composer, $this->io);
  239. $repos = $this->composer->getRepositoryManager()->getRepositories();
  240. $this->assertCount(3, $repos);
  241. $this->assertInstanceOf('Composer\Repository\PackageRepository', $repos[2]);
  242. }
  243. public function testOptionsForAssetRegistryRepositories()
  244. {
  245. $this->package->expects($this->any())
  246. ->method('getExtra')
  247. ->will($this->returnValue(array('asset-registry-options' => array(
  248. 'npm-option1' => 'value 1',
  249. 'bower-option1' => 'value 2',
  250. ))));
  251. $this->plugin->activate($this->composer, $this->io);
  252. }
  253. public function testSubscribeEvents()
  254. {
  255. $this->package->expects($this->any())
  256. ->method('getExtra')
  257. ->will($this->returnValue(array()));
  258. $this->assertCount(3, $this->plugin->getSubscribedEvents());
  259. $this->assertCount(0, $this->composer->getRepositoryManager()->getRepositories());
  260. $event = new VcsRepositoryEvent(AssetEvents::ADD_VCS_REPOSITORIES, array(
  261. array('type' => 'npm-vcs', 'url' => 'http://foo.tld'),
  262. ));
  263. /* @var InstallerEvent $eventInstaller */
  264. $eventInstaller = $this->getMockBuilder('Composer\Installer\InstallerEvent')
  265. ->disableOriginalConstructor()
  266. ->getMock();
  267. /* @var CommandEvent|\PHPUnit_Framework_MockObject_MockObject $eventCommand */
  268. $eventCommand = $this->getMockBuilder('Composer\Plugin\CommandEvent')
  269. ->disableOriginalConstructor()
  270. ->getMock();
  271. $eventCommand->expects($this->any())
  272. ->method('getCommandName')
  273. ->will($this->returnValue('show'));
  274. $this->plugin->activate($this->composer, $this->io);
  275. $this->assertCount(2, $this->composer->getRepositoryManager()->getRepositories());
  276. $this->plugin->onAddVcsRepositories($event);
  277. $this->plugin->onPluginCommand($eventCommand);
  278. $this->plugin->onPreDependenciesSolving($eventInstaller);
  279. $this->assertCount(3, $this->composer->getRepositoryManager()->getRepositories());
  280. }
  281. public function testAssetInstallers()
  282. {
  283. $this->package->expects($this->any())
  284. ->method('getExtra')
  285. ->will($this->returnValue(array()));
  286. $this->plugin->activate($this->composer, $this->io);
  287. $im = $this->composer->getInstallationManager();
  288. $this->assertInstanceOf('Fxp\Composer\AssetPlugin\Installer\BowerInstaller', $im->getInstaller('bower-asset-library'));
  289. $this->assertInstanceOf('Fxp\Composer\AssetPlugin\Installer\AssetInstaller', $im->getInstaller('npm-asset-library'));
  290. }
  291. }