FilesystemRepositoryTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. $json
  68. ->expects($this->once())
  69. ->method('read')
  70. ->will($this->returnValue(array()));
  71. $json
  72. ->expects($this->once())
  73. ->method('exists')
  74. ->will($this->returnValue(true));
  75. $json
  76. ->expects($this->once())
  77. ->method('write')
  78. ->with(array(
  79. array('name' => 'mypkg', 'type' => 'library', 'version' => '0.1.10', 'version_normalized' => '0.1.10.0'),
  80. ));
  81. $repository->addPackage($this->getPackage('mypkg', '0.1.10'));
  82. $repository->write();
  83. }
  84. private function createJsonFileMock()
  85. {
  86. return $this->getMockBuilder('Composer\Json\JsonFile')
  87. ->disableOriginalConstructor()
  88. ->getMock();
  89. }
  90. }