AutoloadGeneratorTest.php 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417
  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 testPSRToClassMapIgnoresNonExistingDir()
  318. {
  319. $package = new Package('a', '1.0', '1.0');
  320. $package->setAutoload(array(
  321. 'psr-0' => array('Prefix' => 'foo/bar/non/existing/'),
  322. 'psr-4' => array('Prefix\\' => 'foo/bar/non/existing2/'),
  323. ));
  324. $this->repository->expects($this->once())
  325. ->method('getCanonicalPackages')
  326. ->will($this->returnValue(array()));
  327. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_8');
  328. $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated.");
  329. $this->assertEquals(
  330. array(),
  331. include $this->vendorDir.'/composer/autoload_classmap.php'
  332. );
  333. }
  334. public function testVendorsClassMapAutoloading()
  335. {
  336. $package = new Package('a', '1.0', '1.0');
  337. $package->setRequires(array(
  338. new Link('a', 'a/a'),
  339. new Link('a', 'b/b')
  340. ));
  341. $packages = array();
  342. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  343. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  344. $a->setAutoload(array('classmap' => array('src/')));
  345. $b->setAutoload(array('classmap' => array('src/', 'lib/')));
  346. $this->repository->expects($this->once())
  347. ->method('getCanonicalPackages')
  348. ->will($this->returnValue($packages));
  349. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  350. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  351. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  352. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/lib');
  353. file_put_contents($this->vendorDir.'/a/a/src/a.php', '<?php class ClassMapFoo {}');
  354. file_put_contents($this->vendorDir.'/b/b/src/b.php', '<?php class ClassMapBar {}');
  355. file_put_contents($this->vendorDir.'/b/b/lib/c.php', '<?php class ClassMapBaz {}');
  356. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_6');
  357. $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated.");
  358. $this->assertEquals(
  359. array(
  360. 'ClassMapBar' => $this->vendorDir.'/b/b/src/b.php',
  361. 'ClassMapBaz' => $this->vendorDir.'/b/b/lib/c.php',
  362. 'ClassMapFoo' => $this->vendorDir.'/a/a/src/a.php',
  363. ),
  364. include $this->vendorDir.'/composer/autoload_classmap.php'
  365. );
  366. $this->assertAutoloadFiles('classmap4', $this->vendorDir.'/composer', 'classmap');
  367. }
  368. public function testVendorsClassMapAutoloadingWithTargetDir()
  369. {
  370. $package = new Package('a', '1.0', '1.0');
  371. $package->setRequires(array(
  372. new Link('a', 'a/a'),
  373. new Link('a', 'b/b')
  374. ));
  375. $packages = array();
  376. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  377. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  378. $a->setAutoload(array('classmap' => array('target/src/', 'lib/')));
  379. $a->setTargetDir('target');
  380. $b->setAutoload(array('classmap' => array('src/')));
  381. $this->repository->expects($this->once())
  382. ->method('getCanonicalPackages')
  383. ->will($this->returnValue($packages));
  384. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  385. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/target/src');
  386. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/target/lib');
  387. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  388. file_put_contents($this->vendorDir.'/a/a/target/src/a.php', '<?php class ClassMapFoo {}');
  389. file_put_contents($this->vendorDir.'/a/a/target/lib/b.php', '<?php class ClassMapBar {}');
  390. file_put_contents($this->vendorDir.'/b/b/src/c.php', '<?php class ClassMapBaz {}');
  391. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_6');
  392. $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated.");
  393. $this->assertEquals(
  394. array(
  395. 'ClassMapBar' => $this->vendorDir.'/a/a/target/lib/b.php',
  396. 'ClassMapBaz' => $this->vendorDir.'/b/b/src/c.php',
  397. 'ClassMapFoo' => $this->vendorDir.'/a/a/target/src/a.php',
  398. ),
  399. include $this->vendorDir.'/composer/autoload_classmap.php'
  400. );
  401. }
  402. public function testClassMapAutoloadingEmptyDirAndExactFile()
  403. {
  404. $package = new Package('a', '1.0', '1.0');
  405. $package->setRequires(array(
  406. new Link('a', 'a/a'),
  407. new Link('a', 'b/b'),
  408. new Link('a', 'c/c')
  409. ));
  410. $packages = array();
  411. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  412. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  413. $packages[] = $c = new Package('c/c', '1.0', '1.0');
  414. $a->setAutoload(array('classmap' => array('')));
  415. $b->setAutoload(array('classmap' => array('test.php')));
  416. $c->setAutoload(array('classmap' => array('./')));
  417. $this->repository->expects($this->once())
  418. ->method('getCanonicalPackages')
  419. ->will($this->returnValue($packages));
  420. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  421. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  422. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b');
  423. $this->fs->ensureDirectoryExists($this->vendorDir.'/c/c/foo');
  424. file_put_contents($this->vendorDir.'/a/a/src/a.php', '<?php class ClassMapFoo {}');
  425. file_put_contents($this->vendorDir.'/b/b/test.php', '<?php class ClassMapBar {}');
  426. file_put_contents($this->vendorDir.'/c/c/foo/test.php', '<?php class ClassMapBaz {}');
  427. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_7');
  428. $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated.");
  429. $this->assertEquals(
  430. array(
  431. 'ClassMapBar' => $this->vendorDir.'/b/b/test.php',
  432. 'ClassMapBaz' => $this->vendorDir.'/c/c/foo/test.php',
  433. 'ClassMapFoo' => $this->vendorDir.'/a/a/src/a.php',
  434. ),
  435. include $this->vendorDir.'/composer/autoload_classmap.php'
  436. );
  437. $this->assertAutoloadFiles('classmap5', $this->vendorDir.'/composer', 'classmap');
  438. $this->assertNotContains('$loader->setClassMapAuthoritative(true);', file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
  439. $this->assertNotContains('$loader->setApcuPrefix(', file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
  440. }
  441. public function testClassMapAutoloadingAuthoritativeAndApcu()
  442. {
  443. $package = new Package('a', '1.0', '1.0');
  444. $package->setRequires(array(
  445. new Link('a', 'a/a'),
  446. new Link('a', 'b/b'),
  447. new Link('a', 'c/c')
  448. ));
  449. $packages = array();
  450. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  451. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  452. $packages[] = $c = new Package('c/c', '1.0', '1.0');
  453. $a->setAutoload(array('psr-4' => array('' => 'src/')));
  454. $b->setAutoload(array('psr-4' => array('' => './')));
  455. $c->setAutoload(array('psr-4' => array('' => 'foo/')));
  456. $this->repository->expects($this->once())
  457. ->method('getCanonicalPackages')
  458. ->will($this->returnValue($packages));
  459. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  460. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  461. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b');
  462. $this->fs->ensureDirectoryExists($this->vendorDir.'/c/c/foo');
  463. file_put_contents($this->vendorDir.'/a/a/src/ClassMapFoo.php', '<?php class ClassMapFoo {}');
  464. file_put_contents($this->vendorDir.'/b/b/ClassMapBar.php', '<?php class ClassMapBar {}');
  465. file_put_contents($this->vendorDir.'/c/c/foo/ClassMapBaz.php', '<?php class ClassMapBaz {}');
  466. $this->generator->setClassMapAuthoritative(true);
  467. $this->generator->setApcu(true);
  468. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_7');
  469. $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated.");
  470. $this->assertEquals(
  471. array(
  472. 'ClassMapBar' => $this->vendorDir.'/b/b/ClassMapBar.php',
  473. 'ClassMapBaz' => $this->vendorDir.'/c/c/foo/ClassMapBaz.php',
  474. 'ClassMapFoo' => $this->vendorDir.'/a/a/src/ClassMapFoo.php',
  475. ),
  476. include $this->vendorDir.'/composer/autoload_classmap.php'
  477. );
  478. $this->assertAutoloadFiles('classmap8', $this->vendorDir.'/composer', 'classmap');
  479. $this->assertContains('$loader->setClassMapAuthoritative(true);', file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
  480. $this->assertContains('$loader->setApcuPrefix(', file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
  481. }
  482. public function testFilesAutoloadGeneration()
  483. {
  484. $package = new Package('a', '1.0', '1.0');
  485. $package->setAutoload(array('files' => array('root.php')));
  486. $package->setRequires(array(
  487. new Link('a', 'a/a'),
  488. new Link('a', 'b/b'),
  489. new Link('a', 'c/c')
  490. ));
  491. $packages = array();
  492. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  493. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  494. $packages[] = $c = new Package('c/c', '1.0', '1.0');
  495. $a->setAutoload(array('files' => array('test.php')));
  496. $b->setAutoload(array('files' => array('test2.php')));
  497. $c->setAutoload(array('files' => array('test3.php', 'foo/bar/test4.php')));
  498. $c->setTargetDir('foo/bar');
  499. $this->repository->expects($this->once())
  500. ->method('getCanonicalPackages')
  501. ->will($this->returnValue($packages));
  502. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a');
  503. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b');
  504. $this->fs->ensureDirectoryExists($this->vendorDir.'/c/c/foo/bar');
  505. file_put_contents($this->vendorDir.'/a/a/test.php', '<?php function testFilesAutoloadGeneration1() {}');
  506. file_put_contents($this->vendorDir.'/b/b/test2.php', '<?php function testFilesAutoloadGeneration2() {}');
  507. file_put_contents($this->vendorDir.'/c/c/foo/bar/test3.php', '<?php function testFilesAutoloadGeneration3() {}');
  508. file_put_contents($this->vendorDir.'/c/c/foo/bar/test4.php', '<?php function testFilesAutoloadGeneration4() {}');
  509. file_put_contents($this->workingDir.'/root.php', '<?php function testFilesAutoloadGenerationRoot() {}');
  510. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'FilesAutoload');
  511. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php');
  512. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_functions.php', $this->vendorDir.'/composer/autoload_real.php');
  513. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_static_functions.php', $this->vendorDir.'/composer/autoload_static.php');
  514. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_files_functions.php', $this->vendorDir.'/composer/autoload_files.php');
  515. include $this->vendorDir . '/autoload.php';
  516. $this->assertTrue(function_exists('testFilesAutoloadGeneration1'));
  517. $this->assertTrue(function_exists('testFilesAutoloadGeneration2'));
  518. $this->assertTrue(function_exists('testFilesAutoloadGeneration3'));
  519. $this->assertTrue(function_exists('testFilesAutoloadGeneration4'));
  520. $this->assertTrue(function_exists('testFilesAutoloadGenerationRoot'));
  521. }
  522. public function testFilesAutoloadGenerationRemoveExtraEntitiesFromAutoloadFiles()
  523. {
  524. $autoloadPackage = new Package('a', '1.0', '1.0');
  525. $autoloadPackage->setAutoload(array('files' => array('root.php')));
  526. $autoloadPackage->setIncludePaths(array('/lib', '/src'));
  527. $notAutoloadPackage = new Package('a', '1.0', '1.0');
  528. $requires = array(
  529. new Link('a', 'a/a'),
  530. new Link('a', 'b/b'),
  531. new Link('a', 'c/c')
  532. );
  533. $autoloadPackage->setRequires($requires);
  534. $notAutoloadPackage->setRequires($requires);
  535. $autoloadPackages = array();
  536. $autoloadPackages[] = $a = new Package('a/a', '1.0', '1.0');
  537. $autoloadPackages[] = $b = new Package('b/b', '1.0', '1.0');
  538. $autoloadPackages[] = $c = new Package('c/c', '1.0', '1.0');
  539. $a->setAutoload(array('files' => array('test.php')));
  540. $a->setIncludePaths(array('lib1', 'src1'));
  541. $b->setAutoload(array('files' => array('test2.php')));
  542. $b->setIncludePaths(array('lib2'));
  543. $c->setAutoload(array('files' => array('test3.php', 'foo/bar/test4.php')));
  544. $c->setIncludePaths(array('lib3'));
  545. $c->setTargetDir('foo/bar');
  546. $notAutoloadPackages = array();
  547. $notAutoloadPackages[] = $a = new Package('a/a', '1.0', '1.0');
  548. $notAutoloadPackages[] = $b = new Package('b/b', '1.0', '1.0');
  549. $notAutoloadPackages[] = $c = new Package('c/c', '1.0', '1.0');
  550. $this->repository->expects($this->at(0))
  551. ->method('getCanonicalPackages')
  552. ->will($this->returnValue($autoloadPackages));
  553. $this->repository->expects($this->at(1))
  554. ->method('getCanonicalPackages')
  555. ->will($this->returnValue($notAutoloadPackages));
  556. $this->repository->expects($this->at(2))
  557. ->method('getCanonicalPackages')
  558. ->will($this->returnValue($notAutoloadPackages));
  559. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a');
  560. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b');
  561. $this->fs->ensureDirectoryExists($this->vendorDir.'/c/c/foo/bar');
  562. file_put_contents($this->vendorDir.'/a/a/test.php', '<?php function testFilesAutoloadGeneration1() {}');
  563. file_put_contents($this->vendorDir.'/b/b/test2.php', '<?php function testFilesAutoloadGeneration2() {}');
  564. file_put_contents($this->vendorDir.'/c/c/foo/bar/test3.php', '<?php function testFilesAutoloadGeneration3() {}');
  565. file_put_contents($this->vendorDir.'/c/c/foo/bar/test4.php', '<?php function testFilesAutoloadGeneration4() {}');
  566. file_put_contents($this->workingDir.'/root.php', '<?php function testFilesAutoloadGenerationRoot() {}');
  567. $this->generator->dump($this->config, $this->repository, $autoloadPackage, $this->im, 'composer', false, 'FilesAutoload');
  568. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php');
  569. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_functions_with_include_paths.php', $this->vendorDir.'/composer/autoload_real.php');
  570. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_static_functions_with_include_paths.php', $this->vendorDir.'/composer/autoload_static.php');
  571. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_files_functions.php', $this->vendorDir.'/composer/autoload_files.php');
  572. $this->assertFileEquals(__DIR__.'/Fixtures/include_paths_functions.php', $this->vendorDir.'/composer/include_paths.php');
  573. $this->generator->dump($this->config, $this->repository, $autoloadPackage, $this->im, 'composer', false, 'FilesAutoload');
  574. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php');
  575. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_functions_with_include_paths.php', $this->vendorDir.'/composer/autoload_real.php');
  576. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_files_functions_with_removed_extra.php', $this->vendorDir.'/composer/autoload_files.php');
  577. $this->assertFileEquals(__DIR__.'/Fixtures/include_paths_functions_with_removed_extra.php', $this->vendorDir.'/composer/include_paths.php');
  578. $this->generator->dump($this->config, $this->repository, $notAutoloadPackage, $this->im, 'composer', false, 'FilesAutoload');
  579. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php');
  580. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_functions_with_removed_include_paths_and_autolad_files.php', $this->vendorDir.'/composer/autoload_real.php');
  581. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_static_functions_with_removed_include_paths_and_autolad_files.php', $this->vendorDir.'/composer/autoload_static.php');
  582. $this->assertFileNotExists($this->vendorDir.'/composer/autoload_files.php');
  583. $this->assertFileNotExists($this->vendorDir.'/composer/include_paths.php');
  584. }
  585. public function testFilesAutoloadOrderByDependencies()
  586. {
  587. $package = new Package('a', '1.0', '1.0');
  588. $package->setAutoload(array('files' => array('root2.php')));
  589. $package->setRequires(array(
  590. new Link('a', 'z/foo'),
  591. new Link('a', 'b/bar'),
  592. new Link('a', 'd/d'),
  593. new Link('a', 'e/e')
  594. ));
  595. $packages = array();
  596. $packages[] = $z = new Package('z/foo', '1.0', '1.0');
  597. $packages[] = $b = new Package('b/bar', '1.0', '1.0');
  598. $packages[] = $d = new Package('d/d', '1.0', '1.0');
  599. $packages[] = $c = new Package('c/lorem', '1.0', '1.0');
  600. $packages[] = $e = new Package('e/e', '1.0', '1.0');
  601. $z->setAutoload(array('files' => array('testA.php')));
  602. $z->setRequires(array(new Link('z/foo', 'c/lorem')));
  603. $b->setAutoload(array('files' => array('testB.php')));
  604. $b->setRequires(array(new Link('b/bar', 'c/lorem'), new Link('b/bar', 'd/d')));
  605. $c->setAutoload(array('files' => array('testC.php')));
  606. $d->setAutoload(array('files' => array('testD.php')));
  607. $d->setRequires(array(new Link('d/d', 'c/lorem')));
  608. $e->setAutoload(array('files' => array('testE.php')));
  609. $e->setRequires(array(new Link('e/e', 'c/lorem')));
  610. $this->repository->expects($this->once())
  611. ->method('getCanonicalPackages')
  612. ->will($this->returnValue($packages));
  613. $this->fs->ensureDirectoryExists($this->vendorDir . '/z/foo');
  614. $this->fs->ensureDirectoryExists($this->vendorDir . '/b/bar');
  615. $this->fs->ensureDirectoryExists($this->vendorDir . '/c/lorem');
  616. $this->fs->ensureDirectoryExists($this->vendorDir . '/d/d');
  617. $this->fs->ensureDirectoryExists($this->vendorDir . '/e/e');
  618. file_put_contents($this->vendorDir . '/z/foo/testA.php', '<?php function testFilesAutoloadOrderByDependency1() {}');
  619. file_put_contents($this->vendorDir . '/b/bar/testB.php', '<?php function testFilesAutoloadOrderByDependency2() {}');
  620. file_put_contents($this->vendorDir . '/c/lorem/testC.php', '<?php function testFilesAutoloadOrderByDependency3() {}');
  621. file_put_contents($this->vendorDir . '/d/d/testD.php', '<?php function testFilesAutoloadOrderByDependency4() {}');
  622. file_put_contents($this->vendorDir . '/e/e/testE.php', '<?php function testFilesAutoloadOrderByDependency5() {}');
  623. file_put_contents($this->workingDir . '/root2.php', '<?php function testFilesAutoloadOrderByDependencyRoot() {}');
  624. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'FilesAutoloadOrder');
  625. $this->assertFileEquals(__DIR__ . '/Fixtures/autoload_functions_by_dependency.php', $this->vendorDir . '/autoload.php');
  626. $this->assertFileEquals(__DIR__ . '/Fixtures/autoload_real_files_by_dependency.php', $this->vendorDir . '/composer/autoload_real.php');
  627. $this->assertFileEquals(__DIR__ . '/Fixtures/autoload_static_files_by_dependency.php', $this->vendorDir . '/composer/autoload_static.php');
  628. require $this->vendorDir . '/autoload.php';
  629. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency1'));
  630. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency2'));
  631. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency3'));
  632. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency4'));
  633. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency5'));
  634. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependencyRoot'));
  635. }
  636. /**
  637. * Test that PSR-0 and PSR-4 mappings are processed in the correct order for
  638. * autoloading and for classmap generation:
  639. * - The main package has priority over other packages.
  640. * - Longer namespaces have priority over shorter namespaces.
  641. */
  642. public function testOverrideVendorsAutoloading()
  643. {
  644. $mainPackage = new Package('z', '1.0', '1.0');
  645. $mainPackage->setAutoload(array(
  646. 'psr-0' => array('A\\B' => $this->workingDir.'/lib'),
  647. 'classmap' => array($this->workingDir.'/src'),
  648. ));
  649. $mainPackage->setRequires(array(
  650. new Link('z', 'a/a'),
  651. new Link('z', 'b/b')
  652. ));
  653. $packages = array();
  654. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  655. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  656. $a->setAutoload(array(
  657. 'psr-0' => array('A' => 'src/', 'A\\B' => 'lib/'),
  658. 'classmap' => array('classmap'),
  659. ));
  660. $b->setAutoload(array(
  661. 'psr-0' => array('B\\Sub\\Name' => 'src/'),
  662. ));
  663. $this->repository->expects($this->once())
  664. ->method('getCanonicalPackages')
  665. ->will($this->returnValue($packages));
  666. $this->fs->ensureDirectoryExists($this->workingDir.'/lib/A/B');
  667. $this->fs->ensureDirectoryExists($this->workingDir.'/src/');
  668. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  669. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/classmap');
  670. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  671. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib/A/B');
  672. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  673. // Define the classes A\B\C and Foo\Bar in the main package.
  674. file_put_contents($this->workingDir.'/lib/A/B/C.php', '<?php namespace A\\B; class C {}');
  675. file_put_contents($this->workingDir.'/src/classes.php', '<?php namespace Foo; class Bar {}');
  676. // Define the same two classes in the package a/a.
  677. file_put_contents($this->vendorDir.'/a/a/lib/A/B/C.php', '<?php namespace A\\B; class C {}');
  678. file_put_contents($this->vendorDir.'/a/a/classmap/classes.php', '<?php namespace Foo; class Bar {}');
  679. $expectedNamespace = <<<EOF
  680. <?php
  681. // autoload_namespaces.php @generated by Composer
  682. \$vendorDir = dirname(dirname(__FILE__));
  683. \$baseDir = dirname(\$vendorDir);
  684. return array(
  685. 'B\\\\Sub\\\\Name' => array(\$vendorDir . '/b/b/src'),
  686. 'A\\\\B' => array(\$baseDir . '/lib', \$vendorDir . '/a/a/lib'),
  687. 'A' => array(\$vendorDir . '/a/a/src'),
  688. );
  689. EOF;
  690. // autoload_psr4.php is expected to be empty in this example.
  691. $expectedPsr4 = <<<EOF
  692. <?php
  693. // autoload_psr4.php @generated by Composer
  694. \$vendorDir = dirname(dirname(__FILE__));
  695. \$baseDir = dirname(\$vendorDir);
  696. return array(
  697. );
  698. EOF;
  699. $expectedClassmap = <<<EOF
  700. <?php
  701. // autoload_classmap.php @generated by Composer
  702. \$vendorDir = dirname(dirname(__FILE__));
  703. \$baseDir = dirname(\$vendorDir);
  704. return array(
  705. 'A\\\\B\\\\C' => \$baseDir . '/lib/A/B/C.php',
  706. 'Foo\\\\Bar' => \$baseDir . '/src/classes.php',
  707. );
  708. EOF;
  709. $this->generator->dump($this->config, $this->repository, $mainPackage, $this->im, 'composer', true, '_9');
  710. $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_namespaces.php', $expectedNamespace);
  711. $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_psr4.php', $expectedPsr4);
  712. $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_classmap.php', $expectedClassmap);
  713. }
  714. public function testIncludePathFileGeneration()
  715. {
  716. $package = new Package('a', '1.0', '1.0');
  717. $packages = array();
  718. $a = new Package("a/a", "1.0", "1.0");
  719. $a->setIncludePaths(array("lib/"));
  720. $b = new Package("b/b", "1.0", "1.0");
  721. $b->setIncludePaths(array("library"));
  722. $c = new Package("c", "1.0", "1.0");
  723. $c->setIncludePaths(array("library"));
  724. $packages[] = $a;
  725. $packages[] = $b;
  726. $packages[] = $c;
  727. $this->repository->expects($this->once())
  728. ->method("getCanonicalPackages")
  729. ->will($this->returnValue($packages));
  730. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  731. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_10');
  732. $this->assertFileEquals(__DIR__.'/Fixtures/include_paths.php', $this->vendorDir.'/composer/include_paths.php');
  733. $this->assertEquals(
  734. array(
  735. $this->vendorDir."/a/a/lib",
  736. $this->vendorDir."/b/b/library",
  737. $this->vendorDir."/c/library",
  738. ),
  739. require $this->vendorDir."/composer/include_paths.php"
  740. );
  741. }
  742. public function testIncludePathsArePrependedInAutoloadFile()
  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. $packages[] = $a;
  749. $this->repository->expects($this->once())
  750. ->method("getCanonicalPackages")
  751. ->will($this->returnValue($packages));
  752. mkdir($this->vendorDir."/composer", 0777, true);
  753. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_11');
  754. $oldIncludePath = get_include_path();
  755. require $this->vendorDir."/autoload.php";
  756. $this->assertEquals(
  757. $this->vendorDir."/a/a/lib".PATH_SEPARATOR.$oldIncludePath,
  758. get_include_path()
  759. );
  760. set_include_path($oldIncludePath);
  761. }
  762. public function testIncludePathsInMainPackage()
  763. {
  764. $package = new Package('a', '1.0', '1.0');
  765. $package->setIncludePaths(array('/lib', '/src'));
  766. $packages = array($a = new Package("a/a", "1.0", "1.0"));
  767. $a->setIncludePaths(array("lib/"));
  768. $this->repository->expects($this->once())
  769. ->method("getCanonicalPackages")
  770. ->will($this->returnValue($packages));
  771. mkdir($this->vendorDir."/composer", 0777, true);
  772. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_12');
  773. $oldIncludePath = get_include_path();
  774. require $this->vendorDir."/autoload.php";
  775. $this->assertEquals(
  776. $this->workingDir."/lib".PATH_SEPARATOR.$this->workingDir."/src".PATH_SEPARATOR.$this->vendorDir."/a/a/lib".PATH_SEPARATOR.$oldIncludePath,
  777. get_include_path()
  778. );
  779. set_include_path($oldIncludePath);
  780. }
  781. public function testIncludePathFileWithoutPathsIsSkipped()
  782. {
  783. $package = new Package('a', '1.0', '1.0');
  784. $packages = array();
  785. $a = new Package("a/a", "1.0", "1.0");
  786. $packages[] = $a;
  787. $this->repository->expects($this->once())
  788. ->method("getCanonicalPackages")
  789. ->will($this->returnValue($packages));
  790. mkdir($this->vendorDir."/composer", 0777, true);
  791. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_12');
  792. $this->assertFileNotExists($this->vendorDir."/composer/include_paths.php");
  793. }
  794. public function testPreAndPostEventsAreDispatchedDuringAutoloadDump()
  795. {
  796. $this->eventDispatcher
  797. ->expects($this->at(0))
  798. ->method('dispatchScript')
  799. ->with(ScriptEvents::PRE_AUTOLOAD_DUMP, false);
  800. $this->eventDispatcher
  801. ->expects($this->at(1))
  802. ->method('dispatchScript')
  803. ->with(ScriptEvents::POST_AUTOLOAD_DUMP, false);
  804. $package = new Package('a', '1.0', '1.0');
  805. $package->setAutoload(array('psr-0' => array('foo/bar/non/existing/')));
  806. $this->repository->expects($this->once())
  807. ->method('getCanonicalPackages')
  808. ->will($this->returnValue(array()));
  809. $this->generator->setRunScripts(true);
  810. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_8');
  811. }
  812. public function testUseGlobalIncludePath()
  813. {
  814. $package = new Package('a', '1.0', '1.0');
  815. $package->setAutoload(array(
  816. 'psr-0' => array('Main\\Foo' => '', 'Main\\Bar' => ''),
  817. ));
  818. $package->setTargetDir('Main/Foo/');
  819. $this->repository->expects($this->once())
  820. ->method('getCanonicalPackages')
  821. ->will($this->returnValue(array()));
  822. $this->configValueMap['use-include-path'] = true;
  823. $this->fs->ensureDirectoryExists($this->vendorDir.'/a');
  824. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'IncludePath');
  825. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_include_path.php', $this->vendorDir.'/composer/autoload_real.php');
  826. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_static_include_path.php', $this->vendorDir.'/composer/autoload_static.php');
  827. }
  828. public function testVendorDirExcludedFromWorkingDir()
  829. {
  830. $workingDir = $this->vendorDir.'/working-dir';
  831. $vendorDir = $workingDir.'/../vendor';
  832. $this->fs->ensureDirectoryExists($workingDir);
  833. chdir($workingDir);
  834. $package = new Package('a', '1.0', '1.0');
  835. $package->setAutoload(array(
  836. 'psr-0' => array('Foo' => 'src'),
  837. 'psr-4' => array('Acme\Foo\\' => 'src-psr4'),
  838. 'classmap' => array('classmap'),
  839. 'files' => array('test.php'),
  840. ));
  841. $package->setRequires(array(
  842. new Link('a', 'b/b')
  843. ));
  844. $vendorPackage = new Package('b/b', '1.0', '1.0');
  845. $vendorPackage->setAutoload(array(
  846. 'psr-0' => array('Bar' => 'lib'),
  847. 'psr-4' => array('Acme\Bar\\' => 'lib-psr4'),
  848. 'classmap' => array('classmaps'),
  849. 'files' => array('bootstrap.php'),
  850. ));
  851. $this->repository->expects($this->once())
  852. ->method('getCanonicalPackages')
  853. ->will($this->returnValue(array($vendorPackage)));
  854. $im = $this->getMockBuilder('Composer\Installer\InstallationManager')
  855. ->disableOriginalConstructor()
  856. ->getMock();
  857. $im->expects($this->any())
  858. ->method('getInstallPath')
  859. ->will($this->returnCallback(function ($package) use ($vendorDir) {
  860. $targetDir = $package->getTargetDir();
  861. return $vendorDir.'/'.$package->getName() . ($targetDir ? '/'.$targetDir : '');
  862. }));
  863. $this->fs->ensureDirectoryExists($workingDir.'/src/Foo');
  864. $this->fs->ensureDirectoryExists($workingDir.'/classmap');
  865. $this->fs->ensureDirectoryExists($vendorDir.'/composer');
  866. $this->fs->ensureDirectoryExists($vendorDir.'/b/b/lib/Bar');
  867. $this->fs->ensureDirectoryExists($vendorDir.'/b/b/classmaps');
  868. file_put_contents($workingDir.'/src/Foo/Bar.php', '<?php namespace Foo; class Bar {}');
  869. file_put_contents($workingDir.'/classmap/classes.php', '<?php namespace Foo; class Foo {}');
  870. file_put_contents($workingDir.'/test.php', '<?php class Foo {}');
  871. file_put_contents($vendorDir.'/b/b/lib/Bar/Foo.php', '<?php namespace Bar; class Foo {}');
  872. file_put_contents($vendorDir.'/b/b/classmaps/classes.php', '<?php namespace Bar; class Bar {}');
  873. file_put_contents($vendorDir.'/b/b/bootstrap.php', '<?php class Bar {}');
  874. $oldVendorDir = $this->vendorDir;
  875. $this->vendorDir = $vendorDir;
  876. $this->generator->dump($this->config, $this->repository, $package, $im, 'composer', true, '_13');
  877. $this->vendorDir = $oldVendorDir;
  878. $expectedNamespace = <<<'EOF'
  879. <?php
  880. // autoload_namespaces.php @generated by Composer
  881. $vendorDir = dirname(dirname(__FILE__));
  882. $baseDir = dirname($vendorDir).'/working-dir';
  883. return array(
  884. 'Foo' => array($baseDir . '/src'),
  885. 'Bar' => array($vendorDir . '/b/b/lib'),
  886. );
  887. EOF;
  888. $expectedPsr4 = <<<'EOF'
  889. <?php
  890. // autoload_psr4.php @generated by Composer
  891. $vendorDir = dirname(dirname(__FILE__));
  892. $baseDir = dirname($vendorDir).'/working-dir';
  893. return array(
  894. 'Acme\\Foo\\' => array($baseDir . '/src-psr4'),
  895. 'Acme\\Bar\\' => array($vendorDir . '/b/b/lib-psr4'),
  896. );
  897. EOF;
  898. $expectedClassmap = <<<'EOF'
  899. <?php
  900. // autoload_classmap.php @generated by Composer
  901. $vendorDir = dirname(dirname(__FILE__));
  902. $baseDir = dirname($vendorDir).'/working-dir';
  903. return array(
  904. 'Bar\\Bar' => $vendorDir . '/b/b/classmaps/classes.php',
  905. 'Bar\\Foo' => $vendorDir . '/b/b/lib/Bar/Foo.php',
  906. 'Foo\\Bar' => $baseDir . '/src/Foo/Bar.php',
  907. 'Foo\\Foo' => $baseDir . '/classmap/classes.php',
  908. );
  909. EOF;
  910. $this->assertStringEqualsFile($vendorDir.'/composer/autoload_namespaces.php', $expectedNamespace);
  911. $this->assertStringEqualsFile($vendorDir.'/composer/autoload_psr4.php', $expectedPsr4);
  912. $this->assertStringEqualsFile($vendorDir.'/composer/autoload_classmap.php', $expectedClassmap);
  913. $this->assertContains("\$vendorDir . '/b/b/bootstrap.php',\n", file_get_contents($vendorDir.'/composer/autoload_files.php'));
  914. $this->assertContains("\$baseDir . '/test.php',\n", file_get_contents($vendorDir.'/composer/autoload_files.php'));
  915. }
  916. public function testUpLevelRelativePaths()
  917. {
  918. $workingDir = $this->workingDir.'/working-dir';
  919. mkdir($workingDir, 0777, true);
  920. chdir($workingDir);
  921. $package = new Package('a', '1.0', '1.0');
  922. $package->setAutoload(array(
  923. 'psr-0' => array('Foo' => '../path/../src'),
  924. 'psr-4' => array('Acme\Foo\\' => '../path/../src-psr4'),
  925. 'classmap' => array('../classmap'),
  926. 'files' => array('../test.php'),
  927. 'exclude-from-classmap' => array('./../classmap/excluded'),
  928. ));
  929. $this->repository->expects($this->once())
  930. ->method('getCanonicalPackages')
  931. ->will($this->returnValue(array()));
  932. $this->fs->ensureDirectoryExists($this->workingDir.'/src/Foo');
  933. $this->fs->ensureDirectoryExists($this->workingDir.'/classmap/excluded');
  934. file_put_contents($this->workingDir.'/src/Foo/Bar.php', '<?php namespace Foo; class Bar {}');
  935. file_put_contents($this->workingDir.'/classmap/classes.php', '<?php namespace Foo; class Foo {}');
  936. file_put_contents($this->workingDir.'/classmap/excluded/classes.php', '<?php namespace Foo; class Boo {}');
  937. file_put_contents($this->workingDir.'/test.php', '<?php class Foo {}');
  938. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_14');
  939. $expectedNamespace = <<<'EOF'
  940. <?php
  941. // autoload_namespaces.php @generated by Composer
  942. $vendorDir = dirname(dirname(__FILE__));
  943. $baseDir = dirname($vendorDir).'/working-dir';
  944. return array(
  945. 'Foo' => array($baseDir . '/../src'),
  946. );
  947. EOF;
  948. $expectedPsr4 = <<<'EOF'
  949. <?php
  950. // autoload_psr4.php @generated by Composer
  951. $vendorDir = dirname(dirname(__FILE__));
  952. $baseDir = dirname($vendorDir).'/working-dir';
  953. return array(
  954. 'Acme\\Foo\\' => array($baseDir . '/../src-psr4'),
  955. );
  956. EOF;
  957. $expectedClassmap = <<<'EOF'
  958. <?php
  959. // autoload_classmap.php @generated by Composer
  960. $vendorDir = dirname(dirname(__FILE__));
  961. $baseDir = dirname($vendorDir).'/working-dir';
  962. return array(
  963. 'Foo\\Bar' => $baseDir . '/../src/Foo/Bar.php',
  964. 'Foo\\Foo' => $baseDir . '/../classmap/classes.php',
  965. );
  966. EOF;
  967. $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_namespaces.php', $expectedNamespace);
  968. $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_psr4.php', $expectedPsr4);
  969. $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_classmap.php', $expectedClassmap);
  970. $this->assertContains("\$baseDir . '/../test.php',\n", file_get_contents($this->vendorDir.'/composer/autoload_files.php'));
  971. }
  972. public function testEmptyPaths()
  973. {
  974. $package = new Package('a', '1.0', '1.0');
  975. $package->setAutoload(array(
  976. 'psr-0' => array('Foo' => ''),
  977. 'psr-4' => array('Acme\Foo\\' => ''),
  978. 'classmap' => array(''),
  979. ));
  980. $this->repository->expects($this->once())
  981. ->method('getCanonicalPackages')
  982. ->will($this->returnValue(array()));
  983. $this->fs->ensureDirectoryExists($this->workingDir.'/Foo');
  984. file_put_contents($this->workingDir.'/Foo/Bar.php', '<?php namespace Foo; class Bar {}');
  985. file_put_contents($this->workingDir.'/class.php', '<?php namespace Classmap; class Foo {}');
  986. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_15');
  987. $expectedNamespace = <<<'EOF'
  988. <?php
  989. // autoload_namespaces.php @generated by Composer
  990. $vendorDir = dirname(dirname(__FILE__));
  991. $baseDir = dirname($vendorDir);
  992. return array(
  993. 'Foo' => array($baseDir . '/'),
  994. );
  995. EOF;
  996. $expectedPsr4 = <<<'EOF'
  997. <?php
  998. // autoload_psr4.php @generated by Composer
  999. $vendorDir = dirname(dirname(__FILE__));
  1000. $baseDir = dirname($vendorDir);
  1001. return array(
  1002. 'Acme\\Foo\\' => array($baseDir . '/'),
  1003. );
  1004. EOF;
  1005. $expectedClassmap = <<<'EOF'
  1006. <?php
  1007. // autoload_classmap.php @generated by Composer
  1008. $vendorDir = dirname(dirname(__FILE__));
  1009. $baseDir = dirname($vendorDir);
  1010. return array(
  1011. 'Classmap\\Foo' => $baseDir . '/class.php',
  1012. 'Foo\\Bar' => $baseDir . '/Foo/Bar.php',
  1013. );
  1014. EOF;
  1015. $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_namespaces.php', $expectedNamespace);
  1016. $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_psr4.php', $expectedPsr4);
  1017. $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_classmap.php', $expectedClassmap);
  1018. }
  1019. public function testVendorSubstringPath()
  1020. {
  1021. $package = new Package('a', '1.0', '1.0');
  1022. $package->setAutoload(array(
  1023. 'psr-0' => array('Foo' => 'composer-test-autoload-src/src'),
  1024. 'psr-4' => array('Acme\Foo\\' => 'composer-test-autoload-src/src-psr4'),
  1025. ));
  1026. $this->repository->expects($this->once())
  1027. ->method('getCanonicalPackages')
  1028. ->will($this->returnValue(array()));
  1029. $this->fs->ensureDirectoryExists($this->vendorDir.'/a');
  1030. $expectedNamespace = <<<'EOF'
  1031. <?php
  1032. // autoload_namespaces.php @generated by Composer
  1033. $vendorDir = dirname(dirname(__FILE__));
  1034. $baseDir = dirname($vendorDir);
  1035. return array(
  1036. 'Foo' => array($baseDir . '/composer-test-autoload-src/src'),
  1037. );
  1038. EOF;
  1039. $expectedPsr4 = <<<'EOF'
  1040. <?php
  1041. // autoload_psr4.php @generated by Composer
  1042. $vendorDir = dirname(dirname(__FILE__));
  1043. $baseDir = dirname($vendorDir);
  1044. return array(
  1045. 'Acme\\Foo\\' => array($baseDir . '/composer-test-autoload-src/src-psr4'),
  1046. );
  1047. EOF;
  1048. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'VendorSubstring');
  1049. $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_namespaces.php', $expectedNamespace);
  1050. $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_psr4.php', $expectedPsr4);
  1051. }
  1052. public function testExcludeFromClassmap()
  1053. {
  1054. $package = new Package('a', '1.0', '1.0');
  1055. $package->setAutoload(array(
  1056. 'psr-0' => array(
  1057. 'Main' => 'src/',
  1058. 'Lala' => array('src/', 'lib/'),
  1059. ),
  1060. 'psr-4' => array(
  1061. 'Acme\Fruit\\' => 'src-fruit/',
  1062. 'Acme\Cake\\' => array('src-cake/', 'lib-cake/'),
  1063. ),
  1064. 'classmap' => array('composersrc/'),
  1065. 'exclude-from-classmap' => array(
  1066. '/composersrc/foo/bar/',
  1067. '/composersrc/excludedTests/',
  1068. '/composersrc/ClassToExclude.php',
  1069. '/composersrc/*/excluded/excsubpath',
  1070. '**/excsubpath',
  1071. ),
  1072. ));
  1073. $this->repository->expects($this->once())
  1074. ->method('getCanonicalPackages')
  1075. ->will($this->returnValue(array()));
  1076. $this->fs->ensureDirectoryExists($this->workingDir.'/composer');
  1077. $this->fs->ensureDirectoryExists($this->workingDir.'/src/Lala/Test');
  1078. $this->fs->ensureDirectoryExists($this->workingDir.'/lib');
  1079. file_put_contents($this->workingDir.'/src/Lala/ClassMapMain.php', '<?php namespace Lala; class ClassMapMain {}');
  1080. file_put_contents($this->workingDir.'/src/Lala/Test/ClassMapMainTest.php', '<?php namespace Lala\Test; class ClassMapMainTest {}');
  1081. $this->fs->ensureDirectoryExists($this->workingDir.'/src-fruit');
  1082. $this->fs->ensureDirectoryExists($this->workingDir.'/src-cake');
  1083. $this->fs->ensureDirectoryExists($this->workingDir.'/lib-cake');
  1084. file_put_contents($this->workingDir.'/src-cake/ClassMapBar.php', '<?php namespace Acme\Cake; class ClassMapBar {}');
  1085. $this->fs->ensureDirectoryExists($this->workingDir.'/composersrc');
  1086. $this->fs->ensureDirectoryExists($this->workingDir.'/composersrc/tests');
  1087. file_put_contents($this->workingDir.'/composersrc/foo.php', '<?php class ClassMapFoo {}');
  1088. // this classes should not be found in the classmap
  1089. $this->fs->ensureDirectoryExists($this->workingDir.'/composersrc/excludedTests');
  1090. file_put_contents($this->workingDir.'/composersrc/excludedTests/bar.php', '<?php class ClassExcludeMapFoo {}');
  1091. file_put_contents($this->workingDir.'/composersrc/ClassToExclude.php', '<?php class ClassClassToExclude {}');
  1092. $this->fs->ensureDirectoryExists($this->workingDir.'/composersrc/long/excluded/excsubpath');
  1093. file_put_contents($this->workingDir.'/composersrc/long/excluded/excsubpath/foo.php', '<?php class ClassExcludeMapFoo2 {}');
  1094. file_put_contents($this->workingDir.'/composersrc/long/excluded/excsubpath/bar.php', '<?php class ClassExcludeMapBar {}');
  1095. // symlink directory in project directory in classmap
  1096. $this->fs->ensureDirectoryExists($this->workingDir.'/forks/bar/src/exclude');
  1097. $this->fs->ensureDirectoryExists($this->workingDir.'/composersrc/foo');
  1098. file_put_contents($this->workingDir.'/forks/bar/src/exclude/FooExclClass.php', '<?php class FooExclClass {};');
  1099. $target = $this->workingDir.'/forks/bar/';
  1100. $link = $this->workingDir.'/composersrc/foo/bar';
  1101. $command = Platform::isWindows()
  1102. ? 'mklink /j "' . str_replace('/', '\\', $link) . '" "' . str_replace('/', '\\', $target) . '"'
  1103. : 'ln -s "' . $target . '" "' . $link . '"';
  1104. exec($command);
  1105. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_1');
  1106. // Assert that autoload_classmap.php was correctly generated.
  1107. $this->assertAutoloadFiles('classmap', $this->vendorDir.'/composer', 'classmap');
  1108. }
  1109. private function assertAutoloadFiles($name, $dir, $type = 'namespaces')
  1110. {
  1111. $a = __DIR__.'/Fixtures/autoload_'.$name.'.php';
  1112. $b = $dir.'/autoload_'.$type.'.php';
  1113. $this->assertFileEquals($a, $b);
  1114. }
  1115. public static function assertFileEquals($expected, $actual, $message = '', $canonicalize = false, $ignoreCase = false)
  1116. {
  1117. return self::assertEquals(
  1118. file_get_contents($expected),
  1119. file_get_contents($actual),
  1120. $message ?: $expected.' equals '.$actual,
  1121. 0,
  1122. 10,
  1123. $canonicalize,
  1124. $ignoreCase
  1125. );
  1126. }
  1127. public static function assertEquals($expected, $actual, $message = '', $delta = 0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false)
  1128. {
  1129. return parent::assertEquals(str_replace("\r", '', $expected), str_replace("\r", '', $actual), $message, $delta, $maxDepth, $canonicalize, $ignoreCase);
  1130. }
  1131. }