From 1450ebd837281fe81b99613f180b5a0d48d2de93 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 30 Oct 2011 09:09:46 +0100 Subject: [PATCH 1/2] Fix bug in an exception, added unit tests --- src/Composer/Json/JsonFile.php | 5 +++++ .../Repository/FilesystemRepository.php | 9 ++++---- .../Repository/FilesystemRepositoryTest.php | 22 +++++++++++++++++++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/Composer/Json/JsonFile.php b/src/Composer/Json/JsonFile.php index 8950545d2..2b13d18d3 100644 --- a/src/Composer/Json/JsonFile.php +++ b/src/Composer/Json/JsonFile.php @@ -34,6 +34,11 @@ class JsonFile $this->path = $path; } + public function getPath() + { + return $this->path; + } + /** * Checks whether json file exists. * diff --git a/src/Composer/Repository/FilesystemRepository.php b/src/Composer/Repository/FilesystemRepository.php index 508cb0538..525f71921 100644 --- a/src/Composer/Repository/FilesystemRepository.php +++ b/src/Composer/Repository/FilesystemRepository.php @@ -43,13 +43,14 @@ class FilesystemRepository extends ArrayRepository implements WritableRepository { parent::initialize(); - $packages = null; - if ($this->file->exists()) { - $packages = $this->file->read(); + if (!$this->file->exists()) { + return; } + $packages = $this->file->read(); + if (!is_array($packages)) { - throw new \UnexpectedValueException('Could not parse package list from the '.$this->file.' repository'); + throw new \UnexpectedValueException('Could not parse package list from the '.$this->file->getPath().' repository'); } $loader = new ArrayLoader($this->repositoryManager); diff --git a/tests/Composer/Test/Repository/FilesystemRepositoryTest.php b/tests/Composer/Test/Repository/FilesystemRepositoryTest.php index 8cdd58831..3ee221fa1 100644 --- a/tests/Composer/Test/Repository/FilesystemRepositoryTest.php +++ b/tests/Composer/Test/Repository/FilesystemRepositoryTest.php @@ -43,6 +43,28 @@ class FilesystemRepositoryTest extends \PHPUnit_Framework_TestCase $this->assertSame('vendor', $packages[0]->getType()); } + /** + * @expectedException \UnexpectedValueException + */ + public function testCorruptedRepositoryFile() + { + $json = $this->createJsonFileMock(); + + $repository = new FilesystemRepository($json); + $repository->setRepositoryManager($this->getMock('Composer\Repository\RepositoryManager')); + + $json + ->expects($this->once()) + ->method('read') + ->will($this->returnValue('foo')); + $json + ->expects($this->once()) + ->method('exists') + ->will($this->returnValue(true)); + + $repository->getPackages(); + } + public function testRepositoryWrite() { $json = $this->createJsonFileMock(); From 9c739e71c2b175966034b2c56267744a02f3abee Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 30 Oct 2011 09:10:49 +0100 Subject: [PATCH 2/2] Add some unit tests --- .../Test/Repository/FilesystemRepositoryTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/Composer/Test/Repository/FilesystemRepositoryTest.php b/tests/Composer/Test/Repository/FilesystemRepositoryTest.php index 3ee221fa1..3ef988422 100644 --- a/tests/Composer/Test/Repository/FilesystemRepositoryTest.php +++ b/tests/Composer/Test/Repository/FilesystemRepositoryTest.php @@ -65,6 +65,21 @@ class FilesystemRepositoryTest extends \PHPUnit_Framework_TestCase $repository->getPackages(); } + public function testUnexistentRepositoryFile() + { + $json = $this->createJsonFileMock(); + + $repository = new FilesystemRepository($json); + $repository->setRepositoryManager($this->getMock('Composer\Repository\RepositoryManager')); + + $json + ->expects($this->once()) + ->method('exists') + ->will($this->returnValue(false)); + + $this->assertEquals(array(), $repository->getPackages()); + } + public function testRepositoryWrite() { $json = $this->createJsonFileMock();