AutoloadGeneratorTest.php 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717
  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. );
  88. $this->config->expects($this->atLeastOnce())
  89. ->method('get')
  90. ->will($this->returnCallback(function ($arg) use ($that) {
  91. $ret = null;
  92. if (isset($that->configValueMap[$arg])) {
  93. $ret = $that->configValueMap[$arg];
  94. if (is_callable($ret)) {
  95. $ret = $ret();
  96. }
  97. }
  98. return $ret;
  99. }));
  100. $this->origDir = getcwd();
  101. chdir($this->workingDir);
  102. $this->im = $this->getMockBuilder('Composer\Installer\InstallationManager')
  103. ->disableOriginalConstructor()
  104. ->getMock();
  105. $this->im->expects($this->any())
  106. ->method('getInstallPath')
  107. ->will($this->returnCallback(function ($package) use ($that) {
  108. $targetDir = $package->getTargetDir();
  109. return $that->vendorDir.'/'.$package->getName() . ($targetDir ? '/'.$targetDir : '');
  110. }));
  111. $this->repository = $this->getMockBuilder('Composer\Repository\InstalledRepositoryInterface')->getMock();
  112. $this->eventDispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  113. ->disableOriginalConstructor()
  114. ->getMock();
  115. $this->generator = new AutoloadGenerator($this->eventDispatcher);
  116. }
  117. protected function tearDown()
  118. {
  119. chdir($this->origDir);
  120. if (is_dir($this->workingDir)) {
  121. $this->fs->removeDirectory($this->workingDir);
  122. }
  123. if (is_dir($this->vendorDir)) {
  124. $this->fs->removeDirectory($this->vendorDir);
  125. }
  126. }
  127. public function testMainPackageAutoloading()
  128. {
  129. $package = new Package('a', '1.0', '1.0');
  130. $package->setAutoload(array(
  131. 'psr-0' => array(
  132. 'Main' => 'src/',
  133. 'Lala' => array('src/', 'lib/'),
  134. ),
  135. 'psr-4' => array(
  136. 'Acme\Fruit\\' => 'src-fruit/',
  137. 'Acme\Cake\\' => array('src-cake/', 'lib-cake/'),
  138. ),
  139. 'classmap' => array('composersrc/'),
  140. ));
  141. $this->repository->expects($this->once())
  142. ->method('getCanonicalPackages')
  143. ->will($this->returnValue(array()));
  144. $this->fs->ensureDirectoryExists($this->workingDir.'/composer');
  145. $this->fs->ensureDirectoryExists($this->workingDir.'/src/Lala/Test');
  146. $this->fs->ensureDirectoryExists($this->workingDir.'/lib');
  147. file_put_contents($this->workingDir.'/src/Lala/ClassMapMain.php', '<?php namespace Lala; class ClassMapMain {}');
  148. file_put_contents($this->workingDir.'/src/Lala/Test/ClassMapMainTest.php', '<?php namespace Lala\Test; class ClassMapMainTest {}');
  149. $this->fs->ensureDirectoryExists($this->workingDir.'/src-fruit');
  150. $this->fs->ensureDirectoryExists($this->workingDir.'/src-cake');
  151. $this->fs->ensureDirectoryExists($this->workingDir.'/lib-cake');
  152. file_put_contents($this->workingDir.'/src-cake/ClassMapBar.php', '<?php namespace Acme\Cake; class ClassMapBar {}');
  153. $this->fs->ensureDirectoryExists($this->workingDir.'/composersrc');
  154. file_put_contents($this->workingDir.'/composersrc/foo.php', '<?php class ClassMapFoo {}');
  155. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_1');
  156. // Assert that autoload_namespaces.php was correctly generated.
  157. $this->assertAutoloadFiles('main', $this->vendorDir.'/composer');
  158. // Assert that autoload_psr4.php was correctly generated.
  159. $this->assertAutoloadFiles('psr4', $this->vendorDir.'/composer', 'psr4');
  160. // Assert that autoload_classmap.php was correctly generated.
  161. $this->assertAutoloadFiles('classmap', $this->vendorDir.'/composer', 'classmap');
  162. }
  163. public function testMainPackageDevAutoloading()
  164. {
  165. $package = new Package('a', '1.0', '1.0');
  166. $package->setAutoload(array(
  167. 'psr-0' => array(
  168. 'Main' => 'src/',
  169. ),
  170. ));
  171. $package->setDevAutoload(array(
  172. 'files' => array('devfiles/foo.php'),
  173. 'psr-0' => array(
  174. 'Main' => 'tests/',
  175. ),
  176. ));
  177. $this->repository->expects($this->once())
  178. ->method('getCanonicalPackages')
  179. ->will($this->returnValue(array()));
  180. $this->fs->ensureDirectoryExists($this->workingDir.'/composer');
  181. $this->fs->ensureDirectoryExists($this->workingDir.'/src/Main');
  182. file_put_contents($this->workingDir.'/src/Main/ClassMain.php', '<?php namespace Main; class ClassMain {}');
  183. $this->fs->ensureDirectoryExists($this->workingDir.'/devfiles');
  184. file_put_contents($this->workingDir.'/devfiles/foo.php', '<?php function foo() { echo "foo"; }');
  185. // generate autoload files with the dev mode set to true
  186. $this->generator->setDevMode(true);
  187. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_1');
  188. // check standard autoload
  189. $this->assertAutoloadFiles('main5', $this->vendorDir.'/composer');
  190. $this->assertAutoloadFiles('classmap7', $this->vendorDir.'/composer', 'classmap');
  191. // make sure dev autoload is correctly dumped
  192. $this->assertAutoloadFiles('files2', $this->vendorDir.'/composer', 'files');
  193. }
  194. public function testMainPackageDevAutoloadingDisabledByDefault()
  195. {
  196. $package = new Package('a', '1.0', '1.0');
  197. $package->setAutoload(array(
  198. 'psr-0' => array(
  199. 'Main' => 'src/',
  200. ),
  201. ));
  202. $package->setDevAutoload(array(
  203. 'files' => array('devfiles/foo.php'),
  204. ));
  205. $this->repository->expects($this->once())
  206. ->method('getCanonicalPackages')
  207. ->will($this->returnValue(array()));
  208. $this->fs->ensureDirectoryExists($this->workingDir.'/composer');
  209. $this->fs->ensureDirectoryExists($this->workingDir.'/src/Main');
  210. file_put_contents($this->workingDir.'/src/Main/ClassMain.php', '<?php namespace Main; class ClassMain {}');
  211. $this->fs->ensureDirectoryExists($this->workingDir.'/devfiles');
  212. file_put_contents($this->workingDir.'/devfiles/foo.php', '<?php function foo() { echo "foo"; }');
  213. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_1');
  214. // check standard autoload
  215. $this->assertAutoloadFiles('main4', $this->vendorDir.'/composer');
  216. $this->assertAutoloadFiles('classmap7', $this->vendorDir.'/composer', 'classmap');
  217. // make sure dev autoload is disabled when dev mode is set to false
  218. $this->assertFalse(is_file($this->vendorDir.'/composer/autoload_files.php'));
  219. }
  220. public function testVendorDirSameAsWorkingDir()
  221. {
  222. $this->vendorDir = $this->workingDir;
  223. $package = new Package('a', '1.0', '1.0');
  224. $package->setAutoload(array(
  225. 'psr-0' => array('Main' => 'src/', 'Lala' => 'src/'),
  226. 'psr-4' => array(
  227. 'Acme\Fruit\\' => 'src-fruit/',
  228. 'Acme\Cake\\' => array('src-cake/', 'lib-cake/'),
  229. ),
  230. 'classmap' => array('composersrc/'),
  231. ));
  232. $this->repository->expects($this->once())
  233. ->method('getCanonicalPackages')
  234. ->will($this->returnValue(array()));
  235. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  236. $this->fs->ensureDirectoryExists($this->vendorDir.'/src/Main');
  237. file_put_contents($this->vendorDir.'/src/Main/Foo.php', '<?php namespace Main; class Foo {}');
  238. $this->fs->ensureDirectoryExists($this->vendorDir.'/composersrc');
  239. file_put_contents($this->vendorDir.'/composersrc/foo.php', '<?php class ClassMapFoo {}');
  240. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_2');
  241. $this->assertAutoloadFiles('main3', $this->vendorDir.'/composer');
  242. $this->assertAutoloadFiles('psr4_3', $this->vendorDir.'/composer', 'psr4');
  243. $this->assertAutoloadFiles('classmap3', $this->vendorDir.'/composer', 'classmap');
  244. }
  245. public function testMainPackageAutoloadingAlternativeVendorDir()
  246. {
  247. $package = new Package('a', '1.0', '1.0');
  248. $package->setAutoload(array(
  249. 'psr-0' => array('Main' => 'src/', 'Lala' => 'src/'),
  250. 'psr-4' => array(
  251. 'Acme\Fruit\\' => 'src-fruit/',
  252. 'Acme\Cake\\' => array('src-cake/', 'lib-cake/'),
  253. ),
  254. 'classmap' => array('composersrc/'),
  255. ));
  256. $this->repository->expects($this->once())
  257. ->method('getCanonicalPackages')
  258. ->will($this->returnValue(array()));
  259. $this->vendorDir .= '/subdir';
  260. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  261. $this->fs->ensureDirectoryExists($this->workingDir.'/src');
  262. $this->fs->ensureDirectoryExists($this->workingDir.'/composersrc');
  263. file_put_contents($this->workingDir.'/composersrc/foo.php', '<?php class ClassMapFoo {}');
  264. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_3');
  265. $this->assertAutoloadFiles('main2', $this->vendorDir.'/composer');
  266. $this->assertAutoloadFiles('psr4_2', $this->vendorDir.'/composer', 'psr4');
  267. $this->assertAutoloadFiles('classmap2', $this->vendorDir.'/composer', 'classmap');
  268. }
  269. public function testMainPackageAutoloadingWithTargetDir()
  270. {
  271. $package = new Package('a', '1.0', '1.0');
  272. $package->setAutoload(array(
  273. 'psr-0' => array('Main\\Foo' => '', 'Main\\Bar' => ''),
  274. 'classmap' => array('Main/Foo/src', 'lib'),
  275. 'files' => array('foo.php', 'Main/Foo/bar.php'),
  276. ));
  277. $package->setTargetDir('Main/Foo/');
  278. $this->repository->expects($this->once())
  279. ->method('getCanonicalPackages')
  280. ->will($this->returnValue(array()));
  281. $this->fs->ensureDirectoryExists($this->vendorDir.'/a');
  282. $this->fs->ensureDirectoryExists($this->workingDir.'/src');
  283. $this->fs->ensureDirectoryExists($this->workingDir.'/lib');
  284. file_put_contents($this->workingDir.'/src/rootfoo.php', '<?php class ClassMapFoo {}');
  285. file_put_contents($this->workingDir.'/lib/rootbar.php', '<?php class ClassMapBar {}');
  286. file_put_contents($this->workingDir.'/foo.php', '<?php class FilesFoo {}');
  287. file_put_contents($this->workingDir.'/bar.php', '<?php class FilesBar {}');
  288. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'TargetDir');
  289. $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_target_dir.php', $this->vendorDir.'/autoload.php');
  290. $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_real_target_dir.php', $this->vendorDir.'/composer/autoload_real.php');
  291. $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_static_target_dir.php', $this->vendorDir.'/composer/autoload_static.php');
  292. $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_files_target_dir.php', $this->vendorDir.'/composer/autoload_files.php');
  293. $this->assertAutoloadFiles('classmap6', $this->vendorDir.'/composer', 'classmap');
  294. }
  295. public function testVendorsAutoloading()
  296. {
  297. $package = new Package('a', '1.0', '1.0');
  298. $package->setRequires(array(
  299. new Link('a', 'a/a'),
  300. new Link('a', 'b/b'),
  301. ));
  302. $packages = array();
  303. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  304. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  305. $packages[] = $c = new AliasPackage($b, '1.2', '1.2');
  306. $a->setAutoload(array('psr-0' => array('A' => 'src/', 'A\\B' => 'lib/')));
  307. $b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/')));
  308. $this->repository->expects($this->once())
  309. ->method('getCanonicalPackages')
  310. ->will($this->returnValue($packages));
  311. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  312. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  313. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib');
  314. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  315. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_5');
  316. $this->assertAutoloadFiles('vendors', $this->vendorDir.'/composer');
  317. $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated, even if empty.");
  318. }
  319. public function testNonDevAutoloadExclusionWithRecursion()
  320. {
  321. $package = new Package('a', '1.0', '1.0');
  322. $package->setRequires(array(
  323. new Link('a', 'a/a'),
  324. ));
  325. $packages = array();
  326. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  327. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  328. $a->setAutoload(array('psr-0' => array('A' => 'src/', 'A\\B' => 'lib/')));
  329. $a->setRequires(array(
  330. new Link('a/a', 'b/b'),
  331. ));
  332. $b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/')));
  333. $b->setRequires(array(
  334. new Link('b/b', 'a/a'),
  335. ));
  336. $this->repository->expects($this->once())
  337. ->method('getCanonicalPackages')
  338. ->will($this->returnValue($packages));
  339. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  340. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  341. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib');
  342. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  343. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_5');
  344. $this->assertAutoloadFiles('vendors', $this->vendorDir.'/composer');
  345. $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated, even if empty.");
  346. }
  347. public function testNonDevAutoloadShouldIncludeReplacedPackages()
  348. {
  349. $package = new Package('a', '1.0', '1.0');
  350. $package->setRequires(array(new Link('a', 'a/a')));
  351. $packages = array();
  352. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  353. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  354. $a->setRequires(array(new Link('a/a', 'b/c')));
  355. $b->setAutoload(array('psr-4' => array('B\\' => 'src/')));
  356. $b->setReplaces(
  357. array(new Link('b/b', 'b/c', new Constraint('==', '1.0'), 'replaces'))
  358. );
  359. $this->repository->expects($this->once())
  360. ->method('getCanonicalPackages')
  361. ->will($this->returnValue($packages));
  362. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src/C');
  363. file_put_contents($this->vendorDir.'/b/b/src/C/C.php', '<?php namespace B\\C; class C {}');
  364. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_5');
  365. $this->assertEquals(
  366. array(
  367. 'B\\C\\C' => $this->vendorDir.'/b/b/src/C/C.php',
  368. ),
  369. include $this->vendorDir.'/composer/autoload_classmap.php'
  370. );
  371. }
  372. public function testNonDevAutoloadExclusionWithRecursionReplace()
  373. {
  374. $package = new Package('a', '1.0', '1.0');
  375. $package->setRequires(array(
  376. new Link('a', 'a/a'),
  377. ));
  378. $packages = array();
  379. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  380. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  381. $a->setAutoload(array('psr-0' => array('A' => 'src/', 'A\\B' => 'lib/')));
  382. $a->setRequires(array(
  383. new Link('a/a', 'c/c'),
  384. ));
  385. $b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/')));
  386. $b->setReplaces(array(
  387. new Link('b/b', 'c/c'),
  388. ));
  389. $this->repository->expects($this->once())
  390. ->method('getCanonicalPackages')
  391. ->will($this->returnValue($packages));
  392. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  393. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  394. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib');
  395. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  396. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_5');
  397. $this->assertAutoloadFiles('vendors', $this->vendorDir.'/composer');
  398. $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated, even if empty.");
  399. }
  400. public function testNonDevAutoloadReplacesNestedRequirements()
  401. {
  402. $package = new Package('a', '1.0', '1.0');
  403. $package->setRequires(array(
  404. new Link('a', 'a/a')
  405. ));
  406. $packages = array();
  407. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  408. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  409. $packages[] = $c = new Package('c/c', '1.0', '1.0');
  410. $packages[] = $d = new Package('d/d', '1.0', '1.0');
  411. $packages[] = $e = new Package('e/e', '1.0', '1.0');
  412. $a->setAutoload(array('classmap' => array('src/A.php')));
  413. $a->setRequires(array(
  414. new Link('a/a', 'b/b')
  415. ));
  416. $b->setAutoload(array('classmap' => array('src/B.php')));
  417. $b->setRequires(array(
  418. new Link('b/b', 'e/e')
  419. ));
  420. $c->setAutoload(array('classmap' => array('src/C.php')));
  421. $c->setReplaces(array(
  422. new Link('c/c', 'b/b')
  423. ));
  424. $c->setRequires(array(
  425. new Link('c/c', 'd/d')
  426. ));
  427. $d->setAutoload(array('classmap' => array('src/D.php')));
  428. $e->setAutoload(array('classmap' => array('src/E.php')));
  429. $this->repository->expects($this->once())
  430. ->method('getCanonicalPackages')
  431. ->will($this->returnValue($packages));
  432. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  433. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  434. $this->fs->ensureDirectoryExists($this->vendorDir.'/c/c/src');
  435. $this->fs->ensureDirectoryExists($this->vendorDir.'/d/d/src');
  436. $this->fs->ensureDirectoryExists($this->vendorDir.'/e/e/src');
  437. file_put_contents($this->vendorDir.'/a/a/src/A.php', '<?php class A {}');
  438. file_put_contents($this->vendorDir.'/b/b/src/B.php', '<?php class B {}');
  439. file_put_contents($this->vendorDir.'/c/c/src/C.php', '<?php class C {}');
  440. file_put_contents($this->vendorDir.'/d/d/src/D.php', '<?php class D {}');
  441. file_put_contents($this->vendorDir.'/e/e/src/E.php', '<?php class E {}');
  442. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_5');
  443. $this->assertAutoloadFiles('classmap9', $this->vendorDir.'/composer', 'classmap');
  444. }
  445. public function testPharAutoload()
  446. {
  447. $package = new Package('a', '1.0', '1.0');
  448. $package->setRequires(array(
  449. new Link('a', 'a/a'),
  450. ));
  451. $package->setAutoload(array(
  452. 'psr-0' => array(
  453. 'Foo' => 'foo.phar',
  454. 'Bar' => 'dir/bar.phar/src',
  455. ),
  456. 'psr-4' => array(
  457. 'Baz\\' => 'baz.phar',
  458. 'Qux\\' => 'dir/qux.phar/src',
  459. ),
  460. ));
  461. $vendorPackage = new Package('a/a', '1.0', '1.0');
  462. $vendorPackage->setAutoload(array(
  463. 'psr-0' => array(
  464. 'Lorem' => 'lorem.phar',
  465. 'Ipsum' => 'dir/ipsum.phar/src',
  466. ),
  467. 'psr-4' => array(
  468. 'Dolor\\' => 'dolor.phar',
  469. 'Sit\\' => 'dir/sit.phar/src',
  470. ),
  471. ));
  472. $this->repository->expects($this->once())
  473. ->method('getCanonicalPackages')
  474. ->will($this->returnValue(array($vendorPackage)));
  475. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, 'Phar');
  476. $this->assertAutoloadFiles('phar', $this->vendorDir . '/composer');
  477. $this->assertAutoloadFiles('phar_psr4', $this->vendorDir . '/composer', 'psr4');
  478. $this->assertAutoloadFiles('phar_static', $this->vendorDir . '/composer', 'static');
  479. }
  480. public function testPSRToClassMapIgnoresNonExistingDir()
  481. {
  482. $package = new Package('a', '1.0', '1.0');
  483. $package->setAutoload(array(
  484. 'psr-0' => array('Prefix' => 'foo/bar/non/existing/'),
  485. 'psr-4' => array('Prefix\\' => 'foo/bar/non/existing2/'),
  486. ));
  487. $this->repository->expects($this->once())
  488. ->method('getCanonicalPackages')
  489. ->will($this->returnValue(array()));
  490. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_8');
  491. $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated.");
  492. $this->assertEquals(
  493. array(),
  494. include $this->vendorDir.'/composer/autoload_classmap.php'
  495. );
  496. }
  497. public function testPSRToClassMapIgnoresNonPSRClasses()
  498. {
  499. $package = new Package('a', '1.0', '1.0');
  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. /**
  1305. * @dataProvider platformCheckProvider
  1306. */
  1307. public function testGeneratesPlatformCheck(array $requires, $expectedFixture)
  1308. {
  1309. $package = new Package('a', '1.0', '1.0');
  1310. $package->setRequires($requires);
  1311. $this->repository->expects($this->once())
  1312. ->method('getCanonicalPackages')
  1313. ->will($this->returnValue(array()));
  1314. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_1');
  1315. $this->assertFileContentEquals(__DIR__ . '/Fixtures/platform/' . $expectedFixture . '.php', $this->vendorDir . '/composer/platform_check.php');
  1316. }
  1317. public function platformCheckProvider()
  1318. {
  1319. $versionParser = new VersionParser();
  1320. return array(
  1321. 'Typical project requirements' => array(
  1322. array(
  1323. new Link('a', 'php', $versionParser->parseConstraints('^7.2')),
  1324. new Link('a', 'ext-xml', $versionParser->parseConstraints('*')),
  1325. new Link('a', 'ext-json', $versionParser->parseConstraints('*'))
  1326. ),
  1327. 'typical'
  1328. ),
  1329. 'No PHP lower bound' => array(
  1330. array(
  1331. new Link('a', 'php', $versionParser->parseConstraints('< 8')),
  1332. ),
  1333. 'no_php_lower_bound'
  1334. ),
  1335. 'No PHP upper bound' => array(
  1336. array(
  1337. new Link('a', 'php', $versionParser->parseConstraints('>= 7.2')),
  1338. ),
  1339. 'no_php_upper_bound'
  1340. ),
  1341. 'Specific PHP release version' => array(
  1342. array(
  1343. new Link('a', 'php', $versionParser->parseConstraints('^7.2.8')),
  1344. ),
  1345. 'specific_php_release'
  1346. ),
  1347. 'No PHP required' => array(
  1348. array(
  1349. new Link('a', 'ext-xml', $versionParser->parseConstraints('*')),
  1350. new Link('a', 'ext-json', $versionParser->parseConstraints('*'))
  1351. ),
  1352. 'no_php_required'
  1353. ),
  1354. 'No extensions required' => array(
  1355. array(
  1356. new Link('a', 'php', $versionParser->parseConstraints('^7.2')),
  1357. ),
  1358. 'no_extensions_required'
  1359. )
  1360. );
  1361. }
  1362. private function assertAutoloadFiles($name, $dir, $type = 'namespaces')
  1363. {
  1364. $a = __DIR__.'/Fixtures/autoload_'.$name.'.php';
  1365. $b = $dir.'/autoload_'.$type.'.php';
  1366. $this->assertFileContentEquals($a, $b);
  1367. }
  1368. public static function assertFileContentEquals($expected, $actual, $message = '', $canonicalize = false, $ignoreCase = false)
  1369. {
  1370. self::assertEqualsNormalized(
  1371. file_get_contents($expected),
  1372. file_get_contents($actual),
  1373. $message ?: $expected.' equals '.$actual,
  1374. 0,
  1375. 10,
  1376. $canonicalize,
  1377. $ignoreCase
  1378. );
  1379. }
  1380. public static function assertEqualsNormalized($expected, $actual, $message = '', $delta = 0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false)
  1381. {
  1382. parent::assertEquals(str_replace("\r", '', $expected), str_replace("\r", '', $actual), $message, $delta, $maxDepth, $canonicalize, $ignoreCase);
  1383. }
  1384. }