AutoloadGeneratorTest.php 42 KB

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