AutoloadGeneratorTest.php 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654
  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->assertFileContentEquals(__DIR__.'/Fixtures/autoload_target_dir.php', $this->vendorDir.'/autoload.php');
  289. $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_real_target_dir.php', $this->vendorDir.'/composer/autoload_real.php');
  290. $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_static_target_dir.php', $this->vendorDir.'/composer/autoload_static.php');
  291. $this->assertFileContentEquals(__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 testNonDevAutoloadReplacesNestedRequirements()
  400. {
  401. $package = new Package('a', '1.0', '1.0');
  402. $package->setRequires(array(
  403. new Link('a', 'a/a')
  404. ));
  405. $packages = array();
  406. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  407. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  408. $packages[] = $c = new Package('c/c', '1.0', '1.0');
  409. $packages[] = $d = new Package('d/d', '1.0', '1.0');
  410. $packages[] = $e = new Package('e/e', '1.0', '1.0');
  411. $a->setAutoload(array('classmap' => array('src/A.php')));
  412. $a->setRequires(array(
  413. new Link('a/a', 'b/b')
  414. ));
  415. $b->setAutoload(array('classmap' => array('src/B.php')));
  416. $b->setRequires(array(
  417. new Link('b/b', 'e/e')
  418. ));
  419. $c->setAutoload(array('classmap' => array('src/C.php')));
  420. $c->setReplaces(array(
  421. new Link('c/c', 'b/b')
  422. ));
  423. $c->setRequires(array(
  424. new Link('c/c', 'd/d')
  425. ));
  426. $d->setAutoload(array('classmap' => array('src/D.php')));
  427. $e->setAutoload(array('classmap' => array('src/E.php')));
  428. $this->repository->expects($this->once())
  429. ->method('getCanonicalPackages')
  430. ->will($this->returnValue($packages));
  431. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  432. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  433. $this->fs->ensureDirectoryExists($this->vendorDir.'/c/c/src');
  434. $this->fs->ensureDirectoryExists($this->vendorDir.'/d/d/src');
  435. $this->fs->ensureDirectoryExists($this->vendorDir.'/e/e/src');
  436. file_put_contents($this->vendorDir.'/a/a/src/A.php', '<?php class A {}');
  437. file_put_contents($this->vendorDir.'/b/b/src/B.php', '<?php class B {}');
  438. file_put_contents($this->vendorDir.'/c/c/src/C.php', '<?php class C {}');
  439. file_put_contents($this->vendorDir.'/d/d/src/D.php', '<?php class D {}');
  440. file_put_contents($this->vendorDir.'/e/e/src/E.php', '<?php class E {}');
  441. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_5');
  442. $this->assertAutoloadFiles('classmap9', $this->vendorDir.'/composer', 'classmap');
  443. }
  444. public function testPharAutoload()
  445. {
  446. $package = new Package('a', '1.0', '1.0');
  447. $package->setRequires(array(
  448. new Link('a', 'a/a'),
  449. ));
  450. $package->setAutoload(array(
  451. 'psr-0' => array(
  452. 'Foo' => 'foo.phar',
  453. 'Bar' => 'dir/bar.phar/src',
  454. ),
  455. 'psr-4' => array(
  456. 'Baz\\' => 'baz.phar',
  457. 'Qux\\' => 'dir/qux.phar/src',
  458. ),
  459. ));
  460. $vendorPackage = new Package('a/a', '1.0', '1.0');
  461. $vendorPackage->setAutoload(array(
  462. 'psr-0' => array(
  463. 'Lorem' => 'lorem.phar',
  464. 'Ipsum' => 'dir/ipsum.phar/src',
  465. ),
  466. 'psr-4' => array(
  467. 'Dolor\\' => 'dolor.phar',
  468. 'Sit\\' => 'dir/sit.phar/src',
  469. ),
  470. ));
  471. $this->repository->expects($this->once())
  472. ->method('getCanonicalPackages')
  473. ->will($this->returnValue(array($vendorPackage)));
  474. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, 'Phar');
  475. $this->assertAutoloadFiles('phar', $this->vendorDir . '/composer');
  476. $this->assertAutoloadFiles('phar_psr4', $this->vendorDir . '/composer', 'psr4');
  477. $this->assertAutoloadFiles('phar_static', $this->vendorDir . '/composer', 'static');
  478. }
  479. public function testPSRToClassMapIgnoresNonExistingDir()
  480. {
  481. $package = new Package('a', '1.0', '1.0');
  482. $package->setAutoload(array(
  483. 'psr-0' => array('Prefix' => 'foo/bar/non/existing/'),
  484. 'psr-4' => array('Prefix\\' => 'foo/bar/non/existing2/'),
  485. ));
  486. $this->repository->expects($this->once())
  487. ->method('getCanonicalPackages')
  488. ->will($this->returnValue(array()));
  489. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_8');
  490. $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated.");
  491. $this->assertEquals(
  492. array(),
  493. include $this->vendorDir.'/composer/autoload_classmap.php'
  494. );
  495. }
  496. public function testPSRToClassMapIgnoresNonPSRClasses()
  497. {
  498. $package = new Package('a', '1.0', '1.0');
  499. $this->markTestSkipped('Skipped until ClassMapGenerator ignoring of invalid PSR-x classes is enabled');
  500. $package->setAutoload(array(
  501. 'psr-0' => array('psr0_' => 'psr0/'),
  502. 'psr-4' => array('psr4\\' => 'psr4/'),
  503. ));
  504. $this->repository->expects($this->once())
  505. ->method('getCanonicalPackages')
  506. ->will($this->returnValue(array()));
  507. $this->fs->ensureDirectoryExists($this->workingDir.'/psr0/psr0');
  508. $this->fs->ensureDirectoryExists($this->workingDir.'/psr4');
  509. file_put_contents($this->workingDir.'/psr0/psr0/match.php', '<?php class psr0_match {}');
  510. file_put_contents($this->workingDir.'/psr0/psr0/badfile.php', '<?php class psr0_badclass {}');
  511. file_put_contents($this->workingDir.'/psr4/match.php', '<?php namespace psr4; class match {}');
  512. file_put_contents($this->workingDir.'/psr4/badfile.php', '<?php namespace psr4; class badclass {}');
  513. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_1');
  514. $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated.");
  515. $expectedClassmap = <<<EOF
  516. <?php
  517. // autoload_classmap.php @generated by Composer
  518. \$vendorDir = dirname(dirname(__FILE__));
  519. \$baseDir = dirname(\$vendorDir);
  520. return array(
  521. 'psr0_match' => \$baseDir . '/psr0/psr0/match.php',
  522. 'psr4\\\\match' => \$baseDir . '/psr4/match.php',
  523. );
  524. EOF;
  525. $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_classmap.php', $expectedClassmap);
  526. }
  527. public function testVendorsClassMapAutoloading()
  528. {
  529. $package = new Package('a', '1.0', '1.0');
  530. $package->setRequires(array(
  531. new Link('a', 'a/a'),
  532. new Link('a', 'b/b'),
  533. ));
  534. $packages = array();
  535. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  536. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  537. $a->setAutoload(array('classmap' => array('src/')));
  538. $b->setAutoload(array('classmap' => array('src/', 'lib/')));
  539. $this->repository->expects($this->once())
  540. ->method('getCanonicalPackages')
  541. ->will($this->returnValue($packages));
  542. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  543. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  544. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  545. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/lib');
  546. file_put_contents($this->vendorDir.'/a/a/src/a.php', '<?php class ClassMapFoo {}');
  547. file_put_contents($this->vendorDir.'/b/b/src/b.php', '<?php class ClassMapBar {}');
  548. file_put_contents($this->vendorDir.'/b/b/lib/c.php', '<?php class ClassMapBaz {}');
  549. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_6');
  550. $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated.");
  551. $this->assertEquals(
  552. array(
  553. 'ClassMapBar' => $this->vendorDir.'/b/b/src/b.php',
  554. 'ClassMapBaz' => $this->vendorDir.'/b/b/lib/c.php',
  555. 'ClassMapFoo' => $this->vendorDir.'/a/a/src/a.php',
  556. ),
  557. include $this->vendorDir.'/composer/autoload_classmap.php'
  558. );
  559. $this->assertAutoloadFiles('classmap4', $this->vendorDir.'/composer', 'classmap');
  560. }
  561. public function testVendorsClassMapAutoloadingWithTargetDir()
  562. {
  563. $package = new Package('a', '1.0', '1.0');
  564. $package->setRequires(array(
  565. new Link('a', 'a/a'),
  566. new Link('a', 'b/b'),
  567. ));
  568. $packages = array();
  569. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  570. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  571. $a->setAutoload(array('classmap' => array('target/src/', 'lib/')));
  572. $a->setTargetDir('target');
  573. $b->setAutoload(array('classmap' => array('src/')));
  574. $this->repository->expects($this->once())
  575. ->method('getCanonicalPackages')
  576. ->will($this->returnValue($packages));
  577. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  578. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/target/src');
  579. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/target/lib');
  580. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  581. file_put_contents($this->vendorDir.'/a/a/target/src/a.php', '<?php class ClassMapFoo {}');
  582. file_put_contents($this->vendorDir.'/a/a/target/lib/b.php', '<?php class ClassMapBar {}');
  583. file_put_contents($this->vendorDir.'/b/b/src/c.php', '<?php class ClassMapBaz {}');
  584. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_6');
  585. $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated.");
  586. $this->assertEquals(
  587. array(
  588. 'ClassMapBar' => $this->vendorDir.'/a/a/target/lib/b.php',
  589. 'ClassMapBaz' => $this->vendorDir.'/b/b/src/c.php',
  590. 'ClassMapFoo' => $this->vendorDir.'/a/a/target/src/a.php',
  591. ),
  592. include $this->vendorDir.'/composer/autoload_classmap.php'
  593. );
  594. }
  595. public function testClassMapAutoloadingEmptyDirAndExactFile()
  596. {
  597. $package = new Package('a', '1.0', '1.0');
  598. $package->setRequires(array(
  599. new Link('a', 'a/a'),
  600. new Link('a', 'b/b'),
  601. new Link('a', 'c/c'),
  602. ));
  603. $packages = array();
  604. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  605. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  606. $packages[] = $c = new Package('c/c', '1.0', '1.0');
  607. $a->setAutoload(array('classmap' => array('')));
  608. $b->setAutoload(array('classmap' => array('test.php')));
  609. $c->setAutoload(array('classmap' => array('./')));
  610. $this->repository->expects($this->once())
  611. ->method('getCanonicalPackages')
  612. ->will($this->returnValue($packages));
  613. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  614. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  615. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b');
  616. $this->fs->ensureDirectoryExists($this->vendorDir.'/c/c/foo');
  617. file_put_contents($this->vendorDir.'/a/a/src/a.php', '<?php class ClassMapFoo {}');
  618. file_put_contents($this->vendorDir.'/b/b/test.php', '<?php class ClassMapBar {}');
  619. file_put_contents($this->vendorDir.'/c/c/foo/test.php', '<?php class ClassMapBaz {}');
  620. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_7');
  621. $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated.");
  622. $this->assertEquals(
  623. array(
  624. 'ClassMapBar' => $this->vendorDir.'/b/b/test.php',
  625. 'ClassMapBaz' => $this->vendorDir.'/c/c/foo/test.php',
  626. 'ClassMapFoo' => $this->vendorDir.'/a/a/src/a.php',
  627. ),
  628. include $this->vendorDir.'/composer/autoload_classmap.php'
  629. );
  630. $this->assertAutoloadFiles('classmap5', $this->vendorDir.'/composer', 'classmap');
  631. $this->assertNotContains('$loader->setClassMapAuthoritative(true);', file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
  632. $this->assertNotContains('$loader->setApcuPrefix(', file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
  633. }
  634. public function testClassMapAutoloadingAuthoritativeAndApcu()
  635. {
  636. $package = new Package('a', '1.0', '1.0');
  637. $package->setRequires(array(
  638. new Link('a', 'a/a'),
  639. new Link('a', 'b/b'),
  640. new Link('a', 'c/c'),
  641. ));
  642. $packages = array();
  643. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  644. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  645. $packages[] = $c = new Package('c/c', '1.0', '1.0');
  646. $a->setAutoload(array('psr-4' => array('' => 'src/')));
  647. $b->setAutoload(array('psr-4' => array('' => './')));
  648. $c->setAutoload(array('psr-4' => array('' => 'foo/')));
  649. $this->repository->expects($this->once())
  650. ->method('getCanonicalPackages')
  651. ->will($this->returnValue($packages));
  652. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  653. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  654. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b');
  655. $this->fs->ensureDirectoryExists($this->vendorDir.'/c/c/foo');
  656. file_put_contents($this->vendorDir.'/a/a/src/ClassMapFoo.php', '<?php class ClassMapFoo {}');
  657. file_put_contents($this->vendorDir.'/b/b/ClassMapBar.php', '<?php class ClassMapBar {}');
  658. file_put_contents($this->vendorDir.'/c/c/foo/ClassMapBaz.php', '<?php class ClassMapBaz {}');
  659. $this->generator->setClassMapAuthoritative(true);
  660. $this->generator->setApcu(true);
  661. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_7');
  662. $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated.");
  663. $this->assertEquals(
  664. array(
  665. 'ClassMapBar' => $this->vendorDir.'/b/b/ClassMapBar.php',
  666. 'ClassMapBaz' => $this->vendorDir.'/c/c/foo/ClassMapBaz.php',
  667. 'ClassMapFoo' => $this->vendorDir.'/a/a/src/ClassMapFoo.php',
  668. ),
  669. include $this->vendorDir.'/composer/autoload_classmap.php'
  670. );
  671. $this->assertAutoloadFiles('classmap8', $this->vendorDir.'/composer', 'classmap');
  672. $this->assertContains('$loader->setClassMapAuthoritative(true);', file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
  673. $this->assertContains('$loader->setApcuPrefix(', file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
  674. }
  675. public function testFilesAutoloadGeneration()
  676. {
  677. $package = new Package('a', '1.0', '1.0');
  678. $package->setAutoload(array('files' => array('root.php')));
  679. $package->setRequires(array(
  680. new Link('a', 'a/a'),
  681. new Link('a', 'b/b'),
  682. new Link('a', 'c/c'),
  683. ));
  684. $packages = array();
  685. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  686. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  687. $packages[] = $c = new Package('c/c', '1.0', '1.0');
  688. $a->setAutoload(array('files' => array('test.php')));
  689. $b->setAutoload(array('files' => array('test2.php')));
  690. $c->setAutoload(array('files' => array('test3.php', 'foo/bar/test4.php')));
  691. $c->setTargetDir('foo/bar');
  692. $this->repository->expects($this->once())
  693. ->method('getCanonicalPackages')
  694. ->will($this->returnValue($packages));
  695. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a');
  696. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b');
  697. $this->fs->ensureDirectoryExists($this->vendorDir.'/c/c/foo/bar');
  698. file_put_contents($this->vendorDir.'/a/a/test.php', '<?php function testFilesAutoloadGeneration1() {}');
  699. file_put_contents($this->vendorDir.'/b/b/test2.php', '<?php function testFilesAutoloadGeneration2() {}');
  700. file_put_contents($this->vendorDir.'/c/c/foo/bar/test3.php', '<?php function testFilesAutoloadGeneration3() {}');
  701. file_put_contents($this->vendorDir.'/c/c/foo/bar/test4.php', '<?php function testFilesAutoloadGeneration4() {}');
  702. file_put_contents($this->workingDir.'/root.php', '<?php function testFilesAutoloadGenerationRoot() {}');
  703. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'FilesAutoload');
  704. $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php');
  705. $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_real_functions.php', $this->vendorDir.'/composer/autoload_real.php');
  706. $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_static_functions.php', $this->vendorDir.'/composer/autoload_static.php');
  707. $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_files_functions.php', $this->vendorDir.'/composer/autoload_files.php');
  708. include $this->vendorDir . '/autoload.php';
  709. $this->assertTrue(function_exists('testFilesAutoloadGeneration1'));
  710. $this->assertTrue(function_exists('testFilesAutoloadGeneration2'));
  711. $this->assertTrue(function_exists('testFilesAutoloadGeneration3'));
  712. $this->assertTrue(function_exists('testFilesAutoloadGeneration4'));
  713. $this->assertTrue(function_exists('testFilesAutoloadGenerationRoot'));
  714. }
  715. public function testFilesAutoloadGenerationRemoveExtraEntitiesFromAutoloadFiles()
  716. {
  717. $autoloadPackage = new Package('a', '1.0', '1.0');
  718. $autoloadPackage->setAutoload(array('files' => array('root.php')));
  719. $autoloadPackage->setIncludePaths(array('/lib', '/src'));
  720. $notAutoloadPackage = new Package('a', '1.0', '1.0');
  721. $requires = array(
  722. new Link('a', 'a/a'),
  723. new Link('a', 'b/b'),
  724. new Link('a', 'c/c'),
  725. );
  726. $autoloadPackage->setRequires($requires);
  727. $notAutoloadPackage->setRequires($requires);
  728. $autoloadPackages = array();
  729. $autoloadPackages[] = $a = new Package('a/a', '1.0', '1.0');
  730. $autoloadPackages[] = $b = new Package('b/b', '1.0', '1.0');
  731. $autoloadPackages[] = $c = new Package('c/c', '1.0', '1.0');
  732. $a->setAutoload(array('files' => array('test.php')));
  733. $a->setIncludePaths(array('lib1', 'src1'));
  734. $b->setAutoload(array('files' => array('test2.php')));
  735. $b->setIncludePaths(array('lib2'));
  736. $c->setAutoload(array('files' => array('test3.php', 'foo/bar/test4.php')));
  737. $c->setIncludePaths(array('lib3'));
  738. $c->setTargetDir('foo/bar');
  739. $notAutoloadPackages = array();
  740. $notAutoloadPackages[] = $a = new Package('a/a', '1.0', '1.0');
  741. $notAutoloadPackages[] = $b = new Package('b/b', '1.0', '1.0');
  742. $notAutoloadPackages[] = $c = new Package('c/c', '1.0', '1.0');
  743. $this->repository->expects($this->at(0))
  744. ->method('getCanonicalPackages')
  745. ->will($this->returnValue($autoloadPackages));
  746. $this->repository->expects($this->at(1))
  747. ->method('getCanonicalPackages')
  748. ->will($this->returnValue($notAutoloadPackages));
  749. $this->repository->expects($this->at(2))
  750. ->method('getCanonicalPackages')
  751. ->will($this->returnValue($notAutoloadPackages));
  752. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a');
  753. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b');
  754. $this->fs->ensureDirectoryExists($this->vendorDir.'/c/c/foo/bar');
  755. file_put_contents($this->vendorDir.'/a/a/test.php', '<?php function testFilesAutoloadGeneration1() {}');
  756. file_put_contents($this->vendorDir.'/b/b/test2.php', '<?php function testFilesAutoloadGeneration2() {}');
  757. file_put_contents($this->vendorDir.'/c/c/foo/bar/test3.php', '<?php function testFilesAutoloadGeneration3() {}');
  758. file_put_contents($this->vendorDir.'/c/c/foo/bar/test4.php', '<?php function testFilesAutoloadGeneration4() {}');
  759. file_put_contents($this->workingDir.'/root.php', '<?php function testFilesAutoloadGenerationRoot() {}');
  760. $this->generator->dump($this->config, $this->repository, $autoloadPackage, $this->im, 'composer', false, 'FilesAutoload');
  761. $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php');
  762. $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_real_functions_with_include_paths.php', $this->vendorDir.'/composer/autoload_real.php');
  763. $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_static_functions_with_include_paths.php', $this->vendorDir.'/composer/autoload_static.php');
  764. $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_files_functions.php', $this->vendorDir.'/composer/autoload_files.php');
  765. $this->assertFileContentEquals(__DIR__.'/Fixtures/include_paths_functions.php', $this->vendorDir.'/composer/include_paths.php');
  766. $this->generator->dump($this->config, $this->repository, $autoloadPackage, $this->im, 'composer', false, 'FilesAutoload');
  767. $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php');
  768. $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_real_functions_with_include_paths.php', $this->vendorDir.'/composer/autoload_real.php');
  769. $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_files_functions_with_removed_extra.php', $this->vendorDir.'/composer/autoload_files.php');
  770. $this->assertFileContentEquals(__DIR__.'/Fixtures/include_paths_functions_with_removed_extra.php', $this->vendorDir.'/composer/include_paths.php');
  771. $this->generator->dump($this->config, $this->repository, $notAutoloadPackage, $this->im, 'composer', false, 'FilesAutoload');
  772. $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php');
  773. $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_real_functions_with_removed_include_paths_and_autolad_files.php', $this->vendorDir.'/composer/autoload_real.php');
  774. $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_static_functions_with_removed_include_paths_and_autolad_files.php', $this->vendorDir.'/composer/autoload_static.php');
  775. $this->assertFileNotExists($this->vendorDir.'/composer/autoload_files.php');
  776. $this->assertFileNotExists($this->vendorDir.'/composer/include_paths.php');
  777. }
  778. public function testFilesAutoloadOrderByDependencies()
  779. {
  780. $package = new Package('a', '1.0', '1.0');
  781. $package->setAutoload(array('files' => array('root2.php')));
  782. $package->setRequires(array(
  783. new Link('a', 'z/foo'),
  784. new Link('a', 'b/bar'),
  785. new Link('a', 'd/d'),
  786. new Link('a', 'e/e'),
  787. ));
  788. $packages = array();
  789. $packages[] = $z = new Package('z/foo', '1.0', '1.0');
  790. $packages[] = $b = new Package('b/bar', '1.0', '1.0');
  791. $packages[] = $d = new Package('d/d', '1.0', '1.0');
  792. $packages[] = $c = new Package('c/lorem', '1.0', '1.0');
  793. $packages[] = $e = new Package('e/e', '1.0', '1.0');
  794. $z->setAutoload(array('files' => array('testA.php')));
  795. $z->setRequires(array(new Link('z/foo', 'c/lorem')));
  796. $b->setAutoload(array('files' => array('testB.php')));
  797. $b->setRequires(array(new Link('b/bar', 'c/lorem'), new Link('b/bar', 'd/d')));
  798. $c->setAutoload(array('files' => array('testC.php')));
  799. $d->setAutoload(array('files' => array('testD.php')));
  800. $d->setRequires(array(new Link('d/d', 'c/lorem')));
  801. $e->setAutoload(array('files' => array('testE.php')));
  802. $e->setRequires(array(new Link('e/e', 'c/lorem')));
  803. $this->repository->expects($this->once())
  804. ->method('getCanonicalPackages')
  805. ->will($this->returnValue($packages));
  806. $this->fs->ensureDirectoryExists($this->vendorDir . '/z/foo');
  807. $this->fs->ensureDirectoryExists($this->vendorDir . '/b/bar');
  808. $this->fs->ensureDirectoryExists($this->vendorDir . '/c/lorem');
  809. $this->fs->ensureDirectoryExists($this->vendorDir . '/d/d');
  810. $this->fs->ensureDirectoryExists($this->vendorDir . '/e/e');
  811. file_put_contents($this->vendorDir . '/z/foo/testA.php', '<?php function testFilesAutoloadOrderByDependency1() {}');
  812. file_put_contents($this->vendorDir . '/b/bar/testB.php', '<?php function testFilesAutoloadOrderByDependency2() {}');
  813. file_put_contents($this->vendorDir . '/c/lorem/testC.php', '<?php function testFilesAutoloadOrderByDependency3() {}');
  814. file_put_contents($this->vendorDir . '/d/d/testD.php', '<?php function testFilesAutoloadOrderByDependency4() {}');
  815. file_put_contents($this->vendorDir . '/e/e/testE.php', '<?php function testFilesAutoloadOrderByDependency5() {}');
  816. file_put_contents($this->workingDir . '/root2.php', '<?php function testFilesAutoloadOrderByDependencyRoot() {}');
  817. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'FilesAutoloadOrder');
  818. $this->assertFileContentEquals(__DIR__ . '/Fixtures/autoload_functions_by_dependency.php', $this->vendorDir . '/autoload.php');
  819. $this->assertFileContentEquals(__DIR__ . '/Fixtures/autoload_real_files_by_dependency.php', $this->vendorDir . '/composer/autoload_real.php');
  820. $this->assertFileContentEquals(__DIR__ . '/Fixtures/autoload_static_files_by_dependency.php', $this->vendorDir . '/composer/autoload_static.php');
  821. require $this->vendorDir . '/autoload.php';
  822. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency1'));
  823. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency2'));
  824. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency3'));
  825. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency4'));
  826. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency5'));
  827. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependencyRoot'));
  828. }
  829. /**
  830. * Test that PSR-0 and PSR-4 mappings are processed in the correct order for
  831. * autoloading and for classmap generation:
  832. * - The main package has priority over other packages.
  833. * - Longer namespaces have priority over shorter namespaces.
  834. */
  835. public function testOverrideVendorsAutoloading()
  836. {
  837. $mainPackage = new Package('z', '1.0', '1.0');
  838. $mainPackage->setAutoload(array(
  839. 'psr-0' => array('A\\B' => $this->workingDir.'/lib'),
  840. 'classmap' => array($this->workingDir.'/src'),
  841. ));
  842. $mainPackage->setRequires(array(
  843. new Link('z', 'a/a'),
  844. new Link('z', 'b/b'),
  845. ));
  846. $packages = array();
  847. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  848. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  849. $a->setAutoload(array(
  850. 'psr-0' => array('A' => 'src/', 'A\\B' => 'lib/'),
  851. 'classmap' => array('classmap'),
  852. ));
  853. $b->setAutoload(array(
  854. 'psr-0' => array('B\\Sub\\Name' => 'src/'),
  855. ));
  856. $this->repository->expects($this->once())
  857. ->method('getCanonicalPackages')
  858. ->will($this->returnValue($packages));
  859. $this->fs->ensureDirectoryExists($this->workingDir.'/lib/A/B');
  860. $this->fs->ensureDirectoryExists($this->workingDir.'/src/');
  861. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  862. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/classmap');
  863. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  864. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib/A/B');
  865. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  866. // Define the classes A\B\C and Foo\Bar in the main package.
  867. file_put_contents($this->workingDir.'/lib/A/B/C.php', '<?php namespace A\\B; class C {}');
  868. file_put_contents($this->workingDir.'/src/classes.php', '<?php namespace Foo; class Bar {}');
  869. // Define the same two classes in the package a/a.
  870. file_put_contents($this->vendorDir.'/a/a/lib/A/B/C.php', '<?php namespace A\\B; class C {}');
  871. file_put_contents($this->vendorDir.'/a/a/classmap/classes.php', '<?php namespace Foo; class Bar {}');
  872. $expectedNamespace = <<<EOF
  873. <?php
  874. // autoload_namespaces.php @generated by Composer
  875. \$vendorDir = dirname(dirname(__FILE__));
  876. \$baseDir = dirname(\$vendorDir);
  877. return array(
  878. 'B\\\\Sub\\\\Name' => array(\$vendorDir . '/b/b/src'),
  879. 'A\\\\B' => array(\$baseDir . '/lib', \$vendorDir . '/a/a/lib'),
  880. 'A' => array(\$vendorDir . '/a/a/src'),
  881. );
  882. EOF;
  883. // autoload_psr4.php is expected to be empty in this example.
  884. $expectedPsr4 = <<<EOF
  885. <?php
  886. // autoload_psr4.php @generated by Composer
  887. \$vendorDir = dirname(dirname(__FILE__));
  888. \$baseDir = dirname(\$vendorDir);
  889. return array(
  890. );
  891. EOF;
  892. $expectedClassmap = <<<EOF
  893. <?php
  894. // autoload_classmap.php @generated by Composer
  895. \$vendorDir = dirname(dirname(__FILE__));
  896. \$baseDir = dirname(\$vendorDir);
  897. return array(
  898. 'A\\\\B\\\\C' => \$baseDir . '/lib/A/B/C.php',
  899. 'Foo\\\\Bar' => \$baseDir . '/src/classes.php',
  900. );
  901. EOF;
  902. $this->generator->dump($this->config, $this->repository, $mainPackage, $this->im, 'composer', true, '_9');
  903. $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_namespaces.php', $expectedNamespace);
  904. $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_psr4.php', $expectedPsr4);
  905. $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_classmap.php', $expectedClassmap);
  906. }
  907. public function testIncludePathFileGeneration()
  908. {
  909. $package = new Package('a', '1.0', '1.0');
  910. $packages = array();
  911. $a = new Package("a/a", "1.0", "1.0");
  912. $a->setIncludePaths(array("lib/"));
  913. $b = new Package("b/b", "1.0", "1.0");
  914. $b->setIncludePaths(array("library"));
  915. $c = new Package("c", "1.0", "1.0");
  916. $c->setIncludePaths(array("library"));
  917. $packages[] = $a;
  918. $packages[] = $b;
  919. $packages[] = $c;
  920. $this->repository->expects($this->once())
  921. ->method("getCanonicalPackages")
  922. ->will($this->returnValue($packages));
  923. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  924. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_10');
  925. $this->assertFileContentEquals(__DIR__.'/Fixtures/include_paths.php', $this->vendorDir.'/composer/include_paths.php');
  926. $this->assertEquals(
  927. array(
  928. $this->vendorDir."/a/a/lib",
  929. $this->vendorDir."/b/b/library",
  930. $this->vendorDir."/c/library",
  931. ),
  932. require $this->vendorDir."/composer/include_paths.php"
  933. );
  934. }
  935. public function testIncludePathsArePrependedInAutoloadFile()
  936. {
  937. $package = new Package('a', '1.0', '1.0');
  938. $packages = array();
  939. $a = new Package("a/a", "1.0", "1.0");
  940. $a->setIncludePaths(array("lib/"));
  941. $packages[] = $a;
  942. $this->repository->expects($this->once())
  943. ->method("getCanonicalPackages")
  944. ->will($this->returnValue($packages));
  945. mkdir($this->vendorDir."/composer", 0777, true);
  946. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_11');
  947. $oldIncludePath = get_include_path();
  948. require $this->vendorDir."/autoload.php";
  949. $this->assertEquals(
  950. $this->vendorDir."/a/a/lib".PATH_SEPARATOR.$oldIncludePath,
  951. get_include_path()
  952. );
  953. set_include_path($oldIncludePath);
  954. }
  955. public function testIncludePathsInMainPackage()
  956. {
  957. $package = new Package('a', '1.0', '1.0');
  958. $package->setIncludePaths(array('/lib', '/src'));
  959. $packages = array($a = new Package("a/a", "1.0", "1.0"));
  960. $a->setIncludePaths(array("lib/"));
  961. $this->repository->expects($this->once())
  962. ->method("getCanonicalPackages")
  963. ->will($this->returnValue($packages));
  964. mkdir($this->vendorDir."/composer", 0777, true);
  965. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_12');
  966. $oldIncludePath = get_include_path();
  967. require $this->vendorDir."/autoload.php";
  968. $this->assertEquals(
  969. $this->workingDir."/lib".PATH_SEPARATOR.$this->workingDir."/src".PATH_SEPARATOR.$this->vendorDir."/a/a/lib".PATH_SEPARATOR.$oldIncludePath,
  970. get_include_path()
  971. );
  972. set_include_path($oldIncludePath);
  973. }
  974. public function testIncludePathFileWithoutPathsIsSkipped()
  975. {
  976. $package = new Package('a', '1.0', '1.0');
  977. $packages = array();
  978. $a = new Package("a/a", "1.0", "1.0");
  979. $packages[] = $a;
  980. $this->repository->expects($this->once())
  981. ->method("getCanonicalPackages")
  982. ->will($this->returnValue($packages));
  983. mkdir($this->vendorDir."/composer", 0777, true);
  984. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_12');
  985. $this->assertFileNotExists($this->vendorDir."/composer/include_paths.php");
  986. }
  987. public function testPreAndPostEventsAreDispatchedDuringAutoloadDump()
  988. {
  989. $this->eventDispatcher
  990. ->expects($this->at(0))
  991. ->method('dispatchScript')
  992. ->with(ScriptEvents::PRE_AUTOLOAD_DUMP, false);
  993. $this->eventDispatcher
  994. ->expects($this->at(1))
  995. ->method('dispatchScript')
  996. ->with(ScriptEvents::POST_AUTOLOAD_DUMP, false);
  997. $package = new Package('a', '1.0', '1.0');
  998. $package->setAutoload(array('psr-0' => array('foo/bar/non/existing/')));
  999. $this->repository->expects($this->once())
  1000. ->method('getCanonicalPackages')
  1001. ->will($this->returnValue(array()));
  1002. $this->generator->setRunScripts(true);
  1003. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_8');
  1004. }
  1005. public function testUseGlobalIncludePath()
  1006. {
  1007. $package = new Package('a', '1.0', '1.0');
  1008. $package->setAutoload(array(
  1009. 'psr-0' => array('Main\\Foo' => '', 'Main\\Bar' => ''),
  1010. ));
  1011. $package->setTargetDir('Main/Foo/');
  1012. $this->repository->expects($this->once())
  1013. ->method('getCanonicalPackages')
  1014. ->will($this->returnValue(array()));
  1015. $this->configValueMap['use-include-path'] = true;
  1016. $this->fs->ensureDirectoryExists($this->vendorDir.'/a');
  1017. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'IncludePath');
  1018. $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_real_include_path.php', $this->vendorDir.'/composer/autoload_real.php');
  1019. $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_static_include_path.php', $this->vendorDir.'/composer/autoload_static.php');
  1020. }
  1021. public function testVendorDirExcludedFromWorkingDir()
  1022. {
  1023. $workingDir = $this->vendorDir.'/working-dir';
  1024. $vendorDir = $workingDir.'/../vendor';
  1025. $this->fs->ensureDirectoryExists($workingDir);
  1026. chdir($workingDir);
  1027. $package = new Package('a', '1.0', '1.0');
  1028. $package->setAutoload(array(
  1029. 'psr-0' => array('Foo' => 'src'),
  1030. 'psr-4' => array('Acme\Foo\\' => 'src-psr4'),
  1031. 'classmap' => array('classmap'),
  1032. 'files' => array('test.php'),
  1033. ));
  1034. $package->setRequires(array(
  1035. new Link('a', 'b/b'),
  1036. ));
  1037. $vendorPackage = new Package('b/b', '1.0', '1.0');
  1038. $vendorPackage->setAutoload(array(
  1039. 'psr-0' => array('Bar' => 'lib'),
  1040. 'psr-4' => array('Acme\Bar\\' => 'lib-psr4'),
  1041. 'classmap' => array('classmaps'),
  1042. 'files' => array('bootstrap.php'),
  1043. ));
  1044. $this->repository->expects($this->once())
  1045. ->method('getCanonicalPackages')
  1046. ->will($this->returnValue(array($vendorPackage)));
  1047. $im = $this->getMockBuilder('Composer\Installer\InstallationManager')
  1048. ->disableOriginalConstructor()
  1049. ->getMock();
  1050. $im->expects($this->any())
  1051. ->method('getInstallPath')
  1052. ->will($this->returnCallback(function ($package) use ($vendorDir) {
  1053. $targetDir = $package->getTargetDir();
  1054. return $vendorDir.'/'.$package->getName() . ($targetDir ? '/'.$targetDir : '');
  1055. }));
  1056. $this->fs->ensureDirectoryExists($workingDir.'/src/Foo');
  1057. $this->fs->ensureDirectoryExists($workingDir.'/classmap');
  1058. $this->fs->ensureDirectoryExists($vendorDir.'/composer');
  1059. $this->fs->ensureDirectoryExists($vendorDir.'/b/b/lib/Bar');
  1060. $this->fs->ensureDirectoryExists($vendorDir.'/b/b/classmaps');
  1061. file_put_contents($workingDir.'/src/Foo/Bar.php', '<?php namespace Foo; class Bar {}');
  1062. file_put_contents($workingDir.'/classmap/classes.php', '<?php namespace Foo; class Foo {}');
  1063. file_put_contents($workingDir.'/test.php', '<?php class Foo {}');
  1064. file_put_contents($vendorDir.'/b/b/lib/Bar/Foo.php', '<?php namespace Bar; class Foo {}');
  1065. file_put_contents($vendorDir.'/b/b/classmaps/classes.php', '<?php namespace Bar; class Bar {}');
  1066. file_put_contents($vendorDir.'/b/b/bootstrap.php', '<?php class Bar {}');
  1067. $oldVendorDir = $this->vendorDir;
  1068. $this->vendorDir = $vendorDir;
  1069. $this->generator->dump($this->config, $this->repository, $package, $im, 'composer', true, '_13');
  1070. $this->vendorDir = $oldVendorDir;
  1071. $expectedNamespace = <<<'EOF'
  1072. <?php
  1073. // autoload_namespaces.php @generated by Composer
  1074. $vendorDir = dirname(dirname(__FILE__));
  1075. $baseDir = dirname($vendorDir).'/working-dir';
  1076. return array(
  1077. 'Foo' => array($baseDir . '/src'),
  1078. 'Bar' => array($vendorDir . '/b/b/lib'),
  1079. );
  1080. EOF;
  1081. $expectedPsr4 = <<<'EOF'
  1082. <?php
  1083. // autoload_psr4.php @generated by Composer
  1084. $vendorDir = dirname(dirname(__FILE__));
  1085. $baseDir = dirname($vendorDir).'/working-dir';
  1086. return array(
  1087. 'Acme\\Foo\\' => array($baseDir . '/src-psr4'),
  1088. 'Acme\\Bar\\' => array($vendorDir . '/b/b/lib-psr4'),
  1089. );
  1090. EOF;
  1091. $expectedClassmap = <<<'EOF'
  1092. <?php
  1093. // autoload_classmap.php @generated by Composer
  1094. $vendorDir = dirname(dirname(__FILE__));
  1095. $baseDir = dirname($vendorDir).'/working-dir';
  1096. return array(
  1097. 'Bar\\Bar' => $vendorDir . '/b/b/classmaps/classes.php',
  1098. 'Bar\\Foo' => $vendorDir . '/b/b/lib/Bar/Foo.php',
  1099. 'Foo\\Bar' => $baseDir . '/src/Foo/Bar.php',
  1100. 'Foo\\Foo' => $baseDir . '/classmap/classes.php',
  1101. );
  1102. EOF;
  1103. $this->assertStringEqualsFile($vendorDir.'/composer/autoload_namespaces.php', $expectedNamespace);
  1104. $this->assertStringEqualsFile($vendorDir.'/composer/autoload_psr4.php', $expectedPsr4);
  1105. $this->assertStringEqualsFile($vendorDir.'/composer/autoload_classmap.php', $expectedClassmap);
  1106. $this->assertContains("\$vendorDir . '/b/b/bootstrap.php',\n", file_get_contents($vendorDir.'/composer/autoload_files.php'));
  1107. $this->assertContains("\$baseDir . '/test.php',\n", file_get_contents($vendorDir.'/composer/autoload_files.php'));
  1108. }
  1109. public function testUpLevelRelativePaths()
  1110. {
  1111. $workingDir = $this->workingDir.'/working-dir';
  1112. mkdir($workingDir, 0777, true);
  1113. chdir($workingDir);
  1114. $package = new Package('a', '1.0', '1.0');
  1115. $package->setAutoload(array(
  1116. 'psr-0' => array('Foo' => '../path/../src'),
  1117. 'psr-4' => array('Acme\Foo\\' => '../path/../src-psr4'),
  1118. 'classmap' => array('../classmap'),
  1119. 'files' => array('../test.php'),
  1120. 'exclude-from-classmap' => array('./../classmap/excluded'),
  1121. ));
  1122. $this->repository->expects($this->once())
  1123. ->method('getCanonicalPackages')
  1124. ->will($this->returnValue(array()));
  1125. $this->fs->ensureDirectoryExists($this->workingDir.'/src/Foo');
  1126. $this->fs->ensureDirectoryExists($this->workingDir.'/classmap/excluded');
  1127. file_put_contents($this->workingDir.'/src/Foo/Bar.php', '<?php namespace Foo; class Bar {}');
  1128. file_put_contents($this->workingDir.'/classmap/classes.php', '<?php namespace Foo; class Foo {}');
  1129. file_put_contents($this->workingDir.'/classmap/excluded/classes.php', '<?php namespace Foo; class Boo {}');
  1130. file_put_contents($this->workingDir.'/test.php', '<?php class Foo {}');
  1131. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_14');
  1132. $expectedNamespace = <<<'EOF'
  1133. <?php
  1134. // autoload_namespaces.php @generated by Composer
  1135. $vendorDir = dirname(dirname(__FILE__));
  1136. $baseDir = dirname($vendorDir).'/working-dir';
  1137. return array(
  1138. 'Foo' => array($baseDir . '/../src'),
  1139. );
  1140. EOF;
  1141. $expectedPsr4 = <<<'EOF'
  1142. <?php
  1143. // autoload_psr4.php @generated by Composer
  1144. $vendorDir = dirname(dirname(__FILE__));
  1145. $baseDir = dirname($vendorDir).'/working-dir';
  1146. return array(
  1147. 'Acme\\Foo\\' => array($baseDir . '/../src-psr4'),
  1148. );
  1149. EOF;
  1150. $expectedClassmap = <<<'EOF'
  1151. <?php
  1152. // autoload_classmap.php @generated by Composer
  1153. $vendorDir = dirname(dirname(__FILE__));
  1154. $baseDir = dirname($vendorDir).'/working-dir';
  1155. return array(
  1156. 'Foo\\Bar' => $baseDir . '/../src/Foo/Bar.php',
  1157. 'Foo\\Foo' => $baseDir . '/../classmap/classes.php',
  1158. );
  1159. EOF;
  1160. $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_namespaces.php', $expectedNamespace);
  1161. $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_psr4.php', $expectedPsr4);
  1162. $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_classmap.php', $expectedClassmap);
  1163. $this->assertContains("\$baseDir . '/../test.php',\n", file_get_contents($this->vendorDir.'/composer/autoload_files.php'));
  1164. }
  1165. public function testEmptyPaths()
  1166. {
  1167. $package = new Package('a', '1.0', '1.0');
  1168. $package->setAutoload(array(
  1169. 'psr-0' => array('Foo' => ''),
  1170. 'psr-4' => array('Acme\Foo\\' => ''),
  1171. 'classmap' => array(''),
  1172. ));
  1173. $this->repository->expects($this->once())
  1174. ->method('getCanonicalPackages')
  1175. ->will($this->returnValue(array()));
  1176. $this->fs->ensureDirectoryExists($this->workingDir.'/Foo');
  1177. file_put_contents($this->workingDir.'/Foo/Bar.php', '<?php namespace Foo; class Bar {}');
  1178. file_put_contents($this->workingDir.'/class.php', '<?php namespace Classmap; class Foo {}');
  1179. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_15');
  1180. $expectedNamespace = <<<'EOF'
  1181. <?php
  1182. // autoload_namespaces.php @generated by Composer
  1183. $vendorDir = dirname(dirname(__FILE__));
  1184. $baseDir = dirname($vendorDir);
  1185. return array(
  1186. 'Foo' => array($baseDir . '/'),
  1187. );
  1188. EOF;
  1189. $expectedPsr4 = <<<'EOF'
  1190. <?php
  1191. // autoload_psr4.php @generated by Composer
  1192. $vendorDir = dirname(dirname(__FILE__));
  1193. $baseDir = dirname($vendorDir);
  1194. return array(
  1195. 'Acme\\Foo\\' => array($baseDir . '/'),
  1196. );
  1197. EOF;
  1198. $expectedClassmap = <<<'EOF'
  1199. <?php
  1200. // autoload_classmap.php @generated by Composer
  1201. $vendorDir = dirname(dirname(__FILE__));
  1202. $baseDir = dirname($vendorDir);
  1203. return array(
  1204. 'Classmap\\Foo' => $baseDir . '/class.php',
  1205. 'Foo\\Bar' => $baseDir . '/Foo/Bar.php',
  1206. );
  1207. EOF;
  1208. $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_namespaces.php', $expectedNamespace);
  1209. $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_psr4.php', $expectedPsr4);
  1210. $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_classmap.php', $expectedClassmap);
  1211. }
  1212. public function testVendorSubstringPath()
  1213. {
  1214. $package = new Package('a', '1.0', '1.0');
  1215. $package->setAutoload(array(
  1216. 'psr-0' => array('Foo' => 'composer-test-autoload-src/src'),
  1217. 'psr-4' => array('Acme\Foo\\' => 'composer-test-autoload-src/src-psr4'),
  1218. ));
  1219. $this->repository->expects($this->once())
  1220. ->method('getCanonicalPackages')
  1221. ->will($this->returnValue(array()));
  1222. $this->fs->ensureDirectoryExists($this->vendorDir.'/a');
  1223. $expectedNamespace = <<<'EOF'
  1224. <?php
  1225. // autoload_namespaces.php @generated by Composer
  1226. $vendorDir = dirname(dirname(__FILE__));
  1227. $baseDir = dirname($vendorDir);
  1228. return array(
  1229. 'Foo' => array($baseDir . '/composer-test-autoload-src/src'),
  1230. );
  1231. EOF;
  1232. $expectedPsr4 = <<<'EOF'
  1233. <?php
  1234. // autoload_psr4.php @generated by Composer
  1235. $vendorDir = dirname(dirname(__FILE__));
  1236. $baseDir = dirname($vendorDir);
  1237. return array(
  1238. 'Acme\\Foo\\' => array($baseDir . '/composer-test-autoload-src/src-psr4'),
  1239. );
  1240. EOF;
  1241. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'VendorSubstring');
  1242. $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_namespaces.php', $expectedNamespace);
  1243. $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_psr4.php', $expectedPsr4);
  1244. }
  1245. public function testExcludeFromClassmap()
  1246. {
  1247. $package = new Package('a', '1.0', '1.0');
  1248. $package->setAutoload(array(
  1249. 'psr-0' => array(
  1250. 'Main' => 'src/',
  1251. 'Lala' => array('src/', 'lib/'),
  1252. ),
  1253. 'psr-4' => array(
  1254. 'Acme\Fruit\\' => 'src-fruit/',
  1255. 'Acme\Cake\\' => array('src-cake/', 'lib-cake/'),
  1256. ),
  1257. 'classmap' => array('composersrc/'),
  1258. 'exclude-from-classmap' => array(
  1259. '/composersrc/foo/bar/',
  1260. '/composersrc/excludedTests/',
  1261. '/composersrc/ClassToExclude.php',
  1262. '/composersrc/*/excluded/excsubpath',
  1263. '**/excsubpath',
  1264. 'composers', // should _not_ cause exclusion of /composersrc/**, as it is equivalent to /composers/**
  1265. '/src-ca/', // should _not_ cause exclusion of /src-cake/**, as it is equivalent to /src-ca/**
  1266. ),
  1267. ));
  1268. $this->repository->expects($this->once())
  1269. ->method('getCanonicalPackages')
  1270. ->will($this->returnValue(array()));
  1271. $this->fs->ensureDirectoryExists($this->workingDir.'/composer');
  1272. $this->fs->ensureDirectoryExists($this->workingDir.'/src/Lala/Test');
  1273. $this->fs->ensureDirectoryExists($this->workingDir.'/lib');
  1274. file_put_contents($this->workingDir.'/src/Lala/ClassMapMain.php', '<?php namespace Lala; class ClassMapMain {}');
  1275. file_put_contents($this->workingDir.'/src/Lala/Test/ClassMapMainTest.php', '<?php namespace Lala\Test; class ClassMapMainTest {}');
  1276. $this->fs->ensureDirectoryExists($this->workingDir.'/src-fruit');
  1277. $this->fs->ensureDirectoryExists($this->workingDir.'/src-cake');
  1278. $this->fs->ensureDirectoryExists($this->workingDir.'/lib-cake');
  1279. file_put_contents($this->workingDir.'/src-cake/ClassMapBar.php', '<?php namespace Acme\Cake; class ClassMapBar {}');
  1280. $this->fs->ensureDirectoryExists($this->workingDir.'/composersrc');
  1281. $this->fs->ensureDirectoryExists($this->workingDir.'/composersrc/tests');
  1282. file_put_contents($this->workingDir.'/composersrc/foo.php', '<?php class ClassMapFoo {}');
  1283. // these classes should not be found in the classmap
  1284. $this->fs->ensureDirectoryExists($this->workingDir.'/composersrc/excludedTests');
  1285. file_put_contents($this->workingDir.'/composersrc/excludedTests/bar.php', '<?php class ClassExcludeMapFoo {}');
  1286. file_put_contents($this->workingDir.'/composersrc/ClassToExclude.php', '<?php class ClassClassToExclude {}');
  1287. $this->fs->ensureDirectoryExists($this->workingDir.'/composersrc/long/excluded/excsubpath');
  1288. file_put_contents($this->workingDir.'/composersrc/long/excluded/excsubpath/foo.php', '<?php class ClassExcludeMapFoo2 {}');
  1289. file_put_contents($this->workingDir.'/composersrc/long/excluded/excsubpath/bar.php', '<?php class ClassExcludeMapBar {}');
  1290. // symlink directory in project directory in classmap
  1291. $this->fs->ensureDirectoryExists($this->workingDir.'/forks/bar/src/exclude');
  1292. $this->fs->ensureDirectoryExists($this->workingDir.'/composersrc/foo');
  1293. file_put_contents($this->workingDir.'/forks/bar/src/exclude/FooExclClass.php', '<?php class FooExclClass {};');
  1294. $target = $this->workingDir.'/forks/bar/';
  1295. $link = $this->workingDir.'/composersrc/foo/bar';
  1296. $command = Platform::isWindows()
  1297. ? 'mklink /j "' . str_replace('/', '\\', $link) . '" "' . str_replace('/', '\\', $target) . '"'
  1298. : 'ln -s "' . $target . '" "' . $link . '"';
  1299. exec($command);
  1300. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_1');
  1301. // Assert that autoload_classmap.php was correctly generated.
  1302. $this->assertAutoloadFiles('classmap', $this->vendorDir.'/composer', 'classmap');
  1303. }
  1304. private function assertAutoloadFiles($name, $dir, $type = 'namespaces')
  1305. {
  1306. $a = __DIR__.'/Fixtures/autoload_'.$name.'.php';
  1307. $b = $dir.'/autoload_'.$type.'.php';
  1308. $this->assertFileContentEquals($a, $b);
  1309. }
  1310. public static function assertFileContentEquals($expected, $actual, $message = '', $canonicalize = false, $ignoreCase = false)
  1311. {
  1312. self::assertEqualsNormalized(
  1313. file_get_contents($expected),
  1314. file_get_contents($actual),
  1315. $message ?: $expected.' equals '.$actual,
  1316. 0,
  1317. 10,
  1318. $canonicalize,
  1319. $ignoreCase
  1320. );
  1321. }
  1322. public static function assertEqualsNormalized($expected, $actual, $message = '', $delta = 0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false)
  1323. {
  1324. parent::assertEquals(str_replace("\r", '', $expected), str_replace("\r", '', $actual), $message, $delta, $maxDepth, $canonicalize, $ignoreCase);
  1325. }
  1326. }