FilesystemRepositoryTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. class FilesystemRepositoryTest extends TestCase
  15. {
  16. public function testRepositoryRead()
  17. {
  18. $json = $this->createJsonFileMock();
  19. $repository = new FilesystemRepository($json);
  20. $json
  21. ->expects($this->once())
  22. ->method('read')
  23. ->will($this->returnValue(array(
  24. array('name' => 'package1', 'version' => '1.0.0-beta', 'type' => 'vendor'),
  25. )));
  26. $json
  27. ->expects($this->once())
  28. ->method('exists')
  29. ->will($this->returnValue(true));
  30. $packages = $repository->getPackages();
  31. $this->assertCount(1, $packages);
  32. $this->assertSame('package1', $packages[0]->getName());
  33. $this->assertSame('1.0.0.0-beta', $packages[0]->getVersion());
  34. $this->assertSame('vendor', $packages[0]->getType());
  35. }
  36. /**
  37. * @expectedException \Composer\Repository\InvalidRepositoryException
  38. */
  39. public function testCorruptedRepositoryFile()
  40. {
  41. $json = $this->createJsonFileMock();
  42. $repository = new FilesystemRepository($json);
  43. $json
  44. ->expects($this->once())
  45. ->method('read')
  46. ->will($this->returnValue('foo'));
  47. $json
  48. ->expects($this->once())
  49. ->method('exists')
  50. ->will($this->returnValue(true));
  51. $repository->getPackages();
  52. }
  53. public function testUnexistentRepositoryFile()
  54. {
  55. $json = $this->createJsonFileMock();
  56. $repository = new FilesystemRepository($json);
  57. $json
  58. ->expects($this->once())
  59. ->method('exists')
  60. ->will($this->returnValue(false));
  61. $this->assertEquals(array(), $repository->getPackages());
  62. }
  63. public function testRepositoryWrite()
  64. {
  65. $json = $this->createJsonFileMock();
  66. $repository = new FilesystemRepository($json);
  67. $im = $this->getMockBuilder('Composer\Installer\InstallationManager')
  68. ->disableOriginalConstructor()
  69. ->getMock();
  70. $im->expects($this->once())
  71. ->method('getInstallPath')
  72. ->will($this->returnValue('/foo/bar/vendor/woop/woop'));
  73. $json
  74. ->expects($this->once())
  75. ->method('read')
  76. ->will($this->returnValue(array()));
  77. $json
  78. ->expects($this->once())
  79. ->method('getPath')
  80. ->will($this->returnValue('/foo/bar/vendor/composer/installed.json'));
  81. $json
  82. ->expects($this->once())
  83. ->method('exists')
  84. ->will($this->returnValue(true));
  85. $json
  86. ->expects($this->once())
  87. ->method('write')
  88. ->with(array(
  89. 'packages' => array(array('name' => 'mypkg', 'type' => 'library', 'version' => '0.1.10', 'version_normalized' => '0.1.10.0', 'install-path' => '../woop/woop')),
  90. 'dev' => true,
  91. ));
  92. $repository->addPackage($this->getPackage('mypkg', '0.1.10'));
  93. $repository->write(true, $im);
  94. }
  95. private function createJsonFileMock()
  96. {
  97. return $this->getMockBuilder('Composer\Json\JsonFile')
  98. ->disableOriginalConstructor()
  99. ->getMock();
  100. }
  101. }