FilesystemRepositoryTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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\Repository;
  12. use Composer\Repository\FilesystemRepository;
  13. use Composer\Package\MemoryPackage;
  14. class FilesystemRepositoryTest extends \PHPUnit_Framework_TestCase
  15. {
  16. public function testRepositoryRead()
  17. {
  18. $json = $this->createJsonFileMock();
  19. $repository = new FilesystemRepository($json);
  20. $repository->setRepositoryManager($this->getMock('Composer\Repository\RepositoryManager'));
  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->assertSame(1, count($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 \UnexpectedValueException
  39. */
  40. public function testCorruptedRepositoryFile()
  41. {
  42. $json = $this->createJsonFileMock();
  43. $repository = new FilesystemRepository($json);
  44. $repository->setRepositoryManager($this->getMock('Composer\Repository\RepositoryManager'));
  45. $json
  46. ->expects($this->once())
  47. ->method('read')
  48. ->will($this->returnValue('foo'));
  49. $json
  50. ->expects($this->once())
  51. ->method('exists')
  52. ->will($this->returnValue(true));
  53. $repository->getPackages();
  54. }
  55. public function testRepositoryWrite()
  56. {
  57. $json = $this->createJsonFileMock();
  58. $repository = new FilesystemRepository($json);
  59. $repository->setRepositoryManager($this->getMock('Composer\Repository\RepositoryManager'));
  60. $json
  61. ->expects($this->once())
  62. ->method('read')
  63. ->will($this->returnValue(array()));
  64. $json
  65. ->expects($this->once())
  66. ->method('exists')
  67. ->will($this->returnValue(true));
  68. $json
  69. ->expects($this->once())
  70. ->method('write')
  71. ->with(array(
  72. array('name' => 'mypkg', 'type' => 'library', 'names' => array('mypkg'), 'version' => '0.1.10')
  73. ));
  74. $repository->addPackage(new MemoryPackage('mypkg', '0.1.10'));
  75. $repository->write();
  76. }
  77. private function createJsonFileMock()
  78. {
  79. return $this->getMockBuilder('Composer\Json\JsonFile')
  80. ->disableOriginalConstructor()
  81. ->getMock();
  82. }
  83. }