FilesystemRepositoryTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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\FilesystemRepository;
  13. use Composer\Test\TestCase;
  14. use Composer\Json\JsonFile;
  15. class FilesystemRepositoryTest extends TestCase
  16. {
  17. public function testRepositoryRead()
  18. {
  19. $json = $this->createJsonFileMock();
  20. $repository = new FilesystemRepository($json);
  21. $json
  22. ->expects($this->once())
  23. ->method('read')
  24. ->will($this->returnValue(array(
  25. array('name' => 'package1', 'version' => '1.0.0-beta', 'type' => 'vendor'),
  26. )));
  27. $json
  28. ->expects($this->once())
  29. ->method('exists')
  30. ->will($this->returnValue(true));
  31. $packages = $repository->getPackages();
  32. $this->assertCount(1, $packages);
  33. $this->assertSame('package1', $packages[0]->getName());
  34. $this->assertSame('1.0.0.0-beta', $packages[0]->getVersion());
  35. $this->assertSame('vendor', $packages[0]->getType());
  36. }
  37. /**
  38. * @expectedException \Composer\Repository\InvalidRepositoryException
  39. */
  40. public function testCorruptedRepositoryFile()
  41. {
  42. $json = $this->createJsonFileMock();
  43. $repository = new FilesystemRepository($json);
  44. $json
  45. ->expects($this->once())
  46. ->method('read')
  47. ->will($this->returnValue('foo'));
  48. $json
  49. ->expects($this->once())
  50. ->method('exists')
  51. ->will($this->returnValue(true));
  52. $repository->getPackages();
  53. }
  54. public function testUnexistentRepositoryFile()
  55. {
  56. $json = $this->createJsonFileMock();
  57. $repository = new FilesystemRepository($json);
  58. $json
  59. ->expects($this->once())
  60. ->method('exists')
  61. ->will($this->returnValue(false));
  62. $this->assertEquals(array(), $repository->getPackages());
  63. }
  64. public function testRepositoryWrite()
  65. {
  66. $json = $this->createJsonFileMock();
  67. $repository = new FilesystemRepository($json);
  68. $im = $this->getMockBuilder('Composer\Installer\InstallationManager')
  69. ->disableOriginalConstructor()
  70. ->getMock();
  71. $im->expects($this->once())
  72. ->method('getInstallPath')
  73. ->will($this->returnValue('/foo/bar/vendor/woop/woop'));
  74. $json
  75. ->expects($this->once())
  76. ->method('read')
  77. ->will($this->returnValue(array()));
  78. $json
  79. ->expects($this->once())
  80. ->method('getPath')
  81. ->will($this->returnValue('/foo/bar/vendor/composer/installed.json'));
  82. $json
  83. ->expects($this->once())
  84. ->method('exists')
  85. ->will($this->returnValue(true));
  86. $json
  87. ->expects($this->once())
  88. ->method('write')
  89. ->with(array(
  90. 'packages' => array(array('name' => 'mypkg', 'type' => 'library', 'version' => '0.1.10', 'version_normalized' => '0.1.10.0', 'install-path' => '../woop/woop')),
  91. 'dev' => true,
  92. ));
  93. $repository->addPackage($this->getPackage('mypkg', '0.1.10'));
  94. $repository->write(true, $im);
  95. }
  96. public function testRepositoryWritesInstalledPhp()
  97. {
  98. $dir = $this->getUniqueTmpDirectory();
  99. $json = new JsonFile($dir.'/installed.json');
  100. $rootPackage = $this->getPackage('__root__', 'dev-master', 'Composer\Package\RootPackage');
  101. $rootPackage->setSourceReference('sourceref-by-default');
  102. $rootPackage->setDistReference('distref');
  103. $this->configureLinks($rootPackage, array('provide' => array('foo/impl' => '2.0')));
  104. $rootPackage = $this->getAliasPackage($rootPackage, '1.10.x-dev');
  105. $repository = new FilesystemRepository($json, true, $rootPackage);
  106. $pkg = $this->getPackage('a/provider', '1.1');
  107. $this->configureLinks($pkg, array('provide' => array('foo/impl' => '^1.1', 'foo/impl2' => '2.0')));
  108. $pkg->setDistReference('distref-as-no-source');
  109. $repository->addPackage($pkg);
  110. $pkg = $this->getPackage('a/provider2', '1.2');
  111. $this->configureLinks($pkg, array('provide' => array('foo/impl' => 'self.version', 'foo/impl2' => '2.0')));
  112. $pkg->setSourceReference('sourceref');
  113. $pkg->setDistReference('distref-as-installed-from-dist');
  114. $pkg->setInstallationSource('dist');
  115. $repository->addPackage($pkg);
  116. $repository->addPackage($this->getAliasPackage($pkg, '1.4'));
  117. $pkg = $this->getPackage('b/replacer', '2.2');
  118. $this->configureLinks($pkg, array('replace' => array('foo/impl2' => 'self.version', 'foo/replaced' => '^3.0')));
  119. $repository->addPackage($pkg);
  120. $pkg = $this->getPackage('c/c', '3.0');
  121. $repository->addPackage($pkg);
  122. $im = $this->getMockBuilder('Composer\Installer\InstallationManager')
  123. ->disableOriginalConstructor()
  124. ->getMock();
  125. $im->expects($this->any())
  126. ->method('getInstallPath')
  127. ->will($this->returnValue('/foo/bar/vendor/woop/woop'));
  128. $repository->write(true, $im);
  129. $this->assertSame(require __DIR__.'/Fixtures/installed.php', require $dir.'/installed.php');
  130. }
  131. private function createJsonFileMock()
  132. {
  133. return $this->getMockBuilder('Composer\Json\JsonFile')
  134. ->disableOriginalConstructor()
  135. ->getMock();
  136. }
  137. }