Forráskód Böngészése

Add tests and fixes some issues

Jordi Boggiano 13 éve
szülő
commit
b7f6cf69ab

+ 13 - 3
src/Composer/Autoload/AutoloadGenerator.php

@@ -76,7 +76,12 @@ EOF;
             foreach ($autoloads['psr-0'] as $def) {
                 $exportedPrefix = var_export($def['namespace'], true);
                 $exportedPath = var_export($def['path'], true);
-                $namespacesFile .= "    $exportedPrefix => dirname(dirname(__DIR__)).$exportedPath,\n";
+                if (!$this->isAbsolutePath($def['path'])) {
+                    $baseDir = 'dirname(dirname(__DIR__)).';
+                } else {
+                    $baseDir = '';
+                }
+                $namespacesFile .= "    $exportedPrefix => {$baseDir}{$exportedPath},\n";
             }
         }
 
@@ -106,7 +111,7 @@ EOF;
                 foreach ($mapping as $namespace => $path) {
                     $autoloads[$type][] = array(
                         'namespace'   => $namespace,
-                        'path'      => ($installPath ? '/'.$installPath : '').'/'.$path,
+                        'path'      => $installPath.'/'.$path,
                     );
                 }
             }
@@ -133,10 +138,15 @@ EOF;
 
         if (isset($autoloads['psr-0'])) {
             foreach ($autoloads['psr-0'] as $def) {
-                $loader->add($def['namespace'], '.'.$def['path']);
+                $loader->add($def['namespace'], $def['path']);
             }
         }
 
         return $loader;
     }
+
+    protected function isAbsolutePath($path)
+    {
+        return substr($path, 0, 1) === '/' || substr($path, 1, 1) === ':';
+    }
 }

+ 13 - 10
src/Composer/Installer/InstallerInstaller.php

