AutoloadGeneratorTest.php 52 KB

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