AutoloadGeneratorTest.php 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Test\Autoload;
  12. use Composer\Autoload\AutoloadGenerator;
  13. use Composer\Package\Link;
  14. use Composer\Semver\Constraint\Constraint;
  15. use Composer\Util\Filesystem;
  16. use Composer\Package\AliasPackage;
  17. use Composer\Package\Package;
  18. use Composer\Test\TestCase;
  19. use Composer\Script\ScriptEvents;
  20. use Composer\Repository\InstalledRepositoryInterface;
  21. use Composer\Installer\InstallationManager;
  22. use Composer\Config;
  23. use Composer\EventDispatcher\EventDispatcher;
  24. use Composer\Util\Platform;
  25. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  26. class AutoloadGeneratorTest extends TestCase
  27. {
  28. /**
  29. * @var string
  30. */
  31. public $vendorDir;
  32. /**
  33. * @var Config|MockObject
  34. */
  35. private $config;
  36. /**
  37. * @var string
  38. */
  39. private $workingDir;
  40. /**
  41. * @var string
  42. */
  43. private $origDir;
  44. /**
  45. * @var InstallationManager|MockObject
  46. */
  47. private $im;
  48. /**
  49. * @var InstalledRepositoryInterface|MockObject
  50. */
  51. private $repository;
  52. /**
  53. * @var AutoloadGenerator
  54. */
  55. private $generator;
  56. /**
  57. * @var Filesystem
  58. */
  59. private $fs;
  60. /**
  61. * @var EventDispatcher|MockObject
  62. */
  63. private $eventDispatcher;
  64. /**
  65. * Map of setting name => return value configuration for the stub Config
  66. * object.
  67. *
  68. * Note: must be public for compatibility with PHP 5.3 runtimes where
  69. * closures cannot access private members of the classes they are created
  70. * in.
  71. * @var array
  72. */
  73. public $configValueMap;
  74. protected function setUp()
  75. {
  76. $this->fs = new Filesystem;
  77. $that = $this;
  78. $this->workingDir = $this->getUniqueTmpDirectory();
  79. $this->vendorDir = $this->workingDir.DIRECTORY_SEPARATOR.'composer-test-autoload';
  80. $this->ensureDirectoryExistsAndClear($this->vendorDir);
  81. $this->config = $this->getMockBuilder('Composer\Config')->getMock();
  82. $this->configValueMap = array(
  83. 'vendor-dir' => function () use ($that) {
  84. return $that->vendorDir;
  85. },
  86. );
  87. $this->config->expects($this->atLeastOnce())
  88. ->method('get')
  89. ->will($this->returnCallback(function ($arg) use ($that) {
  90. $ret = null;
  91. if (isset($that->configValueMap[$arg])) {
  92. $ret = $that->configValueMap[$arg];
  93. if (is_callable($ret)) {
  94. $ret = $ret();
  95. }
  96. }
  97. return $ret;
  98. }));
  99. $this->origDir = getcwd();
  100. chdir($this->workingDir);
  101. $this->im = $this->getMockBuilder('Composer\Installer\InstallationManager')
  102. ->disableOriginalConstructor()
  103. ->getMock();
  104. $this->im->expects($this->any())
  105. ->method('getInstallPath')
  106. ->will($this->returnCallback(function ($package) use ($that) {
  107. $targetDir = $package->getTargetDir();
  108. return $that->vendorDir.'/'.$package->getName() . ($targetDir ? '/'.$targetDir : '');
  109. }));
  110. $this->repository = $this->getMockBuilder('Composer\Repository\InstalledRepositoryInterface')->getMock();
  111. $this->eventDispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  112. ->disableOriginalConstructor()
  113. ->getMock();
  114. $this->generator = new AutoloadGenerator($this->eventDispatcher);
  115. }
  116. protected function tearDown()
  117. {
  118. chdir($this->origDir);
  119. if (is_dir($this->workingDir)) {
  120. $this->fs->removeDirectory($this->workingDir);
  121. }
  122. if (is_dir($this->vendorDir)) {
  123. $this->fs->removeDirectory($this->vendorDir);
  124. }
  125. }
  126. public function testMainPackageAutoloading()
  127. {
  128. $package = new Package('a', '1.0', '1.0');
  129. $package->setAutoload(array(
  130. 'psr-0' => array(
  131. 'Main' => 'src/',
  132. 'Lala' => array('src/', 'lib/'),
  133. ),
  134. 'psr-4' => array(
  135. 'Acme\Fruit\\' => 'src-fruit/',
  136. 'Acme\Cake\\' => array('src-cake/', 'lib-cake/'),
  137. ),
  138. 'classmap' => array('composersrc/'),
  139. ));
  140. $this->repository->expects($this->once())
  141. ->method('getCanonicalPackages')
  142. ->will($this->returnValue(array()));
  143. $this->fs->ensureDirectoryExists($this->workingDir.'/composer');
  144. $this->fs->ensureDirectoryExists($this->workingDir.'/src/Lala/Test');
  145. $this->fs->ensureDirectoryExists($this->workingDir.'/lib');
  146. file_put_contents($this->workingDir.'/src/Lala/ClassMapMain.php', '<?php namespace Lala; class ClassMapMain {}');
  147. file_put_contents($this->workingDir.'/src/Lala/Test/ClassMapMainTest.php', '<?php namespace Lala\Test; class ClassMapMainTest {}');
  148. $this->fs->ensureDirectoryExists($this->workingDir.'/src-fruit');
  149. $this->fs->ensureDirectoryExists($this->workingDir.'/src-cake');
  150. $this->fs->ensureDirectoryExists($this->workingDir.'/lib-cake');
  151. file_put_contents($this->workingDir.'/src-cake/ClassMapBar.php', '<?php namespace Acme\Cake; class ClassMapBar {}');
  152. $this->fs->ensureDirectoryExists($this->workingDir.'/composersrc');
  153. file_put_contents($this->workingDir.'/composersrc/foo.php', '<?php class ClassMapFoo {}');
  154. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_1');
  155. // Assert that autoload_namespaces.php was correctly generated.
  156. $this->assertAutoloadFiles('main', $this->vendorDir.'/composer');
  157. // Assert that autoload_psr4.php was correctly generated.
  158. $this->assertAutoloadFiles('psr4', $this->vendorDir.'/composer', 'psr4');
  159. // Assert that autoload_classmap.php was correctly generated.
  160. $this->assertAutoloadFiles('classmap', $this->vendorDir.'/composer', 'classmap');
  161. }
  162. public function testMainPackageDevAutoloading()
  163. {
  164. $package = new Package('a', '1.0', '1.0');
  165. $package->setAutoload(array(
  166. 'psr-0' => array(
  167. 'Main' => 'src/',
  168. ),
  169. ));
  170. $package->setDevAutoload(array(
  171. 'files' => array('devfiles/foo.php'),
  172. 'psr-0' => array(
  173. 'Main' => 'tests/',
  174. ),
  175. ));
  176. $this->repository->expects($this->once())
  177. ->method('getCanonicalPackages')
  178. ->will($this->returnValue(array()));
  179. $this->fs->ensureDirectoryExists($this->workingDir.'/composer');
  180. $this->fs->ensureDirectoryExists($this->workingDir.'/src/Main');
  181. file_put_contents($this->workingDir.'/src/Main/ClassMain.php', '<?php namespace Main; class ClassMain {}');
  182. $this->fs->ensureDirectoryExists($this->workingDir.'/devfiles');
  183. file_put_contents($this->workingDir.'/devfiles/foo.php', '<?php function foo() { echo "foo"; }');
  184. // generate autoload files with the dev mode set to true
  185. $this->generator->setDevMode(true);
  186. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_1');
  187. // check standard autoload
  188. $this->assertAutoloadFiles('main5', $this->vendorDir.'/composer');
  189. $this->assertAutoloadFiles('classmap7', $this->vendorDir.'/composer', 'classmap');
  190. // make sure dev autoload is correctly dumped
  191. $this->assertAutoloadFiles('files2', $this->vendorDir.'/composer', 'files');
  192. }
  193. public function testMainPackageDevAutoloadingDisabledByDefault()
  194. {
  195. $package = new Package('a', '1.0', '1.0');
  196. $package->setAutoload(array(
  197. 'psr-0' => array(
  198. 'Main' => 'src/',
  199. ),
  200. ));
  201. $package->setDevAutoload(array(
  202. 'files' => array('devfiles/foo.php'),
  203. ));
  204. $this->repository->expects($this->once())
  205. ->method('getCanonicalPackages')
  206. ->will($this->returnValue(array()));
  207. $this->fs->ensureDirectoryExists($this->workingDir.'/composer');
  208. $this->fs->ensureDirectoryExists($this->workingDir.'/src/Main');
  209. file_put_contents($this->workingDir.'/src/Main/ClassMain.php', '<?php namespace Main; class ClassMain {}');
  210. $this->fs->ensureDirectoryExists($this->workingDir.'/devfiles');
  211. file_put_contents($this->workingDir.'/devfiles/foo.php', '<?php function foo() { echo "foo"; }');
  212. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_1');
  213. // check standard autoload
  214. $this->assertAutoloadFiles('main4', $this->vendorDir.'/composer');
  215. $this->assertAutoloadFiles('classmap7', $this->vendorDir.'/composer', 'classmap');
  216. // make sure dev autoload is disabled when dev mode is set to false
  217. $this->assertFalse(is_file($this->vendorDir.'/composer/autoload_files.php'));
  218. }
  219. public function testVendorDirSameAsWorkingDir()
  220. {
  221. $this->vendorDir = $this->workingDir;
  222. $package = new Package('a', '1.0', '1.0');
  223. $package->setAutoload(array(
  224. 'psr-0' => array('Main' => 'src/', 'Lala' => 'src/'),
  225. 'psr-4' => array(
  226. 'Acme\Fruit\\' => 'src-fruit/',
  227. 'Acme\Cake\\' => array('src-cake/', 'lib-cake/'),
  228. ),
  229. 'classmap' => array('composersrc/'),
  230. ));
  231. $this->repository->expects($this->once())
  232. ->method('getCanonicalPackages')
  233. ->will($this->returnValue(array()));
  234. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  235. $this->fs->ensureDirectoryExists($this->vendorDir.'/src/Main');
  236. file_put_contents($this->vendorDir.'/src/Main/Foo.php', '<?php namespace Main; class Foo {}');
  237. $this->fs->ensureDirectoryExists($this->vendorDir.'/composersrc');
  238. file_put_contents($this->vendorDir.'/composersrc/foo.php', '<?php class ClassMapFoo {}');
  239. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_2');
  240. $this->assertAutoloadFiles('main3', $this->vendorDir.'/composer');
  241. $this->assertAutoloadFiles('psr4_3', $this->vendorDir.'/composer', 'psr4');
  242. $this->assertAutoloadFiles('classmap3', $this->vendorDir.'/composer', 'classmap');
  243. }
  244. public function testMainPackageAutoloadingAlternativeVendorDir()
  245. {
  246. $package = new Package('a', '1.0', '1.0');
  247. $package->setAutoload(array(
  248. 'psr-0' => array('Main' => 'src/', 'Lala' => 'src/'),
  249. 'psr-4' => array(
  250. 'Acme\Fruit\\' => 'src-fruit/',
  251. 'Acme\Cake\\' => array('src-cake/', 'lib-cake/'),
  252. ),
  253. 'classmap' => array('composersrc/'),
  254. ));
  255. $this->repository->expects($this->once())
  256. ->method('getCanonicalPackages')
  257. ->will($this->returnValue(array()));
  258. $this->vendorDir .= '/subdir';
  259. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  260. $this->fs->ensureDirectoryExists($this->workingDir.'/src');
  261. $this->fs->ensureDirectoryExists($this->workingDir.'/composersrc');
  262. file_put_contents($this->workingDir.'/composersrc/foo.php', '<?php class ClassMapFoo {}');
  263. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_3');
  264. $this->assertAutoloadFiles('main2', $this->vendorDir.'/composer');
  265. $this->assertAutoloadFiles('psr4_2', $this->vendorDir.'/composer', 'psr4');
  266. $this->assertAutoloadFiles('classmap2', $this->vendorDir.'/composer', 'classmap');
  267. }
  268. public function testMainPackageAutoloadingWithTargetDir()
  269. {
  270. $package = new Package('a', '1.0', '1.0');
  271. $package->setAutoload(array(
  272. 'psr-0' => array('Main\\Foo' => '', 'Main\\Bar' => ''),
  273. 'classmap' => array('Main/Foo/src', 'lib'),
  274. 'files' => array('foo.php', 'Main/Foo/bar.php'),
  275. ));
  276. $package->setTargetDir('Main/Foo/');
  277. $this->repository->expects($this->once())
  278. ->method('getCanonicalPackages')
  279. ->will($this->returnValue(array()));
  280. $this->fs->ensureDirectoryExists($this->vendorDir.'/a');
  281. $this->fs->ensureDirectoryExists($this->workingDir.'/src');
  282. $this->fs->ensureDirectoryExists($this->workingDir.'/lib');
  283. file_put_contents($this->workingDir.'/src/rootfoo.php', '<?php class ClassMapFoo {}');
  284. file_put_contents($this->workingDir.'/lib/rootbar.php', '<?php class ClassMapBar {}');
  285. file_put_contents($this->workingDir.'/foo.php', '<?php class FilesFoo {}');
  286. file_put_contents($this->workingDir.'/bar.php', '<?php class FilesBar {}');
  287. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'TargetDir');
  288. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_target_dir.php', $this->vendorDir.'/autoload.php');
  289. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_target_dir.php', $this->vendorDir.'/composer/autoload_real.php');
  290. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_static_target_dir.php', $this->vendorDir.'/composer/autoload_static.php');
  291. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_files_target_dir.php', $this->vendorDir.'/composer/autoload_files.php');
  292. $this->assertAutoloadFiles('classmap6', $this->vendorDir.'/composer', 'classmap');
  293. }
  294. public function testVendorsAutoloading()
  295. {
  296. $package = new Package('a', '1.0', '1.0');
  297. $package->setRequires(array(
  298. new Link('a', 'a/a'),
  299. new Link('a', 'b/b'),
  300. ));
  301. $packages = array();
  302. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  303. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  304. $packages[] = $c = new AliasPackage($b, '1.2', '1.2');
  305. $a->setAutoload(array('psr-0' => array('A' => 'src/', 'A\\B' => 'lib/')));
  306. $b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/')));
  307. $this->repository->expects($this->once())
  308. ->method('getCanonicalPackages')
  309. ->will($this->returnValue($packages));
  310. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  311. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  312. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib');
  313. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  314. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_5');
  315. $this->assertAutoloadFiles('vendors', $this->vendorDir.'/composer');
  316. $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated, even if empty.");
  317. }
  318. public function testNonDevAutoloadExclusionWithRecursion()
  319. {
  320. $package = new Package('a', '1.0', '1.0');
  321. $package->setRequires(array(
  322. new Link('a', 'a/a'),
  323. ));
  324. $packages = array();
  325. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  326. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  327. $a->setAutoload(array('psr-0' => array('A' => 'src/', 'A\\B' => 'lib/')));
  328. $a->setRequires(array(
  329. new Link('a/a', 'b/b'),
  330. ));
  331. $b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/')));
  332. $b->setRequires(array(
  333. new Link('b/b', 'a/a'),
  334. ));
  335. $this->repository->expects($this->once())
  336. ->method('getCanonicalPackages')
  337. ->will($this->returnValue($packages));
  338. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  339. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  340. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib');
  341. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  342. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_5');
  343. $this->assertAutoloadFiles('vendors', $this->vendorDir.'/composer');
  344. $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated, even if empty.");
  345. }
  346. public function testNonDevAutoloadShouldIncludeReplacedPackages()
  347. {
  348. $package = new Package('a', '1.0', '1.0');
  349. $package->setRequires(array(new Link('a', 'a/a')));
  350. $packages = array();
  351. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  352. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  353. $a->setRequires(array(new Link('a/a', 'b/c')));
  354. $b->setAutoload(array('psr-4' => array('B\\' => 'src/')));
  355. $b->setReplaces(
  356. array(new Link('b/b', 'b/c', new Constraint('==', '1.0'), 'replaces'))
  357. );
  358. $this->repository->expects($this->once())
  359. ->method('getCanonicalPackages')
  360. ->will($this->returnValue($packages));
  361. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src/C');
  362. file_put_contents($this->vendorDir.'/b/b/src/C/C.php', '<?php namespace B\\C; class C {}');
  363. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_5');
  364. $this->assertEquals(
  365. array(
  366. 'B\\C\\C' => $this->vendorDir.'/b/b/src/C/C.php',
  367. ),
  368. include $this->vendorDir.'/composer/autoload_classmap.php'
  369. );
  370. }
  371. public function testNonDevAutoloadExclusionWithRecursionReplace()
  372. {
  373. $package = new Package('a', '1.0', '1.0');
  374. $package->setRequires(array(
  375. new Link('a', 'a/a'),
  376. ));
  377. $packages = array();
  378. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  379. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  380. $a->setAutoload(array('psr-0' => array('A' => 'src/', 'A\\B' => 'lib/')));
  381. $a->setRequires(array(
  382. new Link('a/a', 'c/c'),
  383. ));
  384. $b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/')));
  385. $b->setReplaces(array(
  386. new Link('b/b', 'c/c'),
  387. ));
  388. $this->repository->expects($this->once())
  389. ->method('getCanonicalPackages')
  390. ->will($this->returnValue($packages));
  391. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  392. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  393. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib');
  394. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  395. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_5');
  396. $this->assertAutoloadFiles('vendors', $this->vendorDir.'/composer');
  397. $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated, even if empty.");
  398. }
  399. public function testPharAutoload()
  400. {
  401. $package = new Package('a', '1.0', '1.0');
  402. $package->setRequires(array(
  403. new Link('a', 'a/a'),
  404. ));
  405. $package->setAutoload(array(
  406. 'psr-0' => array(
  407. 'Foo' => 'foo.phar',
  408. 'Bar' => 'dir/bar.phar/src',
  409. ),
  410. 'psr-4' => array(
  411. 'Baz\\' => 'baz.phar',
  412. 'Qux\\' => 'dir/qux.phar/src',
  413. ),
  414. ));
  415. $vendorPackage = new Package('a/a', '1.0', '1.0');
  416. $vendorPackage->setAutoload(array(
  417. 'psr-0' => array(
  418. 'Lorem' => 'lorem.phar',
  419. 'Ipsum' => 'dir/ipsum.phar/src',
  420. ),
  421. 'psr-4' => array(
  422. 'Dolor\\' => 'dolor.phar',
  423. 'Sit\\' => 'dir/sit.phar/src',
  424. ),
  425. ));
  426. $this->repository->expects($this->once())
  427. ->method('getCanonicalPackages')
  428. ->will($this->returnValue(array($vendorPackage)));
  429. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, 'Phar');
  430. $this->assertAutoloadFiles('phar', $this->vendorDir . '/composer');
  431. $this->assertAutoloadFiles('phar_psr4', $this->vendorDir . '/composer', 'psr4');
  432. $this->assertAutoloadFiles('phar_static', $this->vendorDir . '/composer', 'static');
  433. }
  434. public function testPSRToClassMapIgnoresNonExistingDir()
  435. {
  436. $package = new Package('a', '1.0', '1.0');
  437. $package->setAutoload(array(
  438. 'psr-0' => array('Prefix' => 'foo/bar/non/existing/'),
  439. 'psr-4' => array('Prefix\\' => 'foo/bar/non/existing2/'),
  440. ));
  441. $this->repository->expects($this->once())
  442. ->method('getCanonicalPackages')
  443. ->will($this->returnValue(array()));
  444. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_8');
  445. $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated.");
  446. $this->assertEquals(
  447. array(),
  448. include $this->vendorDir.'/composer/autoload_classmap.php'
  449. );
  450. }
  451. public function testVendorsClassMapAutoloading()
  452. {
  453. $package = new Package('a', '1.0', '1.0');
  454. $package->setRequires(array(
  455. new Link('a', 'a/a'),
  456. new Link('a', 'b/b'),
  457. ));
  458. $packages = array();
  459. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  460. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  461. $a->setAutoload(array('classmap' => array('src/')));
  462. $b->setAutoload(array('classmap' => array('src/', 'lib/')));
  463. $this->repository->expects($this->once())
  464. ->method('getCanonicalPackages')
  465. ->will($this->returnValue($packages));
  466. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  467. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  468. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  469. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/lib');
  470. file_put_contents($this->vendorDir.'/a/a/src/a.php', '<?php class ClassMapFoo {}');
  471. file_put_contents($this->vendorDir.'/b/b/src/b.php', '<?php class ClassMapBar {}');
  472. file_put_contents($this->vendorDir.'/b/b/lib/c.php', '<?php class ClassMapBaz {}');
  473. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_6');
  474. $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated.");
  475. $this->assertEquals(
  476. array(
  477. 'ClassMapBar' => $this->vendorDir.'/b/b/src/b.php',
  478. 'ClassMapBaz' => $this->vendorDir.'/b/b/lib/c.php',
  479. 'ClassMapFoo' => $this->vendorDir.'/a/a/src/a.php',
  480. ),
  481. include $this->vendorDir.'/composer/autoload_classmap.php'
  482. );
  483. $this->assertAutoloadFiles('classmap4', $this->vendorDir.'/composer', 'classmap');
  484. }
  485. public function testVendorsClassMapAutoloadingWithTargetDir()
  486. {
  487. $package = new Package('a', '1.0', '1.0');
  488. $package->setRequires(array(
  489. new Link('a', 'a/a'),
  490. new Link('a', 'b/b'),
  491. ));
  492. $packages = array();
  493. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  494. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  495. $a->setAutoload(array('classmap' => array('target/src/', 'lib/')));
  496. $a->setTargetDir('target');
  497. $b->setAutoload(array('classmap' => array('src/')));
  498. $this->repository->expects($this->once())
  499. ->method('getCanonicalPackages')
  500. ->will($this->returnValue($packages));
  501. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  502. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/target/src');
  503. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/target/lib');
  504. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  505. file_put_contents($this->vendorDir.'/a/a/target/src/a.php', '<?php class ClassMapFoo {}');
  506. file_put_contents($this->vendorDir.'/a/a/target/lib/b.php', '<?php class ClassMapBar {}');
  507. file_put_contents($this->vendorDir.'/b/b/src/c.php', '<?php class ClassMapBaz {}');
  508. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_6');
  509. $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated.");
  510. $this->assertEquals(
  511. array(
  512. 'ClassMapBar' => $this->vendorDir.'/a/a/target/lib/b.php',
  513. 'ClassMapBaz' => $this->vendorDir.'/b/b/src/c.php',
  514. 'ClassMapFoo' => $this->vendorDir.'/a/a/target/src/a.php',
  515. ),
  516. include $this->vendorDir.'/composer/autoload_classmap.php'
  517. );
  518. }
  519. public function testClassMapAutoloadingEmptyDirAndExactFile()
  520. {
  521. $package = new Package('a', '1.0', '1.0');
  522. $package->setRequires(array(
  523. new Link('a', 'a/a'),
  524. new Link('a', 'b/b'),
  525. new Link('a', 'c/c'),
  526. ));
  527. $packages = array();
  528. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  529. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  530. $packages[] = $c = new Package('c/c', '1.0', '1.0');
  531. $a->setAutoload(array('classmap' => array('')));
  532. $b->setAutoload(array('classmap' => array('test.php')));
  533. $c->setAutoload(array('classmap' => array('./')));
  534. $this->repository->expects($this->once())
  535. ->method('getCanonicalPackages')
  536. ->will($this->returnValue($packages));
  537. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  538. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  539. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b');
  540. $this->fs->ensureDirectoryExists($this->vendorDir.'/c/c/foo');
  541. file_put_contents($this->vendorDir.'/a/a/src/a.php', '<?php class ClassMapFoo {}');
  542. file_put_contents($this->vendorDir.'/b/b/test.php', '<?php class ClassMapBar {}');
  543. file_put_contents($this->vendorDir.'/c/c/foo/test.php', '<?php class ClassMapBaz {}');
  544. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_7');
  545. $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated.");
  546. $this->assertEquals(
  547. array(
  548. 'ClassMapBar' => $this->vendorDir.'/b/b/test.php',
  549. 'ClassMapBaz' => $this->vendorDir.'/c/c/foo/test.php',
  550. 'ClassMapFoo' => $this->vendorDir.'/a/a/src/a.php',
  551. ),
  552. include $this->vendorDir.'/composer/autoload_classmap.php'
  553. );
  554. $this->assertAutoloadFiles('classmap5', $this->vendorDir.'/composer', 'classmap');
  555. $this->assertNotContains('$loader->setClassMapAuthoritative(true);', file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
  556. $this->assertNotContains('$loader->setApcuPrefix(', file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
  557. }
  558. public function testClassMapAutoloadingAuthoritativeAndApcu()
  559. {
  560. $package = new Package('a', '1.0', '1.0');
  561. $package->setRequires(array(
  562. new Link('a', 'a/a'),
  563. new Link('a', 'b/b'),
  564. new Link('a', 'c/c'),
  565. ));
  566. $packages = array();
  567. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  568. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  569. $packages[] = $c = new Package('c/c', '1.0', '1.0');
  570. $a->setAutoload(array('psr-4' => array('' => 'src/')));
  571. $b->setAutoload(array('psr-4' => array('' => './')));
  572. $c->setAutoload(array('psr-4' => array('' => 'foo/')));
  573. $this->repository->expects($this->once())
  574. ->method('getCanonicalPackages')
  575. ->will($this->returnValue($packages));
  576. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  577. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  578. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b');
  579. $this->fs->ensureDirectoryExists($this->vendorDir.'/c/c/foo');
  580. file_put_contents($this->vendorDir.'/a/a/src/ClassMapFoo.php', '<?php class ClassMapFoo {}');
  581. file_put_contents($this->vendorDir.'/b/b/ClassMapBar.php', '<?php class ClassMapBar {}');
  582. file_put_contents($this->vendorDir.'/c/c/foo/ClassMapBaz.php', '<?php class ClassMapBaz {}');
  583. $this->generator->setClassMapAuthoritative(true);
  584. $this->generator->setApcu(true);
  585. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_7');
  586. $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated.");
  587. $this->assertEquals(
  588. array(
  589. 'ClassMapBar' => $this->vendorDir.'/b/b/ClassMapBar.php',
  590. 'ClassMapBaz' => $this->vendorDir.'/c/c/foo/ClassMapBaz.php',
  591. 'ClassMapFoo' => $this->vendorDir.'/a/a/src/ClassMapFoo.php',
  592. ),
  593. include $this->vendorDir.'/composer/autoload_classmap.php'
  594. );
  595. $this->assertAutoloadFiles('classmap8', $this->vendorDir.'/composer', 'classmap');
  596. $this->assertContains('$loader->setClassMapAuthoritative(true);', file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
  597. $this->assertContains('$loader->setApcuPrefix(', file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
  598. }
  599. public function testFilesAutoloadGeneration()
  600. {
  601. $package = new Package('a', '1.0', '1.0');
  602. $package->setAutoload(array('files' => array('root.php')));
  603. $package->setRequires(array(
  604. new Link('a', 'a/a'),
  605. new Link('a', 'b/b'),
  606. new Link('a', 'c/c'),
  607. ));
  608. $packages = array();
  609. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  610. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  611. $packages[] = $c = new Package('c/c', '1.0', '1.0');
  612. $a->setAutoload(array('files' => array('test.php')));
  613. $b->setAutoload(array('files' => array('test2.php')));
  614. $c->setAutoload(array('files' => array('test3.php', 'foo/bar/test4.php')));
  615. $c->setTargetDir('foo/bar');
  616. $this->repository->expects($this->once())
  617. ->method('getCanonicalPackages')
  618. ->will($this->returnValue($packages));
  619. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a');
  620. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b');
  621. $this->fs->ensureDirectoryExists($this->vendorDir.'/c/c/foo/bar');
  622. file_put_contents($this->vendorDir.'/a/a/test.php', '<?php function testFilesAutoloadGeneration1() {}');
  623. file_put_contents($this->vendorDir.'/b/b/test2.php', '<?php function testFilesAutoloadGeneration2() {}');
  624. file_put_contents($this->vendorDir.'/c/c/foo/bar/test3.php', '<?php function testFilesAutoloadGeneration3() {}');
  625. file_put_contents($this->vendorDir.'/c/c/foo/bar/test4.php', '<?php function testFilesAutoloadGeneration4() {}');
  626. file_put_contents($this->workingDir.'/root.php', '<?php function testFilesAutoloadGenerationRoot() {}');
  627. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'FilesAutoload');
  628. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php');
  629. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_functions.php', $this->vendorDir.'/composer/autoload_real.php');
  630. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_static_functions.php', $this->vendorDir.'/composer/autoload_static.php');
  631. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_files_functions.php', $this->vendorDir.'/composer/autoload_files.php');
  632. include $this->vendorDir . '/autoload.php';
  633. $this->assertTrue(function_exists('testFilesAutoloadGeneration1'));
  634. $this->assertTrue(function_exists('testFilesAutoloadGeneration2'));
  635. $this->assertTrue(function_exists('testFilesAutoloadGeneration3'));
  636. $this->assertTrue(function_exists('testFilesAutoloadGeneration4'));
  637. $this->assertTrue(function_exists('testFilesAutoloadGenerationRoot'));
  638. }
  639. public function testFilesAutoloadGenerationRemoveExtraEntitiesFromAutoloadFiles()
  640. {
  641. $autoloadPackage = new Package('a', '1.0', '1.0');
  642. $autoloadPackage->setAutoload(array('files' => array('root.php')));
  643. $autoloadPackage->setIncludePaths(array('/lib', '/src'));
  644. $notAutoloadPackage = new Package('a', '1.0', '1.0');
  645. $requires = array(
  646. new Link('a', 'a/a'),
  647. new Link('a', 'b/b'),
  648. new Link('a', 'c/c'),
  649. );
  650. $autoloadPackage->setRequires($requires);
  651. $notAutoloadPackage->setRequires($requires);
  652. $autoloadPackages = array();
  653. $autoloadPackages[] = $a = new Package('a/a', '1.0', '1.0');
  654. $autoloadPackages[] = $b = new Package('b/b', '1.0', '1.0');
  655. $autoloadPackages[] = $c = new Package('c/c', '1.0', '1.0');
  656. $a->setAutoload(array('files' => array('test.php')));
  657. $a->setIncludePaths(array('lib1', 'src1'));
  658. $b->setAutoload(array('files' => array('test2.php')));
  659. $b->setIncludePaths(array('lib2'));
  660. $c->setAutoload(array('files' => array('test3.php', 'foo/bar/test4.php')));
  661. $c->setIncludePaths(array('lib3'));
  662. $c->setTargetDir('foo/bar');
  663. $notAutoloadPackages = array();
  664. $notAutoloadPackages[] = $a = new Package('a/a', '1.0', '1.0');
  665. $notAutoloadPackages[] = $b = new Package('b/b', '1.0', '1.0');
  666. $notAutoloadPackages[] = $c = new Package('c/c', '1.0', '1.0');
  667. $this->repository->expects($this->at(0))
  668. ->method('getCanonicalPackages')
  669. ->will($this->returnValue($autoloadPackages));
  670. $this->repository->expects($this->at(1))
  671. ->method('getCanonicalPackages')
  672. ->will($this->returnValue($notAutoloadPackages));
  673. $this->repository->expects($this->at(2))
  674. ->method('getCanonicalPackages')
  675. ->will($this->returnValue($notAutoloadPackages));
  676. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a');
  677. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b');
  678. $this->fs->ensureDirectoryExists($this->vendorDir.'/c/c/foo/bar');
  679. file_put_contents($this->vendorDir.'/a/a/test.php', '<?php function testFilesAutoloadGeneration1() {}');
  680. file_put_contents($this->vendorDir.'/b/b/test2.php', '<?php function testFilesAutoloadGeneration2() {}');
  681. file_put_contents($this->vendorDir.'/c/c/foo/bar/test3.php', '<?php function testFilesAutoloadGeneration3() {}');
  682. file_put_contents($this->vendorDir.'/c/c/foo/bar/test4.php', '<?php function testFilesAutoloadGeneration4() {}');
  683. file_put_contents($this->workingDir.'/root.php', '<?php function testFilesAutoloadGenerationRoot() {}');
  684. $this->generator->dump($this->config, $this->repository, $autoloadPackage, $this->im, 'composer', false, 'FilesAutoload');
  685. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php');
  686. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_functions_with_include_paths.php', $this->vendorDir.'/composer/autoload_real.php');
  687. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_static_functions_with_include_paths.php', $this->vendorDir.'/composer/autoload_static.php');
  688. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_files_functions.php', $this->vendorDir.'/composer/autoload_files.php');
  689. $this->assertFileEquals(__DIR__.'/Fixtures/include_paths_functions.php', $this->vendorDir.'/composer/include_paths.php');
  690. $this->generator->dump($this->config, $this->repository, $autoloadPackage, $this->im, 'composer', false, 'FilesAutoload');
  691. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php');
  692. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_functions_with_include_paths.php', $this->vendorDir.'/composer/autoload_real.php');
  693. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_files_functions_with_removed_extra.php', $this->vendorDir.'/composer/autoload_files.php');
  694. $this->assertFileEquals(__DIR__.'/Fixtures/include_paths_functions_with_removed_extra.php', $this->vendorDir.'/composer/include_paths.php');
  695. $this->generator->dump($this->config, $this->repository, $notAutoloadPackage, $this->im, 'composer', false, 'FilesAutoload');
  696. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php');
  697. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_functions_with_removed_include_paths_and_autolad_files.php', $this->vendorDir.'/composer/autoload_real.php');
  698. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_static_functions_with_removed_include_paths_and_autolad_files.php', $this->vendorDir.'/composer/autoload_static.php');
  699. $this->assertFileNotExists($this->vendorDir.'/composer/autoload_files.php');
  700. $this->assertFileNotExists($this->vendorDir.'/composer/include_paths.php');
  701. }
  702. public function testFilesAutoloadOrderByDependencies()
  703. {
  704. $package = new Package('a', '1.0', '1.0');
  705. $package->setAutoload(array('files' => array('root2.php')));
  706. $package->setRequires(array(
  707. new Link('a', 'z/foo'),
  708. new Link('a', 'b/bar'),
  709. new Link('a', 'd/d'),
  710. new Link('a', 'e/e'),
  711. ));
  712. $packages = array();
  713. $packages[] = $z = new Package('z/foo', '1.0', '1.0');
  714. $packages[] = $b = new Package('b/bar', '1.0', '1.0');
  715. $packages[] = $d = new Package('d/d', '1.0', '1.0');
  716. $packages[] = $c = new Package('c/lorem', '1.0', '1.0');
  717. $packages[] = $e = new Package('e/e', '1.0', '1.0');
  718. $z->setAutoload(array('files' => array('testA.php')));
  719. $z->setRequires(array(new Link('z/foo', 'c/lorem')));
  720. $b->setAutoload(array('files' => array('testB.php')));
  721. $b->setRequires(array(new Link('b/bar', 'c/lorem'), new Link('b/bar', 'd/d')));
  722. $c->setAutoload(array('files' => array('testC.php')));
  723. $d->setAutoload(array('files' => array('testD.php')));
  724. $d->setRequires(array(new Link('d/d', 'c/lorem')));
  725. $e->setAutoload(array('files' => array('testE.php')));
  726. $e->setRequires(array(new Link('e/e', 'c/lorem')));
  727. $this->repository->expects($this->once())
  728. ->method('getCanonicalPackages')
  729. ->will($this->returnValue($packages));
  730. $this->fs->ensureDirectoryExists($this->vendorDir . '/z/foo');
  731. $this->fs->ensureDirectoryExists($this->vendorDir . '/b/bar');
  732. $this->fs->ensureDirectoryExists($this->vendorDir . '/c/lorem');
  733. $this->fs->ensureDirectoryExists($this->vendorDir . '/d/d');
  734. $this->fs->ensureDirectoryExists($this->vendorDir . '/e/e');
  735. file_put_contents($this->vendorDir . '/z/foo/testA.php', '<?php function testFilesAutoloadOrderByDependency1() {}');
  736. file_put_contents($this->vendorDir . '/b/bar/testB.php', '<?php function testFilesAutoloadOrderByDependency2() {}');
  737. file_put_contents($this->vendorDir . '/c/lorem/testC.php', '<?php function testFilesAutoloadOrderByDependency3() {}');
  738. file_put_contents($this->vendorDir . '/d/d/testD.php', '<?php function testFilesAutoloadOrderByDependency4() {}');
  739. file_put_contents($this->vendorDir . '/e/e/testE.php', '<?php function testFilesAutoloadOrderByDependency5() {}');
  740. file_put_contents($this->workingDir . '/root2.php', '<?php function testFilesAutoloadOrderByDependencyRoot() {}');
  741. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'FilesAutoloadOrder');
  742. $this->assertFileEquals(__DIR__ . '/Fixtures/autoload_functions_by_dependency.php', $this->vendorDir . '/autoload.php');
  743. $this->assertFileEquals(__DIR__ . '/Fixtures/autoload_real_files_by_dependency.php', $this->vendorDir . '/composer/autoload_real.php');
  744. $this->assertFileEquals(__DIR__ . '/Fixtures/autoload_static_files_by_dependency.php', $this->vendorDir . '/composer/autoload_static.php');
  745. require $this->vendorDir . '/autoload.php';
  746. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency1'));
  747. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency2'));
  748. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency3'));
  749. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency4'));
  750. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency5'));
  751. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependencyRoot'));
  752. }
  753. /**
  754. * Test that PSR-0 and PSR-4 mappings are processed in the correct order for
  755. * autoloading and for classmap generation:
  756. * - The main package has priority over other packages.
  757. * - Longer namespaces have priority over shorter namespaces.
  758. */
  759. public function testOverrideVendorsAutoloading()
  760. {
  761. $mainPackage = new Package('z', '1.0', '1.0');
  762. $mainPackage->setAutoload(array(
  763. 'psr-0' => array('A\\B' => $this->workingDir.'/lib'),
  764. 'classmap' => array($this->workingDir.'/src'),
  765. ));
  766. $mainPackage->setRequires(array(
  767. new Link('z', 'a/a'),
  768. new Link('z', 'b/b'),
  769. ));
  770. $packages = array();
  771. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  772. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  773. $a->setAutoload(array(
  774. 'psr-0' => array('A' => 'src/', 'A\\B' => 'lib/'),
  775. 'classmap' => array('classmap'),
  776. ));
  777. $b->setAutoload(array(
  778. 'psr-0' => array('B\\Sub\\Name' => 'src/'),
  779. ));
  780. $this->repository->expects($this->once())
  781. ->method('getCanonicalPackages')
  782. ->will($this->returnValue($packages));
  783. $this->fs->ensureDirectoryExists($this->workingDir.'/lib/A/B');
  784. $this->fs->ensureDirectoryExists($this->workingDir.'/src/');
  785. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  786. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/classmap');
  787. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  788. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib/A/B');
  789. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  790. // Define the classes A\B\C and Foo\Bar in the main package.
  791. file_put_contents($this->workingDir.'/lib/A/B/C.php', '<?php namespace A\\B; class C {}');
  792. file_put_contents($this->workingDir.'/src/classes.php', '<?php namespace Foo; class Bar {}');
  793. // Define the same two classes in the package a/a.
  794. file_put_contents($this->vendorDir.'/a/a/lib/A/B/C.php', '<?php namespace A\\B; class C {}');
  795. file_put_contents($this->vendorDir.'/a/a/classmap/classes.php', '<?php namespace Foo; class Bar {}');
  796. $expectedNamespace = <<<EOF
  797. <?php
  798. // autoload_namespaces.php @generated by Composer
  799. \$vendorDir = dirname(dirname(__FILE__));
  800. \$baseDir = dirname(\$vendorDir);
  801. return array(
  802. 'B\\\\Sub\\\\Name' => array(\$vendorDir . '/b/b/src'),
  803. 'A\\\\B' => array(\$baseDir . '/lib', \$vendorDir . '/a/a/lib'),
  804. 'A' => array(\$vendorDir . '/a/a/src'),
  805. );
  806. EOF;
  807. // autoload_psr4.php is expected to be empty in this example.
  808. $expectedPsr4 = <<<EOF
  809. <?php
  810. // autoload_psr4.php @generated by Composer
  811. \$vendorDir = dirname(dirname(__FILE__));
  812. \$baseDir = dirname(\$vendorDir);
  813. return array(
  814. );
  815. EOF;
  816. $expectedClassmap = <<<EOF
  817. <?php
  818. // autoload_classmap.php @generated by Composer
  819. \$vendorDir = dirname(dirname(__FILE__));
  820. \$baseDir = dirname(\$vendorDir);
  821. return array(
  822. 'A\\\\B\\\\C' => \$baseDir . '/lib/A/B/C.php',
  823. 'Foo\\\\Bar' => \$baseDir . '/src/classes.php',
  824. );
  825. EOF;
  826. $this->generator->dump($this->config, $this->repository, $mainPackage, $this->im, 'composer', true, '_9');
  827. $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_namespaces.php', $expectedNamespace);
  828. $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_psr4.php', $expectedPsr4);
  829. $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_classmap.php', $expectedClassmap);
  830. }
  831. public function testIncludePathFileGeneration()
  832. {
  833. $package = new Package('a', '1.0', '1.0');
  834. $packages = array();
  835. $a = new Package("a/a", "1.0", "1.0");
  836. $a->setIncludePaths(array("lib/"));
  837. $b = new Package("b/b", "1.0", "1.0");
  838. $b->setIncludePaths(array("library"));
  839. $c = new Package("c", "1.0", "1.0");
  840. $c->setIncludePaths(array("library"));
  841. $packages[] = $a;
  842. $packages[] = $b;
  843. $packages[] = $c;
  844. $this->repository->expects($this->once())
  845. ->method("getCanonicalPackages")
  846. ->will($this->returnValue($packages));
  847. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  848. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_10');
  849. $this->assertFileEquals(__DIR__.'/Fixtures/include_paths.php', $this->vendorDir.'/composer/include_paths.php');
  850. $this->assertEquals(
  851. array(
  852. $this->vendorDir."/a/a/lib",
  853. $this->vendorDir."/b/b/library",
  854. $this->vendorDir."/c/library",
  855. ),
  856. require $this->vendorDir."/composer/include_paths.php"
  857. );
  858. }
  859. public function testIncludePathsArePrependedInAutoloadFile()
  860. {
  861. $package = new Package('a', '1.0', '1.0');
  862. $packages = array();
  863. $a = new Package("a/a", "1.0", "1.0");
  864. $a->setIncludePaths(array("lib/"));
  865. $packages[] = $a;
  866. $this->repository->expects($this->once())
  867. ->method("getCanonicalPackages")
  868. ->will($this->returnValue($packages));
  869. mkdir($this->vendorDir."/composer", 0777, true);
  870. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_11');
  871. $oldIncludePath = get_include_path();
  872. require $this->vendorDir."/autoload.php";
  873. $this->assertEquals(
  874. $this->vendorDir."/a/a/lib".PATH_SEPARATOR.$oldIncludePath,
  875. get_include_path()
  876. );
  877. set_include_path($oldIncludePath);
  878. }
  879. public function testIncludePathsInMainPackage()
  880. {
  881. $package = new Package('a', '1.0', '1.0');
  882. $package->setIncludePaths(array('/lib', '/src'));
  883. $packages = array($a = new Package("a/a", "1.0", "1.0"));
  884. $a->setIncludePaths(array("lib/"));
  885. $this->repository->expects($this->once())
  886. ->method("getCanonicalPackages")
  887. ->will($this->returnValue($packages));
  888. mkdir($this->vendorDir."/composer", 0777, true);
  889. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_12');
  890. $oldIncludePath = get_include_path();
  891. require $this->vendorDir."/autoload.php";
  892. $this->assertEquals(
  893. $this->workingDir."/lib".PATH_SEPARATOR.$this->workingDir."/src".PATH_SEPARATOR.$this->vendorDir."/a/a/lib".PATH_SEPARATOR.$oldIncludePath,
  894. get_include_path()
  895. );
  896. set_include_path($oldIncludePath);
  897. }
  898. public function testIncludePathFileWithoutPathsIsSkipped()
  899. {
  900. $package = new Package('a', '1.0', '1.0');
  901. $packages = array();
  902. $a = new Package("a/a", "1.0", "1.0");
  903. $packages[] = $a;
  904. $this->repository->expects($this->once())
  905. ->method("getCanonicalPackages")
  906. ->will($this->returnValue($packages));
  907. mkdir($this->vendorDir."/composer", 0777, true);
  908. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_12');
  909. $this->assertFileNotExists($this->vendorDir."/composer/include_paths.php");
  910. }
  911. public function testPreAndPostEventsAreDispatchedDuringAutoloadDump()
  912. {
  913. $this->eventDispatcher
  914. ->expects($this->at(0))
  915. ->method('dispatchScript')
  916. ->with(ScriptEvents::PRE_AUTOLOAD_DUMP, false);
  917. $this->eventDispatcher
  918. ->expects($this->at(1))
  919. ->method('dispatchScript')
  920. ->with(ScriptEvents::POST_AUTOLOAD_DUMP, false);
  921. $package = new Package('a', '1.0', '1.0');
  922. $package->setAutoload(array('psr-0' => array('foo/bar/non/existing/')));
  923. $this->repository->expects($this->once())
  924. ->method('getCanonicalPackages')
  925. ->will($this->returnValue(array()));
  926. $this->generator->setRunScripts(true);
  927. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_8');
  928. }
  929. public function testUseGlobalIncludePath()
  930. {
  931. $package = new Package('a', '1.0', '1.0');
  932. $package->setAutoload(array(
  933. 'psr-0' => array('Main\\Foo' => '', 'Main\\Bar' => ''),
  934. ));
  935. $package->setTargetDir('Main/Foo/');
  936. $this->repository->expects($this->once())
  937. ->method('getCanonicalPackages')
  938. ->will($this->returnValue(array()));
  939. $this->configValueMap['use-include-path'] = true;
  940. $this->fs->ensureDirectoryExists($this->vendorDir.'/a');
  941. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'IncludePath');
  942. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_include_path.php', $this->vendorDir.'/composer/autoload_real.php');
  943. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_static_include_path.php', $this->vendorDir.'/composer/autoload_static.php');
  944. }
  945. public function testVendorDirExcludedFromWorkingDir()
  946. {
  947. $workingDir = $this->vendorDir.'/working-dir';
  948. $vendorDir = $workingDir.'/../vendor';
  949. $this->fs->ensureDirectoryExists($workingDir);
  950. chdir($workingDir);
  951. $package = new Package('a', '1.0', '1.0');
  952. $package->setAutoload(array(
  953. 'psr-0' => array('Foo' => 'src'),
  954. 'psr-4' => array('Acme\Foo\\' => 'src-psr4'),
  955. 'classmap' => array('classmap'),
  956. 'files' => array('test.php'),
  957. ));
  958. $package->setRequires(array(
  959. new Link('a', 'b/b'),
  960. ));
  961. $vendorPackage = new Package('b/b', '1.0', '1.0');
  962. $vendorPackage->setAutoload(array(
  963. 'psr-0' => array('Bar' => 'lib'),
  964. 'psr-4' => array('Acme\Bar\\' => 'lib-psr4'),
  965. 'classmap' => array('classmaps'),
  966. 'files' => array('bootstrap.php'),
  967. ));
  968. $this->repository->expects($this->once())
  969. ->method('getCanonicalPackages')
  970. ->will($this->returnValue(array($vendorPackage)));
  971. $im = $this->getMockBuilder('Composer\Installer\InstallationManager')
  972. ->disableOriginalConstructor()
  973. ->getMock();
  974. $im->expects($this->any())
  975. ->method('getInstallPath')
  976. ->will($this->returnCallback(function ($package) use ($vendorDir) {
  977. $targetDir = $package->getTargetDir();
  978. return $vendorDir.'/'.$package->getName() . ($targetDir ? '/'.$targetDir : '');
  979. }));
  980. $this->fs->ensureDirectoryExists($workingDir.'/src/Foo');
  981. $this->fs->ensureDirectoryExists($workingDir.'/classmap');
  982. $this->fs->ensureDirectoryExists($vendorDir.'/composer');
  983. $this->fs->ensureDirectoryExists($vendorDir.'/b/b/lib/Bar');
  984. $this->fs->ensureDirectoryExists($vendorDir.'/b/b/classmaps');
  985. file_put_contents($workingDir.'/src/Foo/Bar.php', '<?php namespace Foo; class Bar {}');
  986. file_put_contents($workingDir.'/classmap/classes.php', '<?php namespace Foo; class Foo {}');
  987. file_put_contents($workingDir.'/test.php', '<?php class Foo {}');
  988. file_put_contents($vendorDir.'/b/b/lib/Bar/Foo.php', '<?php namespace Bar; class Foo {}');
  989. file_put_contents($vendorDir.'/b/b/classmaps/classes.php', '<?php namespace Bar; class Bar {}');
  990. file_put_contents($vendorDir.'/b/b/bootstrap.php', '<?php class Bar {}');
  991. $oldVendorDir = $this->vendorDir;
  992. $this->vendorDir = $vendorDir;
  993. $this->generator->dump($this->config, $this->repository, $package, $im, 'composer', true, '_13');
  994. $this->vendorDir = $oldVendorDir;
  995. $expectedNamespace = <<<'EOF'
  996. <?php
  997. // autoload_namespaces.php @generated by Composer
  998. $vendorDir = dirname(dirname(__FILE__));
  999. $baseDir = dirname($vendorDir).'/working-dir';
  1000. return array(
  1001. 'Foo' => array($baseDir . '/src'),
  1002. 'Bar' => array($vendorDir . '/b/b/lib'),
  1003. );
  1004. EOF;
  1005. $expectedPsr4 = <<<'EOF'
  1006. <?php
  1007. // autoload_psr4.php @generated by Composer
  1008. $vendorDir = dirname(dirname(__FILE__));
  1009. $baseDir = dirname($vendorDir).'/working-dir';
  1010. return array(
  1011. 'Acme\\Foo\\' => array($baseDir . '/src-psr4'),
  1012. 'Acme\\Bar\\' => array($vendorDir . '/b/b/lib-psr4'),
  1013. );
  1014. EOF;
  1015. $expectedClassmap = <<<'EOF'
  1016. <?php
  1017. // autoload_classmap.php @generated by Composer
  1018. $vendorDir = dirname(dirname(__FILE__));
  1019. $baseDir = dirname($vendorDir).'/working-dir';
  1020. return array(
  1021. 'Bar\\Bar' => $vendorDir . '/b/b/classmaps/classes.php',
  1022. 'Bar\\Foo' => $vendorDir . '/b/b/lib/Bar/Foo.php',
  1023. 'Foo\\Bar' => $baseDir . '/src/Foo/Bar.php',
  1024. 'Foo\\Foo' => $baseDir . '/classmap/classes.php',
  1025. );
  1026. EOF;
  1027. $this->assertStringEqualsFile($vendorDir.'/composer/autoload_namespaces.php', $expectedNamespace);
  1028. $this->assertStringEqualsFile($vendorDir.'/composer/autoload_psr4.php', $expectedPsr4);
  1029. $this->assertStringEqualsFile($vendorDir.'/composer/autoload_classmap.php', $expectedClassmap);
  1030. $this->assertContains("\$vendorDir . '/b/b/bootstrap.php',\n", file_get_contents($vendorDir.'/composer/autoload_files.php'));
  1031. $this->assertContains("\$baseDir . '/test.php',\n", file_get_contents($vendorDir.'/composer/autoload_files.php'));
  1032. }
  1033. public function testUpLevelRelativePaths()
  1034. {
  1035. $workingDir = $this->workingDir.'/working-dir';
  1036. mkdir($workingDir, 0777, true);
  1037. chdir($workingDir);
  1038. $package = new Package('a', '1.0', '1.0');
  1039. $package->setAutoload(array(
  1040. 'psr-0' => array('Foo' => '../path/../src'),
  1041. 'psr-4' => array('Acme\Foo\\' => '../path/../src-psr4'),
  1042. 'classmap' => array('../classmap'),
  1043. 'files' => array('../test.php'),
  1044. 'exclude-from-classmap' => array('./../classmap/excluded'),
  1045. ));
  1046. $this->repository->expects($this->once())
  1047. ->method('getCanonicalPackages')
  1048. ->will($this->returnValue(array()));
  1049. $this->fs->ensureDirectoryExists($this->workingDir.'/src/Foo');
  1050. $this->fs->ensureDirectoryExists($this->workingDir.'/classmap/excluded');
  1051. file_put_contents($this->workingDir.'/src/Foo/Bar.php', '<?php namespace Foo; class Bar {}');
  1052. file_put_contents($this->workingDir.'/classmap/classes.php', '<?php namespace Foo; class Foo {}');
  1053. file_put_contents($this->workingDir.'/classmap/excluded/classes.php', '<?php namespace Foo; class Boo {}');
  1054. file_put_contents($this->workingDir.'/test.php', '<?php class Foo {}');
  1055. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_14');
  1056. $expectedNamespace = <<<'EOF'
  1057. <?php
  1058. // autoload_namespaces.php @generated by Composer
  1059. $vendorDir = dirname(dirname(__FILE__));
  1060. $baseDir = dirname($vendorDir).'/working-dir';
  1061. return array(
  1062. 'Foo' => array($baseDir . '/../src'),
  1063. );
  1064. EOF;
  1065. $expectedPsr4 = <<<'EOF'
  1066. <?php
  1067. // autoload_psr4.php @generated by Composer
  1068. $vendorDir = dirname(dirname(__FILE__));
  1069. $baseDir = dirname($vendorDir).'/working-dir';
  1070. return array(
  1071. 'Acme\\Foo\\' => array($baseDir . '/../src-psr4'),
  1072. );
  1073. EOF;
  1074. $expectedClassmap = <<<'EOF'
  1075. <?php
  1076. // autoload_classmap.php @generated by Composer
  1077. $vendorDir = dirname(dirname(__FILE__));
  1078. $baseDir = dirname($vendorDir).'/working-dir';
  1079. return array(
  1080. 'Foo\\Bar' => $baseDir . '/../src/Foo/Bar.php',
  1081. 'Foo\\Foo' => $baseDir . '/../classmap/classes.php',
  1082. );
  1083. EOF;
  1084. $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_namespaces.php', $expectedNamespace);
  1085. $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_psr4.php', $expectedPsr4);
  1086. $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_classmap.php', $expectedClassmap);
  1087. $this->assertContains("\$baseDir . '/../test.php',\n", file_get_contents($this->vendorDir.'/composer/autoload_files.php'));
  1088. }
  1089. public function testEmptyPaths()
  1090. {
  1091. $package = new Package('a', '1.0', '1.0');
  1092. $package->setAutoload(array(
  1093. 'psr-0' => array('Foo' => ''),
  1094. 'psr-4' => array('Acme\Foo\\' => ''),
  1095. 'classmap' => array(''),
  1096. ));
  1097. $this->repository->expects($this->once())
  1098. ->method('getCanonicalPackages')
  1099. ->will($this->returnValue(array()));
  1100. $this->fs->ensureDirectoryExists($this->workingDir.'/Foo');
  1101. file_put_contents($this->workingDir.'/Foo/Bar.php', '<?php namespace Foo; class Bar {}');
  1102. file_put_contents($this->workingDir.'/class.php', '<?php namespace Classmap; class Foo {}');
  1103. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_15');
  1104. $expectedNamespace = <<<'EOF'
  1105. <?php
  1106. // autoload_namespaces.php @generated by Composer
  1107. $vendorDir = dirname(dirname(__FILE__));
  1108. $baseDir = dirname($vendorDir);
  1109. return array(
  1110. 'Foo' => array($baseDir . '/'),
  1111. );
  1112. EOF;
  1113. $expectedPsr4 = <<<'EOF'
  1114. <?php
  1115. // autoload_psr4.php @generated by Composer
  1116. $vendorDir = dirname(dirname(__FILE__));
  1117. $baseDir = dirname($vendorDir);
  1118. return array(
  1119. 'Acme\\Foo\\' => array($baseDir . '/'),
  1120. );
  1121. EOF;
  1122. $expectedClassmap = <<<'EOF'
  1123. <?php
  1124. // autoload_classmap.php @generated by Composer
  1125. $vendorDir = dirname(dirname(__FILE__));
  1126. $baseDir = dirname($vendorDir);
  1127. return array(
  1128. 'Classmap\\Foo' => $baseDir . '/class.php',
  1129. 'Foo\\Bar' => $baseDir . '/Foo/Bar.php',
  1130. );
  1131. EOF;
  1132. $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_namespaces.php', $expectedNamespace);
  1133. $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_psr4.php', $expectedPsr4);
  1134. $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_classmap.php', $expectedClassmap);
  1135. }
  1136. public function testVendorSubstringPath()
  1137. {
  1138. $package = new Package('a', '1.0', '1.0');
  1139. $package->setAutoload(array(
  1140. 'psr-0' => array('Foo' => 'composer-test-autoload-src/src'),
  1141. 'psr-4' => array('Acme\Foo\\' => 'composer-test-autoload-src/src-psr4'),
  1142. ));
  1143. $this->repository->expects($this->once())
  1144. ->method('getCanonicalPackages')
  1145. ->will($this->returnValue(array()));
  1146. $this->fs->ensureDirectoryExists($this->vendorDir.'/a');
  1147. $expectedNamespace = <<<'EOF'
  1148. <?php
  1149. // autoload_namespaces.php @generated by Composer
  1150. $vendorDir = dirname(dirname(__FILE__));
  1151. $baseDir = dirname($vendorDir);
  1152. return array(
  1153. 'Foo' => array($baseDir . '/composer-test-autoload-src/src'),
  1154. );
  1155. EOF;
  1156. $expectedPsr4 = <<<'EOF'
  1157. <?php
  1158. // autoload_psr4.php @generated by Composer
  1159. $vendorDir = dirname(dirname(__FILE__));
  1160. $baseDir = dirname($vendorDir);
  1161. return array(
  1162. 'Acme\\Foo\\' => array($baseDir . '/composer-test-autoload-src/src-psr4'),
  1163. );
  1164. EOF;
  1165. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'VendorSubstring');
  1166. $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_namespaces.php', $expectedNamespace);
  1167. $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_psr4.php', $expectedPsr4);
  1168. }
  1169. public function testExcludeFromClassmap()
  1170. {
  1171. $package = new Package('a', '1.0', '1.0');
  1172. $package->setAutoload(array(
  1173. 'psr-0' => array(
  1174. 'Main' => 'src/',
  1175. 'Lala' => array('src/', 'lib/'),
  1176. ),
  1177. 'psr-4' => array(
  1178. 'Acme\Fruit\\' => 'src-fruit/',
  1179. 'Acme\Cake\\' => array('src-cake/', 'lib-cake/'),
  1180. ),
  1181. 'classmap' => array('composersrc/'),
  1182. 'exclude-from-classmap' => array(
  1183. '/composersrc/foo/bar/',
  1184. '/composersrc/excludedTests/',
  1185. '/composersrc/ClassToExclude.php',
  1186. '/composersrc/*/excluded/excsubpath',
  1187. '**/excsubpath',
  1188. ),
  1189. ));
  1190. $this->repository->expects($this->once())
  1191. ->method('getCanonicalPackages')
  1192. ->will($this->returnValue(array()));
  1193. $this->fs->ensureDirectoryExists($this->workingDir.'/composer');
  1194. $this->fs->ensureDirectoryExists($this->workingDir.'/src/Lala/Test');
  1195. $this->fs->ensureDirectoryExists($this->workingDir.'/lib');
  1196. file_put_contents($this->workingDir.'/src/Lala/ClassMapMain.php', '<?php namespace Lala; class ClassMapMain {}');
  1197. file_put_contents($this->workingDir.'/src/Lala/Test/ClassMapMainTest.php', '<?php namespace Lala\Test; class ClassMapMainTest {}');
  1198. $this->fs->ensureDirectoryExists($this->workingDir.'/src-fruit');
  1199. $this->fs->ensureDirectoryExists($this->workingDir.'/src-cake');
  1200. $this->fs->ensureDirectoryExists($this->workingDir.'/lib-cake');
  1201. file_put_contents($this->workingDir.'/src-cake/ClassMapBar.php', '<?php namespace Acme\Cake; class ClassMapBar {}');
  1202. $this->fs->ensureDirectoryExists($this->workingDir.'/composersrc');
  1203. $this->fs->ensureDirectoryExists($this->workingDir.'/composersrc/tests');
  1204. file_put_contents($this->workingDir.'/composersrc/foo.php', '<?php class ClassMapFoo {}');
  1205. // this classes should not be found in the classmap
  1206. $this->fs->ensureDirectoryExists($this->workingDir.'/composersrc/excludedTests');
  1207. file_put_contents($this->workingDir.'/composersrc/excludedTests/bar.php', '<?php class ClassExcludeMapFoo {}');
  1208. file_put_contents($this->workingDir.'/composersrc/ClassToExclude.php', '<?php class ClassClassToExclude {}');
  1209. $this->fs->ensureDirectoryExists($this->workingDir.'/composersrc/long/excluded/excsubpath');
  1210. file_put_contents($this->workingDir.'/composersrc/long/excluded/excsubpath/foo.php', '<?php class ClassExcludeMapFoo2 {}');
  1211. file_put_contents($this->workingDir.'/composersrc/long/excluded/excsubpath/bar.php', '<?php class ClassExcludeMapBar {}');
  1212. // symlink directory in project directory in classmap
  1213. $this->fs->ensureDirectoryExists($this->workingDir.'/forks/bar/src/exclude');
  1214. $this->fs->ensureDirectoryExists($this->workingDir.'/composersrc/foo');
  1215. file_put_contents($this->workingDir.'/forks/bar/src/exclude/FooExclClass.php', '<?php class FooExclClass {};');
  1216. $target = $this->workingDir.'/forks/bar/';
  1217. $link = $this->workingDir.'/composersrc/foo/bar';
  1218. $command = Platform::isWindows()
  1219. ? 'mklink /j "' . str_replace('/', '\\', $link) . '" "' . str_replace('/', '\\', $target) . '"'
  1220. : 'ln -s "' . $target . '" "' . $link . '"';
  1221. exec($command);
  1222. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_1');
  1223. // Assert that autoload_classmap.php was correctly generated.
  1224. $this->assertAutoloadFiles('classmap', $this->vendorDir.'/composer', 'classmap');
  1225. }
  1226. private function assertAutoloadFiles($name, $dir, $type = 'namespaces')
  1227. {
  1228. $a = __DIR__.'/Fixtures/autoload_'.$name.'.php';
  1229. $b = $dir.'/autoload_'.$type.'.php';
  1230. $this->assertFileEquals($a, $b);
  1231. }
  1232. public static function assertFileEquals($expected, $actual, $message = '', $canonicalize = false, $ignoreCase = false)
  1233. {
  1234. return self::assertEquals(
  1235. file_get_contents($expected),
  1236. file_get_contents($actual),
  1237. $message ?: $expected.' equals '.$actual,
  1238. 0,
  1239. 10,
  1240. $canonicalize,
  1241. $ignoreCase
  1242. );
  1243. }
  1244. public static function assertEquals($expected, $actual, $message = '', $delta = 0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false)
  1245. {
  1246. return parent::assertEquals(str_replace("\r", '', $expected), str_replace("\r", '', $actual), $message, $delta, $maxDepth, $canonicalize, $ignoreCase);
  1247. }
  1248. }