AutoloadGeneratorTest.php 56 KB

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