AutoloadGeneratorTest.php 42 KB

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