FilesystemRepositoryTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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\Test\TestCase;
  14. class FilesystemRepositoryTest extends 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 testUnexistentRepositoryFile()
  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('exists')
  63. ->will($this->returnValue(false));
  64. $this->assertEquals(array(), $repository->getPackages());
  65. }
  66. public function testRepositoryWrite()
  67. {
  68. $json = $this->createJsonFileMock();
  69. $repository = new FilesystemRepository($json);
  70. $repository->setRepositoryManager($this->getMock('Composer\Repository\RepositoryManager'));
  71. $json
  72. ->expects($this->once())
  73. ->method('read')
  74. ->will($this->returnValue(array()));
  75. $json
  76. ->expects($this->once())
  77. ->method('exists')
  78. ->will($this->returnValue(true));
  79. $json
  80. ->expects($this->once())
  81. ->method('write')
  82. ->with(array(
  83. array('name' => 'mypkg', 'type' => 'library', 'names' => array('mypkg'), 'version' => '0.1.10', 'version_normalized' => '0.1.10.0')
  84. ));
  85. $repository->addPackage($this->getPackage('mypkg', '0.1.10'));
  86. $repository->write();
  87. }
  88. private function createJsonFileMock()
  89. {
  90. return $this->getMockBuilder('Composer\Json\JsonFile')
  91. ->disableOriginalConstructor()
  92. ->getMock();
  93. }
  94. }