CompositeRepositoryTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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\CompositeRepository;
  13. use Composer\Repository\ArrayRepository;
  14. use Composer\Test\TestCase;
  15. class CompositeRepositoryTest extends TestCase
  16. {
  17. public function testHasPackage()
  18. {
  19. $arrayRepoOne = new ArrayRepository;
  20. $arrayRepoOne->addPackage($this->getPackage('foo', '1'));
  21. $arrayRepoTwo = new ArrayRepository;
  22. $arrayRepoTwo->addPackage($this->getPackage('bar', '1'));
  23. $repo = new CompositeRepository(array($arrayRepoOne, $arrayRepoTwo));
  24. $this->assertTrue($repo->hasPackage($this->getPackage('foo', '1')), "Should have package 'foo/1'");
  25. $this->assertTrue($repo->hasPackage($this->getPackage('bar', '1')), "Should have package 'bar/1'");
  26. $this->assertFalse($repo->hasPackage($this->getPackage('foo', '2')), "Should not have package 'foo/2'");
  27. $this->assertFalse($repo->hasPackage($this->getPackage('bar', '2')), "Should not have package 'bar/2'");
  28. }
  29. public function testFindPackage()
  30. {
  31. $arrayRepoOne = new ArrayRepository;
  32. $arrayRepoOne->addPackage($this->getPackage('foo', '1'));
  33. $arrayRepoTwo = new ArrayRepository;
  34. $arrayRepoTwo->addPackage($this->getPackage('bar', '1'));
  35. $repo = new CompositeRepository(array($arrayRepoOne, $arrayRepoTwo));
  36. $this->assertEquals('foo', $repo->findPackage('foo', '1')->getName(), "Should find package 'foo/1' and get name of 'foo'");
  37. $this->assertEquals('1', $repo->findPackage('foo', '1')->getPrettyVersion(), "Should find package 'foo/1' and get pretty version of '1'");
  38. $this->assertEquals('bar', $repo->findPackage('bar', '1')->getName(), "Should find package 'bar/1' and get name of 'bar'");
  39. $this->assertEquals('1', $repo->findPackage('bar', '1')->getPrettyVersion(), "Should find package 'bar/1' and get pretty version of '1'");
  40. $this->assertNull($repo->findPackage('foo', '2'), "Should not find package 'foo/2'");
  41. }
  42. public function testFindPackages()
  43. {
  44. $arrayRepoOne = new ArrayRepository;
  45. $arrayRepoOne->addPackage($this->getPackage('foo', '1'));
  46. $arrayRepoOne->addPackage($this->getPackage('foo', '2'));
  47. $arrayRepoOne->addPackage($this->getPackage('bat', '1'));
  48. $arrayRepoTwo = new ArrayRepository;
  49. $arrayRepoTwo->addPackage($this->getPackage('bar', '1'));
  50. $arrayRepoTwo->addPackage($this->getPackage('bar', '2'));
  51. $arrayRepoTwo->addPackage($this->getPackage('foo', '3'));
  52. $repo = new CompositeRepository(array($arrayRepoOne, $arrayRepoTwo));
  53. $bats = $repo->findPackages('bat');
  54. $this->assertCount(1, $bats, "Should find one instance of 'bats' (defined in just one repository)");
  55. $this->assertEquals('bat', $bats[0]->getName(), "Should find packages named 'bat'");
  56. $bars = $repo->findPackages('bar');
  57. $this->assertCount(2, $bars, "Should find two instances of 'bar' (both defined in the same repository)");
  58. $this->assertEquals('bar', $bars[0]->getName(), "Should find packages named 'bar'");
  59. $foos = $repo->findPackages('foo');
  60. $this->assertCount(3, $foos, "Should find three instances of 'foo' (two defined in one repository, the third in the other)");
  61. $this->assertEquals('foo', $foos[0]->getName(), "Should find packages named 'foo'");
  62. }
  63. public function testGetPackages()
  64. {
  65. $arrayRepoOne = new ArrayRepository;
  66. $arrayRepoOne->addPackage($this->getPackage('foo', '1'));
  67. $arrayRepoTwo = new ArrayRepository;
  68. $arrayRepoTwo->addPackage($this->getPackage('bar', '1'));
  69. $repo = new CompositeRepository(array($arrayRepoOne, $arrayRepoTwo));
  70. $packages = $repo->getPackages();
  71. $this->assertCount(2, $packages, "Should get two packages");
  72. $this->assertEquals("foo", $packages[0]->getName(), "First package should have name of 'foo'");
  73. $this->assertEquals("1", $packages[0]->getPrettyVersion(), "First package should have pretty version of '1'");
  74. $this->assertEquals("bar", $packages[1]->getName(), "Second package should have name of 'bar'");
  75. $this->assertEquals("1", $packages[1]->getPrettyVersion(), "Second package should have pretty version of '1'");
  76. }
  77. public function testAddRepository()
  78. {
  79. $arrayRepoOne = new ArrayRepository;
  80. $arrayRepoOne->addPackage($this->getPackage('foo', '1'));
  81. $arrayRepoTwo = new ArrayRepository;
  82. $arrayRepoTwo->addPackage($this->getPackage('bar', '1'));
  83. $arrayRepoTwo->addPackage($this->getPackage('bar', '2'));
  84. $arrayRepoTwo->addPackage($this->getPackage('bar', '3'));
  85. $repo = new CompositeRepository(array($arrayRepoOne));
  86. $this->assertCount(1, $repo, "Composite repository should have just one package before addRepository() is called");
  87. $repo->addRepository($arrayRepoTwo);
  88. $this->assertCount(4, $repo, "Composite repository should have four packages after addRepository() is called");
  89. }
  90. public function testCount()
  91. {
  92. $arrayRepoOne = new ArrayRepository;
  93. $arrayRepoOne->addPackage($this->getPackage('foo', '1'));
  94. $arrayRepoTwo = new ArrayRepository;
  95. $arrayRepoTwo->addPackage($this->getPackage('bar', '1'));
  96. $repo = new CompositeRepository(array($arrayRepoOne, $arrayRepoTwo));
  97. $this->assertEquals(2, count($repo), "Should return '2' for count(\$repo)");
  98. }
  99. /**
  100. * @dataProvider provideMethodCalls
  101. */
  102. public function testNoRepositories($method, $args)
  103. {
  104. $repo = new CompositeRepository(array());
  105. $this->assertEquals(array(), call_user_func_array(array($repo, $method), $args));
  106. }
  107. public function provideMethodCalls()
  108. {
  109. return array(
  110. array('findPackages', array('foo')),
  111. array('search', array('foo')),
  112. array('getPackages', array()),
  113. );
  114. }
  115. }