FilesystemRepositoryTest.php 3.0 KB

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