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;
|
2012-02-09 17:45:28 +00:00
|
|
|
use Composer\Util\Filesystem;
|
2011-12-03 22:20:06 +00:00
|
|
|
use Composer\Package\MemoryPackage;
|
2012-02-24 11:28:41 +00:00
|
|
|
use Composer\Test\TestCase;
|
2011-12-03 22:20:06 +00:00
|
|
|
|
2012-02-24 11:28:41 +00:00
|
|
|
class AutoloadGeneratorTest extends TestCase
|
2011-12-03 22:20:06 +00:00
|
|
|
{
|
|
|
|
public $vendorDir;
|
|
|
|
private $workingDir;
|
|
|
|
private $im;
|
|
|
|
private $repository;
|
|
|
|
private $generator;
|
2012-02-24 09:40:47 +00:00
|
|
|
private $fs;
|
2011-12-03 22:20:06 +00:00
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
2012-02-24 09:40:47 +00:00
|
|
|
$this->fs = new Filesystem;
|
2011-12-03 22:20:06 +00:00
|
|
|
$that = $this;
|
|
|
|
|
2011-12-21 15:38:31 +00:00
|
|
|
$this->workingDir = realpath(sys_get_temp_dir());
|
2011-12-03 22:20:06 +00:00
|
|
|
$this->vendorDir = $this->workingDir.DIRECTORY_SEPARATOR.'composer-test-autoload';
|
2012-02-24 11:28:41 +00:00
|
|
|
$this->ensureDirectoryExistsAndClear($this->vendorDir);
|
2011-12-03 22:20:06 +00:00
|
|
|
|
|
|
|
$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;
|
|
|
|
}));
|
|
|
|
|
2012-02-24 11:28:41 +00:00
|
|
|
$this->repository = $this->getMock('Composer\Repository\RepositoryInterface');
|
2011-12-03 22:20:06 +00:00
|
|
|
|
|
|
|
$this->generator = new AutoloadGenerator();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function tearDown()
|
|
|
|
{
|
2012-02-24 11:28:41 +00:00
|
|
|
if ($this->vendorDir === $this->workingDir) {
|
|
|
|
if (is_dir($this->workingDir.'/.composer')) {
|
|
|
|
$this->fs->removeDirectory($this->workingDir.'/.composer');
|
|
|
|
}
|
|
|
|
} elseif (is_dir($this->vendorDir)) {
|
2012-02-24 09:40:47 +00:00
|
|
|
$this->fs->removeDirectory($this->vendorDir);
|
|
|
|
}
|
2012-03-21 12:58:35 +00:00
|
|
|
if (is_dir($this->workingDir.'/.composersrc')) {
|
|
|
|
$this->fs->removeDirectory($this->workingDir.'/.composersrc');
|
|
|
|
}
|
|
|
|
|
2011-12-03 22:20:06 +00:00
|
|
|
chdir($this->dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMainPackageAutoloading()
|
|
|
|
{
|
|
|
|
$package = new MemoryPackage('a', '1.0', '1.0');
|
2012-03-21 12:58:35 +00:00
|
|
|
$package->setAutoload(array(
|
2012-03-28 15:09:07 +00:00
|
|
|
'psr-0' => array('Main' => 'src/', 'Lala' => array('src/', 'lib/')),
|
2012-03-21 12:58:35 +00:00
|
|
|
'classmap' => array('.composersrc/'),
|
|
|
|
));
|
2011-12-03 22:20:06 +00:00
|
|
|
|
2012-02-24 11:28:41 +00:00
|
|
|
$this->repository->expects($this->once())
|
2011-12-03 22:20:06 +00:00
|
|
|
->method('getPackages')
|
|
|
|
->will($this->returnValue(array()));
|
|
|
|
|
2012-03-21 12:58:35 +00:00
|
|
|
if (!is_dir($this->vendorDir.'/.composer')) {
|
|
|
|
mkdir($this->vendorDir.'/.composer');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->createClassFile($this->workingDir);
|
|
|
|
|
2012-02-24 11:28:41 +00:00
|
|
|
$this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/.composer');
|
2011-12-03 22:20:06 +00:00
|
|
|
$this->assertAutoloadFiles('main', $this->vendorDir.'/.composer');
|
2012-03-21 12:58:35 +00:00
|
|
|
$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()
|
|
|
|
{
|
2012-02-02 15:38:48 +00:00
|
|
|
$this->vendorDir = $this->workingDir;
|
|
|
|
|
|
|
|
$package = new MemoryPackage('a', '1.0', '1.0');
|
2012-03-21 12:58:35 +00:00
|
|
|
$package->setAutoload(array(
|
|
|
|
'psr-0' => array('Main' => 'src/', 'Lala' => 'src/'),
|
|
|
|
'classmap' => array('.composersrc/'),
|
|
|
|
));
|
2012-02-02 15:38:48 +00:00
|
|
|
|
2012-02-24 11:28:41 +00:00
|
|
|
$this->repository->expects($this->once())
|
2012-02-02 15:38:48 +00:00
|
|
|
->method('getPackages')
|
|
|
|
->will($this->returnValue(array()));
|
|
|
|
|
2012-02-03 19:33:57 +00:00
|
|
|
if (!is_dir($this->vendorDir.'/.composer')) {
|
|
|
|
mkdir($this->vendorDir.'/.composer', 0777, true);
|
|
|
|
}
|
2012-02-24 11:28:41 +00:00
|
|
|
|
2012-03-21 12:58:35 +00:00
|
|
|
$this->createClassFile($this->vendorDir);
|
|
|
|
|
2012-02-24 11:28:41 +00:00
|
|
|
$this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/.composer');
|
2012-02-02 15:38:48 +00:00
|
|
|
$this->assertAutoloadFiles('main3', $this->vendorDir.'/.composer');
|
2012-03-21 12:58:35 +00:00
|
|
|
$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 MemoryPackage('a', '1.0', '1.0');
|
2012-03-21 12:58:35 +00:00
|
|
|
$package->setAutoload(array(
|
|
|
|
'psr-0' => array('Main' => 'src/', 'Lala' => 'src/'),
|
|
|
|
'classmap' => array('.composersrc/'),
|
|
|
|
));
|
2011-12-03 22:20:06 +00:00
|
|
|
|
2012-02-24 11:28:41 +00:00
|
|
|
$this->repository->expects($this->once())
|
2011-12-03 22:20:06 +00:00
|
|
|
->method('getPackages')
|
|
|
|
->will($this->returnValue(array()));
|
|
|
|
|
|
|
|
$this->vendorDir .= '/subdir';
|
|
|
|
mkdir($this->vendorDir.'/.composer', 0777, true);
|
2012-03-21 12:58:35 +00:00
|
|
|
$this->createClassFile($this->workingDir);
|
2012-02-24 11:28:41 +00:00
|
|
|
$this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/.composer');
|
2011-12-03 22:20:06 +00:00
|
|
|
$this->assertAutoloadFiles('main2', $this->vendorDir.'/.composer');
|
2012-03-21 12:58:35 +00:00
|
|
|
$this->assertAutoloadFiles('classmap2', $this->vendorDir.'/.composer', 'classmap');
|
2011-12-03 22:20:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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/')));
|
|
|
|
|
2012-02-24 11:28:41 +00:00
|
|
|
$this->repository->expects($this->once())
|
2011-12-03 22:20:06 +00:00
|
|
|
->method('getPackages')
|
|
|
|
->will($this->returnValue($packages));
|
|
|
|
|
|
|
|
mkdir($this->vendorDir.'/.composer', 0777, true);
|
2012-02-24 11:28:41 +00:00
|
|
|
$this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/.composer');
|
2011-12-03 22:20:06 +00:00
|
|
|
$this->assertAutoloadFiles('vendors', $this->vendorDir.'/.composer');
|
2012-03-05 13:10:01 +00:00
|
|
|
$this->assertTrue(file_exists($this->vendorDir.'/.composer/autoload_classmap.php'), "ClassMap file needs to be generated, even if empty.");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testVendorsClassMapAutoloading()
|
|
|
|
{
|
|
|
|
$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('classmap' => array('src/')));
|
|
|
|
$b->setAutoload(array('classmap' => array('src/', 'lib/')));
|
|
|
|
|
|
|
|
$this->repository->expects($this->once())
|
|
|
|
->method('getPackages')
|
|
|
|
->will($this->returnValue($packages));
|
|
|
|
|
|
|
|
@mkdir($this->vendorDir.'/.composer', 0777, true);
|
|
|
|
mkdir($this->vendorDir.'/a/a/src', 0777, true);
|
|
|
|
mkdir($this->vendorDir.'/b/b/src', 0777, true);
|
|
|
|
mkdir($this->vendorDir.'/b/b/lib', 0777, true);
|
|
|
|
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 {}');
|
|
|
|
|
|
|
|
$this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/.composer');
|
2012-03-10 00:59:59 +00:00
|
|
|
$this->assertTrue(file_exists($this->vendorDir.'/.composer/autoload_classmap.php'), "ClassMap file needs to be generated.");
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'ClassMapFoo' => $this->workingDir.'/composer-test-autoload/a/a/src/a.php',
|
|
|
|
'ClassMapBar' => $this->workingDir.'/composer-test-autoload/b/b/src/b.php',
|
|
|
|
'ClassMapBaz' => $this->workingDir.'/composer-test-autoload/b/b/lib/c.php',
|
2012-03-05 13:10:01 +00:00
|
|
|
),
|
|
|
|
include ($this->vendorDir.'/.composer/autoload_classmap.php')
|
|
|
|
);
|
2012-03-21 12:58:35 +00:00
|
|
|
$this->assertAutoloadFiles('classmap4', $this->vendorDir.'/.composer', 'classmap');
|
2011-12-03 22:20:06 +00:00
|
|
|
}
|
|
|
|
|
2012-01-13 03:14:30 +00:00
|
|
|
public function testOverrideVendorsAutoloading()
|
|
|
|
{
|
|
|
|
$package = new MemoryPackage('a', '1.0', '1.0');
|
|
|
|
$package->setAutoload(array('psr-0' => array('A\\B' => '/home/deveuser/local-packages/a-a/lib')));
|
|
|
|
|
|
|
|
$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/')));
|
|
|
|
|
2012-02-24 11:28:41 +00:00
|
|
|
$this->repository->expects($this->once())
|
2012-01-13 03:14:30 +00:00
|
|
|
->method('getPackages')
|
|
|
|
->will($this->returnValue($packages));
|
|
|
|
|
|
|
|
mkdir($this->vendorDir.'/.composer', 0777, true);
|
2012-02-24 11:28:41 +00:00
|
|
|
$this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/.composer');
|
2012-01-13 03:14:30 +00:00
|
|
|
$this->assertAutoloadFiles('override_vendors', $this->vendorDir.'/.composer');
|
|
|
|
}
|
|
|
|
|
2012-03-21 12:58:35 +00:00
|
|
|
private function createClassFile($basedir)
|
|
|
|
{
|
|
|
|
if (!is_dir($basedir.'/.composersrc')) {
|
|
|
|
mkdir($basedir.'/.composersrc', 0777, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
file_put_contents($basedir.'/.composersrc/foo.php', '<?php class ClassMapFoo {}');
|
|
|
|
}
|
|
|
|
|
|
|
|
private function assertAutoloadFiles($name, $dir, $type = 'namespaces')
|
2011-12-03 22:20:06 +00:00
|
|
|
{
|
2012-03-21 12:58:35 +00:00
|
|
|
$this->assertFileEquals(__DIR__.'/Fixtures/autoload_'.$name.'.php', $dir.'/autoload_'.$type.'.php');
|
2011-12-03 22:20:06 +00:00
|
|
|
}
|
|
|
|
}
|