RepositoryManagerTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Test\Repository;
  12. use Composer\Repository\RepositoryManager;
  13. use Composer\Test\TestCase;
  14. use Composer\Util\Filesystem;
  15. class RepositoryManagerTest extends TestCase
  16. {
  17. protected $tmpdir;
  18. public function setUp()
  19. {
  20. $this->tmpdir = $this->getUniqueTmpDirectory();
  21. }
  22. public function tearDown()
  23. {
  24. if (is_dir($this->tmpdir)) {
  25. $fs = new Filesystem();
  26. $fs->removeDirectory($this->tmpdir);
  27. }
  28. }
  29. public function testPrepend()
  30. {
  31. $rm = new RepositoryManager(
  32. $this->getMockBuilder('Composer\IO\IOInterface')->getMock(),
  33. $this->getMockBuilder('Composer\Config')->getMock(),
  34. $this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock(),
  35. $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')->disableOriginalConstructor()->getMock()
  36. );
  37. $repository1 = $this->getMockBuilder('Composer\Repository\RepositoryInterface')->getMock();
  38. $repository2 = $this->getMockBuilder('Composer\Repository\RepositoryInterface')->getMock();
  39. $rm->addRepository($repository1);
  40. $rm->prependRepository($repository2);
  41. $this->assertEquals(array($repository2, $repository1), $rm->getRepositories());
  42. }
  43. /**
  44. * @dataProvider creationCases
  45. */
  46. public function testRepoCreation($type, $options, $exception = null)
  47. {
  48. if ($exception) {
  49. $this->setExpectedException($exception);
  50. }
  51. $rm = new RepositoryManager(
  52. $this->getMockBuilder('Composer\IO\IOInterface')->getMock(),
  53. $config = $this->getMockBuilder('Composer\Config')->setMethods(array('get'))->getMock(),
  54. $this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock(),
  55. $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')->disableOriginalConstructor()->getMock()
  56. );
  57. $tmpdir = $this->tmpdir;
  58. $config
  59. ->expects($this->any())
  60. ->method('get')
  61. ->will($this->returnCallback(function ($arg) use ($tmpdir) {
  62. return 'cache-repo-dir' === $arg ? $tmpdir : null;
  63. }))
  64. ;
  65. $rm->setRepositoryClass('composer', 'Composer\Repository\ComposerRepository');
  66. $rm->setRepositoryClass('vcs', 'Composer\Repository\VcsRepository');
  67. $rm->setRepositoryClass('package', 'Composer\Repository\PackageRepository');
  68. $rm->setRepositoryClass('pear', 'Composer\Repository\PearRepository');
  69. $rm->setRepositoryClass('git', 'Composer\Repository\VcsRepository');
  70. $rm->setRepositoryClass('svn', 'Composer\Repository\VcsRepository');
  71. $rm->setRepositoryClass('perforce', 'Composer\Repository\VcsRepository');
  72. $rm->setRepositoryClass('hg', 'Composer\Repository\VcsRepository');
  73. $rm->setRepositoryClass('artifact', 'Composer\Repository\ArtifactRepository');
  74. $rm->createRepository('composer', array('url' => 'http://example.org'));
  75. $this->assertInstanceOf('Composer\Repository\RepositoryInterface', $rm->createRepository($type, $options));
  76. }
  77. public function creationCases()
  78. {
  79. $cases = array(
  80. array('composer', array('url' => 'http://example.org')),
  81. array('vcs', array('url' => 'http://github.com/foo/bar')),
  82. array('git', array('url' => 'http://github.com/foo/bar')),
  83. array('git', array('url' => 'git@example.org:foo/bar.git')),
  84. array('svn', array('url' => 'svn://example.org/foo/bar')),
  85. array('pear', array('url' => 'http://pear.example.org/foo'), 'RuntimeException'),
  86. array('package', array('package' => array())),
  87. array('invalid', array(), 'InvalidArgumentException'),
  88. );
  89. if (class_exists('ZipArchive')) {
  90. $cases[] = array('artifact', array('url' => '/path/to/zips'));
  91. }
  92. return $cases;
  93. }
  94. public function testFilterRepoWrapping()
  95. {
  96. $rm = new RepositoryManager(
  97. $this->getMockBuilder('Composer\IO\IOInterface')->getMock(),
  98. $config = $this->getMockBuilder('Composer\Config')->setMethods(array('get'))->getMock(),
  99. $this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock(),
  100. $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')->disableOriginalConstructor()->getMock()
  101. );
  102. $rm->setRepositoryClass('path', 'Composer\Repository\PathRepository');
  103. $repo = $rm->createRepository('path', array('type' => 'path', 'url' => __DIR__, 'only' => array('foo/bar')));
  104. $this->assertInstanceOf('Composer\Repository\FilterRepository', $repo);
  105. $this->assertInstanceOf('Composer\Repository\PathRepository', $repo->getRepository());
  106. }
  107. }