Browse Source

Fix bug in an exception, added unit tests

Fabien Potencier 13 years ago
parent
commit
1450ebd837

+ 5 - 0
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.
      *

+ 5 - 4
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);

+ 22 - 0
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();