RepositoryManagerTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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\EventDispatcher\EventDispatcher')->disableOriginalConstructor()->getMock()
  35. );
  36. $repository1 = $this->getMockBuilder('Composer\Repository\RepositoryInterface')->getMock();
  37. $repository2 = $this->getMockBuilder('Composer\Repository\RepositoryInterface')->getMock();
  38. $rm->addRepository($repository1);
  39. $rm->prependRepository($repository2);
  40. $this->assertEquals(array($repository2, $repository1), $rm->getRepositories());
  41. }
  42. /**
  43. * @dataProvider creationCases
  44. */
  45. public function testRepoCreation($type, $options, $exception = null)
  46. {
  47. if ($exception) {
  48. $this->setExpectedException($exception);
  49. }
  50. $rm = new RepositoryManager(
  51. $this->getMockBuilder('Composer\IO\IOInterface')->getMock(),
  52. $config = $this->getMockBuilder('Composer\Config')->setMethods(array('get'))->getMock(),
  53. $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')->disableOriginalConstructor()->getMock()
  54. );
  55. $tmpdir = $this->tmpdir;
  56. $config
  57. ->expects($this->any())
  58. ->method('get')
  59. ->will($this->returnCallback(function ($arg) use ($tmpdir) {
  60. return 'cache-repo-dir' === $arg ? $tmpdir : null;
  61. }))
  62. ;
  63. $rm->setRepositoryClass('composer', 'Composer\Repository\ComposerRepository');
  64. $rm->setRepositoryClass('vcs', 'Composer\Repository\VcsRepository');
  65. $rm->setRepositoryClass('package', 'Composer\Repository\PackageRepository');
  66. $rm->setRepositoryClass('pear', 'Composer\Repository\PearRepository');
  67. $rm->setRepositoryClass('git', 'Composer\Repository\VcsRepository');
  68. $rm->setRepositoryClass('svn', 'Composer\Repository\VcsRepository');
  69. $rm->setRepositoryClass('perforce', 'Composer\Repository\VcsRepository');
  70. $rm->setRepositoryClass('hg', 'Composer\Repository\VcsRepository');
  71. $rm->setRepositoryClass('artifact', 'Composer\Repository\ArtifactRepository');
  72. $rm->createRepository('composer', array('url' => 'http://example.org'));
  73. $this->assertInstanceOf('Composer\Repository\RepositoryInterface', $rm->createRepository($type, $options));
  74. }
  75. public function creationCases()
  76. {
  77. $cases = array(
  78. array('composer', array('url' => 'http://example.org')),
  79. array('vcs', array('url' => 'http://github.com/foo/bar')),
  80. array('git', array('url' => 'http://github.com/foo/bar')),
  81. array('git', array('url' => 'git@example.org:foo/bar.git')),
  82. array('svn', array('url' => 'svn://example.org/foo/bar')),
  83. array('pear', array('url' => 'http://pear.example.org/foo')),
  84. array('package', array('package' => array())),
  85. array('invalid', array(), 'InvalidArgumentException'),
  86. );
  87. if (class_exists('ZipArchive')) {
  88. $cases[] = array('artifact', array('url' => '/path/to/zips'));
  89. }
  90. return $cases;
  91. }
  92. }