From 81e8261692042c53ded136e9e10a7f4560859227 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Haso=C5=88?= Date: Wed, 21 Mar 2012 12:57:50 +0100 Subject: [PATCH 1/5] Added tests for traits --- .../Test/Autoload/ClassMapGeneratorTest.php | 16 ++++++++++- .../Autoload/Fixtures/classmap/multipleNs.php | 3 ++ .../Test/Autoload/Fixtures/php5.4/traits.php | 28 +++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 tests/Composer/Test/Autoload/Fixtures/php5.4/traits.php diff --git a/tests/Composer/Test/Autoload/ClassMapGeneratorTest.php b/tests/Composer/Test/Autoload/ClassMapGeneratorTest.php index d40321768..3937e8a99 100644 --- a/tests/Composer/Test/Autoload/ClassMapGeneratorTest.php +++ b/tests/Composer/Test/Autoload/ClassMapGeneratorTest.php @@ -25,7 +25,7 @@ class ClassMapGeneratorTest extends \PHPUnit_Framework_TestCase public function getTestCreateMapTests() { - return array( + $data = array( array(__DIR__.'/Fixtures/Namespaced', array( 'Namespaced\\Bar' => realpath(__DIR__).'/Fixtures/Namespaced/Bar.php', 'Namespaced\\Foo' => realpath(__DIR__).'/Fixtures/Namespaced/Foo.php', @@ -44,6 +44,7 @@ class ClassMapGeneratorTest extends \PHPUnit_Framework_TestCase array(__DIR__.'/Fixtures/classmap', array( 'Foo\\Bar\\A' => realpath(__DIR__).'/Fixtures/classmap/sameNsMultipleClasses.php', 'Foo\\Bar\\B' => realpath(__DIR__).'/Fixtures/classmap/sameNsMultipleClasses.php', + 'A' => realpath(__DIR__).'/Fixtures/classmap/multipleNs.php', 'Alpha\\A' => realpath(__DIR__).'/Fixtures/classmap/multipleNs.php', 'Alpha\\B' => realpath(__DIR__).'/Fixtures/classmap/multipleNs.php', 'Beta\\A' => realpath(__DIR__).'/Fixtures/classmap/multipleNs.php', @@ -53,6 +54,19 @@ class ClassMapGeneratorTest extends \PHPUnit_Framework_TestCase 'ClassMap\\SomeClass' => realpath(__DIR__).'/Fixtures/classmap/SomeClass.php', )), ); + + if (version_compare(PHP_VERSION, '5.4', '>=')) { + $data[] = array(__DIR__.'/Fixtures/php5.4', array( + 'TFoo' => __DIR__.'/Fixtures/php5.4/traits.php', + 'CFoo' => __DIR__.'/Fixtures/php5.4/traits.php', + 'Foo\\TBar' => __DIR__.'/Fixtures/php5.4/traits.php', + 'Foo\\IBar' => __DIR__.'/Fixtures/php5.4/traits.php', + 'Foo\\TFooBar' => __DIR__.'/Fixtures/php5.4/traits.php', + 'Foo\\CBar' => __DIR__.'/Fixtures/php5.4/traits.php', + )); + } + + return $data; } public function testCreateMapFinderSupport() diff --git a/tests/Composer/Test/Autoload/Fixtures/classmap/multipleNs.php b/tests/Composer/Test/Autoload/Fixtures/classmap/multipleNs.php index 27f96f704..d19e07fc1 100644 --- a/tests/Composer/Test/Autoload/Fixtures/classmap/multipleNs.php +++ b/tests/Composer/Test/Autoload/Fixtures/classmap/multipleNs.php @@ -1,4 +1,7 @@ Date: Wed, 21 Mar 2012 13:58:35 +0100 Subject: [PATCH 2/5] Added test for generated classmap files --- .../Test/Autoload/AutoloadGeneratorTest.php | 46 ++++++++++++++++--- .../Autoload/Fixtures/autoload_classmap.php | 10 ++++ .../Autoload/Fixtures/autoload_classmap2.php | 10 ++++ .../Autoload/Fixtures/autoload_classmap3.php | 10 ++++ .../Autoload/Fixtures/autoload_classmap4.php | 12 +++++ 5 files changed, 82 insertions(+), 6 deletions(-) create mode 100644 tests/Composer/Test/Autoload/Fixtures/autoload_classmap.php create mode 100644 tests/Composer/Test/Autoload/Fixtures/autoload_classmap2.php create mode 100644 tests/Composer/Test/Autoload/Fixtures/autoload_classmap3.php create mode 100644 tests/Composer/Test/Autoload/Fixtures/autoload_classmap4.php diff --git a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php index 9d2d243a9..31be61d39 100644 --- a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php +++ b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php @@ -66,21 +66,34 @@ class AutoloadGeneratorTest extends TestCase } elseif (is_dir($this->vendorDir)) { $this->fs->removeDirectory($this->vendorDir); } + if (is_dir($this->workingDir.'/.composersrc')) { + $this->fs->removeDirectory($this->workingDir.'/.composersrc'); + } + chdir($this->dir); } public function testMainPackageAutoloading() { $package = new MemoryPackage('a', '1.0', '1.0'); - $package->setAutoload(array('psr-0' => array('Main' => 'src/', 'Lala' => 'src/'))); + $package->setAutoload(array( + 'psr-0' => array('Main' => 'src/', 'Lala' => 'src/'), + 'classmap' => array('.composersrc/'), + )); $this->repository->expects($this->once()) ->method('getPackages') ->will($this->returnValue(array())); - mkdir($this->vendorDir.'/.composer'); + if (!is_dir($this->vendorDir.'/.composer')) { + mkdir($this->vendorDir.'/.composer'); + } + + $this->createClassFile($this->workingDir); + $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/.composer'); $this->assertAutoloadFiles('main', $this->vendorDir.'/.composer'); + $this->assertAutoloadFiles('classmap', $this->vendorDir.'/.composer', 'classmap'); } public function testVendorDirSameAsWorkingDir() @@ -88,7 +101,10 @@ class AutoloadGeneratorTest extends TestCase $this->vendorDir = $this->workingDir; $package = new MemoryPackage('a', '1.0', '1.0'); - $package->setAutoload(array('psr-0' => array('Main' => 'src/', 'Lala' => 'src/'))); + $package->setAutoload(array( + 'psr-0' => array('Main' => 'src/', 'Lala' => 'src/'), + 'classmap' => array('.composersrc/'), + )); $this->repository->expects($this->once()) ->method('getPackages') @@ -98,14 +114,20 @@ class AutoloadGeneratorTest extends TestCase mkdir($this->vendorDir.'/.composer', 0777, true); } + $this->createClassFile($this->vendorDir); + $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/.composer'); $this->assertAutoloadFiles('main3', $this->vendorDir.'/.composer'); + $this->assertAutoloadFiles('classmap3', $this->vendorDir.'/.composer', 'classmap'); } public function testMainPackageAutoloadingAlternativeVendorDir() { $package = new MemoryPackage('a', '1.0', '1.0'); - $package->setAutoload(array('psr-0' => array('Main' => 'src/', 'Lala' => 'src/'))); + $package->setAutoload(array( + 'psr-0' => array('Main' => 'src/', 'Lala' => 'src/'), + 'classmap' => array('.composersrc/'), + )); $this->repository->expects($this->once()) ->method('getPackages') @@ -113,8 +135,10 @@ class AutoloadGeneratorTest extends TestCase $this->vendorDir .= '/subdir'; mkdir($this->vendorDir.'/.composer', 0777, true); + $this->createClassFile($this->workingDir); $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/.composer'); $this->assertAutoloadFiles('main2', $this->vendorDir.'/.composer'); + $this->assertAutoloadFiles('classmap2', $this->vendorDir.'/.composer', 'classmap'); } public function testVendorsAutoloading() @@ -169,6 +193,7 @@ class AutoloadGeneratorTest extends TestCase ), include ($this->vendorDir.'/.composer/autoload_classmap.php') ); + $this->assertAutoloadFiles('classmap4', $this->vendorDir.'/.composer', 'classmap'); } public function testOverrideVendorsAutoloading() @@ -191,8 +216,17 @@ class AutoloadGeneratorTest extends TestCase $this->assertAutoloadFiles('override_vendors', $this->vendorDir.'/.composer'); } - private function assertAutoloadFiles($name, $dir) + private function createClassFile($basedir) + { + if (!is_dir($basedir.'/.composersrc')) { + mkdir($basedir.'/.composersrc', 0777, true); + } + + file_put_contents($basedir.'/.composersrc/foo.php', 'assertFileEquals(__DIR__.'/Fixtures/autoload_'.$name.'.php', $dir.'/autoload_namespaces.php'); + $this->assertFileEquals(__DIR__.'/Fixtures/autoload_'.$name.'.php', $dir.'/autoload_'.$type.'.php'); } } diff --git a/tests/Composer/Test/Autoload/Fixtures/autoload_classmap.php b/tests/Composer/Test/Autoload/Fixtures/autoload_classmap.php new file mode 100644 index 000000000..8c1ab4190 --- /dev/null +++ b/tests/Composer/Test/Autoload/Fixtures/autoload_classmap.php @@ -0,0 +1,10 @@ + $baseDir . '/.composersrc/foo.php', +); diff --git a/tests/Composer/Test/Autoload/Fixtures/autoload_classmap2.php b/tests/Composer/Test/Autoload/Fixtures/autoload_classmap2.php new file mode 100644 index 000000000..2931e0f09 --- /dev/null +++ b/tests/Composer/Test/Autoload/Fixtures/autoload_classmap2.php @@ -0,0 +1,10 @@ + $baseDir . '/.composersrc/foo.php', +); diff --git a/tests/Composer/Test/Autoload/Fixtures/autoload_classmap3.php b/tests/Composer/Test/Autoload/Fixtures/autoload_classmap3.php new file mode 100644 index 000000000..15917ec1a --- /dev/null +++ b/tests/Composer/Test/Autoload/Fixtures/autoload_classmap3.php @@ -0,0 +1,10 @@ + $baseDir . '/.composersrc/foo.php', +); diff --git a/tests/Composer/Test/Autoload/Fixtures/autoload_classmap4.php b/tests/Composer/Test/Autoload/Fixtures/autoload_classmap4.php new file mode 100644 index 000000000..944a80bed --- /dev/null +++ b/tests/Composer/Test/Autoload/Fixtures/autoload_classmap4.php @@ -0,0 +1,12 @@ + $baseDir . '/composer-test-autoload/b/b/lib/c.php', + 'ClassMapFoo' => $baseDir . '/composer-test-autoload/a/a/src/a.php', + 'ClassMapBar' => $baseDir . '/composer-test-autoload/b/b/src/b.php', +); From afc04c9e53bfad3029a1927b25455302e4fcd6f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Haso=C5=88?= Date: Wed, 21 Mar 2012 14:29:24 +0100 Subject: [PATCH 3/5] Fixed creating shortest paths in AutoloadGenerator --- src/Composer/Autoload/AutoloadGenerator.php | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index 036b40b95..f504a6bca 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -58,7 +58,7 @@ EOF; $filesystem = new Filesystem(); $vendorPath = strtr(realpath($installationManager->getVendorPath()), '\\', '/'); - $relVendorPath = $filesystem->findShortestPath(getcwd(), $vendorPath); + $relVendorPath = $filesystem->findShortestPath(getcwd().'/inDir', $vendorPath); $vendorDirCode = $filesystem->findShortestPathCode(realpath($targetDir), $vendorPath, true); $appBaseDir = $filesystem->findShortestPathCode($vendorPath, getcwd(), true); @@ -85,11 +85,7 @@ EOF; $path = strtr($path, '\\', '/'); $baseDir = ''; if (!$filesystem->isAbsolutePath($path)) { - // vendor dir == working dir - if (preg_match('{^(\./?)?$}', $relVendorPath)) { - $path = '/'.$path; - $baseDir = '$vendorDir . '; - } elseif (strpos($path, $relVendorPath) === 0) { + if (strpos($path, $relVendorPath) === 0) { // path starts with vendor dir $path = substr($path, strlen($relVendorPath)); $baseDir = '$vendorDir . '; @@ -129,7 +125,7 @@ EOF; $autoloads['classmap'] = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($autoloads['classmap'])); foreach ($autoloads['classmap'] as $dir) { foreach (ClassMapGenerator::createMap($dir) as $class => $path) { - $path = '/'.$filesystem->findShortestPath(getcwd(), $path); + $path = '/'.$filesystem->findShortestPath(getcwd().'/inDir', $path); $classmapFile .= ' '.var_export($class, true).' => $baseDir . '.var_export($path, true).",\n"; } } From 1f8c9eeb6c1ec516405678746f0b3d326fcdc77f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Haso=C5=88?= Date: Thu, 22 Mar 2012 10:11:48 +0100 Subject: [PATCH 4/5] Added parameter $inDirectory to method Filesystem::findShortestPath --- src/Composer/Autoload/AutoloadGenerator.php | 4 ++-- src/Composer/Util/Filesystem.php | 9 +++++++-- tests/Composer/Test/Util/FilesystemTest.php | 10 ++++++++-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index f504a6bca..0bf0ca087 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -58,7 +58,7 @@ EOF; $filesystem = new Filesystem(); $vendorPath = strtr(realpath($installationManager->getVendorPath()), '\\', '/'); - $relVendorPath = $filesystem->findShortestPath(getcwd().'/inDir', $vendorPath); + $relVendorPath = $filesystem->findShortestPath(getcwd(), $vendorPath, true); $vendorDirCode = $filesystem->findShortestPathCode(realpath($targetDir), $vendorPath, true); $appBaseDir = $filesystem->findShortestPathCode($vendorPath, getcwd(), true); @@ -125,7 +125,7 @@ EOF; $autoloads['classmap'] = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($autoloads['classmap'])); foreach ($autoloads['classmap'] as $dir) { foreach (ClassMapGenerator::createMap($dir) as $class => $path) { - $path = '/'.$filesystem->findShortestPath(getcwd().'/inDir', $path); + $path = '/'.$filesystem->findShortestPath(getcwd(), $path, true); $classmapFile .= ' '.var_export($class, true).' => $baseDir . '.var_export($path, true).",\n"; } } diff --git a/src/Composer/Util/Filesystem.php b/src/Composer/Util/Filesystem.php index dd0163717..0cd687a40 100644 --- a/src/Composer/Util/Filesystem.php +++ b/src/Composer/Util/Filesystem.php @@ -58,9 +58,10 @@ class Filesystem * * @param string $from * @param string $to + * @param Boolean $inDirectory if true, the source is considered to be in the directory * @return string */ - public function findShortestPath($from, $to) + public function findShortestPath($from, $to, $inDirectory = false) { if (!$this->isAbsolutePath($from) || !$this->isAbsolutePath($to)) { throw new \InvalidArgumentException('from and to must be absolute paths'); @@ -69,6 +70,10 @@ class Filesystem $from = lcfirst(rtrim(strtr($from, '\\', '/'), '/')); $to = lcfirst(rtrim(strtr($to, '\\', '/'), '/')); + if ($inDirectory) { + $from .= '/dummy_file'; + } + if (dirname($from) === dirname($to)) { return './'.basename($to); } @@ -143,4 +148,4 @@ class Filesystem { return new ProcessExecutor; } -} \ No newline at end of file +} diff --git a/tests/Composer/Test/Util/FilesystemTest.php b/tests/Composer/Test/Util/FilesystemTest.php index 81ece7d35..739b30cbe 100644 --- a/tests/Composer/Test/Util/FilesystemTest.php +++ b/tests/Composer/Test/Util/FilesystemTest.php @@ -58,10 +58,10 @@ class FilesystemTest extends TestCase /** * @dataProvider providePathCouples */ - public function testFindShortestPath($a, $b, $expected) + public function testFindShortestPath($a, $b, $expected, $inDirectory = false) { $fs = new Filesystem; - $this->assertEquals($expected, $fs->findShortestPath($a, $b)); + $this->assertEquals($expected, $fs->findShortestPath($a, $b, $inDirectory)); } public function providePathCouples() @@ -69,8 +69,13 @@ class FilesystemTest extends TestCase return array( array('/foo/bar', '/foo/bar', "./bar"), array('/foo/bar', '/foo/baz', "./baz"), + array('/foo/bar/', '/foo/baz', "./baz"), + array('/foo/bar', '/foo/bar', "./", true), + array('/foo/bar', '/foo/baz', "../baz", true), + array('/foo/bar/', '/foo/baz', "../baz", true), array('/foo/bin/run', '/foo/vendor/acme/bin/run', "../vendor/acme/bin/run"), array('/foo/bin/run', '/bar/bin/run', "/bar/bin/run"), + array('/foo/bin/run', '/bar/bin/run', "/bar/bin/run", true), array('c:/bin/run', 'c:/vendor/acme/bin/run', "../vendor/acme/bin/run"), array('c:\\bin\\run', 'c:/vendor/acme/bin/run', "../vendor/acme/bin/run"), array('c:/bin/run', 'd:/vendor/acme/bin/run', "d:/vendor/acme/bin/run"), @@ -79,6 +84,7 @@ class FilesystemTest extends TestCase array('/tmp/test', '/tmp', "./"), array('C:/Temp/test/sub', 'C:\Temp', "../"), array('/tmp/test/sub', '/tmp', "../"), + array('/tmp/test/sub', '/tmp', "../../", true), array('/tmp', '/tmp/test', "test"), array('C:/Temp', 'C:\Temp\test', "test"), array('C:/Temp', 'c:\Temp\test', "test"), From 36d9513d7be56286017830338d0bdf010d3a8d75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Haso=C5=88?= Date: Fri, 23 Mar 2012 12:49:29 +0100 Subject: [PATCH 5/5] Changed parameter name in Filesystem::findShortestPath --- src/Composer/Util/Filesystem.php | 6 +++--- tests/Composer/Test/Util/FilesystemTest.php | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Composer/Util/Filesystem.php b/src/Composer/Util/Filesystem.php index 0cd687a40..88e8a77c2 100644 --- a/src/Composer/Util/Filesystem.php +++ b/src/Composer/Util/Filesystem.php @@ -58,10 +58,10 @@ class Filesystem * * @param string $from * @param string $to - * @param Boolean $inDirectory if true, the source is considered to be in the directory + * @param Boolean $directories if true, the source/target are considered to be directories * @return string */ - public function findShortestPath($from, $to, $inDirectory = false) + public function findShortestPath($from, $to, $directories = false) { if (!$this->isAbsolutePath($from) || !$this->isAbsolutePath($to)) { throw new \InvalidArgumentException('from and to must be absolute paths'); @@ -70,7 +70,7 @@ class Filesystem $from = lcfirst(rtrim(strtr($from, '\\', '/'), '/')); $to = lcfirst(rtrim(strtr($to, '\\', '/'), '/')); - if ($inDirectory) { + if ($directories) { $from .= '/dummy_file'; } diff --git a/tests/Composer/Test/Util/FilesystemTest.php b/tests/Composer/Test/Util/FilesystemTest.php index 739b30cbe..37e9f1129 100644 --- a/tests/Composer/Test/Util/FilesystemTest.php +++ b/tests/Composer/Test/Util/FilesystemTest.php @@ -58,10 +58,10 @@ class FilesystemTest extends TestCase /** * @dataProvider providePathCouples */ - public function testFindShortestPath($a, $b, $expected, $inDirectory = false) + public function testFindShortestPath($a, $b, $expected, $directory = false) { $fs = new Filesystem; - $this->assertEquals($expected, $fs->findShortestPath($a, $b, $inDirectory)); + $this->assertEquals($expected, $fs->findShortestPath($a, $b, $directory)); } public function providePathCouples()