AutoloadGeneratorTest.php 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  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('classmap' => array('')));
  426. $b->setAutoload(array('classmap' => array('test.php')));
  427. $c->setAutoload(array('classmap' => array('./')));
  428. $this->repository->expects($this->once())
  429. ->method('getCanonicalPackages')
  430. ->will($this->returnValue($packages));
  431. $this->configValueMap['classmap-authoritative'] = true;
  432. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  433. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  434. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b');
  435. $this->fs->ensureDirectoryExists($this->vendorDir.'/c/c/foo');
  436. file_put_contents($this->vendorDir.'/a/a/src/a.php', '<?php class ClassMapFoo {}');
  437. file_put_contents($this->vendorDir.'/b/b/test.php', '<?php class ClassMapBar {}');
  438. file_put_contents($this->vendorDir.'/c/c/foo/test.php', '<?php class ClassMapBaz {}');
  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/test.php',
  444. 'ClassMapBaz' => $this->vendorDir.'/c/c/foo/test.php',
  445. 'ClassMapFoo' => $this->vendorDir.'/a/a/src/a.php',
  446. ),
  447. include $this->vendorDir.'/composer/autoload_classmap.php'
  448. );
  449. $this->assertAutoloadFiles('classmap5', $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. $notAutoloadPackage = new Package('a', '1.0', '1.0');
  491. $autoloadPackages = array();
  492. $autoloadPackages[] = $a = new Package('a/a', '1.0', '1.0');
  493. $autoloadPackages[] = $b = new Package('b/b', '1.0', '1.0');
  494. $autoloadPackages[] = $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. $notAutoloadPackages = array();
  500. $notAutoloadPackages[] = $a = new Package('a/a', '1.0', '1.0');
  501. $notAutoloadPackages[] = $b = new Package('b/b', '1.0', '1.0');
  502. $notAutoloadPackages[] = $c = new Package('c/c', '1.0', '1.0');
  503. $this->repository->expects($this->at(0))
  504. ->method('getCanonicalPackages')
  505. ->will($this->returnValue($autoloadPackages));
  506. $this->repository->expects($this->at(1))
  507. ->method('getCanonicalPackages')
  508. ->will($this->returnValue($notAutoloadPackages));
  509. $this->repository->expects($this->at(2))
  510. ->method('getCanonicalPackages')
  511. ->will($this->returnValue($notAutoloadPackages));
  512. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a');
  513. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b');
  514. $this->fs->ensureDirectoryExists($this->vendorDir.'/c/c/foo/bar');
  515. file_put_contents($this->vendorDir.'/a/a/test.php', '<?php function testFilesAutoloadGeneration1() {}');
  516. file_put_contents($this->vendorDir.'/b/b/test2.php', '<?php function testFilesAutoloadGeneration2() {}');
  517. file_put_contents($this->vendorDir.'/c/c/foo/bar/test3.php', '<?php function testFilesAutoloadGeneration3() {}');
  518. file_put_contents($this->vendorDir.'/c/c/foo/bar/test4.php', '<?php function testFilesAutoloadGeneration4() {}');
  519. file_put_contents($this->workingDir.'/root.php', '<?php function testFilesAutoloadGenerationRoot() {}');
  520. $this->generator->dump($this->config, $this->repository, $autoloadPackage, $this->im, 'composer', false, 'FilesAutoload');
  521. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php');
  522. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_functions.php', $this->vendorDir.'/composer/autoload_real.php');
  523. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_files_functions.php', $this->vendorDir.'/composer/autoload_files.php');
  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.php', $this->vendorDir.'/composer/autoload_real.php');
  527. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_files_functions_with_removed_extra.php', $this->vendorDir.'/composer/autoload_files.php');
  528. $this->generator->dump($this->config, $this->repository, $notAutoloadPackage, $this->im, 'composer', false, 'FilesAutoload');
  529. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php');
  530. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_functions_with_removed_extra.php', $this->vendorDir.'/composer/autoload_real.php');
  531. $this->assertFileNotExists($this->vendorDir.'/composer/autoload_files.php');
  532. include $this->vendorDir . '/autoload.php';
  533. $this->assertFalse(function_exists('testFilesAutoloadGeneration1'));
  534. $this->assertFalse(function_exists('testFilesAutoloadGeneration2'));
  535. $this->assertFalse(function_exists('testFilesAutoloadGeneration3'));
  536. $this->assertFalse(function_exists('testFilesAutoloadGeneration4'));
  537. $this->assertFalse(function_exists('testFilesAutoloadGenerationRoot'));
  538. }
  539. public function testFilesAutoloadOrderByDependencies()
  540. {
  541. $package = new Package('a', '1.0', '1.0');
  542. $package->setAutoload(array('files' => array('root.php')));
  543. $package->setRequires(array(new Link('a', 'z/foo')));
  544. $package->setRequires(array(new Link('a', 'd/d')));
  545. $package->setRequires(array(new Link('a', 'e/e')));
  546. $packages = array();
  547. $packages[] = $z = new Package('z/foo', '1.0', '1.0');
  548. $packages[] = $b = new Package('b/bar', '1.0', '1.0');
  549. $packages[] = $d = new Package('d/d', '1.0', '1.0');
  550. $packages[] = $c = new Package('c/lorem', '1.0', '1.0');
  551. $packages[] = $e = new Package('e/e', '1.0', '1.0');
  552. $z->setAutoload(array('files' => array('testA.php')));
  553. $z->setRequires(array(new Link('z/foo', 'c/lorem')));
  554. $b->setAutoload(array('files' => array('testB.php')));
  555. $b->setRequires(array(new Link('b/bar', 'c/lorem'), new Link('b/bar', 'd/d')));
  556. $c->setAutoload(array('files' => array('testC.php')));
  557. $d->setAutoload(array('files' => array('testD.php')));
  558. $d->setRequires(array(new Link('d/d', 'c/lorem')));
  559. $e->setAutoload(array('files' => array('testE.php')));
  560. $e->setRequires(array(new Link('e/e', 'c/lorem')));
  561. $this->repository->expects($this->once())
  562. ->method('getCanonicalPackages')
  563. ->will($this->returnValue($packages));
  564. $this->fs->ensureDirectoryExists($this->vendorDir . '/z/foo');
  565. $this->fs->ensureDirectoryExists($this->vendorDir . '/b/bar');
  566. $this->fs->ensureDirectoryExists($this->vendorDir . '/c/lorem');
  567. $this->fs->ensureDirectoryExists($this->vendorDir . '/d/d');
  568. $this->fs->ensureDirectoryExists($this->vendorDir . '/e/e');
  569. file_put_contents($this->vendorDir . '/z/foo/testA.php', '<?php function testFilesAutoloadOrderByDependency1() {}');
  570. file_put_contents($this->vendorDir . '/b/bar/testB.php', '<?php function testFilesAutoloadOrderByDependency2() {}');
  571. file_put_contents($this->vendorDir . '/c/lorem/testC.php', '<?php function testFilesAutoloadOrderByDependency3() {}');
  572. file_put_contents($this->vendorDir . '/d/d/testD.php', '<?php function testFilesAutoloadOrderByDependency4() {}');
  573. file_put_contents($this->vendorDir . '/e/e/testE.php', '<?php function testFilesAutoloadOrderByDependency5() {}');
  574. file_put_contents($this->workingDir . '/root.php', '<?php function testFilesAutoloadOrderByDependencyRoot() {}');
  575. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'FilesAutoloadOrder');
  576. $this->assertFileEquals(__DIR__ . '/Fixtures/autoload_functions_by_dependency.php', $this->vendorDir . '/autoload.php');
  577. $this->assertFileEquals(__DIR__ . '/Fixtures/autoload_real_files_by_dependency.php', $this->vendorDir . '/composer/autoload_real.php');
  578. require $this->vendorDir . '/autoload.php';
  579. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency1'));
  580. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency2'));
  581. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency3'));
  582. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency4'));
  583. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency5'));
  584. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependencyRoot'));
  585. }
  586. /**
  587. * Test that PSR-0 and PSR-4 mappings are processed in the correct order for
  588. * autoloading and for classmap generation:
  589. * - The main package has priority over other packages.
  590. * - Longer namespaces have priority over shorter namespaces.
  591. */
  592. public function testOverrideVendorsAutoloading()
  593. {
  594. $mainPackage = new Package('z', '1.0', '1.0');
  595. $mainPackage->setAutoload(array(
  596. 'psr-0' => array('A\\B' => $this->workingDir.'/lib'),
  597. 'classmap' => array($this->workingDir.'/src')
  598. ));
  599. $mainPackage->setRequires(array(new Link('z', 'a/a')));
  600. $packages = array();
  601. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  602. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  603. $a->setAutoload(array(
  604. 'psr-0' => array('A' => 'src/', 'A\\B' => 'lib/'),
  605. 'classmap' => array('classmap'),
  606. ));
  607. $b->setAutoload(array(
  608. 'psr-0' => array('B\\Sub\\Name' => 'src/'),
  609. ));
  610. $this->repository->expects($this->once())
  611. ->method('getCanonicalPackages')
  612. ->will($this->returnValue($packages));
  613. $this->fs->ensureDirectoryExists($this->workingDir.'/lib/A/B');
  614. $this->fs->ensureDirectoryExists($this->workingDir.'/src/');
  615. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  616. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/classmap');
  617. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  618. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib/A/B');
  619. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  620. // Define the classes A\B\C and Foo\Bar in the main package.
  621. file_put_contents($this->workingDir.'/lib/A/B/C.php', '<?php namespace A\\B; class C {}');
  622. file_put_contents($this->workingDir.'/src/classes.php', '<?php namespace Foo; class Bar {}');
  623. // Define the same two classes in the package a/a.
  624. file_put_contents($this->vendorDir.'/a/a/lib/A/B/C.php', '<?php namespace A\\B; class C {}');
  625. file_put_contents($this->vendorDir.'/a/a/classmap/classes.php', '<?php namespace Foo; class Bar {}');
  626. $expectedNamespace = <<<EOF
  627. <?php
  628. // autoload_namespaces.php @generated by Composer
  629. \$vendorDir = dirname(dirname(__FILE__));
  630. \$baseDir = dirname(\$vendorDir);
  631. return array(
  632. 'B\\\\Sub\\\\Name' => array(\$vendorDir . '/b/b/src'),
  633. 'A\\\\B' => array(\$baseDir . '/lib', \$vendorDir . '/a/a/lib'),
  634. 'A' => array(\$vendorDir . '/a/a/src'),
  635. );
  636. EOF;
  637. // autoload_psr4.php is expected to be empty in this example.
  638. $expectedPsr4 = <<<EOF
  639. <?php
  640. // autoload_psr4.php @generated by Composer
  641. \$vendorDir = dirname(dirname(__FILE__));
  642. \$baseDir = dirname(\$vendorDir);
  643. return array(
  644. );
  645. EOF;
  646. $expectedClassmap = <<<EOF
  647. <?php
  648. // autoload_classmap.php @generated by Composer
  649. \$vendorDir = dirname(dirname(__FILE__));
  650. \$baseDir = dirname(\$vendorDir);
  651. return array(
  652. 'A\\\\B\\\\C' => \$baseDir . '/lib/A/B/C.php',
  653. 'Foo\\\\Bar' => \$baseDir . '/src/classes.php',
  654. );
  655. EOF;
  656. $this->generator->dump($this->config, $this->repository, $mainPackage, $this->im, 'composer', true, '_9');
  657. $this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php'));
  658. $this->assertEquals($expectedPsr4, file_get_contents($this->vendorDir.'/composer/autoload_psr4.php'));
  659. $this->assertEquals($expectedClassmap, file_get_contents($this->vendorDir.'/composer/autoload_classmap.php'));
  660. }
  661. public function testIncludePathFileGeneration()
  662. {
  663. $package = new Package('a', '1.0', '1.0');
  664. $packages = array();
  665. $a = new Package("a/a", "1.0", "1.0");
  666. $a->setIncludePaths(array("lib/"));
  667. $b = new Package("b/b", "1.0", "1.0");
  668. $b->setIncludePaths(array("library"));
  669. $c = new Package("c", "1.0", "1.0");
  670. $c->setIncludePaths(array("library"));
  671. $packages[] = $a;
  672. $packages[] = $b;
  673. $packages[] = $c;
  674. $this->repository->expects($this->once())
  675. ->method("getCanonicalPackages")
  676. ->will($this->returnValue($packages));
  677. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  678. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_10');
  679. $this->assertFileEquals(__DIR__.'/Fixtures/include_paths.php', $this->vendorDir.'/composer/include_paths.php');
  680. $this->assertEquals(
  681. array(
  682. $this->vendorDir."/a/a/lib",
  683. $this->vendorDir."/b/b/library",
  684. $this->vendorDir."/c/library",
  685. ),
  686. require $this->vendorDir."/composer/include_paths.php"
  687. );
  688. }
  689. public function testIncludePathsArePrependedInAutoloadFile()
  690. {
  691. $package = new Package('a', '1.0', '1.0');
  692. $packages = array();
  693. $a = new Package("a/a", "1.0", "1.0");
  694. $a->setIncludePaths(array("lib/"));
  695. $packages[] = $a;
  696. $this->repository->expects($this->once())
  697. ->method("getCanonicalPackages")
  698. ->will($this->returnValue($packages));
  699. mkdir($this->vendorDir."/composer", 0777, true);
  700. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_11');
  701. $oldIncludePath = get_include_path();
  702. require $this->vendorDir."/autoload.php";
  703. $this->assertEquals(
  704. $this->vendorDir."/a/a/lib".PATH_SEPARATOR.$oldIncludePath,
  705. get_include_path()
  706. );
  707. set_include_path($oldIncludePath);
  708. }
  709. public function testIncludePathsInMainPackage()
  710. {
  711. $package = new Package('a', '1.0', '1.0');
  712. $package->setIncludePaths(array('/lib', '/src'));
  713. $packages = array($a = new Package("a/a", "1.0", "1.0"));
  714. $a->setIncludePaths(array("lib/"));
  715. $this->repository->expects($this->once())
  716. ->method("getCanonicalPackages")
  717. ->will($this->returnValue($packages));
  718. mkdir($this->vendorDir."/composer", 0777, true);
  719. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_12');
  720. $oldIncludePath = get_include_path();
  721. require $this->vendorDir."/autoload.php";
  722. $this->assertEquals(
  723. $this->workingDir."/lib".PATH_SEPARATOR.$this->workingDir."/src".PATH_SEPARATOR.$this->vendorDir."/a/a/lib".PATH_SEPARATOR.$oldIncludePath,
  724. get_include_path()
  725. );
  726. set_include_path($oldIncludePath);
  727. }
  728. public function testIncludePathFileWithoutPathsIsSkipped()
  729. {
  730. $package = new Package('a', '1.0', '1.0');
  731. $packages = array();
  732. $a = new Package("a/a", "1.0", "1.0");
  733. $packages[] = $a;
  734. $this->repository->expects($this->once())
  735. ->method("getCanonicalPackages")
  736. ->will($this->returnValue($packages));
  737. mkdir($this->vendorDir."/composer", 0777, true);
  738. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_12');
  739. $this->assertFalse(file_exists($this->vendorDir."/composer/include_paths.php"));
  740. }
  741. public function testPreAndPostEventsAreDispatchedDuringAutoloadDump()
  742. {
  743. $this->eventDispatcher
  744. ->expects($this->at(0))
  745. ->method('dispatchScript')
  746. ->with(ScriptEvents::PRE_AUTOLOAD_DUMP, false);
  747. $this->eventDispatcher
  748. ->expects($this->at(1))
  749. ->method('dispatchScript')
  750. ->with(ScriptEvents::POST_AUTOLOAD_DUMP, false);
  751. $package = new Package('a', '1.0', '1.0');
  752. $package->setAutoload(array('psr-0' => array('foo/bar/non/existing/')));
  753. $this->repository->expects($this->once())
  754. ->method('getCanonicalPackages')
  755. ->will($this->returnValue(array()));
  756. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_8');
  757. }
  758. public function testUseGlobalIncludePath()
  759. {
  760. $package = new Package('a', '1.0', '1.0');
  761. $package->setAutoload(array(
  762. 'psr-0' => array('Main\\Foo' => '', 'Main\\Bar' => ''),
  763. ));
  764. $package->setTargetDir('Main/Foo/');
  765. $this->repository->expects($this->once())
  766. ->method('getCanonicalPackages')
  767. ->will($this->returnValue(array()));
  768. $this->configValueMap['use-include-path'] = true;
  769. $this->fs->ensureDirectoryExists($this->vendorDir.'/a');
  770. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'IncludePath');
  771. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_include_path.php', $this->vendorDir.'/composer/autoload_real.php');
  772. }
  773. public function testVendorDirExcludedFromWorkingDir()
  774. {
  775. $workingDir = $this->vendorDir.'/working-dir';
  776. $vendorDir = $workingDir.'/../vendor';
  777. $this->fs->ensureDirectoryExists($workingDir);
  778. chdir($workingDir);
  779. $package = new Package('a', '1.0', '1.0');
  780. $package->setAutoload(array(
  781. 'psr-0' => array('Foo' => 'src'),
  782. 'psr-4' => array('Acme\Foo\\' => 'src-psr4'),
  783. 'classmap' => array('classmap'),
  784. 'files' => array('test.php'),
  785. ));
  786. $vendorPackage = new Package('b/b', '1.0', '1.0');
  787. $vendorPackage->setAutoload(array(
  788. 'psr-0' => array('Bar' => 'lib'),
  789. 'psr-4' => array('Acme\Bar\\' => 'lib-psr4'),
  790. 'classmap' => array('classmaps'),
  791. 'files' => array('bootstrap.php'),
  792. ));
  793. $this->repository->expects($this->once())
  794. ->method('getCanonicalPackages')
  795. ->will($this->returnValue(array($vendorPackage)));
  796. $im = $this->getMockBuilder('Composer\Installer\InstallationManager')
  797. ->disableOriginalConstructor()
  798. ->getMock();
  799. $im->expects($this->any())
  800. ->method('getInstallPath')
  801. ->will($this->returnCallback(function ($package) use ($vendorDir) {
  802. $targetDir = $package->getTargetDir();
  803. return $vendorDir.'/'.$package->getName() . ($targetDir ? '/'.$targetDir : '');
  804. }));
  805. $this->fs->ensureDirectoryExists($workingDir.'/src/Foo');
  806. $this->fs->ensureDirectoryExists($workingDir.'/classmap');
  807. $this->fs->ensureDirectoryExists($vendorDir.'/composer');
  808. $this->fs->ensureDirectoryExists($vendorDir.'/b/b/lib/Bar');
  809. $this->fs->ensureDirectoryExists($vendorDir.'/b/b/classmaps');
  810. file_put_contents($workingDir.'/src/Foo/Bar.php', '<?php namespace Foo; class Bar {}');
  811. file_put_contents($workingDir.'/classmap/classes.php', '<?php namespace Foo; class Foo {}');
  812. file_put_contents($workingDir.'/test.php', '<?php class Foo {}');
  813. file_put_contents($vendorDir.'/b/b/lib/Bar/Foo.php', '<?php namespace Bar; class Foo {}');
  814. file_put_contents($vendorDir.'/b/b/classmaps/classes.php', '<?php namespace Bar; class Bar {}');
  815. file_put_contents($vendorDir.'/b/b/bootstrap.php', '<?php class Bar {}');
  816. $oldVendorDir = $this->vendorDir;
  817. $this->vendorDir = $vendorDir;
  818. $this->generator->dump($this->config, $this->repository, $package, $im, 'composer', true, '_13');
  819. $this->vendorDir = $oldVendorDir;
  820. $expectedNamespace = <<<'EOF'
  821. <?php
  822. // autoload_namespaces.php @generated by Composer
  823. $vendorDir = dirname(dirname(__FILE__));
  824. $baseDir = dirname($vendorDir).'/working-dir';
  825. return array(
  826. 'Foo' => array($baseDir . '/src'),
  827. 'Bar' => array($vendorDir . '/b/b/lib'),
  828. );
  829. EOF;
  830. $expectedPsr4 = <<<'EOF'
  831. <?php
  832. // autoload_psr4.php @generated by Composer
  833. $vendorDir = dirname(dirname(__FILE__));
  834. $baseDir = dirname($vendorDir).'/working-dir';
  835. return array(
  836. 'Acme\\Foo\\' => array($baseDir . '/src-psr4'),
  837. 'Acme\\Bar\\' => array($vendorDir . '/b/b/lib-psr4'),
  838. );
  839. EOF;
  840. $expectedClassmap = <<<'EOF'
  841. <?php
  842. // autoload_classmap.php @generated by Composer
  843. $vendorDir = dirname(dirname(__FILE__));
  844. $baseDir = dirname($vendorDir).'/working-dir';
  845. return array(
  846. 'Bar\\Bar' => $vendorDir . '/b/b/classmaps/classes.php',
  847. 'Bar\\Foo' => $vendorDir . '/b/b/lib/Bar/Foo.php',
  848. 'Foo\\Bar' => $baseDir . '/src/Foo/Bar.php',
  849. 'Foo\\Foo' => $baseDir . '/classmap/classes.php',
  850. );
  851. EOF;
  852. $this->assertEquals($expectedNamespace, file_get_contents($vendorDir.'/composer/autoload_namespaces.php'));
  853. $this->assertEquals($expectedPsr4, file_get_contents($vendorDir.'/composer/autoload_psr4.php'));
  854. $this->assertEquals($expectedClassmap, file_get_contents($vendorDir.'/composer/autoload_classmap.php'));
  855. $this->assertContains("\n \$vendorDir . '/b/b/bootstrap.php',\n", file_get_contents($vendorDir.'/composer/autoload_files.php'));
  856. $this->assertContains("\n \$baseDir . '/test.php',\n", file_get_contents($vendorDir.'/composer/autoload_files.php'));
  857. }
  858. public function testUpLevelRelativePaths()
  859. {
  860. $workingDir = $this->workingDir.'/working-dir';
  861. mkdir($workingDir, 0777, true);
  862. chdir($workingDir);
  863. $package = new Package('a', '1.0', '1.0');
  864. $package->setAutoload(array(
  865. 'psr-0' => array('Foo' => '../path/../src'),
  866. 'psr-4' => array('Acme\Foo\\' => '../path/../src-psr4'),
  867. 'classmap' => array('../classmap'),
  868. 'files' => array('../test.php'),
  869. ));
  870. $this->repository->expects($this->once())
  871. ->method('getCanonicalPackages')
  872. ->will($this->returnValue(array()));
  873. $this->fs->ensureDirectoryExists($this->workingDir.'/src/Foo');
  874. $this->fs->ensureDirectoryExists($this->workingDir.'/classmap');
  875. file_put_contents($this->workingDir.'/src/Foo/Bar.php', '<?php namespace Foo; class Bar {}');
  876. file_put_contents($this->workingDir.'/classmap/classes.php', '<?php namespace Foo; class Foo {}');
  877. file_put_contents($this->workingDir.'/test.php', '<?php class Foo {}');
  878. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_14');
  879. $expectedNamespace = <<<'EOF'
  880. <?php
  881. // autoload_namespaces.php @generated by Composer
  882. $vendorDir = dirname(dirname(__FILE__));
  883. $baseDir = dirname($vendorDir).'/working-dir';
  884. return array(
  885. 'Foo' => array($baseDir . '/../src'),
  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. );
  896. EOF;
  897. $expectedClassmap = <<<'EOF'
  898. <?php
  899. // autoload_classmap.php @generated by Composer
  900. $vendorDir = dirname(dirname(__FILE__));
  901. $baseDir = dirname($vendorDir).'/working-dir';
  902. return array(
  903. 'Foo\\Bar' => $baseDir . '/../src/Foo/Bar.php',
  904. 'Foo\\Foo' => $baseDir . '/../classmap/classes.php',
  905. );
  906. EOF;
  907. $this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php'));
  908. $this->assertEquals($expectedPsr4, file_get_contents($this->vendorDir.'/composer/autoload_psr4.php'));
  909. $this->assertEquals($expectedClassmap, file_get_contents($this->vendorDir.'/composer/autoload_classmap.php'));
  910. $this->assertContains("\n \$baseDir . '/../test.php',\n", file_get_contents($this->vendorDir.'/composer/autoload_files.php'));
  911. }
  912. public function testEmptyPaths()
  913. {
  914. $package = new Package('a', '1.0', '1.0');
  915. $package->setAutoload(array(
  916. 'psr-0' => array('Foo' => ''),
  917. 'psr-4' => array('Acme\Foo\\' => ''),
  918. 'classmap' => array(''),
  919. ));
  920. $this->repository->expects($this->once())
  921. ->method('getCanonicalPackages')
  922. ->will($this->returnValue(array()));
  923. $this->fs->ensureDirectoryExists($this->workingDir.'/Foo');
  924. file_put_contents($this->workingDir.'/Foo/Bar.php', '<?php namespace Foo; class Bar {}');
  925. file_put_contents($this->workingDir.'/class.php', '<?php namespace Classmap; class Foo {}');
  926. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_15');
  927. $expectedNamespace = <<<'EOF'
  928. <?php
  929. // autoload_namespaces.php @generated by Composer
  930. $vendorDir = dirname(dirname(__FILE__));
  931. $baseDir = dirname($vendorDir);
  932. return array(
  933. 'Foo' => array($baseDir . '/'),
  934. );
  935. EOF;
  936. $expectedPsr4 = <<<'EOF'
  937. <?php
  938. // autoload_psr4.php @generated by Composer
  939. $vendorDir = dirname(dirname(__FILE__));
  940. $baseDir = dirname($vendorDir);
  941. return array(
  942. 'Acme\\Foo\\' => array($baseDir . '/'),
  943. );
  944. EOF;
  945. $expectedClassmap = <<<'EOF'
  946. <?php
  947. // autoload_classmap.php @generated by Composer
  948. $vendorDir = dirname(dirname(__FILE__));
  949. $baseDir = dirname($vendorDir);
  950. return array(
  951. 'Classmap\\Foo' => $baseDir . '/class.php',
  952. 'Foo\\Bar' => $baseDir . '/Foo/Bar.php',
  953. );
  954. EOF;
  955. $this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php'));
  956. $this->assertEquals($expectedPsr4, file_get_contents($this->vendorDir.'/composer/autoload_psr4.php'));
  957. $this->assertEquals($expectedClassmap, file_get_contents($this->vendorDir.'/composer/autoload_classmap.php'));
  958. }
  959. public function testVendorSubstringPath()
  960. {
  961. $package = new Package('a', '1.0', '1.0');
  962. $package->setAutoload(array(
  963. 'psr-0' => array('Foo' => 'composer-test-autoload-src/src'),
  964. 'psr-4' => array('Acme\Foo\\' => 'composer-test-autoload-src/src-psr4'),
  965. ));
  966. $this->repository->expects($this->once())
  967. ->method('getCanonicalPackages')
  968. ->will($this->returnValue(array()));
  969. $this->fs->ensureDirectoryExists($this->vendorDir.'/a');
  970. $expectedNamespace = <<<'EOF'
  971. <?php
  972. // autoload_namespaces.php @generated by Composer
  973. $vendorDir = dirname(dirname(__FILE__));
  974. $baseDir = dirname($vendorDir);
  975. return array(
  976. 'Foo' => array($baseDir . '/composer-test-autoload-src/src'),
  977. );
  978. EOF;
  979. $expectedPsr4 = <<<'EOF'
  980. <?php
  981. // autoload_psr4.php @generated by Composer
  982. $vendorDir = dirname(dirname(__FILE__));
  983. $baseDir = dirname($vendorDir);
  984. return array(
  985. 'Acme\\Foo\\' => array($baseDir . '/composer-test-autoload-src/src-psr4'),
  986. );
  987. EOF;
  988. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'VendorSubstring');
  989. $this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php'));
  990. $this->assertEquals($expectedPsr4, file_get_contents($this->vendorDir.'/composer/autoload_psr4.php'));
  991. }
  992. private function assertAutoloadFiles($name, $dir, $type = 'namespaces')
  993. {
  994. $a = __DIR__.'/Fixtures/autoload_'.$name.'.php';
  995. $b = $dir.'/autoload_'.$type.'.php';
  996. $this->assertFileEquals($a, $b);
  997. }
  998. public static function assertFileEquals($expected, $actual, $message = '', $canonicalize = false, $ignoreCase = false)
  999. {
  1000. return self::assertEquals(
  1001. file_get_contents($expected),
  1002. file_get_contents($actual),
  1003. $message ?: $expected.' equals '.$actual,
  1004. 0,
  1005. 10,
  1006. $canonicalize,
  1007. $ignoreCase
  1008. );
  1009. }
  1010. public static function assertEquals($expected, $actual, $message = '', $delta = 0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false)
  1011. {
  1012. return parent::assertEquals(str_replace("\r", '', $expected), str_replace("\r", '', $actual), $message, $delta, $maxDepth, $canonicalize, $ignoreCase);
  1013. }
  1014. }