1
0
Fork 0
composer/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php

870 lines
36 KiB
PHP
Raw Normal View History

2011-12-03 22:20:06 +00:00
<?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.
*/
2012-03-08 13:17:22 +00:00
namespace Composer\Test\Autoload;
2011-12-03 22:20:06 +00:00
use Composer\Autoload\AutoloadGenerator;
use Composer\Package\Link;
use Composer\Util\Filesystem;
2012-05-14 12:01:37 +00:00
use Composer\Package\AliasPackage;
use Composer\Package\Package;
use Composer\Test\TestCase;
2013-01-06 19:34:52 +00:00
use Composer\Script\ScriptEvents;
2011-12-03 22:20:06 +00:00
class AutoloadGeneratorTest extends TestCase
2011-12-03 22:20:06 +00:00
{
public $vendorDir;
private $config;
2011-12-03 22:20:06 +00:00
private $workingDir;
private $im;
private $repository;
private $generator;
private $fs;
2013-01-06 19:34:52 +00:00
private $eventDispatcher;
2011-12-03 22:20:06 +00:00
protected function setUp()
{
$this->fs = new Filesystem;
2011-12-03 22:20:06 +00:00
$that = $this;
2013-02-20 15:50:26 +00:00
$this->workingDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'cmptest-'.md5(uniqid('', true));
$this->fs->ensureDirectoryExists($this->workingDir);
2013-02-20 15:50:26 +00:00
$this->vendorDir = $this->workingDir.DIRECTORY_SEPARATOR.'composer-test-autoload';
$this->ensureDirectoryExistsAndClear($this->vendorDir);
2011-12-03 22:20:06 +00:00
$this->config = $this->getMock('Composer\Config');
$this->config->expects($this->at(0))
->method('get')
->with($this->equalTo('vendor-dir'))
->will($this->returnCallback(function () use ($that) {
return $that->vendorDir;
}));
$this->config->expects($this->at(1))
->method('get')
->with($this->equalTo('vendor-dir'))
->will($this->returnCallback(function () use ($that) {
return $that->vendorDir;
}));
2013-02-20 15:50:26 +00:00
$this->origDir = getcwd();
2011-12-03 22:20:06 +00:00
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) {
$targetDir = $package->getTargetDir();
return $that->vendorDir.'/'.$package->getName() . ($targetDir ? '/'.$targetDir : '');
2011-12-03 22:20:06 +00:00
}));
$this->repository = $this->getMock('Composer\Repository\InstalledRepositoryInterface');
2011-12-03 22:20:06 +00:00
2013-01-06 19:34:52 +00:00
$this->eventDispatcher = $this->getMockBuilder('Composer\Script\EventDispatcher')
->disableOriginalConstructor()
->getMock();
$this->generator = new AutoloadGenerator($this->eventDispatcher);
2011-12-03 22:20:06 +00:00
}
protected function tearDown()
{
2013-02-20 15:50:26 +00:00
chdir($this->origDir);
if (is_dir($this->workingDir)) {
$this->fs->removeDirectory($this->workingDir);
}
if (is_dir($this->vendorDir)) {
$this->fs->removeDirectory($this->vendorDir);
}
2011-12-03 22:20:06 +00:00
}
public function testMainPackageAutoloading()
{
$package = new Package('a', '1.0', '1.0');
$package->setAutoload(array(
'psr-0' => array('Main' => 'src/', 'Lala' => array('src/', 'lib/')),
'classmap' => array('composersrc/'),
));
2011-12-03 22:20:06 +00:00
$this->repository->expects($this->once())
->method('getCanonicalPackages')
2011-12-03 22:20:06 +00:00
->will($this->returnValue(array()));
$this->fs->ensureDirectoryExists($this->workingDir.'/composer');
$this->fs->ensureDirectoryExists($this->workingDir.'/src');
$this->fs->ensureDirectoryExists($this->workingDir.'/lib');
$this->fs->ensureDirectoryExists($this->workingDir.'/composersrc');
file_put_contents($this->workingDir.'/composersrc/foo.php', '<?php class ClassMapFoo {}');
2012-08-14 18:28:49 +00:00
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_1');
$this->assertAutoloadFiles('main', $this->vendorDir.'/composer');
$this->assertAutoloadFiles('classmap', $this->vendorDir.'/composer', 'classmap');
2011-12-03 22:20:06 +00:00
}
2012-01-31 14:04:49 +00:00
public function testVendorDirSameAsWorkingDir()
{
$this->vendorDir = $this->workingDir;
$package = new Package('a', '1.0', '1.0');
$package->setAutoload(array(
'psr-0' => array('Main' => 'src/', 'Lala' => 'src/'),
'classmap' => array('composersrc/'),
));
$this->repository->expects($this->once())
->method('getCanonicalPackages')
->will($this->returnValue(array()));
$this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
$this->fs->ensureDirectoryExists($this->vendorDir.'/src/Main');
file_put_contents($this->vendorDir.'/src/Main/Foo.php', '<?php namespace Main; class Foo {}');
$this->fs->ensureDirectoryExists($this->vendorDir.'/composersrc');
file_put_contents($this->vendorDir.'/composersrc/foo.php', '<?php class ClassMapFoo {}');
2012-08-14 18:28:49 +00:00
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_2');
$this->assertAutoloadFiles('main3', $this->vendorDir.'/composer');
$this->assertAutoloadFiles('classmap3', $this->vendorDir.'/composer', 'classmap');
2012-01-31 14:04:49 +00:00
}
2011-12-03 22:20:06 +00:00
public function testMainPackageAutoloadingAlternativeVendorDir()
{
$package = new Package('a', '1.0', '1.0');
$package->setAutoload(array(
'psr-0' => array('Main' => 'src/', 'Lala' => 'src/'),
'classmap' => array('composersrc/'),
));
2011-12-03 22:20:06 +00:00
$this->repository->expects($this->once())
->method('getCanonicalPackages')
2011-12-03 22:20:06 +00:00
->will($this->returnValue(array()));
$this->vendorDir .= '/subdir';
$this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
$this->fs->ensureDirectoryExists($this->workingDir.'/src');
$this->fs->ensureDirectoryExists($this->workingDir.'/composersrc');
file_put_contents($this->workingDir.'/composersrc/foo.php', '<?php class ClassMapFoo {}');
2012-08-14 18:28:49 +00:00
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_3');
$this->assertAutoloadFiles('main2', $this->vendorDir.'/composer');
$this->assertAutoloadFiles('classmap2', $this->vendorDir.'/composer', 'classmap');
2011-12-03 22:20:06 +00:00
}
public function testMainPackageAutoloadingWithTargetDir()
{
$package = new Package('a', '1.0', '1.0');
$package->setAutoload(array(
2012-04-19 20:04:49 +00:00
'psr-0' => array('Main\\Foo' => '', 'Main\\Bar' => ''),
'classmap' => array('Main/Foo/src', 'lib'),
'files' => array('foo.php', 'Main/Foo/bar.php'),
));
2012-04-19 20:04:49 +00:00
$package->setTargetDir('Main/Foo/');
$this->repository->expects($this->once())
->method('getCanonicalPackages')
->will($this->returnValue(array()));
$this->fs->ensureDirectoryExists($this->vendorDir.'/a');
$this->fs->ensureDirectoryExists($this->workingDir.'/src');
$this->fs->ensureDirectoryExists($this->workingDir.'/lib');
file_put_contents($this->workingDir.'/src/rootfoo.php', '<?php class ClassMapFoo {}');
file_put_contents($this->workingDir.'/lib/rootbar.php', '<?php class ClassMapBar {}');
file_put_contents($this->workingDir.'/foo.php', '<?php class FilesFoo {}');
file_put_contents($this->workingDir.'/bar.php', '<?php class FilesBar {}');
2012-08-14 18:28:49 +00:00
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'TargetDir');
$this->assertFileEquals(__DIR__.'/Fixtures/autoload_target_dir.php', $this->vendorDir.'/autoload.php');
$this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_target_dir.php', $this->vendorDir.'/composer/autoload_real.php');
$this->assertAutoloadFiles('classmap6', $this->vendorDir.'/composer', 'classmap');
}
2011-12-03 22:20:06 +00:00
public function testVendorsAutoloading()
{
$package = new Package('a', '1.0', '1.0');
2011-12-03 22:20:06 +00:00
$packages = array();
$packages[] = $a = new Package('a/a', '1.0', '1.0');
$packages[] = $b = new Package('b/b', '1.0', '1.0');
2012-05-14 12:01:37 +00:00
$packages[] = $c = new AliasPackage($b, '1.2', '1.2');
2011-12-03 22:20:06 +00:00
$a->setAutoload(array('psr-0' => array('A' => 'src/', 'A\\B' => 'lib/')));
$b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/')));
$this->repository->expects($this->once())
->method('getCanonicalPackages')
2011-12-03 22:20:06 +00:00
->will($this->returnValue($packages));
$this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
$this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
$this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib');
$this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
2012-08-14 18:28:49 +00:00
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_5');
$this->assertAutoloadFiles('vendors', $this->vendorDir.'/composer');
$this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated, even if empty.");
2012-03-05 13:10:01 +00:00
}
public function testPSR0ToClassMapIgnoresNonExistingDir()
{
$package = new Package('a', '1.0', '1.0');
$package->setAutoload(array('psr-0' => array('foo/bar/non/existing/')));
$this->repository->expects($this->once())
->method('getCanonicalPackages')
->will($this->returnValue(array()));
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_8');
$this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated.");
$this->assertEquals(
array(),
include $this->vendorDir.'/composer/autoload_classmap.php'
);
}
2012-03-05 13:10:01 +00:00
public function testVendorsClassMapAutoloading()
{
$package = new Package('a', '1.0', '1.0');
2012-03-05 13:10:01 +00:00
$packages = array();
$packages[] = $a = new Package('a/a', '1.0', '1.0');
$packages[] = $b = new Package('b/b', '1.0', '1.0');
2012-03-05 13:10:01 +00:00
$a->setAutoload(array('classmap' => array('src/')));
$b->setAutoload(array('classmap' => array('src/', 'lib/')));
2012-03-05 13:10:01 +00:00
$this->repository->expects($this->once())
->method('getCanonicalPackages')
2012-03-05 13:10:01 +00:00
->will($this->returnValue($packages));
$this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
$this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
$this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
$this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/lib');
2012-03-05 13:10:01 +00:00
file_put_contents($this->vendorDir.'/a/a/src/a.php', '<?php class ClassMapFoo {}');
file_put_contents($this->vendorDir.'/b/b/src/b.php', '<?php class ClassMapBar {}');
file_put_contents($this->vendorDir.'/b/b/lib/c.php', '<?php class ClassMapBaz {}');
2012-08-14 18:28:49 +00:00
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_6');
$this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated.");
$this->assertEquals(
2013-03-14 09:23:35 +00:00
array(
'ClassMapBar' => $this->vendorDir.'/b/b/src/b.php',
'ClassMapBaz' => $this->vendorDir.'/b/b/lib/c.php',
'ClassMapFoo' => $this->vendorDir.'/a/a/src/a.php',
2013-03-14 09:23:35 +00:00
),
include $this->vendorDir.'/composer/autoload_classmap.php'
2012-03-05 13:10:01 +00:00
);
$this->assertAutoloadFiles('classmap4', $this->vendorDir.'/composer', 'classmap');
2011-12-03 22:20:06 +00:00
}
public function testVendorsClassMapAutoloadingWithTargetDir()
{
$package = new Package('a', '1.0', '1.0');
$packages = array();
$packages[] = $a = new Package('a/a', '1.0', '1.0');
$packages[] = $b = new Package('b/b', '1.0', '1.0');
$a->setAutoload(array('classmap' => array('target/src/', 'lib/')));
$a->setTargetDir('target');
$b->setAutoload(array('classmap' => array('src/')));
$this->repository->expects($this->once())
->method('getCanonicalPackages')
->will($this->returnValue($packages));
$this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
$this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/target/src');
$this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/target/lib');
$this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
file_put_contents($this->vendorDir.'/a/a/target/src/a.php', '<?php class ClassMapFoo {}');
file_put_contents($this->vendorDir.'/a/a/target/lib/b.php', '<?php class ClassMapBar {}');
file_put_contents($this->vendorDir.'/b/b/src/c.php', '<?php class ClassMapBaz {}');
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_6');
$this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated.");
$this->assertEquals(
2013-03-14 09:23:35 +00:00
array(
'ClassMapBar' => $this->vendorDir.'/a/a/target/lib/b.php',
'ClassMapBaz' => $this->vendorDir.'/b/b/src/c.php',
'ClassMapFoo' => $this->vendorDir.'/a/a/target/src/a.php',
2013-03-14 09:23:35 +00:00
),
include $this->vendorDir.'/composer/autoload_classmap.php'
);
}
public function testClassMapAutoloadingEmptyDirAndExactFile()
{
$package = new Package('a', '1.0', '1.0');
$packages = array();
$packages[] = $a = new Package('a/a', '1.0', '1.0');
$packages[] = $b = new Package('b/b', '1.0', '1.0');
$packages[] = $c = new Package('c/c', '1.0', '1.0');
$a->setAutoload(array('classmap' => array('')));
$b->setAutoload(array('classmap' => array('test.php')));
$c->setAutoload(array('classmap' => array('./')));
$this->repository->expects($this->once())
->method('getCanonicalPackages')
->will($this->returnValue($packages));
$this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
$this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
$this->fs->ensureDirectoryExists($this->vendorDir.'/b/b');
$this->fs->ensureDirectoryExists($this->vendorDir.'/c/c/foo');
file_put_contents($this->vendorDir.'/a/a/src/a.php', '<?php class ClassMapFoo {}');
file_put_contents($this->vendorDir.'/b/b/test.php', '<?php class ClassMapBar {}');
file_put_contents($this->vendorDir.'/c/c/foo/test.php', '<?php class ClassMapBaz {}');
2012-08-14 18:28:49 +00:00
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_7');
$this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated.");
$this->assertEquals(
2013-03-14 09:23:35 +00:00
array(
'ClassMapBar' => $this->vendorDir.'/b/b/test.php',
'ClassMapBaz' => $this->vendorDir.'/c/c/foo/test.php',
'ClassMapFoo' => $this->vendorDir.'/a/a/src/a.php',
2013-03-14 09:23:35 +00:00
),
include $this->vendorDir.'/composer/autoload_classmap.php'
);
$this->assertAutoloadFiles('classmap5', $this->vendorDir.'/composer', 'classmap');
}
public function testFilesAutoloadGeneration()
{
$package = new Package('a', '1.0', '1.0');
$package->setAutoload(array('files' => array('root.php')));
$packages = array();
$packages[] = $a = new Package('a/a', '1.0', '1.0');
$packages[] = $b = new Package('b/b', '1.0', '1.0');
$packages[] = $c = new Package('c/c', '1.0', '1.0');
$a->setAutoload(array('files' => array('test.php')));
$b->setAutoload(array('files' => array('test2.php')));
$c->setAutoload(array('files' => array('test3.php', 'foo/bar/test4.php')));
$c->setTargetDir('foo/bar');
$this->repository->expects($this->once())
->method('getCanonicalPackages')
->will($this->returnValue($packages));
$this->fs->ensureDirectoryExists($this->vendorDir.'/a/a');
$this->fs->ensureDirectoryExists($this->vendorDir.'/b/b');
$this->fs->ensureDirectoryExists($this->vendorDir.'/c/c/foo/bar');
file_put_contents($this->vendorDir.'/a/a/test.php', '<?php function testFilesAutoloadGeneration1() {}');
file_put_contents($this->vendorDir.'/b/b/test2.php', '<?php function testFilesAutoloadGeneration2() {}');
file_put_contents($this->vendorDir.'/c/c/foo/bar/test3.php', '<?php function testFilesAutoloadGeneration3() {}');
file_put_contents($this->vendorDir.'/c/c/foo/bar/test4.php', '<?php function testFilesAutoloadGeneration4() {}');
file_put_contents($this->workingDir.'/root.php', '<?php function testFilesAutoloadGenerationRoot() {}');
2012-08-14 18:28:49 +00:00
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'FilesAutoload');
2012-06-11 12:15:08 +00:00
$this->assertFileEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php');
$this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_functions.php', $this->vendorDir.'/composer/autoload_real.php');
include $this->vendorDir . '/autoload.php';
$this->assertTrue(function_exists('testFilesAutoloadGeneration1'));
$this->assertTrue(function_exists('testFilesAutoloadGeneration2'));
$this->assertTrue(function_exists('testFilesAutoloadGeneration3'));
$this->assertTrue(function_exists('testFilesAutoloadGeneration4'));
$this->assertTrue(function_exists('testFilesAutoloadGenerationRoot'));
}
public function testFilesAutoloadOrderByDependencies()
{
$package = new Package('a', '1.0', '1.0');
$package->setAutoload(array('files' => array('root.php')));
$package->setRequires(array(new Link('a', 'z/foo')));
$package->setRequires(array(new Link('a', 'd/d')));
$package->setRequires(array(new Link('a', 'e/e')));
$packages = array();
$packages[] = $z = new Package('z/foo', '1.0', '1.0');
$packages[] = $b = new Package('b/bar', '1.0', '1.0');
$packages[] = $d = new Package('d/d', '1.0', '1.0');
$packages[] = $c = new Package('c/lorem', '1.0', '1.0');
$packages[] = $e = new Package('e/e', '1.0', '1.0');
$z->setAutoload(array('files' => array('testA.php')));
$z->setRequires(array(new Link('z/foo', 'c/lorem')));
$b->setAutoload(array('files' => array('testB.php')));
$b->setRequires(array(new Link('b/bar', 'c/lorem'), new Link('b/bar', 'd/d')));
$c->setAutoload(array('files' => array('testC.php')));
$d->setAutoload(array('files' => array('testD.php')));
$d->setRequires(array(new Link('d/d', 'c/lorem')));
$e->setAutoload(array('files' => array('testE.php')));
$e->setRequires(array(new Link('e/e', 'c/lorem')));
$this->repository->expects($this->once())
->method('getCanonicalPackages')
->will($this->returnValue($packages));
$this->fs->ensureDirectoryExists($this->vendorDir . '/z/foo');
$this->fs->ensureDirectoryExists($this->vendorDir . '/b/bar');
$this->fs->ensureDirectoryExists($this->vendorDir . '/c/lorem');
$this->fs->ensureDirectoryExists($this->vendorDir . '/d/d');
$this->fs->ensureDirectoryExists($this->vendorDir . '/e/e');
file_put_contents($this->vendorDir . '/z/foo/testA.php', '<?php function testFilesAutoloadOrderByDependency1() {}');
file_put_contents($this->vendorDir . '/b/bar/testB.php', '<?php function testFilesAutoloadOrderByDependency2() {}');
file_put_contents($this->vendorDir . '/c/lorem/testC.php', '<?php function testFilesAutoloadOrderByDependency3() {}');
file_put_contents($this->vendorDir . '/d/d/testD.php', '<?php function testFilesAutoloadOrderByDependency4() {}');
file_put_contents($this->vendorDir . '/e/e/testE.php', '<?php function testFilesAutoloadOrderByDependency5() {}');
file_put_contents($this->workingDir . '/root.php', '<?php function testFilesAutoloadOrderByDependencyRoot() {}');
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'FilesAutoloadOrder');
$this->assertFileEquals(__DIR__ . '/Fixtures/autoload_functions_by_dependency.php', $this->vendorDir . '/autoload.php');
$this->assertFileEquals(__DIR__ . '/Fixtures/autoload_real_files_by_dependency.php', $this->vendorDir . '/composer/autoload_real.php');
require $this->vendorDir . '/autoload.php';
$this->assertTrue(function_exists('testFilesAutoloadOrderByDependency1'));
$this->assertTrue(function_exists('testFilesAutoloadOrderByDependency2'));
$this->assertTrue(function_exists('testFilesAutoloadOrderByDependency3'));
$this->assertTrue(function_exists('testFilesAutoloadOrderByDependency4'));
$this->assertTrue(function_exists('testFilesAutoloadOrderByDependency5'));
$this->assertTrue(function_exists('testFilesAutoloadOrderByDependencyRoot'));
}
public function testOverrideVendorsAutoloading()
{
$package = new Package('z', '1.0', '1.0');
$package->setAutoload(array('psr-0' => array('A\\B' => $this->workingDir.'/lib'), 'classmap' => array($this->workingDir.'/src')));
$package->setRequires(array(new Link('z', 'a/a')));
$packages = array();
$packages[] = $a = new Package('a/a', '1.0', '1.0');
$packages[] = $b = new Package('b/b', '1.0', '1.0');
$a->setAutoload(array('psr-0' => array('A' => 'src/', 'A\\B' => 'lib/'), 'classmap' => array('classmap')));
$b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/')));
$this->repository->expects($this->once())
->method('getCanonicalPackages')
->will($this->returnValue($packages));
$this->fs->ensureDirectoryExists($this->workingDir.'/lib/A/B');
$this->fs->ensureDirectoryExists($this->workingDir.'/src/');
$this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
$this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/classmap');
$this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
$this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib/A/B');
$this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
file_put_contents($this->workingDir.'/lib/A/B/C.php', '<?php namespace A\\B; class C {}');
file_put_contents($this->workingDir.'/src/classes.php', '<?php namespace Foo; class Bar {}');
file_put_contents($this->vendorDir.'/a/a/lib/A/B/C.php', '<?php namespace A\\B; class C {}');
file_put_contents($this->vendorDir.'/a/a/classmap/classes.php', '<?php namespace Foo; class Bar {}');
$expectedNamespace = <<<EOF
<?php
// autoload_namespaces.php generated by Composer
\$vendorDir = dirname(dirname(__FILE__));
\$baseDir = dirname(\$vendorDir);
return array(
2013-03-14 09:23:35 +00:00
'B\\\\Sub\\\\Name' => \$vendorDir . '/b/b/src',
'A\\\\B' => array(\$baseDir . '/lib', \$vendorDir . '/a/a/lib'),
'A' => \$vendorDir . '/a/a/src',
);
EOF;
$expectedClassmap = <<<EOF
<?php
// autoload_classmap.php generated by Composer
\$vendorDir = dirname(dirname(__FILE__));
\$baseDir = dirname(\$vendorDir);
return array(
'A\\\\B\\\\C' => \$baseDir . '/lib/A/B/C.php',
'Foo\\\\Bar' => \$baseDir . '/src/classes.php',
);
EOF;
2012-08-14 18:28:49 +00:00
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_9');
$this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php'));
$this->assertEquals($expectedClassmap, file_get_contents($this->vendorDir.'/composer/autoload_classmap.php'));
}
2012-04-04 13:15:37 +00:00
public function testIncludePathFileGeneration()
{
$package = new Package('a', '1.0', '1.0');
2012-04-04 13:15:37 +00:00
$packages = array();
$a = new Package("a/a", "1.0", "1.0");
2012-04-04 13:15:37 +00:00
$a->setIncludePaths(array("lib/"));
$b = new Package("b/b", "1.0", "1.0");
2012-04-04 13:15:37 +00:00
$b->setIncludePaths(array("library"));
$c = new Package("c", "1.0", "1.0");
$c->setIncludePaths(array("library"));
2012-04-04 13:15:37 +00:00
$packages[] = $a;
$packages[] = $b;
$packages[] = $c;
2012-04-04 13:15:37 +00:00
$this->repository->expects($this->once())
->method("getCanonicalPackages")
2012-04-04 13:15:37 +00:00
->will($this->returnValue($packages));
$this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
2012-04-04 13:15:37 +00:00
2012-08-14 18:28:49 +00:00
$this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_10');
2012-04-04 13:15:37 +00:00
$this->assertFileEquals(__DIR__.'/Fixtures/include_paths.php', $this->vendorDir.'/composer/include_paths.php');
2012-04-04 13:15:37 +00:00
$this->assertEquals(
array(
$this->vendorDir."/a/a/lib",
$this->vendorDir."/b/b/library",
$this->vendorDir."/c/library",
2012-04-04 13:15:37 +00:00
),
require $this->vendorDir."/composer/include_paths.php"
2012-04-04 13:15:37 +00:00
);
}
public function testIncludePathsArePrependedInAutoloadFile()
2012-04-04 13:15:37 +00:00
{
$package = new Package('a', '1.0', '1.0');
2012-04-04 13:15:37 +00:00
$packages = array();
$a = new Package("a/a", "1.0", "1.0");
2012-04-04 13:15:37 +00:00
$a->setIncludePaths(array("lib/"));
$packages[] = $a;
$this->repository->expects($this->once())
->method("getCanonicalPackages")
2012-04-04 13:15:37 +00:00
->will($this->returnValue($packages));
mkdir($this->vendorDir."/composer", 0777, true);
2012-04-04 13:15:37 +00:00
2012-08-14 18:28:49 +00:00
$this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_11');
2012-04-04 13:15:37 +00:00
$oldIncludePath = get_include_path();
require $this->vendorDir."/autoload.php";
2012-04-04 13:15:37 +00:00
$this->assertEquals(
$this->vendorDir."/a/a/lib".PATH_SEPARATOR.$oldIncludePath,
2012-04-04 13:15:37 +00:00
get_include_path()
);
set_include_path($oldIncludePath);
}
public function testIncludePathsInMainPackage()
{
$package = new Package('a', '1.0', '1.0');
$package->setIncludePaths(array('/lib', '/src'));
$packages = array($a = new Package("a/a", "1.0", "1.0"));
$a->setIncludePaths(array("lib/"));
$this->repository->expects($this->once())
->method("getCanonicalPackages")
->will($this->returnValue($packages));
mkdir($this->vendorDir."/composer", 0777, true);
$this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_12');
$oldIncludePath = get_include_path();
require $this->vendorDir."/autoload.php";
$this->assertEquals(
$this->workingDir."/lib".PATH_SEPARATOR.$this->workingDir."/src".PATH_SEPARATOR.$this->vendorDir."/a/a/lib".PATH_SEPARATOR.$oldIncludePath,
get_include_path()
);
set_include_path($oldIncludePath);
}
public function testIncludePathFileWithoutPathsIsSkipped()
2012-04-04 13:15:37 +00:00
{
$package = new Package('a', '1.0', '1.0');
2012-04-04 13:15:37 +00:00
$packages = array();
$a = new Package("a/a", "1.0", "1.0");
2012-04-04 13:15:37 +00:00
$packages[] = $a;
$this->repository->expects($this->once())
->method("getCanonicalPackages")
2012-04-04 13:15:37 +00:00
->will($this->returnValue($packages));
mkdir($this->vendorDir."/composer", 0777, true);
2012-04-04 13:15:37 +00:00
2012-08-14 18:28:49 +00:00
$this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_12');
2012-04-04 13:15:37 +00:00
$this->assertFalse(file_exists($this->vendorDir."/composer/include_paths.php"));
2012-04-04 13:15:37 +00:00
}
2013-01-06 19:34:52 +00:00
public function testEventIsDispatchedAfterAutoloadDump()
{
$this->eventDispatcher
->expects($this->once())
->method('dispatch')
->with(ScriptEvents::POST_AUTOLOAD_DUMP, false);
$package = new Package('a', '1.0', '1.0');
$package->setAutoload(array('psr-0' => array('foo/bar/non/existing/')));
$this->repository->expects($this->once())
->method('getCanonicalPackages')
2013-01-06 19:34:52 +00:00
->will($this->returnValue(array()));
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_8');
}
public function testUseGlobalIncludePath()
2013-02-04 08:12:41 +00:00
{
$package = new Package('a', '1.0', '1.0');
$package->setAutoload(array(
'psr-0' => array('Main\\Foo' => '', 'Main\\Bar' => ''),
));
$package->setTargetDir('Main/Foo/');
$this->repository->expects($this->once())
->method('getCanonicalPackages')
2013-02-04 08:12:41 +00:00
->will($this->returnValue(array()));
$this->config->expects($this->at(2))
2013-02-04 08:12:41 +00:00
->method('get')
->with($this->equalTo('use-include-path'))
->will($this->returnValue(true));
$this->fs->ensureDirectoryExists($this->vendorDir.'/a');
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'IncludePath');
$this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_include_path.php', $this->vendorDir.'/composer/autoload_real.php');
}
2012-04-04 13:15:37 +00:00
public function testVendorDirExcludedFromWorkingDir()
{
$workingDir = $this->vendorDir.'/working-dir';
$vendorDir = $workingDir.'/../vendor';
$this->fs->ensureDirectoryExists($workingDir);
chdir($workingDir);
$package = new Package('a', '1.0', '1.0');
$package->setAutoload(array(
'psr-0' => array('Foo' => 'src'),
'classmap' => array('classmap'),
'files' => array('test.php'),
));
$vendorPackage = new Package('b/b', '1.0', '1.0');
$vendorPackage->setAutoload(array(
'psr-0' => array('Bar' => 'lib'),
'classmap' => array('classmaps'),
'files' => array('bootstrap.php'),
));
$this->repository->expects($this->once())
->method('getCanonicalPackages')
->will($this->returnValue(array($vendorPackage)));
$im = $this->getMockBuilder('Composer\Installer\InstallationManager')
->disableOriginalConstructor()
->getMock();
$im->expects($this->any())
->method('getInstallPath')
->will($this->returnCallback(function ($package) use ($vendorDir) {
$targetDir = $package->getTargetDir();
return $vendorDir.'/'.$package->getName() . ($targetDir ? '/'.$targetDir : '');
}));
$this->fs->ensureDirectoryExists($workingDir.'/src/Foo');
$this->fs->ensureDirectoryExists($workingDir.'/classmap');
$this->fs->ensureDirectoryExists($vendorDir.'/composer');
$this->fs->ensureDirectoryExists($vendorDir.'/b/b/lib/Bar');
$this->fs->ensureDirectoryExists($vendorDir.'/b/b/classmaps');
file_put_contents($workingDir.'/src/Foo/Bar.php', '<?php namespace Foo; class Bar {}');
file_put_contents($workingDir.'/classmap/classes.php', '<?php namespace Foo; class Foo {}');
file_put_contents($workingDir.'/test.php', '<?php class Foo {}');
file_put_contents($vendorDir.'/b/b/lib/Bar/Foo.php', '<?php namespace Bar; class Foo {}');
file_put_contents($vendorDir.'/b/b/classmaps/classes.php', '<?php namespace Bar; class Bar {}');
file_put_contents($vendorDir.'/b/b/bootstrap.php', '<?php class Bar {}');
$oldVendorDir = $this->vendorDir;
$this->vendorDir = $vendorDir;
$this->generator->dump($this->config, $this->repository, $package, $im, 'composer', true, '_13');
$this->vendorDir = $oldVendorDir;
$expectedNamespace = <<<'EOF'
<?php
// autoload_namespaces.php generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir).'/working-dir';
return array(
'Foo' => $baseDir . '/src',
'Bar' => $vendorDir . '/b/b/lib',
);
EOF;
$expectedClassmap = <<<'EOF'
<?php
// autoload_classmap.php generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir).'/working-dir';
return array(
'Bar\\Bar' => $vendorDir . '/b/b/classmaps/classes.php',
'Bar\\Foo' => $vendorDir . '/b/b/lib/Bar/Foo.php',
'Foo\\Bar' => $baseDir . '/src/Foo/Bar.php',
'Foo\\Foo' => $baseDir . '/classmap/classes.php',
);
EOF;
$this->assertEquals($expectedNamespace, file_get_contents($vendorDir.'/composer/autoload_namespaces.php'));
$this->assertEquals($expectedClassmap, file_get_contents($vendorDir.'/composer/autoload_classmap.php'));
$this->assertContains("require \$vendorDir . '/b/b/bootstrap.php';", file_get_contents($vendorDir.'/composer/autoload_real.php'));
$this->assertContains("require \$baseDir . '/test.php';", file_get_contents($vendorDir.'/composer/autoload_real.php'));
}
public function testUpLevelRelativePaths()
{
$workingDir = $this->workingDir.'/working-dir';
mkdir($workingDir, 0777, true);
chdir($workingDir);
$package = new Package('a', '1.0', '1.0');
$package->setAutoload(array(
'psr-0' => array('Foo' => '../path/../src'),
'classmap' => array('../classmap'),
'files' => array('../test.php'),
));
$this->repository->expects($this->once())
->method('getCanonicalPackages')
->will($this->returnValue(array()));
$this->fs->ensureDirectoryExists($this->workingDir.'/src/Foo');
$this->fs->ensureDirectoryExists($this->workingDir.'/classmap');
file_put_contents($this->workingDir.'/src/Foo/Bar.php', '<?php namespace Foo; class Bar {}');
file_put_contents($this->workingDir.'/classmap/classes.php', '<?php namespace Foo; class Foo {}');
file_put_contents($this->workingDir.'/test.php', '<?php class Foo {}');
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_14');
$expectedNamespace = <<<'EOF'
<?php
// autoload_namespaces.php generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir).'/working-dir';
return array(
'Foo' => $baseDir . '/../src',
);
EOF;
$expectedClassmap = <<<'EOF'
<?php
// autoload_classmap.php generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir).'/working-dir';
return array(
'Foo\\Bar' => $baseDir . '/../src/Foo/Bar.php',
'Foo\\Foo' => $baseDir . '/../classmap/classes.php',
);
EOF;
$this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php'));
$this->assertEquals($expectedClassmap, file_get_contents($this->vendorDir.'/composer/autoload_classmap.php'));
$this->assertContains("require \$baseDir . '/../test.php';", file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
}
public function testEmptyPaths()
{
$package = new Package('a', '1.0', '1.0');
$package->setAutoload(array(
'psr-0' => array('Foo' => ''),
'classmap' => array(''),
));
$this->repository->expects($this->once())
->method('getCanonicalPackages')
->will($this->returnValue(array()));
$this->fs->ensureDirectoryExists($this->workingDir.'/Foo');
file_put_contents($this->workingDir.'/Foo/Bar.php', '<?php namespace Foo; class Bar {}');
file_put_contents($this->workingDir.'/class.php', '<?php namespace Classmap; class Foo {}');
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_15');
$expectedNamespace = <<<'EOF'
<?php
// autoload_namespaces.php generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Foo' => $baseDir . '/',
);
EOF;
$expectedClassmap = <<<'EOF'
<?php
// autoload_classmap.php generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Classmap\\Foo' => $baseDir . '/class.php',
'Foo\\Bar' => $baseDir . '/Foo/Bar.php',
);
EOF;
$this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php'));
$this->assertEquals($expectedClassmap, file_get_contents($this->vendorDir.'/composer/autoload_classmap.php'));
}
private function assertAutoloadFiles($name, $dir, $type = 'namespaces')
2011-12-03 22:20:06 +00:00
{
$a = __DIR__.'/Fixtures/autoload_'.$name.'.php';
$b = $dir.'/autoload_'.$type.'.php';
2013-03-14 09:23:35 +00:00
$this->assertFileEquals($a, $b);
2011-12-03 22:20:06 +00:00
}
}