AutoloadGeneratorTest.php 60 KB

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