@@ -26,6 +26,7 @@ use Composer\Package\PackageInterface;
 class InstallerInstaller extends LibraryInstaller
 {
     private $installationManager;
+    private static $classCounter = 0;
 
     /**
      * @param   string                      $dir        relative path for packages home
@@ -74,18 +75,20 @@ class InstallerInstaller extends LibraryInstaller
     {
         $downloadPath = $this->getInstallPath($package);
 
-        $extra = $target->getExtra();
+        $extra = $package->getExtra();
         $class = $extra['class'];
+
+        $generator = new AutoloadGenerator;
+        $map = $generator->parseAutoloads(array(array($package, $downloadPath)));
+        $classLoader = $generator->createLoader($map);
+        $classLoader->register();
+
         if (class_exists($class, false)) {
-            $reflClass = new \ReflectionClass($class);
-            $code = file_get_contents($reflClass->getFileName());
-            $code = preg_replace('{^class (\S+)}mi', 'class $1_composer_tmp', $code);
-            eval($code);
-            $class .= '_composer_tmp';
-        } else {
-            $generator = new AutoloadGenerator;
-            $map = $generator->parseAutoloads(array($package, $downloadPath));
-            $generator->createLoader($map)->register();
+            $code = file_get_contents($classLoader->findFile($class));
+            $code = preg_replace('{^class\s+(\S+)}mi', 'class $1_composer_tmp'.self::$classCounter, $code);
+            eval('?>'.$code);
+            $class .= '_composer_tmp'.self::$classCounter;
+            self::$classCounter++;
         }
 
         $extra = $package->getExtra();

+ 18 - 0
tests/Composer/Test/Installer/Fixtures/installer-v1/Installer/Custom.php

@@ -0,0 +1,18 @@
+<?php
+
+namespace Installer;
+
+use Composer\Installer\InstallerInterface;
+use Composer\Package\PackageInterface;
+
+class Custom implements InstallerInterface
+{
+    public $version = 'installer-v1';
+
+    public function supports($packageType) {}
+    public function isInstalled(PackageInterface $package) {}
+    public function install(PackageInterface $package) {}
+    public function update(PackageInterface $initial, PackageInterface $target) {}
+    public function uninstall(PackageInterface $package) {}
+    public function getInstallPath(PackageInterface $package) {}
+}

+ 7 - 0
tests/Composer/Test/Installer/Fixtures/installer-v1/Installer/Exception.php

@@ -0,0 +1,7 @@
+<?php
+
+namespace Installer;
+
+class Exception extends \Exception
+{
+}

+ 9 - 0
tests/Composer/Test/Installer/Fixtures/installer-v1/composer.json

@@ -0,0 +1,9 @@
+{
+    "name": "",
+    "version": "1.0.0",
+    "type": "composer-installer",
+    "autoload": { "psr-0": { "Installer": "" } },
+    "extra": {
+        "class": "Installer\\Custom"
+    }
+}

+ 18 - 0
tests/Composer/Test/Installer/Fixtures/installer-v2/Installer/Custom2.php

@@ -0,0 +1,18 @@
+<?php
+
+namespace Installer;
+
+use Composer\Installer\InstallerInterface;
+use Composer\Package\PackageInterface;
+
+class Custom2 implements InstallerInterface
+{
+    public $version = 'installer-v2';
+
+    public function supports($packageType) {}
+    public function isInstalled(PackageInterface $package) {}
+    public function install(PackageInterface $package) {}
+    public function update(PackageInterface $initial, PackageInterface $target) {}
+    public function uninstall(PackageInterface $package) {}
+    public function getInstallPath(PackageInterface $package) {}
+}

+ 7 - 0
tests/Composer/Test/Installer/Fixtures/installer-v2/Installer/Exception.php

@@ -0,0 +1,7 @@
+<?php
+
+namespace Installer;
+
+class Exception extends \Exception
+{
+}

+ 9 - 0
tests/Composer/Test/Installer/Fixtures/installer-v2/composer.json

@@ -0,0 +1,9 @@
+{
+    "name": "",
+    "version": "2.0.0",
+    "type": "composer-installer",
+    "autoload": { "psr-0": { "Installer": "" } },
+    "extra": {
+        "class": "Installer\\Custom2"
+    }
+}

+ 18 - 0
tests/Composer/Test/Installer/Fixtures/installer-v3/Installer/Custom2.php

@@ -0,0 +1,18 @@
+<?php
+
+namespace Installer;
+
+use Composer\Installer\InstallerInterface;
+use Composer\Package\PackageInterface;
+
+class Custom2 implements InstallerInterface
+{
+    public $version = 'installer-v3';
+
+    public function supports($packageType) {}
+    public function isInstalled(PackageInterface $package) {}
+    public function install(PackageInterface $package) {}
+    public function update(PackageInterface $initial, PackageInterface $target) {}
+    public function uninstall(PackageInterface $package) {}
+    public function getInstallPath(PackageInterface $package) {}
+}

+ 7 - 0
tests/Composer/Test/Installer/Fixtures/installer-v3/Installer/Exception.php

@@ -0,0 +1,7 @@
+<?php
+
+namespace Installer;
+
+class Exception extends \Exception
+{
+}

+ 9 - 0
tests/Composer/Test/Installer/Fixtures/installer-v3/composer.json

@@ -0,0 +1,9 @@
+{
+    "name": "",
+    "version": "3.0.0",
+    "type": "composer-installer",
+    "autoload": { "psr-0": { "Installer": "" } },
+    "extra": {
+        "class": "Installer\\Custom2"
+    }
+}

+ 119 - 0
tests/Composer/Test/Installer/InstallerInstallerTest.php

@@ -0,0 +1,119 @@
+<?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\Test\Installer;
+
+use Composer\Installer\InstallerInstaller;
+use Composer\Package\Loader\JsonLoader;
+use Composer\Package\PackageInterface;
+
+class InstallerInstallerTest extends \PHPUnit_Framework_TestCase
+{
+    protected function setUp()
+    {
+        $repositoryManager = $this->getMockBuilder('Composer\Repository\RepositoryManager')
+            ->disableOriginalConstructor()
+            ->getMock();
+
+        $loader = new JsonLoader($repositoryManager);
+        $this->packages = array();
+        for ($i = 1; $i <= 3; $i++) {
+            $this->packages[] = $loader->load(__DIR__.'/Fixtures/installer-v'.$i.'/composer.json');
+        }
+
+        $this->dm = $this->getMockBuilder('Composer\Downloader\DownloadManager')
+            ->disableOriginalConstructor()
+            ->getMock();
+
+        $this->im = $this->getMockBuilder('Composer\Installer\InstallationManager')
+            ->disableOriginalConstructor()
+            ->getMock();
+
+        $this->repository = $this->getMockBuilder('Composer\Repository\WritableRepositoryInterface')
+            ->disableOriginalConstructor()
+            ->getMock();
+    }
+
+    public function testInstallNewInstaller()
+    {
+        $this->repository
+            ->expects($this->once())
+            ->method('getPackages')
+            ->will($this->returnValue(array()));
+        $installer = new InstallerInstallerMock(__DIR__.'/Fixtures/', $this->dm, $this->repository, $this->im);
+
+        $test = $this;
+        $this->im
+            ->expects($this->once())
+            ->method('addInstaller')
+            ->will($this->returnCallback(function ($installer) use ($test) {
+                $test->assertEquals('installer-v1', $installer->version);
+            }));
+
+        $installer->install($this->packages[0]);
+    }
+
+    public function testUpgradeWithNewClassName()
+    {
+        $this->repository
+            ->expects($this->once())
+            ->method('getPackages')
+            ->will($this->returnValue(array($this->packages[0])));
+        $this->repository
+            ->expects($this->once())
+            ->method('hasPackage')
+            ->will($this->returnValue(true));
+        $installer = new InstallerInstallerMock(__DIR__.'/Fixtures/', $this->dm, $this->repository, $this->im);
+
+        $test = $this;
+        $this->im
+            ->expects($this->once())
+            ->method('addInstaller')
+            ->will($this->returnCallback(function ($installer) use ($test) {
+                $test->assertEquals('installer-v2', $installer->version);
+            }));
+
+        $installer->update($this->packages[0], $this->packages[1]);
+    }
+
+    public function testUpgradeWithSameClassName()
+    {
+        $this->repository
+            ->expects($this->once())
+            ->method('getPackages')
+            ->will($this->returnValue(array($this->packages[1])));
+        $this->repository
+            ->expects($this->once())
+            ->method('hasPackage')
+            ->will($this->returnValue(true));
+        $installer = new InstallerInstallerMock(__DIR__.'/Fixtures/', $this->dm, $this->repository, $this->im);
+
+        $test = $this;
+        $this->im
+            ->expects($this->once())
+            ->method('addInstaller')
+            ->will($this->returnCallback(function ($installer) use ($test) {
+                $test->assertEquals('installer-v3', $installer->version);
+            }));
+
+        $installer->update($this->packages[1], $this->packages[2]);
+    }
+}
+
+class InstallerInstallerMock extends InstallerInstaller
+{
+    public function getInstallPath(PackageInterface $package)
+    {
+        $version = $package->getVersion();
+        return __DIR__.'/Fixtures/installer-v'.$version[0].'/';
+    }
+}

+ 9 - 0
tests/Composer/Test/Json/JsonFileTest.php

@@ -42,6 +42,15 @@ class JsonFileTest extends \PHPUnit_Framework_TestCase
         $this->expectParseException('unescaped backslash (\\) on line 2, char 12', $json);
     }
 
+    public function testParseErrorSkipsEscapedBackslash()
+    {
+        $json = '{
+        "fo\\\\o": "bar"
+        "a": "b"
+}';
+        $this->expectParseException('missing comma on line 2, char 23', $json);
+    }
+
     public function testParseErrorDetectSingleQuotes()
     {
         $json = '{