Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

413 linhas
14KB

  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\Repository;
  11. use Composer\EventDispatcher\EventDispatcher;
  12. use Composer\Config;
  13. use Composer\Package\AliasPackage;
  14. use Composer\Package\CompletePackage;
  15. use Composer\Package\PackageInterface;
  16. use Composer\Repository\InvalidRepositoryException;
  17. use Fxp\Composer\AssetPlugin\Repository\AssetVcsRepository;
  18. use Fxp\Composer\AssetPlugin\Repository\VcsPackageFilter;
  19. use Fxp\Composer\AssetPlugin\Tests\Fixtures\IO\MockIO;
  20. use Fxp\Composer\AssetPlugin\Tests\Fixtures\Repository\Vcs\MockVcsDriver;
  21. /**
  22. * Tests of asset vcs repository.
  23. *
  24. * @author François Pluchino <francois.pluchino@gmail.com>
  25. */
  26. class AssetVcsRepositoryTest extends \PHPUnit_Framework_TestCase
  27. {
  28. /**
  29. * @var Config
  30. */
  31. protected $config;
  32. /**
  33. * @var EventDispatcher
  34. */
  35. protected $dispatcher;
  36. /**
  37. * @var MockIO
  38. */
  39. protected $io;
  40. /**
  41. * @var AssetVcsRepository
  42. */
  43. protected $repository;
  44. protected function setUp()
  45. {
  46. $this->config = new Config();
  47. /* @var EventDispatcher $dispatcher */
  48. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  49. ->disableOriginalConstructor()
  50. ->getMock();
  51. $this->dispatcher = $dispatcher;
  52. }
  53. protected function tearDown()
  54. {
  55. $this->config = null;
  56. $this->dispatcher = null;
  57. $this->io = null;
  58. $this->repository = null;
  59. }
  60. /**
  61. * Data provider.
  62. *
  63. * @return array
  64. */
  65. public function getDefaultDrivers()
  66. {
  67. return array(
  68. array('npm-github', 'http://example.org/foo.git', 'Fxp\Composer\AssetPlugin\Repository\Vcs\GitHubDriver'),
  69. array('npm-git', 'http://example.org/foo.git', 'Fxp\Composer\AssetPlugin\Repository\Vcs\GitDriver'),
  70. array('bower-github', 'http://example.org/foo.git', 'Fxp\Composer\AssetPlugin\Repository\Vcs\GitHubDriver'),
  71. array('bower-git', 'http://example.org/foo.git', 'Fxp\Composer\AssetPlugin\Repository\Vcs\GitDriver'),
  72. );
  73. }
  74. /**
  75. * @dataProvider getDefaultDrivers
  76. */
  77. public function testDefaultConstructor($type, $url)
  78. {
  79. $this->init(false, $type, $url, '', false, array());
  80. $this->assertEquals(0, $this->repository->count());
  81. }
  82. /**
  83. * Data provider.
  84. *
  85. * @return array
  86. */
  87. public function getMockDrivers()
  88. {
  89. return array(
  90. array('npm-mock', 'http://example.org/foo', 'Fxp\Composer\AssetPlugin\Tests\Fixtures\Repository\Vcs\MockVcsDriver'),
  91. array('bower-mock', 'http://example.org/foo', 'Fxp\Composer\AssetPlugin\Tests\Fixtures\Repository\Vcs\MockVcsDriver'),
  92. );
  93. }
  94. /**
  95. * @dataProvider getMockDrivers
  96. */
  97. public function testNotDriverFound($type, $url, $class)
  98. {
  99. $this->setExpectedException('InvalidArgumentException', 'No driver found to handle Asset VCS repository '.$url);
  100. $this->init(false, $type, $url, $class);
  101. $this->repository->getPackages();
  102. }
  103. /**
  104. * @dataProvider getMockDrivers
  105. */
  106. public function testWithoutValidPackage($type, $url, $class)
  107. {
  108. $this->setExpectedException('Composer\Repository\InvalidRepositoryException');
  109. $this->init(true, $type, $url, $class);
  110. $this->repository->getPackages();
  111. }
  112. /**
  113. * Data provider.
  114. *
  115. * @return array
  116. */
  117. public function getMockDriversSkipParsing()
  118. {
  119. return array(
  120. array('npm-mock', 'http://example.org/foo', 'Fxp\Composer\AssetPlugin\Tests\Fixtures\Repository\Vcs\MockVcsDriverSkipParsing', false),
  121. array('bower-mock', 'http://example.org/foo', 'Fxp\Composer\AssetPlugin\Tests\Fixtures\Repository\Vcs\MockVcsDriverSkipParsing', false),
  122. array('npm-mock', 'http://example.org/foo', 'Fxp\Composer\AssetPlugin\Tests\Fixtures\Repository\Vcs\MockVcsDriverSkipParsing', true),
  123. array('bower-mock', 'http://example.org/foo', 'Fxp\Composer\AssetPlugin\Tests\Fixtures\Repository\Vcs\MockVcsDriverSkipParsing', true),
  124. );
  125. }
  126. /**
  127. * @dataProvider getMockDriversSkipParsing
  128. */
  129. public function testSkipParsingFile($type, $url, $class, $verbose)
  130. {
  131. $validTraces = array('');
  132. if ($verbose) {
  133. $validTraces = array(
  134. '<error>Skipped parsing ROOT, MESSAGE with ROOT</error>',
  135. '',
  136. );
  137. }
  138. $this->init(true, $type, $url, $class, $verbose);
  139. try {
  140. $this->repository->getPackages();
  141. } catch (InvalidRepositoryException $e) {
  142. // for analysis the IO traces
  143. }
  144. $this->assertSame($validTraces, $this->io->getTraces());
  145. }
  146. /**
  147. * Data provider.
  148. *
  149. * @return array
  150. */
  151. public function getMockDriversWithVersions()
  152. {
  153. return array(
  154. array('npm-mock', 'http://example.org/foo', 'Fxp\Composer\AssetPlugin\Tests\Fixtures\Repository\Vcs\MockVcsDriverWithPackages', false),
  155. array('bower-mock', 'http://example.org/foo', 'Fxp\Composer\AssetPlugin\Tests\Fixtures\Repository\Vcs\MockVcsDriverWithPackages', false),
  156. array('npm-mock', 'http://example.org/foo', 'Fxp\Composer\AssetPlugin\Tests\Fixtures\Repository\Vcs\MockVcsDriverWithPackages', true),
  157. array('bower-mock', 'http://example.org/foo', 'Fxp\Composer\AssetPlugin\Tests\Fixtures\Repository\Vcs\MockVcsDriverWithPackages', true),
  158. );
  159. }
  160. /**
  161. * @dataProvider getMockDriversWithVersions
  162. */
  163. public function testRepositoryPackageName($type, $url, $class, $verbose)
  164. {
  165. $packageName = 'asset-package-name';
  166. $valid = str_replace('-mock', '-asset', $type).'/'.$packageName;
  167. $this->init(true, $type, $url, $class, $verbose, null, $packageName);
  168. $this->assertEquals($valid, $this->repository->getComposerPackageName());
  169. }
  170. /**
  171. * @dataProvider getMockDriversWithVersions
  172. */
  173. public function testWithTagsAndBranchs($type, $url, $class, $verbose)
  174. {
  175. $validPackageName = substr($type, 0, strpos($type, '-')).'-asset/foobar';
  176. $validTraces = array('');
  177. if ($verbose) {
  178. $validTraces = array(
  179. '<warning>Skipped tag invalid, invalid tag name</warning>',
  180. '',
  181. );
  182. }
  183. $this->init(true, $type, $url, $class, $verbose);
  184. /* @var PackageInterface[] $packages */
  185. $packages = $this->repository->getPackages();
  186. $this->assertCount(7, $packages);
  187. foreach ($packages as $package) {
  188. if ($package instanceof AliasPackage) {
  189. $package = $package->getAliasOf();
  190. }
  191. $this->assertInstanceOf('Composer\Package\CompletePackage', $package);
  192. $this->assertSame($validPackageName, $package->getName());
  193. }
  194. $this->assertSame($validTraces, $this->io->getTraces());
  195. }
  196. /**
  197. * Data provider.
  198. *
  199. * @return array
  200. */
  201. public function getMockDriversWithVersionsAndWithoutName()
  202. {
  203. return array(
  204. array('npm-mock', 'http://example.org/foo', 'Fxp\Composer\AssetPlugin\Tests\Fixtures\Repository\Vcs\MockVcsDriverWithUrlPackages', false),
  205. array('bower-mock', 'http://example.org/foo', 'Fxp\Composer\AssetPlugin\Tests\Fixtures\Repository\Vcs\MockVcsDriverWithUrlPackages', false),
  206. array('npm-mock', 'http://example.org/foo', 'Fxp\Composer\AssetPlugin\Tests\Fixtures\Repository\Vcs\MockVcsDriverWithUrlPackages', true),
  207. array('bower-mock', 'http://example.org/foo', 'Fxp\Composer\AssetPlugin\Tests\Fixtures\Repository\Vcs\MockVcsDriverWithUrlPackages', true),
  208. );
  209. }
  210. /**
  211. * @dataProvider getMockDriversWithVersionsAndWithoutName
  212. */
  213. public function testWithTagsAndBranchsWithoutPackageName($type, $url, $class, $verbose)
  214. {
  215. $validPackageName = $url;
  216. $validTraces = array('');
  217. if ($verbose) {
  218. $validTraces = array(
  219. '<warning>Skipped tag invalid, invalid tag name</warning>',
  220. '',
  221. );
  222. }
  223. $this->init(true, $type, $url, $class, $verbose);
  224. /* @var PackageInterface[] $packages */
  225. $packages = $this->repository->getPackages();
  226. $this->assertCount(7, $packages);
  227. foreach ($packages as $package) {
  228. if ($package instanceof AliasPackage) {
  229. $package = $package->getAliasOf();
  230. }
  231. $this->assertInstanceOf('Composer\Package\CompletePackage', $package);
  232. $this->assertSame($validPackageName, $package->getName());
  233. }
  234. $this->assertSame($validTraces, $this->io->getTraces());
  235. }
  236. /**
  237. * @dataProvider getMockDriversWithVersions
  238. */
  239. public function testWithTagsAndBranchsWithRegistryPackageName($type, $url, $class, $verbose)
  240. {
  241. $validPackageName = substr($type, 0, strpos($type, '-')).'-asset/registry-foobar';
  242. $validTraces = array('');
  243. if ($verbose) {
  244. $validTraces = array(
  245. '<warning>Skipped tag invalid, invalid tag name</warning>',
  246. '',
  247. );
  248. }
  249. $this->init(true, $type, $url, $class, $verbose, null, 'registry-foobar');
  250. /* @var PackageInterface[] $packages */
  251. $packages = $this->repository->getPackages();
  252. $this->assertCount(7, $packages);
  253. foreach ($packages as $package) {
  254. if ($package instanceof AliasPackage) {
  255. $package = $package->getAliasOf();
  256. }
  257. $this->assertInstanceOf('Composer\Package\CompletePackage', $package);
  258. $this->assertSame($validPackageName, $package->getName());
  259. }
  260. $this->assertSame($validTraces, $this->io->getTraces());
  261. }
  262. /**
  263. * @dataProvider getMockDriversWithVersions
  264. */
  265. public function testWithFilterTags($type, $url, $class, $verbose)
  266. {
  267. $validPackageName = substr($type, 0, strpos($type, '-')).'-asset/registry-foobar';
  268. $validTraces = array('');
  269. if ($verbose) {
  270. $validTraces = array();
  271. }
  272. $filter = $this->getMockBuilder('Fxp\Composer\AssetPlugin\Repository\VcsPackageFilter')
  273. ->disableOriginalConstructor()
  274. ->getMock();
  275. $filter->expects($this->any())
  276. ->method('skip')
  277. ->will($this->returnValue(true));
  278. /* @var VcsPackageFilter $filter */
  279. $this->init(true, $type, $url, $class, $verbose, null, 'registry-foobar', $filter);
  280. /* @var PackageInterface[] $packages */
  281. $packages = $this->repository->getPackages();
  282. $this->assertCount(5, $packages);
  283. foreach ($packages as $package) {
  284. if ($package instanceof AliasPackage) {
  285. $package = $package->getAliasOf();
  286. }
  287. $this->assertInstanceOf('Composer\Package\CompletePackage', $package);
  288. $this->assertSame($validPackageName, $package->getName());
  289. }
  290. $this->assertSame($validTraces, $this->io->getTraces());
  291. }
  292. /**
  293. * @dataProvider getMockDrivers
  294. */
  295. public function testPackageWithRegistryVersions($type, $url, $class)
  296. {
  297. $registryPackages = array(
  298. new CompletePackage('package1', '0.1.0.0', '0.1'),
  299. new CompletePackage('package1', '0.2.0.0', '0.2'),
  300. new CompletePackage('package1', '0.3.0.0', '0.3'),
  301. new CompletePackage('package1', '0.4.0.0', '0.4'),
  302. new CompletePackage('package1', '0.5.0.0', '0.5'),
  303. new CompletePackage('package1', '0.6.0.0', '0.6'),
  304. new CompletePackage('package1', '0.7.0.0', '0.7'),
  305. new CompletePackage('package1', '0.8.0.0', '0.8'),
  306. new CompletePackage('package1', '0.9.0.0', '0.9'),
  307. new CompletePackage('package1', '1.0.0.0', '1.0'),
  308. );
  309. $this->init(true, $type, $url, $class, false, null, 'registry-foobar', null, $registryPackages);
  310. /* @var PackageInterface[] $packages */
  311. $packages = $this->repository->getPackages();
  312. $this->assertCount(10, $packages);
  313. $this->assertSame($registryPackages, $packages);
  314. }
  315. /**
  316. * Init the test.
  317. *
  318. * @param bool $supported
  319. * @param string $type
  320. * @param string $url
  321. * @param string $class
  322. * @param bool $verbose
  323. * @param array|null $drivers
  324. * @param string|null $registryName
  325. * @param VcsPackageFilter|null $vcsPackageFilter
  326. * @param array $registryPackages
  327. */
  328. protected function init($supported, $type, $url, $class, $verbose = false, $drivers = null, $registryName = null, VcsPackageFilter $vcsPackageFilter = null, array $registryPackages = array())
  329. {
  330. MockVcsDriver::$supported = $supported;
  331. $driverType = substr($type, strpos($type, '-') + 1);
  332. $repoConfig = array('type' => $type, 'url' => $url, 'name' => $registryName, 'vcs-package-filter' => $vcsPackageFilter);
  333. if (null === $drivers) {
  334. $drivers = array(
  335. $driverType => $class,
  336. );
  337. }
  338. if (count($registryPackages) > 0) {
  339. $repoConfig['registry-versions'] = $registryPackages;
  340. }
  341. $this->io = $this->createIO($verbose);
  342. $this->repository = new AssetVcsRepository($repoConfig, $this->io, $this->config, $this->dispatcher, $drivers);
  343. }
  344. /**
  345. * @param bool $verbose
  346. *
  347. * @return MockIO
  348. */
  349. protected function createIO($verbose = false)
  350. {
  351. return new MockIO($verbose);
  352. }
  353. }