AutoloadGeneratorTest.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  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. public function testOverrideVendorsAutoloading()
  424. {
  425. $mainPackage = new Package('z', '1.0', '1.0');
  426. $mainPackage->setAutoload(array(
  427. 'psr-0' => array('A\\B' => $this->workingDir.'/lib'),
  428. 'classmap' => array($this->workingDir.'/src')
  429. ));
  430. $mainPackage->setRequires(array(new Link('z', 'a/a')));
  431. $packages = array();
  432. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  433. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  434. $a->setAutoload(array(
  435. 'psr-0' => array('A' => 'src/', 'A\\B' => 'lib/'),
  436. 'classmap' => array('classmap'),
  437. ));
  438. $b->setAutoload(array(
  439. 'psr-0' => array('B\\Sub\\Name' => 'src/'),
  440. ));
  441. $this->repository->expects($this->once())
  442. ->method('getCanonicalPackages')
  443. ->will($this->returnValue($packages));
  444. $this->fs->ensureDirectoryExists($this->workingDir.'/lib/A/B');
  445. $this->fs->ensureDirectoryExists($this->workingDir.'/src/');
  446. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  447. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/classmap');
  448. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  449. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib/A/B');
  450. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  451. file_put_contents($this->workingDir.'/lib/A/B/C.php', '<?php namespace A\\B; class C {}');
  452. file_put_contents($this->workingDir.'/src/classes.php', '<?php namespace Foo; class Bar {}');
  453. file_put_contents($this->vendorDir.'/a/a/lib/A/B/C.php', '<?php namespace A\\B; class C {}');
  454. file_put_contents($this->vendorDir.'/a/a/classmap/classes.php', '<?php namespace Foo; class Bar {}');
  455. $expectedNamespace = <<<EOF
  456. <?php
  457. // autoload_namespaces.php @generated by Composer
  458. \$vendorDir = dirname(dirname(__FILE__));
  459. \$baseDir = dirname(\$vendorDir);
  460. return array(
  461. 'B\\\\Sub\\\\Name' => array(\$vendorDir . '/b/b/src'),
  462. 'A\\\\B' => array(\$baseDir . '/lib', \$vendorDir . '/a/a/lib'),
  463. 'A' => array(\$vendorDir . '/a/a/src'),
  464. );
  465. EOF;
  466. // autoload_psr4.php is expected to be empty in this example.
  467. $expectedPsr4 = <<<EOF
  468. <?php
  469. // autoload_psr4.php @generated by Composer
  470. \$vendorDir = dirname(dirname(__FILE__));
  471. \$baseDir = dirname(\$vendorDir);
  472. return array(
  473. );
  474. EOF;
  475. $expectedClassmap = <<<EOF
  476. <?php
  477. // autoload_classmap.php @generated by Composer
  478. \$vendorDir = dirname(dirname(__FILE__));
  479. \$baseDir = dirname(\$vendorDir);
  480. return array(
  481. 'A\\\\B\\\\C' => \$baseDir . '/lib/A/B/C.php',
  482. 'Foo\\\\Bar' => \$baseDir . '/src/classes.php',
  483. );
  484. EOF;
  485. $this->generator->dump($this->config, $this->repository, $mainPackage, $this->im, 'composer', true, '_9');
  486. $this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php'));
  487. $this->assertEquals($expectedPsr4, file_get_contents($this->vendorDir.'/composer/autoload_psr4.php'));
  488. $this->assertEquals($expectedClassmap, file_get_contents($this->vendorDir.'/composer/autoload_classmap.php'));
  489. }
  490. public function testIncludePathFileGeneration()
  491. {
  492. $package = new Package('a', '1.0', '1.0');
  493. $packages = array();
  494. $a = new Package("a/a", "1.0", "1.0");
  495. $a->setIncludePaths(array("lib/"));
  496. $b = new Package("b/b", "1.0", "1.0");
  497. $b->setIncludePaths(array("library"));
  498. $c = new Package("c", "1.0", "1.0");
  499. $c->setIncludePaths(array("library"));
  500. $packages[] = $a;
  501. $packages[] = $b;
  502. $packages[] = $c;
  503. $this->repository->expects($this->once())
  504. ->method("getCanonicalPackages")
  505. ->will($this->returnValue($packages));
  506. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  507. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_10');
  508. $this->assertFileEquals(__DIR__.'/Fixtures/include_paths.php', $this->vendorDir.'/composer/include_paths.php');
  509. $this->assertEquals(
  510. array(
  511. $this->vendorDir."/a/a/lib",
  512. $this->vendorDir."/b/b/library",
  513. $this->vendorDir."/c/library",
  514. ),
  515. require $this->vendorDir."/composer/include_paths.php"
  516. );
  517. }
  518. public function testIncludePathsArePrependedInAutoloadFile()
  519. {
  520. $package = new Package('a', '1.0', '1.0');
  521. $packages = array();
  522. $a = new Package("a/a", "1.0", "1.0");
  523. $a->setIncludePaths(array("lib/"));
  524. $packages[] = $a;
  525. $this->repository->expects($this->once())
  526. ->method("getCanonicalPackages")
  527. ->will($this->returnValue($packages));
  528. mkdir($this->vendorDir."/composer", 0777, true);
  529. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_11');
  530. $oldIncludePath = get_include_path();
  531. require $this->vendorDir."/autoload.php";
  532. $this->assertEquals(
  533. $this->vendorDir."/a/a/lib".PATH_SEPARATOR.$oldIncludePath,
  534. get_include_path()
  535. );
  536. set_include_path($oldIncludePath);
  537. }
  538. public function testIncludePathsInMainPackage()
  539. {
  540. $package = new Package('a', '1.0', '1.0');
  541. $package->setIncludePaths(array('/lib', '/src'));
  542. $packages = array($a = new Package("a/a", "1.0", "1.0"));
  543. $a->setIncludePaths(array("lib/"));
  544. $this->repository->expects($this->once())
  545. ->method("getCanonicalPackages")
  546. ->will($this->returnValue($packages));
  547. mkdir($this->vendorDir."/composer", 0777, true);
  548. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_12');
  549. $oldIncludePath = get_include_path();
  550. require $this->vendorDir."/autoload.php";
  551. $this->assertEquals(
  552. $this->workingDir."/lib".PATH_SEPARATOR.$this->workingDir."/src".PATH_SEPARATOR.$this->vendorDir."/a/a/lib".PATH_SEPARATOR.$oldIncludePath,
  553. get_include_path()
  554. );
  555. set_include_path($oldIncludePath);
  556. }
  557. public function testIncludePathFileWithoutPathsIsSkipped()
  558. {
  559. $package = new Package('a', '1.0', '1.0');
  560. $packages = array();
  561. $a = new Package("a/a", "1.0", "1.0");
  562. $packages[] = $a;
  563. $this->repository->expects($this->once())
  564. ->method("getCanonicalPackages")
  565. ->will($this->returnValue($packages));
  566. mkdir($this->vendorDir."/composer", 0777, true);
  567. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_12');
  568. $this->assertFalse(file_exists($this->vendorDir."/composer/include_paths.php"));
  569. }
  570. public function testPreAndPostEventsAreDispatchedDuringAutoloadDump()
  571. {
  572. $this->eventDispatcher
  573. ->expects($this->at(0))
  574. ->method('dispatchScript')
  575. ->with(ScriptEvents::PRE_AUTOLOAD_DUMP, false);
  576. $this->eventDispatcher
  577. ->expects($this->at(1))
  578. ->method('dispatchScript')
  579. ->with(ScriptEvents::POST_AUTOLOAD_DUMP, false);
  580. $package = new Package('a', '1.0', '1.0');
  581. $package->setAutoload(array('psr-0' => array('foo/bar/non/existing/')));
  582. $this->repository->expects($this->once())
  583. ->method('getCanonicalPackages')
  584. ->will($this->returnValue(array()));
  585. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_8');
  586. }
  587. public function testUseGlobalIncludePath()
  588. {
  589. $package = new Package('a', '1.0', '1.0');
  590. $package->setAutoload(array(
  591. 'psr-0' => array('Main\\Foo' => '', 'Main\\Bar' => ''),
  592. ));
  593. $package->setTargetDir('Main/Foo/');
  594. $this->repository->expects($this->once())
  595. ->method('getCanonicalPackages')
  596. ->will($this->returnValue(array()));
  597. $this->config->expects($this->at(2))
  598. ->method('get')
  599. ->with($this->equalTo('use-include-path'))
  600. ->will($this->returnValue(true));
  601. $this->fs->ensureDirectoryExists($this->vendorDir.'/a');
  602. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'IncludePath');
  603. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_include_path.php', $this->vendorDir.'/composer/autoload_real.php');
  604. }
  605. public function testVendorDirExcludedFromWorkingDir()
  606. {
  607. $workingDir = $this->vendorDir.'/working-dir';
  608. $vendorDir = $workingDir.'/../vendor';
  609. $this->fs->ensureDirectoryExists($workingDir);
  610. chdir($workingDir);
  611. $package = new Package('a', '1.0', '1.0');
  612. $package->setAutoload(array(
  613. 'psr-0' => array('Foo' => 'src'),
  614. 'psr-4' => array('Acme\Foo\\' => 'src-psr4'),
  615. 'classmap' => array('classmap'),
  616. 'files' => array('test.php'),
  617. ));
  618. $vendorPackage = new Package('b/b', '1.0', '1.0');
  619. $vendorPackage->setAutoload(array(
  620. 'psr-0' => array('Bar' => 'lib'),
  621. 'psr-4' => array('Acme\Bar\\' => 'lib-psr4'),
  622. 'classmap' => array('classmaps'),
  623. 'files' => array('bootstrap.php'),
  624. ));
  625. $this->repository->expects($this->once())
  626. ->method('getCanonicalPackages')
  627. ->will($this->returnValue(array($vendorPackage)));
  628. $im = $this->getMockBuilder('Composer\Installer\InstallationManager')
  629. ->disableOriginalConstructor()
  630. ->getMock();
  631. $im->expects($this->any())
  632. ->method('getInstallPath')
  633. ->will($this->returnCallback(function ($package) use ($vendorDir) {
  634. $targetDir = $package->getTargetDir();
  635. return $vendorDir.'/'.$package->getName() . ($targetDir ? '/'.$targetDir : '');
  636. }));
  637. $this->fs->ensureDirectoryExists($workingDir.'/src/Foo');
  638. $this->fs->ensureDirectoryExists($workingDir.'/classmap');
  639. $this->fs->ensureDirectoryExists($vendorDir.'/composer');
  640. $this->fs->ensureDirectoryExists($vendorDir.'/b/b/lib/Bar');
  641. $this->fs->ensureDirectoryExists($vendorDir.'/b/b/classmaps');
  642. file_put_contents($workingDir.'/src/Foo/Bar.php', '<?php namespace Foo; class Bar {}');
  643. file_put_contents($workingDir.'/classmap/classes.php', '<?php namespace Foo; class Foo {}');
  644. file_put_contents($workingDir.'/test.php', '<?php class Foo {}');
  645. file_put_contents($vendorDir.'/b/b/lib/Bar/Foo.php', '<?php namespace Bar; class Foo {}');
  646. file_put_contents($vendorDir.'/b/b/classmaps/classes.php', '<?php namespace Bar; class Bar {}');
  647. file_put_contents($vendorDir.'/b/b/bootstrap.php', '<?php class Bar {}');
  648. $oldVendorDir = $this->vendorDir;
  649. $this->vendorDir = $vendorDir;
  650. $this->generator->dump($this->config, $this->repository, $package, $im, 'composer', true, '_13');
  651. $this->vendorDir = $oldVendorDir;
  652. $expectedNamespace = <<<'EOF'
  653. <?php
  654. // autoload_namespaces.php @generated by Composer
  655. $vendorDir = dirname(dirname(__FILE__));
  656. $baseDir = dirname($vendorDir).'/working-dir';
  657. return array(
  658. 'Foo' => array($baseDir . '/src'),
  659. 'Bar' => array($vendorDir . '/b/b/lib'),
  660. );
  661. EOF;
  662. $expectedPsr4 = <<<'EOF'
  663. <?php
  664. // autoload_psr4.php @generated by Composer
  665. $vendorDir = dirname(dirname(__FILE__));
  666. $baseDir = dirname($vendorDir).'/working-dir';
  667. return array(
  668. 'Acme\\Foo\\' => array($baseDir . '/src-psr4'),
  669. 'Acme\\Bar\\' => array($vendorDir . '/b/b/lib-psr4'),
  670. );
  671. EOF;
  672. $expectedClassmap = <<<'EOF'
  673. <?php
  674. // autoload_classmap.php @generated by Composer
  675. $vendorDir = dirname(dirname(__FILE__));
  676. $baseDir = dirname($vendorDir).'/working-dir';
  677. return array(
  678. 'Bar\\Bar' => $vendorDir . '/b/b/classmaps/classes.php',
  679. 'Bar\\Foo' => $vendorDir . '/b/b/lib/Bar/Foo.php',
  680. 'Foo\\Bar' => $baseDir . '/src/Foo/Bar.php',
  681. 'Foo\\Foo' => $baseDir . '/classmap/classes.php',
  682. );
  683. EOF;
  684. $this->assertEquals($expectedNamespace, file_get_contents($vendorDir.'/composer/autoload_namespaces.php'));
  685. $this->assertEquals($expectedPsr4, file_get_contents($vendorDir.'/composer/autoload_psr4.php'));
  686. $this->assertEquals($expectedClassmap, file_get_contents($vendorDir.'/composer/autoload_classmap.php'));
  687. $this->assertContains("\n \$vendorDir . '/b/b/bootstrap.php',\n", file_get_contents($vendorDir.'/composer/autoload_files.php'));
  688. $this->assertContains("\n \$baseDir . '/test.php',\n", file_get_contents($vendorDir.'/composer/autoload_files.php'));
  689. }
  690. public function testUpLevelRelativePaths()
  691. {
  692. $workingDir = $this->workingDir.'/working-dir';
  693. mkdir($workingDir, 0777, true);
  694. chdir($workingDir);
  695. $package = new Package('a', '1.0', '1.0');
  696. $package->setAutoload(array(
  697. 'psr-0' => array('Foo' => '../path/../src'),
  698. 'psr-4' => array('Acme\Foo\\' => '../path/../src-psr4'),
  699. 'classmap' => array('../classmap'),
  700. 'files' => array('../test.php'),
  701. ));
  702. $this->repository->expects($this->once())
  703. ->method('getCanonicalPackages')
  704. ->will($this->returnValue(array()));
  705. $this->fs->ensureDirectoryExists($this->workingDir.'/src/Foo');
  706. $this->fs->ensureDirectoryExists($this->workingDir.'/classmap');
  707. file_put_contents($this->workingDir.'/src/Foo/Bar.php', '<?php namespace Foo; class Bar {}');
  708. file_put_contents($this->workingDir.'/classmap/classes.php', '<?php namespace Foo; class Foo {}');
  709. file_put_contents($this->workingDir.'/test.php', '<?php class Foo {}');
  710. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_14');
  711. $expectedNamespace = <<<'EOF'
  712. <?php
  713. // autoload_namespaces.php @generated by Composer
  714. $vendorDir = dirname(dirname(__FILE__));
  715. $baseDir = dirname($vendorDir).'/working-dir';
  716. return array(
  717. 'Foo' => array($baseDir . '/../src'),
  718. );
  719. EOF;
  720. $expectedPsr4 = <<<'EOF'
  721. <?php
  722. // autoload_psr4.php @generated by Composer
  723. $vendorDir = dirname(dirname(__FILE__));
  724. $baseDir = dirname($vendorDir).'/working-dir';
  725. return array(
  726. 'Acme\\Foo\\' => array($baseDir . '/../src-psr4'),
  727. );
  728. EOF;
  729. $expectedClassmap = <<<'EOF'
  730. <?php
  731. // autoload_classmap.php @generated by Composer
  732. $vendorDir = dirname(dirname(__FILE__));
  733. $baseDir = dirname($vendorDir).'/working-dir';
  734. return array(
  735. 'Foo\\Bar' => $baseDir . '/../src/Foo/Bar.php',
  736. 'Foo\\Foo' => $baseDir . '/../classmap/classes.php',
  737. );
  738. EOF;
  739. $this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php'));
  740. $this->assertEquals($expectedPsr4, file_get_contents($this->vendorDir.'/composer/autoload_psr4.php'));
  741. $this->assertEquals($expectedClassmap, file_get_contents($this->vendorDir.'/composer/autoload_classmap.php'));
  742. $this->assertContains("\n \$baseDir . '/../test.php',\n", file_get_contents($this->vendorDir.'/composer/autoload_files.php'));
  743. }
  744. public function testEmptyPaths()
  745. {
  746. $package = new Package('a', '1.0', '1.0');
  747. $package->setAutoload(array(
  748. 'psr-0' => array('Foo' => ''),
  749. 'psr-4' => array('Acme\Foo\\' => ''),
  750. 'classmap' => array(''),
  751. ));
  752. $this->repository->expects($this->once())
  753. ->method('getCanonicalPackages')
  754. ->will($this->returnValue(array()));
  755. $this->fs->ensureDirectoryExists($this->workingDir.'/Foo');
  756. file_put_contents($this->workingDir.'/Foo/Bar.php', '<?php namespace Foo; class Bar {}');
  757. file_put_contents($this->workingDir.'/class.php', '<?php namespace Classmap; class Foo {}');
  758. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_15');
  759. $expectedNamespace = <<<'EOF'
  760. <?php
  761. // autoload_namespaces.php @generated by Composer
  762. $vendorDir = dirname(dirname(__FILE__));
  763. $baseDir = dirname($vendorDir);
  764. return array(
  765. 'Foo' => array($baseDir . '/'),
  766. );
  767. EOF;
  768. $expectedPsr4 = <<<'EOF'
  769. <?php
  770. // autoload_psr4.php @generated by Composer
  771. $vendorDir = dirname(dirname(__FILE__));
  772. $baseDir = dirname($vendorDir);
  773. return array(
  774. 'Acme\\Foo\\' => array($baseDir . '/'),
  775. );
  776. EOF;
  777. $expectedClassmap = <<<'EOF'
  778. <?php
  779. // autoload_classmap.php @generated by Composer
  780. $vendorDir = dirname(dirname(__FILE__));
  781. $baseDir = dirname($vendorDir);
  782. return array(
  783. 'Classmap\\Foo' => $baseDir . '/class.php',
  784. 'Foo\\Bar' => $baseDir . '/Foo/Bar.php',
  785. );
  786. EOF;
  787. $this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php'));
  788. $this->assertEquals($expectedPsr4, file_get_contents($this->vendorDir.'/composer/autoload_psr4.php'));
  789. $this->assertEquals($expectedClassmap, file_get_contents($this->vendorDir.'/composer/autoload_classmap.php'));
  790. }
  791. public function testVendorSubstringPath()
  792. {
  793. $package = new Package('a', '1.0', '1.0');
  794. $package->setAutoload(array(
  795. 'psr-0' => array('Foo' => 'composer-test-autoload-src/src'),
  796. 'psr-4' => array('Acme\Foo\\' => 'composer-test-autoload-src/src-psr4'),
  797. ));
  798. $this->repository->expects($this->once())
  799. ->method('getCanonicalPackages')
  800. ->will($this->returnValue(array()));
  801. $this->fs->ensureDirectoryExists($this->vendorDir.'/a');
  802. $expectedNamespace = <<<'EOF'
  803. <?php
  804. // autoload_namespaces.php @generated by Composer
  805. $vendorDir = dirname(dirname(__FILE__));
  806. $baseDir = dirname($vendorDir);
  807. return array(
  808. 'Foo' => array($baseDir . '/composer-test-autoload-src/src'),
  809. );
  810. EOF;
  811. $expectedPsr4 = <<<'EOF'
  812. <?php
  813. // autoload_psr4.php @generated by Composer
  814. $vendorDir = dirname(dirname(__FILE__));
  815. $baseDir = dirname($vendorDir);
  816. return array(
  817. 'Acme\\Foo\\' => array($baseDir . '/composer-test-autoload-src/src-psr4'),
  818. );
  819. EOF;
  820. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'VendorSubstring');
  821. $this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php'));
  822. $this->assertEquals($expectedPsr4, file_get_contents($this->vendorDir.'/composer/autoload_psr4.php'));
  823. }
  824. private function assertAutoloadFiles($name, $dir, $type = 'namespaces')
  825. {
  826. $a = __DIR__.'/Fixtures/autoload_'.$name.'.php';
  827. $b = $dir.'/autoload_'.$type.'.php';
  828. $this->assertFileEquals($a, $b);
  829. }
  830. }