1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762 |
- <?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\Autoload;
- use Composer\Autoload\AutoloadGenerator;
- use Composer\Package\Link;
- use Composer\Package\Version\VersionParser;
- use Composer\Semver\Constraint\Constraint;
- use Composer\Util\Filesystem;
- use Composer\Package\AliasPackage;
- use Composer\Package\Package;
- use Composer\Test\TestCase;
- use Composer\Script\ScriptEvents;
- use Composer\Repository\InstalledRepositoryInterface;
- use Composer\Installer\InstallationManager;
- use Composer\Config;
- use Composer\EventDispatcher\EventDispatcher;
- use Composer\Util\Platform;
- use PHPUnit_Framework_MockObject_MockObject as MockObject;
- class AutoloadGeneratorTest extends TestCase
- {
- /**
- * @var string
- */
- public $vendorDir;
- /**
- * @var Config|MockObject
- */
- private $config;
- /**
- * @var string
- */
- private $workingDir;
- /**
- * @var string
- */
- private $origDir;
- /**
- * @var InstallationManager|MockObject
- */
- private $im;
- /**
- * @var InstalledRepositoryInterface|MockObject
- */
- private $repository;
- /**
- * @var AutoloadGenerator
- */
- private $generator;
- /**
- * @var Filesystem
- */
- private $fs;
- /**
- * @var EventDispatcher|MockObject
- */
- private $eventDispatcher;
- /**
- * Map of setting name => return value configuration for the stub Config
- * object.
- *
- * Note: must be public for compatibility with PHP 5.3 runtimes where
- * closures cannot access private members of the classes they are created
- * in.
- * @var array
- */
- public $configValueMap;
- protected function setUp()
- {
- $this->fs = new Filesystem;
- $that = $this;
- $this->workingDir = $this->getUniqueTmpDirectory();
- $this->vendorDir = $this->workingDir.DIRECTORY_SEPARATOR.'composer-test-autoload';
- $this->ensureDirectoryExistsAndClear($this->vendorDir);
- $this->config = $this->getMockBuilder('Composer\Config')->getMock();
- $this->configValueMap = array(
- 'vendor-dir' => function () use ($that) {
- return $that->vendorDir;
- },
- 'platform-check' => function () {
- return true;
- },
- );
- $this->config->expects($this->atLeastOnce())
- ->method('get')
- ->will($this->returnCallback(function ($arg) use ($that) {
- $ret = null;
- if (isset($that->configValueMap[$arg])) {
- $ret = $that->configValueMap[$arg];
- if (is_callable($ret)) {
- $ret = $ret();
- }
- }
- return $ret;
- }));
- $this->origDir = 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) {
- $targetDir = $package->getTargetDir();
- return $that->vendorDir.'/'.$package->getName() . ($targetDir ? '/'.$targetDir : '');
- }));
- $this->repository = $this->getMockBuilder('Composer\Repository\InstalledRepositoryInterface')->getMock();
- $this->eventDispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
- ->disableOriginalConstructor()
- ->getMock();
- $this->generator = new AutoloadGenerator($this->eventDispatcher);
- }
- protected function tearDown()
- {
- chdir($this->origDir);
- if (is_dir($this->workingDir)) {
- $this->fs->removeDirectory($this->workingDir);
- }
- if (is_dir($this->vendorDir)) {
- $this->fs->removeDirectory($this->vendorDir);
- }
- }
- public function testMainPackageAutoloading()
- {
- $package = new Package('a', '1.0', '1.0');
- $package->setAutoload(array(
- 'psr-0' => array(
- 'Main' => 'src/',
- 'Lala' => array('src/', 'lib/'),
- ),
- 'psr-4' => array(
- 'Acme\Fruit\\' => 'src-fruit/',
- 'Acme\Cake\\' => array('src-cake/', 'lib-cake/'),
- ),
- 'classmap' => array('composersrc/'),
- ));
- $this->repository->expects($this->once())
- ->method('getCanonicalPackages')
- ->will($this->returnValue(array()));
- $this->fs->ensureDirectoryExists($this->workingDir.'/composer');
- $this->fs->ensureDirectoryExists($this->workingDir.'/src/Lala/Test');
- $this->fs->ensureDirectoryExists($this->workingDir.'/lib');
- file_put_contents($this->workingDir.'/src/Lala/ClassMapMain.php', '<?php namespace Lala; class ClassMapMain {}');
- file_put_contents($this->workingDir.'/src/Lala/Test/ClassMapMainTest.php', '<?php namespace Lala\Test; class ClassMapMainTest {}');
- $this->fs->ensureDirectoryExists($this->workingDir.'/src-fruit');
- $this->fs->ensureDirectoryExists($this->workingDir.'/src-cake');
- $this->fs->ensureDirectoryExists($this->workingDir.'/lib-cake');
- file_put_contents($this->workingDir.'/src-cake/ClassMapBar.php', '<?php namespace Acme\Cake; class ClassMapBar {}');
- $this->fs->ensureDirectoryExists($this->workingDir.'/composersrc');
- file_put_contents($this->workingDir.'/composersrc/foo.php', '<?php class ClassMapFoo {}');
- $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_1');
- // Assert that autoload_namespaces.php was correctly generated.
- $this->assertAutoloadFiles('main', $this->vendorDir.'/composer');
- // Assert that autoload_psr4.php was correctly generated.
- $this->assertAutoloadFiles('psr4', $this->vendorDir.'/composer', 'psr4');
- // Assert that autoload_classmap.php was correctly generated.
- $this->assertAutoloadFiles('classmap', $this->vendorDir.'/composer', 'classmap');
- }
- public function testMainPackageDevAutoloading()
- {
- $package = new Package('a', '1.0', '1.0');
- $package->setAutoload(array(
- 'psr-0' => array(
- 'Main' => 'src/',
- ),
- ));
- $package->setDevAutoload(array(
- 'files' => array('devfiles/foo.php'),
- 'psr-0' => array(
- 'Main' => 'tests/',
- ),
- ));
- $this->repository->expects($this->once())
- ->method('getCanonicalPackages')
- ->will($this->returnValue(array()));
- $this->fs->ensureDirectoryExists($this->workingDir.'/composer');
- $this->fs->ensureDirectoryExists($this->workingDir.'/src/Main');
- file_put_contents($this->workingDir.'/src/Main/ClassMain.php', '<?php namespace Main; class ClassMain {}');
- $this->fs->ensureDirectoryExists($this->workingDir.'/devfiles');
- file_put_contents($this->workingDir.'/devfiles/foo.php', '<?php function foo() { echo "foo"; }');
- // generate autoload files with the dev mode set to true
- $this->generator->setDevMode(true);
- $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_1');
- // check standard autoload
- $this->assertAutoloadFiles('main5', $this->vendorDir.'/composer');
- $this->assertAutoloadFiles('classmap7', $this->vendorDir.'/composer', 'classmap');
- // make sure dev autoload is correctly dumped
- $this->assertAutoloadFiles('files2', $this->vendorDir.'/composer', 'files');
- }
- public function testMainPackageDevAutoloadingDisabledByDefault()
- {
- $package = new Package('a', '1.0', '1.0');
- $package->setAutoload(array(
- 'psr-0' => array(
- 'Main' => 'src/',
- ),
- ));
- $package->setDevAutoload(array(
- 'files' => array('devfiles/foo.php'),
- ));
- $this->repository->expects($this->once())
- ->method('getCanonicalPackages')
- ->will($this->returnValue(array()));
- $this->fs->ensureDirectoryExists($this->workingDir.'/composer');
- $this->fs->ensureDirectoryExists($this->workingDir.'/src/Main');
- file_put_contents($this->workingDir.'/src/Main/ClassMain.php', '<?php namespace Main; class ClassMain {}');
- $this->fs->ensureDirectoryExists($this->workingDir.'/devfiles');
- file_put_contents($this->workingDir.'/devfiles/foo.php', '<?php function foo() { echo "foo"; }');
- $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_1');
- // check standard autoload
- $this->assertAutoloadFiles('main4', $this->vendorDir.'/composer');
- $this->assertAutoloadFiles('classmap7', $this->vendorDir.'/composer', 'classmap');
- // make sure dev autoload is disabled when dev mode is set to false
- $this->assertFalse(is_file($this->vendorDir.'/composer/autoload_files.php'));
- }
- 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/'),
- 'psr-4' => array(
- 'Acme\Fruit\\' => 'src-fruit/',
- 'Acme\Cake\\' => array('src-cake/', 'lib-cake/'),
- ),
- '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 {}');
- $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_2');
- $this->assertAutoloadFiles('main3', $this->vendorDir.'/composer');
- $this->assertAutoloadFiles('psr4_3', $this->vendorDir.'/composer', 'psr4');
- $this->assertAutoloadFiles('classmap3', $this->vendorDir.'/composer', 'classmap');
- }
- public function testMainPackageAutoloadingAlternativeVendorDir()
- {
- $package = new Package('a', '1.0', '1.0');
- $package->setAutoload(array(
- 'psr-0' => array('Main' => 'src/', 'Lala' => 'src/'),
- 'psr-4' => array(
- 'Acme\Fruit\\' => 'src-fruit/',
- 'Acme\Cake\\' => array('src-cake/', 'lib-cake/'),
- ),
- 'classmap' => array('composersrc/'),
- ));
- $this->repository->expects($this->once())
- ->method('getCanonicalPackages')
- ->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 {}');
- $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_3');
- $this->assertAutoloadFiles('main2', $this->vendorDir.'/composer');
- $this->assertAutoloadFiles('psr4_2', $this->vendorDir.'/composer', 'psr4');
- $this->assertAutoloadFiles('classmap2', $this->vendorDir.'/composer', 'classmap');
- }
- public function testMainPackageAutoloadingWithTargetDir()
- {
- $package = new Package('a', '1.0', '1.0');
- $package->setAutoload(array(
- 'psr-0' => array('Main\\Foo' => '', 'Main\\Bar' => ''),
- 'classmap' => array('Main/Foo/src', 'lib'),
- 'files' => array('foo.php', 'Main/Foo/bar.php'),
- ));
- $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 {}');
- $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'TargetDir');
- $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_target_dir.php', $this->vendorDir.'/autoload.php');
- $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_real_target_dir.php', $this->vendorDir.'/composer/autoload_real.php');
- $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_static_target_dir.php', $this->vendorDir.'/composer/autoload_static.php');
- $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_files_target_dir.php', $this->vendorDir.'/composer/autoload_files.php');
- $this->assertAutoloadFiles('classmap6', $this->vendorDir.'/composer', 'classmap');
- }
- public function testVendorsAutoloading()
- {
- $package = new Package('a', '1.0', '1.0');
- $package->setRequires(array(
- new Link('a', 'a/a'),
- new Link('a', 'b/b'),
- ));
- $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 AliasPackage($b, '1.2', '1.2');
- $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')
- ->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');
- $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_5');
- $this->assertAutoloadFiles('vendors', $this->vendorDir.'/composer');
- $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated, even if empty.");
- }
- public function testNonDevAutoloadExclusionWithRecursion()
- {
- $package = new Package('a', '1.0', '1.0');
- $package->setRequires(array(
- new Link('a', '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/')));
- $a->setRequires(array(
- new Link('a/a', 'b/b'),
- ));
- $b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/')));
- $b->setRequires(array(
- new Link('b/b', 'a/a'),
- ));
- $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.'/a/a/lib');
- $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
- $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_5');
- $this->assertAutoloadFiles('vendors', $this->vendorDir.'/composer');
- $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated, even if empty.");
- }
- public function testNonDevAutoloadShouldIncludeReplacedPackages()
- {
- $package = new Package('a', '1.0', '1.0');
- $package->setRequires(array(new Link('a', '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->setRequires(array(new Link('a/a', 'b/c')));
- $b->setAutoload(array('psr-4' => array('B\\' => 'src/')));
- $b->setReplaces(
- array(new Link('b/b', 'b/c', new Constraint('==', '1.0'), 'replaces'))
- );
- $this->repository->expects($this->once())
- ->method('getCanonicalPackages')
- ->will($this->returnValue($packages));
- $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src/C');
- file_put_contents($this->vendorDir.'/b/b/src/C/C.php', '<?php namespace B\\C; class C {}');
- $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_5');
- $this->assertEquals(
- array(
- 'B\\C\\C' => $this->vendorDir.'/b/b/src/C/C.php',
- 'Composer\\InstalledVersions' => $this->vendorDir . '/composer/InstalledVersions.php',
- ),
- include $this->vendorDir.'/composer/autoload_classmap.php'
- );
- }
- public function testNonDevAutoloadExclusionWithRecursionReplace()
- {
- $package = new Package('a', '1.0', '1.0');
- $package->setRequires(array(
- new Link('a', '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/')));
- $a->setRequires(array(
- new Link('a/a', 'c/c'),
- ));
- $b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/')));
- $b->setReplaces(array(
- new Link('b/b', 'c/c'),
- ));
- $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.'/a/a/lib');
- $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
- $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_5');
- $this->assertAutoloadFiles('vendors', $this->vendorDir.'/composer');
- $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated, even if empty.");
- }
- public function testNonDevAutoloadReplacesNestedRequirements()
- {
- $package = new Package('a', '1.0', '1.0');
- $package->setRequires(array(
- new Link('a', 'a/a')
- ));
- $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');
- $packages[] = $d = new Package('d/d', '1.0', '1.0');
- $packages[] = $e = new Package('e/e', '1.0', '1.0');
- $a->setAutoload(array('classmap' => array('src/A.php')));
- $a->setRequires(array(
- new Link('a/a', 'b/b')
- ));
- $b->setAutoload(array('classmap' => array('src/B.php')));
- $b->setRequires(array(
- new Link('b/b', 'e/e')
- ));
- $c->setAutoload(array('classmap' => array('src/C.php')));
- $c->setReplaces(array(
- new Link('c/c', 'b/b')
- ));
- $c->setRequires(array(
- new Link('c/c', 'd/d')
- ));
- $d->setAutoload(array('classmap' => array('src/D.php')));
- $e->setAutoload(array('classmap' => array('src/E.php')));
- $this->repository->expects($this->once())
- ->method('getCanonicalPackages')
- ->will($this->returnValue($packages));
- $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
- $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
- $this->fs->ensureDirectoryExists($this->vendorDir.'/c/c/src');
- $this->fs->ensureDirectoryExists($this->vendorDir.'/d/d/src');
- $this->fs->ensureDirectoryExists($this->vendorDir.'/e/e/src');
- file_put_contents($this->vendorDir.'/a/a/src/A.php', '<?php class A {}');
- file_put_contents($this->vendorDir.'/b/b/src/B.php', '<?php class B {}');
- file_put_contents($this->vendorDir.'/c/c/src/C.php', '<?php class C {}');
- file_put_contents($this->vendorDir.'/d/d/src/D.php', '<?php class D {}');
- file_put_contents($this->vendorDir.'/e/e/src/E.php', '<?php class E {}');
- $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_5');
- $this->assertAutoloadFiles('classmap9', $this->vendorDir.'/composer', 'classmap');
- }
- public function testPharAutoload()
- {
- $package = new Package('a', '1.0', '1.0');
- $package->setRequires(array(
- new Link('a', 'a/a'),
- ));
- $package->setAutoload(array(
- 'psr-0' => array(
- 'Foo' => 'foo.phar',
- 'Bar' => 'dir/bar.phar/src',
- ),
- 'psr-4' => array(
- 'Baz\\' => 'baz.phar',
- 'Qux\\' => 'dir/qux.phar/src',
- ),
- ));
- $vendorPackage = new Package('a/a', '1.0', '1.0');
- $vendorPackage->setAutoload(array(
- 'psr-0' => array(
- 'Lorem' => 'lorem.phar',
- 'Ipsum' => 'dir/ipsum.phar/src',
- ),
- 'psr-4' => array(
- 'Dolor\\' => 'dolor.phar',
- 'Sit\\' => 'dir/sit.phar/src',
- ),
- ));
- $this->repository->expects($this->once())
- ->method('getCanonicalPackages')
- ->will($this->returnValue(array($vendorPackage)));
- $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, 'Phar');
- $this->assertAutoloadFiles('phar', $this->vendorDir . '/composer');
- $this->assertAutoloadFiles('phar_psr4', $this->vendorDir . '/composer', 'psr4');
- $this->assertAutoloadFiles('phar_static', $this->vendorDir . '/composer', 'static');
- }
- public function testPSRToClassMapIgnoresNonExistingDir()
- {
- $package = new Package('a', '1.0', '1.0');
- $package->setAutoload(array(
- 'psr-0' => array('Prefix' => 'foo/bar/non/existing/'),
- 'psr-4' => array('Prefix\\' => 'foo/bar/non/existing2/'),
- ));
- $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->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated.");
- $this->assertEquals(
- array(
- 'Composer\\InstalledVersions' => $this->vendorDir.'/composer/InstalledVersions.php',
- ),
- include $this->vendorDir.'/composer/autoload_classmap.php'
- );
- }
- public function testPSRToClassMapIgnoresNonPSRClasses()
- {
- $package = new Package('a', '1.0', '1.0');
- $package->setAutoload(array(
- 'psr-0' => array('psr0_' => 'psr0/'),
- 'psr-4' => array('psr4\\' => 'psr4/'),
- ));
- $this->repository->expects($this->once())
- ->method('getCanonicalPackages')
- ->will($this->returnValue(array()));
- $this->fs->ensureDirectoryExists($this->workingDir.'/psr0/psr0');
- $this->fs->ensureDirectoryExists($this->workingDir.'/psr4');
- file_put_contents($this->workingDir.'/psr0/psr0/match.php', '<?php class psr0_match {}');
- file_put_contents($this->workingDir.'/psr0/psr0/badfile.php', '<?php class psr0_badclass {}');
- file_put_contents($this->workingDir.'/psr4/match.php', '<?php namespace psr4; class match {}');
- file_put_contents($this->workingDir.'/psr4/badfile.php', '<?php namespace psr4; class badclass {}');
- $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_1');
- $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated.");
- $expectedClassmap = <<<EOF
- <?php
- // autoload_classmap.php @generated by Composer
- \$vendorDir = dirname(dirname(__FILE__));
- \$baseDir = dirname(\$vendorDir);
- return array(
- 'Composer\\\\InstalledVersions' => \$vendorDir . '/composer/InstalledVersions.php',
- 'psr0_match' => \$baseDir . '/psr0/psr0/match.php',
- 'psr4\\\\match' => \$baseDir . '/psr4/match.php',
- );
- EOF;
- $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_classmap.php', $expectedClassmap);
- }
- public function testVendorsClassMapAutoloading()
- {
- $package = new Package('a', '1.0', '1.0');
- $package->setRequires(array(
- new Link('a', 'a/a'),
- new Link('a', 'b/b'),
- ));
- $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('src/')));
- $b->setAutoload(array('classmap' => array('src/', 'lib/')));
- $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/src');
- $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/lib');
- 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->config, $this->repository, $package, $this->im, 'composer', false, '_6');
- $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated.");
- $this->assertEquals(
- 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',
- 'Composer\\InstalledVersions' => $this->vendorDir.'/composer/InstalledVersions.php',
- ),
- include $this->vendorDir.'/composer/autoload_classmap.php'
- );
- $this->assertAutoloadFiles('classmap4', $this->vendorDir.'/composer', 'classmap');
- }
- public function testVendorsClassMapAutoloadingWithTargetDir()
- {
- $package = new Package('a', '1.0', '1.0');
- $package->setRequires(array(
- new Link('a', 'a/a'),
- new Link('a', 'b/b'),
- ));
- $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->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated.");
- $this->assertEquals(
- 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',
- 'Composer\\InstalledVersions' => $this->vendorDir.'/composer/InstalledVersions.php',
- ),
- include $this->vendorDir.'/composer/autoload_classmap.php'
- );
- }
- public function testClassMapAutoloadingEmptyDirAndExactFile()
- {
- $package = new Package('a', '1.0', '1.0');
- $package->setRequires(array(
- new Link('a', 'a/a'),
- new Link('a', 'b/b'),
- new Link('a', 'c/c'),
- ));
- $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 {}');
- $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_7');
- $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated.");
- $this->assertEquals(
- array(
- 'ClassMapBar' => $this->vendorDir.'/b/b/test.php',
- 'ClassMapBaz' => $this->vendorDir.'/c/c/foo/test.php',
- 'ClassMapFoo' => $this->vendorDir.'/a/a/src/a.php',
- 'Composer\\InstalledVersions' => $this->vendorDir.'/composer/InstalledVersions.php',
- ),
- include $this->vendorDir.'/composer/autoload_classmap.php'
- );
- $this->assertAutoloadFiles('classmap5', $this->vendorDir.'/composer', 'classmap');
- $this->assertNotContains('$loader->setClassMapAuthoritative(true);', file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
- $this->assertNotContains('$loader->setApcuPrefix(', file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
- }
- public function testClassMapAutoloadingAuthoritativeAndApcu()
- {
- $package = new Package('a', '1.0', '1.0');
- $package->setRequires(array(
- new Link('a', 'a/a'),
- new Link('a', 'b/b'),
- new Link('a', 'c/c'),
- ));
- $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('psr-4' => array('' => 'src/')));
- $b->setAutoload(array('psr-4' => array('' => './')));
- $c->setAutoload(array('psr-4' => array('' => 'foo/')));
- $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/ClassMapFoo.php', '<?php class ClassMapFoo {}');
- file_put_contents($this->vendorDir.'/b/b/ClassMapBar.php', '<?php class ClassMapBar {}');
- file_put_contents($this->vendorDir.'/c/c/foo/ClassMapBaz.php', '<?php class ClassMapBaz {}');
- $this->generator->setClassMapAuthoritative(true);
- $this->generator->setApcu(true);
- $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_7');
- $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated.");
- $this->assertEquals(
- array(
- 'ClassMapBar' => $this->vendorDir.'/b/b/ClassMapBar.php',
- 'ClassMapBaz' => $this->vendorDir.'/c/c/foo/ClassMapBaz.php',
- 'ClassMapFoo' => $this->vendorDir.'/a/a/src/ClassMapFoo.php',
- 'Composer\\InstalledVersions' => $this->vendorDir.'/composer/InstalledVersions.php',
- ),
- include $this->vendorDir.'/composer/autoload_classmap.php'
- );
- $this->assertAutoloadFiles('classmap8', $this->vendorDir.'/composer', 'classmap');
- $this->assertContains('$loader->setClassMapAuthoritative(true);', file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
- $this->assertContains('$loader->setApcuPrefix(', file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
- }
- public function testFilesAutoloadGeneration()
- {
- $package = new Package('a', '1.0', '1.0');
- $package->setAutoload(array('files' => array('root.php')));
- $package->setRequires(array(
- new Link('a', 'a/a'),
- new Link('a', 'b/b'),
- new Link('a', 'c/c'),
- ));
- $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() {}');
- $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'FilesAutoload');
- $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php');
- $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_real_functions.php', $this->vendorDir.'/composer/autoload_real.php');
- $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_static_functions.php', $this->vendorDir.'/composer/autoload_static.php');
- $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_files_functions.php', $this->vendorDir.'/composer/autoload_files.php');
- $loader = require $this->vendorDir . '/autoload.php';
- $loader->unregister();
- $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 testFilesAutoloadGenerationRemoveExtraEntitiesFromAutoloadFiles()
- {
- $autoloadPackage = new Package('a', '1.0', '1.0');
- $autoloadPackage->setAutoload(array('files' => array('root.php')));
- $autoloadPackage->setIncludePaths(array('/lib', '/src'));
- $notAutoloadPackage = new Package('a', '1.0', '1.0');
- $requires = array(
- new Link('a', 'a/a'),
- new Link('a', 'b/b'),
- new Link('a', 'c/c'),
- );
- $autoloadPackage->setRequires($requires);
- $notAutoloadPackage->setRequires($requires);
- $autoloadPackages = array();
- $autoloadPackages[] = $a = new Package('a/a', '1.0', '1.0');
- $autoloadPackages[] = $b = new Package('b/b', '1.0', '1.0');
- $autoloadPackages[] = $c = new Package('c/c', '1.0', '1.0');
- $a->setAutoload(array('files' => array('test.php')));
- $a->setIncludePaths(array('lib1', 'src1'));
- $b->setAutoload(array('files' => array('test2.php')));
- $b->setIncludePaths(array('lib2'));
- $c->setAutoload(array('files' => array('test3.php', 'foo/bar/test4.php')));
- $c->setIncludePaths(array('lib3'));
- $c->setTargetDir('foo/bar');
- $notAutoloadPackages = array();
- $notAutoloadPackages[] = $a = new Package('a/a', '1.0', '1.0');
- $notAutoloadPackages[] = $b = new Package('b/b', '1.0', '1.0');
- $notAutoloadPackages[] = $c = new Package('c/c', '1.0', '1.0');
- $this->repository->expects($this->at(0))
- ->method('getCanonicalPackages')
- ->will($this->returnValue($autoloadPackages));
- $this->repository->expects($this->at(1))
- ->method('getCanonicalPackages')
- ->will($this->returnValue($notAutoloadPackages));
- $this->repository->expects($this->at(2))
- ->method('getCanonicalPackages')
- ->will($this->returnValue($notAutoloadPackages));
- $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() {}');
- $this->generator->dump($this->config, $this->repository, $autoloadPackage, $this->im, 'composer', false, 'FilesAutoload');
- $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php');
- $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_real_functions_with_include_paths.php', $this->vendorDir.'/composer/autoload_real.php');
- $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_static_functions_with_include_paths.php', $this->vendorDir.'/composer/autoload_static.php');
- $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_files_functions.php', $this->vendorDir.'/composer/autoload_files.php');
- $this->assertFileContentEquals(__DIR__.'/Fixtures/include_paths_functions.php', $this->vendorDir.'/composer/include_paths.php');
- $this->generator->dump($this->config, $this->repository, $autoloadPackage, $this->im, 'composer', false, 'FilesAutoload');
- $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php');
- $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_real_functions_with_include_paths.php', $this->vendorDir.'/composer/autoload_real.php');
- $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_files_functions_with_removed_extra.php', $this->vendorDir.'/composer/autoload_files.php');
- $this->assertFileContentEquals(__DIR__.'/Fixtures/include_paths_functions_with_removed_extra.php', $this->vendorDir.'/composer/include_paths.php');
- $this->generator->dump($this->config, $this->repository, $notAutoloadPackage, $this->im, 'composer', false, 'FilesAutoload');
- $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php');
- $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_real_functions_with_removed_include_paths_and_autolad_files.php', $this->vendorDir.'/composer/autoload_real.php');
- $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_static_functions_with_removed_include_paths_and_autolad_files.php', $this->vendorDir.'/composer/autoload_static.php');
- $this->assertFileNotExists($this->vendorDir.'/composer/autoload_files.php');
- $this->assertFileNotExists($this->vendorDir.'/composer/include_paths.php');
- }
- public function testFilesAutoloadOrderByDependencies()
- {
- $package = new Package('a', '1.0', '1.0');
- $package->setAutoload(array('files' => array('root2.php')));
- $package->setRequires(array(
- new Link('a', 'z/foo'),
- new Link('a', 'b/bar'),
- new Link('a', 'd/d'),
- 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 . '/root2.php', '<?php function testFilesAutoloadOrderByDependencyRoot() {}');
- $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'FilesAutoloadOrder');
- $this->assertFileContentEquals(__DIR__ . '/Fixtures/autoload_functions_by_dependency.php', $this->vendorDir . '/autoload.php');
- $this->assertFileContentEquals(__DIR__ . '/Fixtures/autoload_real_files_by_dependency.php', $this->vendorDir . '/composer/autoload_real.php');
- $this->assertFileContentEquals(__DIR__ . '/Fixtures/autoload_static_files_by_dependency.php', $this->vendorDir . '/composer/autoload_static.php');
- $loader = require $this->vendorDir . '/autoload.php';
- $loader->unregister();
- $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'));
- }
- /**
- * Test that PSR-0 and PSR-4 mappings are processed in the correct order for
- * autoloading and for classmap generation:
- * - The main package has priority over other packages.
- * - Longer namespaces have priority over shorter namespaces.
- */
- public function testOverrideVendorsAutoloading()
- {
- $mainPackage = new Package('z', '1.0', '1.0');
- $mainPackage->setAutoload(array(
- 'psr-0' => array('A\\B' => $this->workingDir.'/lib'),
- 'classmap' => array($this->workingDir.'/src'),
- ));
- $mainPackage->setRequires(array(
- new Link('z', 'a/a'),
- new Link('z', 'b/b'),
- ));
- $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');
- // Define the classes A\B\C and Foo\Bar in the main package.
- 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 {}');
- // Define the same two classes in the package a/a.
- 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(
- 'B\\\\Sub\\\\Name' => array(\$vendorDir . '/b/b/src'),
- 'A\\\\B' => array(\$baseDir . '/lib', \$vendorDir . '/a/a/lib'),
- 'A' => array(\$vendorDir . '/a/a/src'),
- );
- EOF;
- // autoload_psr4.php is expected to be empty in this example.
- $expectedPsr4 = <<<EOF
- <?php
- // autoload_psr4.php @generated by Composer
- \$vendorDir = dirname(dirname(__FILE__));
- \$baseDir = dirname(\$vendorDir);
- return array(
- );
- 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',
- 'Composer\\\\InstalledVersions' => \$vendorDir . '/composer/InstalledVersions.php',
- 'Foo\\\\Bar' => \$baseDir . '/src/classes.php',
- );
- EOF;
- $this->generator->dump($this->config, $this->repository, $mainPackage, $this->im, 'composer', true, '_9');
- $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_namespaces.php', $expectedNamespace);
- $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_psr4.php', $expectedPsr4);
- $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_classmap.php', $expectedClassmap);
- }
- public function testIncludePathFileGeneration()
- {
- $package = new Package('a', '1.0', '1.0');
- $packages = array();
- $a = new Package("a/a", "1.0", "1.0");
- $a->setIncludePaths(array("lib/"));
- $b = new Package("b/b", "1.0", "1.0");
- $b->setIncludePaths(array("library"));
- $c = new Package("c", "1.0", "1.0");
- $c->setIncludePaths(array("library"));
- $packages[] = $a;
- $packages[] = $b;
- $packages[] = $c;
- $this->repository->expects($this->once())
- ->method("getCanonicalPackages")
- ->will($this->returnValue($packages));
- $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
- $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_10');
- $this->assertFileContentEquals(__DIR__.'/Fixtures/include_paths.php', $this->vendorDir.'/composer/include_paths.php');
- $this->assertEquals(
- array(
- $this->vendorDir."/a/a/lib",
- $this->vendorDir."/b/b/library",
- $this->vendorDir."/c/library",
- ),
- require $this->vendorDir."/composer/include_paths.php"
- );
- }
- public function testIncludePathsArePrependedInAutoloadFile()
- {
- $package = new Package('a', '1.0', '1.0');
- $packages = array();
- $a = new Package("a/a", "1.0", "1.0");
- $a->setIncludePaths(array("lib/"));
- $packages[] = $a;
- $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, '_11');
- $oldIncludePath = get_include_path();
- $loader = require $this->vendorDir."/autoload.php";
- $loader->unregister();
- $this->assertEquals(
- $this->vendorDir."/a/a/lib".PATH_SEPARATOR.$oldIncludePath,
- 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();
- $loader = require $this->vendorDir."/autoload.php";
- $loader->unregister();
- $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()
- {
- $package = new Package('a', '1.0', '1.0');
- $packages = array();
- $a = new Package("a/a", "1.0", "1.0");
- $packages[] = $a;
- $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');
- $this->assertFileNotExists($this->vendorDir."/composer/include_paths.php");
- }
- public function testPreAndPostEventsAreDispatchedDuringAutoloadDump()
- {
- $this->eventDispatcher
- ->expects($this->at(0))
- ->method('dispatchScript')
- ->with(ScriptEvents::PRE_AUTOLOAD_DUMP, false);
- $this->eventDispatcher
- ->expects($this->at(1))
- ->method('dispatchScript')
- ->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')
- ->will($this->returnValue(array()));
- $this->generator->setRunScripts(true);
- $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_8');
- }
- public function testUseGlobalIncludePath()
- {
- $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')
- ->will($this->returnValue(array()));
- $this->configValueMap['use-include-path'] = true;
- $this->fs->ensureDirectoryExists($this->vendorDir.'/a');
- $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'IncludePath');
- $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_real_include_path.php', $this->vendorDir.'/composer/autoload_real.php');
- $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_static_include_path.php', $this->vendorDir.'/composer/autoload_static.php');
- }
- 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'),
- 'psr-4' => array('Acme\Foo\\' => 'src-psr4'),
- 'classmap' => array('classmap'),
- 'files' => array('test.php'),
- ));
- $package->setRequires(array(
- new Link('a', 'b/b'),
- ));
- $vendorPackage = new Package('b/b', '1.0', '1.0');
- $vendorPackage->setAutoload(array(
- 'psr-0' => array('Bar' => 'lib'),
- 'psr-4' => array('Acme\Bar\\' => 'lib-psr4'),
- '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' => array($baseDir . '/src'),
- 'Bar' => array($vendorDir . '/b/b/lib'),
- );
- EOF;
- $expectedPsr4 = <<<'EOF'
- <?php
- // autoload_psr4.php @generated by Composer
- $vendorDir = dirname(dirname(__FILE__));
- $baseDir = dirname($vendorDir).'/working-dir';
- return array(
- 'Acme\\Foo\\' => array($baseDir . '/src-psr4'),
- 'Acme\\Bar\\' => array($vendorDir . '/b/b/lib-psr4'),
- );
- 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',
- 'Composer\\InstalledVersions' => $vendorDir . '/composer/InstalledVersions.php',
- 'Foo\\Bar' => $baseDir . '/src/Foo/Bar.php',
- 'Foo\\Foo' => $baseDir . '/classmap/classes.php',
- );
- EOF;
- $this->assertStringEqualsFile($vendorDir.'/composer/autoload_namespaces.php', $expectedNamespace);
- $this->assertStringEqualsFile($vendorDir.'/composer/autoload_psr4.php', $expectedPsr4);
- $this->assertStringEqualsFile($vendorDir.'/composer/autoload_classmap.php', $expectedClassmap);
- $this->assertContains("\$vendorDir . '/b/b/bootstrap.php',\n", file_get_contents($vendorDir.'/composer/autoload_files.php'));
- $this->assertContains("\$baseDir . '/test.php',\n", file_get_contents($vendorDir.'/composer/autoload_files.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'),
- 'psr-4' => array('Acme\Foo\\' => '../path/../src-psr4'),
- 'classmap' => array('../classmap'),
- 'files' => array('../test.php'),
- 'exclude-from-classmap' => array('./../classmap/excluded'),
- ));
- $this->repository->expects($this->once())
- ->method('getCanonicalPackages')
- ->will($this->returnValue(array()));
- $this->fs->ensureDirectoryExists($this->workingDir.'/src/Foo');
- $this->fs->ensureDirectoryExists($this->workingDir.'/classmap/excluded');
- 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.'/classmap/excluded/classes.php', '<?php namespace Foo; class Boo {}');
- 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' => array($baseDir . '/../src'),
- );
- EOF;
- $expectedPsr4 = <<<'EOF'
- <?php
- // autoload_psr4.php @generated by Composer
- $vendorDir = dirname(dirname(__FILE__));
- $baseDir = dirname($vendorDir).'/working-dir';
- return array(
- 'Acme\\Foo\\' => array($baseDir . '/../src-psr4'),
- );
- EOF;
- $expectedClassmap = <<<'EOF'
- <?php
- // autoload_classmap.php @generated by Composer
- $vendorDir = dirname(dirname(__FILE__));
- $baseDir = dirname($vendorDir).'/working-dir';
- return array(
- 'Composer\\InstalledVersions' => $vendorDir . '/composer/InstalledVersions.php',
- 'Foo\\Bar' => $baseDir . '/../src/Foo/Bar.php',
- 'Foo\\Foo' => $baseDir . '/../classmap/classes.php',
- );
- EOF;
- $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_namespaces.php', $expectedNamespace);
- $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_psr4.php', $expectedPsr4);
- $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_classmap.php', $expectedClassmap);
- $this->assertContains("\$baseDir . '/../test.php',\n", file_get_contents($this->vendorDir.'/composer/autoload_files.php'));
- }
- public function testEmptyPaths()
- {
- $package = new Package('a', '1.0', '1.0');
- $package->setAutoload(array(
- 'psr-0' => array('Foo' => ''),
- 'psr-4' => array('Acme\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' => array($baseDir . '/'),
- );
- EOF;
- $expectedPsr4 = <<<'EOF'
- <?php
- // autoload_psr4.php @generated by Composer
- $vendorDir = dirname(dirname(__FILE__));
- $baseDir = dirname($vendorDir);
- return array(
- 'Acme\\Foo\\' => array($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',
- 'Composer\\InstalledVersions' => $vendorDir . '/composer/InstalledVersions.php',
- 'Foo\\Bar' => $baseDir . '/Foo/Bar.php',
- );
- EOF;
- $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_namespaces.php', $expectedNamespace);
- $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_psr4.php', $expectedPsr4);
- $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_classmap.php', $expectedClassmap);
- }
- public function testVendorSubstringPath()
- {
- $package = new Package('a', '1.0', '1.0');
- $package->setAutoload(array(
- 'psr-0' => array('Foo' => 'composer-test-autoload-src/src'),
- 'psr-4' => array('Acme\Foo\\' => 'composer-test-autoload-src/src-psr4'),
- ));
- $this->repository->expects($this->once())
- ->method('getCanonicalPackages')
- ->will($this->returnValue(array()));
- $this->fs->ensureDirectoryExists($this->vendorDir.'/a');
- $expectedNamespace = <<<'EOF'
- <?php
- // autoload_namespaces.php @generated by Composer
- $vendorDir = dirname(dirname(__FILE__));
- $baseDir = dirname($vendorDir);
- return array(
- 'Foo' => array($baseDir . '/composer-test-autoload-src/src'),
- );
- EOF;
- $expectedPsr4 = <<<'EOF'
- <?php
- // autoload_psr4.php @generated by Composer
- $vendorDir = dirname(dirname(__FILE__));
- $baseDir = dirname($vendorDir);
- return array(
- 'Acme\\Foo\\' => array($baseDir . '/composer-test-autoload-src/src-psr4'),
- );
- EOF;
- $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'VendorSubstring');
- $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_namespaces.php', $expectedNamespace);
- $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_psr4.php', $expectedPsr4);
- }
- public function testExcludeFromClassmap()
- {
- $package = new Package('a', '1.0', '1.0');
- $package->setAutoload(array(
- 'psr-0' => array(
- 'Main' => 'src/',
- 'Lala' => array('src/', 'lib/'),
- ),
- 'psr-4' => array(
- 'Acme\Fruit\\' => 'src-fruit/',
- 'Acme\Cake\\' => array('src-cake/', 'lib-cake/'),
- ),
- 'classmap' => array('composersrc/'),
- 'exclude-from-classmap' => array(
- '/composersrc/foo/bar/',
- '/composersrc/excludedTests/',
- '/composersrc/ClassToExclude.php',
- '/composersrc/*/excluded/excsubpath',
- '**/excsubpath',
- 'composers', // should _not_ cause exclusion of /composersrc/**, as it is equivalent to /composers/**
- '/src-ca/', // should _not_ cause exclusion of /src-cake/**, as it is equivalent to /src-ca/**
- ),
- ));
- $this->repository->expects($this->once())
- ->method('getCanonicalPackages')
- ->will($this->returnValue(array()));
- $this->fs->ensureDirectoryExists($this->workingDir.'/composer');
- $this->fs->ensureDirectoryExists($this->workingDir.'/src/Lala/Test');
- $this->fs->ensureDirectoryExists($this->workingDir.'/lib');
- file_put_contents($this->workingDir.'/src/Lala/ClassMapMain.php', '<?php namespace Lala; class ClassMapMain {}');
- file_put_contents($this->workingDir.'/src/Lala/Test/ClassMapMainTest.php', '<?php namespace Lala\Test; class ClassMapMainTest {}');
- $this->fs->ensureDirectoryExists($this->workingDir.'/src-fruit');
- $this->fs->ensureDirectoryExists($this->workingDir.'/src-cake');
- $this->fs->ensureDirectoryExists($this->workingDir.'/lib-cake');
- file_put_contents($this->workingDir.'/src-cake/ClassMapBar.php', '<?php namespace Acme\Cake; class ClassMapBar {}');
- $this->fs->ensureDirectoryExists($this->workingDir.'/composersrc');
- $this->fs->ensureDirectoryExists($this->workingDir.'/composersrc/tests');
- file_put_contents($this->workingDir.'/composersrc/foo.php', '<?php class ClassMapFoo {}');
- // these classes should not be found in the classmap
- $this->fs->ensureDirectoryExists($this->workingDir.'/composersrc/excludedTests');
- file_put_contents($this->workingDir.'/composersrc/excludedTests/bar.php', '<?php class ClassExcludeMapFoo {}');
- file_put_contents($this->workingDir.'/composersrc/ClassToExclude.php', '<?php class ClassClassToExclude {}');
- $this->fs->ensureDirectoryExists($this->workingDir.'/composersrc/long/excluded/excsubpath');
- file_put_contents($this->workingDir.'/composersrc/long/excluded/excsubpath/foo.php', '<?php class ClassExcludeMapFoo2 {}');
- file_put_contents($this->workingDir.'/composersrc/long/excluded/excsubpath/bar.php', '<?php class ClassExcludeMapBar {}');
- // symlink directory in project directory in classmap
- $this->fs->ensureDirectoryExists($this->workingDir.'/forks/bar/src/exclude');
- $this->fs->ensureDirectoryExists($this->workingDir.'/composersrc/foo');
- file_put_contents($this->workingDir.'/forks/bar/src/exclude/FooExclClass.php', '<?php class FooExclClass {};');
- $target = $this->workingDir.'/forks/bar/';
- $link = $this->workingDir.'/composersrc/foo/bar';
- $command = Platform::isWindows()
- ? 'mklink /j "' . str_replace('/', '\\', $link) . '" "' . str_replace('/', '\\', $target) . '"'
- : 'ln -s "' . $target . '" "' . $link . '"';
- exec($command);
- $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_1');
- // Assert that autoload_classmap.php was correctly generated.
- $this->assertAutoloadFiles('classmap', $this->vendorDir.'/composer', 'classmap');
- }
- /**
- * @dataProvider platformCheckProvider
- */
- public function testGeneratesPlatformCheck(array $requires, $expectedFixture, array $provides = array(), array $replaces = array())
- {
- $package = new Package('a', '1.0', '1.0');
- $package->setRequires($requires);
- if ($provides) {
- $package->setProvides($provides);
- }
- if ($replaces) {
- $package->setReplaces($replaces);
- }
- $this->repository->expects($this->once())
- ->method('getCanonicalPackages')
- ->will($this->returnValue(array()));
- $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_1');
- $this->assertFileContentEquals(__DIR__ . '/Fixtures/platform/' . $expectedFixture . '.php', $this->vendorDir . '/composer/platform_check.php');
- }
- public function platformCheckProvider()
- {
- $versionParser = new VersionParser();
- return array(
- 'Typical project requirements' => array(
- array(
- new Link('a', 'php', $versionParser->parseConstraints('^7.2')),
- new Link('a', 'ext-xml', $versionParser->parseConstraints('*')),
- new Link('a', 'ext-json', $versionParser->parseConstraints('*'))
- ),
- 'typical'
- ),
- 'No PHP lower bound' => array(
- array(
- new Link('a', 'php', $versionParser->parseConstraints('< 8')),
- ),
- 'no_php_lower_bound'
- ),
- 'No PHP upper bound' => array(
- array(
- new Link('a', 'php', $versionParser->parseConstraints('>= 7.2')),
- ),
- 'no_php_upper_bound'
- ),
- 'Specific PHP release version' => array(
- array(
- new Link('a', 'php', $versionParser->parseConstraints('^7.2.8')),
- ),
- 'specific_php_release'
- ),
- 'No PHP required' => array(
- array(
- new Link('a', 'ext-xml', $versionParser->parseConstraints('*')),
- new Link('a', 'ext-json', $versionParser->parseConstraints('*'))
- ),
- 'no_php_required'
- ),
- 'No extensions required' => array(
- array(
- new Link('a', 'php', $versionParser->parseConstraints('^7.2')),
- ),
- 'no_extensions_required'
- ),
- 'Replaced/provided extensions are not checked for + checking case insensitivity' => array(
- array(
- new Link('a', 'ext-xml', $versionParser->parseConstraints('^7.2')),
- new Link('a', 'ext-Pdo', $versionParser->parseConstraints('^7.2')),
- new Link('a', 'ext-bcMath', $versionParser->parseConstraints('^7.2')),
- ),
- 'replaced_provided_exts',
- array(
- // constraint does not satisfy all the ^7.2 requirement so we do not accept it as being replaced
- new Link('a', 'ext-PDO', $versionParser->parseConstraints('7.1.*')),
- // valid replace of bcmath so no need to check for it
- new Link('a', 'ext-BCMath', $versionParser->parseConstraints('^7.1')),
- ),
- array(
- // valid provide of ext-xml so no need to check for it
- new Link('a', 'ext-XML', $versionParser->parseConstraints('*')),
- ),
- ),
- );
- }
- private function assertAutoloadFiles($name, $dir, $type = 'namespaces')
- {
- $a = __DIR__.'/Fixtures/autoload_'.$name.'.php';
- $b = $dir.'/autoload_'.$type.'.php';
- $this->assertFileContentEquals($a, $b);
- }
- public static function assertFileContentEquals($expected, $actual, $message = '', $canonicalize = false, $ignoreCase = false)
- {
- self::assertEqualsNormalized(
- file_get_contents($expected),
- file_get_contents($actual),
- $message ?: $expected.' equals '.$actual,
- 0,
- 10,
- $canonicalize,
- $ignoreCase
- );
- }
- public static function assertEqualsNormalized($expected, $actual, $message = '', $delta = 0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false)
- {
- parent::assertEquals(str_replace("\r", '', $expected), str_replace("\r", '', $actual), $message, $delta, $maxDepth, $canonicalize, $ignoreCase);
- }
- }
|