AutoloadGeneratorTest.php 71 KB

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