AutoloadGeneratorTest.php 73 KB

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