浏览代码

Added repository tests

Samuel ROZE 9 年之前
父节点
当前提交
6c16153302

+ 8 - 4
src/Composer/Repository/PathRepository.php

@@ -16,6 +16,7 @@ use Composer\Config;
 use Composer\IO\IOInterface;
 use Composer\Json\JsonFile;
 use Composer\Package\Loader\ArrayLoader;
+use Composer\Package\Loader\LoaderInterface;
 use Composer\Package\Version\VersionGuesser;
 use Composer\Package\Version\VersionParser;
 use Composer\Util\ProcessExecutor;
@@ -83,19 +84,22 @@ class PathRepository extends ArrayRepository
      * @param array $packageConfig
      * @param IOInterface $io
      * @param Config $config
+     * @param LoaderInterface $loader
+     * @param Filesystem $filesystem
+     * @param VersionGuesser $versionGuesser
      */
-    public function __construct(array $packageConfig, IOInterface $io, Config $config)
+    public function __construct(array $packageConfig, IOInterface $io, Config $config, LoaderInterface $loader = null, Filesystem $filesystem = null, VersionGuesser $versionGuesser = null)
     {
         if (!isset($packageConfig['url'])) {
             throw new \RuntimeException('You must specify the `url` configuration for the path repository');
         }
 
-        $this->fileSystem = new Filesystem();
-        $this->loader = new ArrayLoader();
+        $this->fileSystem = $filesystem ?: new Filesystem();
+        $this->loader = $loader ?: new ArrayLoader();
         $this->config = $config;
         $this->packageConfig = $packageConfig;
         $this->path = realpath(rtrim($packageConfig['url'], '/')) . '/';
-        $this->versionGuesser = new VersionGuesser(new ProcessExecutor($io), new VersionParser(), $this->path);
+        $this->versionGuesser = $versionGuesser ?: new VersionGuesser(new ProcessExecutor($io), new VersionParser(), $this->path);
     }
 
     /**

+ 4 - 0
tests/Composer/Test/Repository/Fixtures/path/with-version/composer.json

@@ -0,0 +1,4 @@
+{
+  "name": "test/path",
+  "version": "0.0.2"
+}

+ 3 - 0
tests/Composer/Test/Repository/Fixtures/path/without-version/composer.json

@@ -0,0 +1,3 @@
+{
+  "name": "test/path"
+}

+ 62 - 0
tests/Composer/Test/Repository/PathRepositoryTest.php

@@ -0,0 +1,62 @@
+<?php
+
+/*
+ * This file is part of Composer.
+ *
+ * (c) Nils Adermann <naderman@naderman.de>
+ *     Jordi Boggiano <j.boggiano@seld.be>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Composer\Repository;
+
+use Composer\Package\Loader\ArrayLoader;
+use Composer\Package\Version\VersionParser;
+use Composer\TestCase;
+use Composer\IO\NullIO;
+use Composer\Config;
+use Composer\Package\BasePackage;
+
+class PathRepositoryTest extends TestCase
+{
+    public function testLoadPackageFromFileSystemWithVersion()
+    {
+        $ioInterface = $this->getMockBuilder('Composer\IO\IOInterface')
+            ->getMock();
+
+        $config = new \Composer\Config();
+        $loader = new ArrayLoader(new VersionParser());
+        $versionGuesser = null;
+
+        $repositoryUrl = implode(DIRECTORY_SEPARATOR, array(__DIR__, 'Fixtures', 'path', 'with-version'));
+        $repository = new PathRepository(array('url' => $repositoryUrl), $ioInterface, $config, $loader);
+        $repository->getPackages();
+
+        $this->assertEquals(1, $repository->count());
+        $this->assertTrue($repository->hasPackage($this->getPackage('test/path', '0.0.2')));
+    }
+
+    public function testLoadPackageFromFileSystemWithoutVersion()
+    {
+        $ioInterface = $this->getMockBuilder('Composer\IO\IOInterface')
+            ->getMock();
+
+        $config = new \Composer\Config();
+        $loader = new ArrayLoader(new VersionParser());
+        $versionGuesser = null;
+
+        $repositoryUrl = implode(DIRECTORY_SEPARATOR, array(__DIR__, 'Fixtures', 'path', 'without-version'));
+        $repository = new PathRepository(array('url' => $repositoryUrl), $ioInterface, $config, $loader);
+        $packages = $repository->getPackages();
+
+        $this->assertEquals(1, $repository->count());
+
+        $package = $packages[0];
+        $this->assertEquals('test/path', $package->getName());
+
+        $packageVersion = $package->getVersion();
+        $this->assertTrue(!empty($packageVersion));
+    }
+}