ArrayRepositoryTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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\ArrayRepository;
  13. use Composer\Repository\RepositoryInterface;
  14. use Composer\Test\TestCase;
  15. class ArrayRepositoryTest extends TestCase
  16. {
  17. public function testAddPackage()
  18. {
  19. $repo = new ArrayRepository;
  20. $repo->addPackage($this->getPackage('foo', '1'));
  21. $this->assertCount(1, $repo);
  22. }
  23. public function testRemovePackage()
  24. {
  25. $package = $this->getPackage('bar', '2');
  26. $repo = new ArrayRepository;
  27. $repo->addPackage($this->getPackage('foo', '1'));
  28. $repo->addPackage($package);
  29. $this->assertCount(2, $repo);
  30. $repo->removePackage($this->getPackage('foo', '1'));
  31. $this->assertCount(1, $repo);
  32. $this->assertEquals(array($package), $repo->getPackages());
  33. }
  34. public function testHasPackage()
  35. {
  36. $repo = new ArrayRepository;
  37. $repo->addPackage($this->getPackage('foo', '1'));
  38. $repo->addPackage($this->getPackage('bar', '2'));
  39. $this->assertTrue($repo->hasPackage($this->getPackage('foo', '1')));
  40. $this->assertFalse($repo->hasPackage($this->getPackage('bar', '1')));
  41. }
  42. public function testFindPackages()
  43. {
  44. $repo = new ArrayRepository();
  45. $repo->addPackage($this->getPackage('foo', '1'));
  46. $repo->addPackage($this->getPackage('bar', '2'));
  47. $repo->addPackage($this->getPackage('bar', '3'));
  48. $foo = $repo->findPackages('foo');
  49. $this->assertCount(1, $foo);
  50. $this->assertEquals('foo', $foo[0]->getName());
  51. $bar = $repo->findPackages('bar');
  52. $this->assertCount(2, $bar);
  53. $this->assertEquals('bar', $bar[0]->getName());
  54. }
  55. public function testAutomaticallyAddAliasedPackageButNotRemove()
  56. {
  57. $repo = new ArrayRepository();
  58. $package = $this->getPackage('foo', '1');
  59. $alias = $this->getAliasPackage($package, '2');
  60. $repo->addPackage($alias);
  61. $this->assertCount(2, $repo);
  62. $this->assertTrue($repo->hasPackage($this->getPackage('foo', '1')));
  63. $this->assertTrue($repo->hasPackage($this->getPackage('foo', '2')));
  64. $repo->removePackage($alias);
  65. $this->assertCount(1, $repo);
  66. }
  67. public function testSearch()
  68. {
  69. $repo = new ArrayRepository();
  70. $repo->addPackage($this->getPackage('foo', '1'));
  71. $repo->addPackage($this->getPackage('bar', '1'));
  72. $this->assertSame(
  73. array(array('name' => 'foo', 'description' => null)),
  74. $repo->search('foo', RepositoryInterface::SEARCH_FULLTEXT)
  75. );
  76. $this->assertSame(
  77. array(array('name' => 'bar', 'description' => null)),
  78. $repo->search('bar')
  79. );
  80. $this->assertEmpty(
  81. $repo->search('foobar')
  82. );
  83. }
  84. public function testSearchWithPackageType()
  85. {
  86. $repo = new ArrayRepository();
  87. $repo->addPackage($this->getPackage('foo', '1', 'Composer\Package\CompletePackage'));
  88. $repo->addPackage($this->getPackage('bar', '1', 'Composer\Package\CompletePackage'));
  89. $package = $this->getPackage('foobar', '1', 'Composer\Package\CompletePackage');
  90. $package->setType('composer-plugin');
  91. $repo->addPackage($package);
  92. $this->assertSame(
  93. array(array('name' => 'foo', 'description' => null)),
  94. $repo->search('foo', RepositoryInterface::SEARCH_FULLTEXT, 'library')
  95. );
  96. $this->assertEmpty($repo->search('bar', RepositoryInterface::SEARCH_FULLTEXT, 'package'));
  97. $this->assertSame(
  98. array(array('name' => 'foobar', 'description' => null)),
  99. $repo->search('foo', 0, 'composer-plugin')
  100. );
  101. }
  102. }