AutoloadGeneratorTest.php 47 KB

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