From abb926a60c55573205ed33d0c0b2a5dc37d7b06d Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sat, 3 Dec 2011 23:20:06 +0100 Subject: [PATCH] Add tests for autoloader --- .../Test/Autoload/AutoloadGeneratorTest.php | 120 ++++++++++++++++++ .../Test/Autoload/Fixtures/autoload_main.php | 10 ++ .../Test/Autoload/Fixtures/autoload_main2.php | 10 ++ .../Autoload/Fixtures/autoload_vendors.php | 11 ++ 4 files changed, 151 insertions(+) create mode 100644 tests/Composer/Test/Autoload/AutoloadGeneratorTest.php create mode 100644 tests/Composer/Test/Autoload/Fixtures/autoload_main.php create mode 100644 tests/Composer/Test/Autoload/Fixtures/autoload_main2.php create mode 100644 tests/Composer/Test/Autoload/Fixtures/autoload_vendors.php diff --git a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php new file mode 100644 index 000000000..f7b327092 --- /dev/null +++ b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php @@ -0,0 +1,120 @@ + + * Jordi Boggiano + * + * 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\Autoload\AutoloadGenerator; +use Composer\Downloader\Util\Filesystem; +use Composer\Package\MemoryPackage; + +class AutoloadGeneratorTest extends \PHPUnit_Framework_TestCase +{ + public $vendorDir; + private $workingDir; + private $im; + private $repository; + private $generator; + + protected function setUp() + { + $fs = new Filesystem; + $that = $this; + + $this->workingDir = sys_get_temp_dir(); + $this->vendorDir = $this->workingDir.DIRECTORY_SEPARATOR.'composer-test-autoload'; + if (is_dir($this->vendorDir)) { + $fs->removeDirectory($this->vendorDir); + } + mkdir($this->vendorDir); + + $this->dir = getcwd(); + chdir($this->workingDir); + + $this->im = $this->getMockBuilder('Composer\Installer\InstallationManager') + ->disableOriginalConstructor() + ->getMock(); + $this->im->expects($this->any()) + ->method('getInstallPath') + ->will($this->returnCallback(function ($package) use ($that) { + return $that->vendorDir.'/'.$package->getName(); + })); + $this->im->expects($this->any()) + ->method('getVendorPath') + ->will($this->returnCallback(function () use ($that) { + return $that->vendorDir; + })); + + $this->repo = $this->getMockBuilder('Composer\Repository\RepositoryInterface') + ->disableOriginalConstructor() + ->getMock(); + + $this->generator = new AutoloadGenerator(); + } + + protected function tearDown() + { + chdir($this->dir); + } + + public function testMainPackageAutoloading() + { + $package = new MemoryPackage('a', '1.0', '1.0'); + $package->setAutoload(array('psr-0' => array('Main' => 'src/', 'Lala' => 'src/'))); + + $this->repo->expects($this->once()) + ->method('getPackages') + ->will($this->returnValue(array())); + + mkdir($this->vendorDir.'/.composer'); + $this->generator->dump($this->repo, $package, $this->im, $this->vendorDir.'/.composer'); + $this->assertAutoloadFiles('main', $this->vendorDir.'/.composer'); + } + + public function testMainPackageAutoloadingAlternativeVendorDir() + { + $package = new MemoryPackage('a', '1.0', '1.0'); + $package->setAutoload(array('psr-0' => array('Main' => 'src/', 'Lala' => 'src/'))); + + $this->repo->expects($this->once()) + ->method('getPackages') + ->will($this->returnValue(array())); + + $this->vendorDir .= '/subdir'; + mkdir($this->vendorDir.'/.composer', 0777, true); + $this->generator->dump($this->repo, $package, $this->im, $this->vendorDir.'/.composer'); + $this->assertAutoloadFiles('main2', $this->vendorDir.'/.composer'); + } + + public function testVendorsAutoloading() + { + $package = new MemoryPackage('a', '1.0', '1.0'); + + $packages = array(); + $packages[] = $a = new MemoryPackage('a/a', '1.0', '1.0'); + $packages[] = $b = new MemoryPackage('b/b', '1.0', '1.0'); + $a->setAutoload(array('psr-0' => array('A' => 'src/', 'A\\B' => 'lib/'))); + $b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/'))); + + $this->repo->expects($this->once()) + ->method('getPackages') + ->will($this->returnValue($packages)); + + mkdir($this->vendorDir.'/.composer', 0777, true); + $this->generator->dump($this->repo, $package, $this->im, $this->vendorDir.'/.composer'); + $this->assertAutoloadFiles('vendors', $this->vendorDir.'/.composer'); + } + + private function assertAutoloadFiles($name, $dir) + { + $this->assertEquals(file_get_contents(__DIR__.'/Fixtures/autoload_'.$name.'.php'), file_get_contents($dir.'/autoload_namespaces.php')); + } +} diff --git a/tests/Composer/Test/Autoload/Fixtures/autoload_main.php b/tests/Composer/Test/Autoload/Fixtures/autoload_main.php new file mode 100644 index 000000000..157d888f3 --- /dev/null +++ b/tests/Composer/Test/Autoload/Fixtures/autoload_main.php @@ -0,0 +1,10 @@ + dirname($vendorDir) . '/src/', + 'Lala' => dirname($vendorDir) . '/src/', +); diff --git a/tests/Composer/Test/Autoload/Fixtures/autoload_main2.php b/tests/Composer/Test/Autoload/Fixtures/autoload_main2.php new file mode 100644 index 000000000..a0fa2c7db --- /dev/null +++ b/tests/Composer/Test/Autoload/Fixtures/autoload_main2.php @@ -0,0 +1,10 @@ + dirname(dirname($vendorDir)) . '/src/', + 'Lala' => dirname(dirname($vendorDir)) . '/src/', +); diff --git a/tests/Composer/Test/Autoload/Fixtures/autoload_vendors.php b/tests/Composer/Test/Autoload/Fixtures/autoload_vendors.php new file mode 100644 index 000000000..b149046df --- /dev/null +++ b/tests/Composer/Test/Autoload/Fixtures/autoload_vendors.php @@ -0,0 +1,11 @@ + $vendorDir . '/b/b/src/', + 'A\\B' => $vendorDir . '/a/a/lib/', + 'A' => $vendorDir . '/a/a/src/', +);