AutoloadGeneratorTest.php 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344
  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('psr-4' => array('' => 'src/')));
  426. $b->setAutoload(array('psr-4' => array('' => './')));
  427. $c->setAutoload(array('psr-4' => array('' => 'foo/')));
  428. $this->repository->expects($this->once())
  429. ->method('getCanonicalPackages')
  430. ->will($this->returnValue($packages));
  431. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  432. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  433. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b');
  434. $this->fs->ensureDirectoryExists($this->vendorDir.'/c/c/foo');
  435. file_put_contents($this->vendorDir.'/a/a/src/ClassMapFoo.php', '<?php class ClassMapFoo {}');
  436. file_put_contents($this->vendorDir.'/b/b/ClassMapBar.php', '<?php class ClassMapBar {}');
  437. file_put_contents($this->vendorDir.'/c/c/foo/ClassMapBaz.php', '<?php class ClassMapBaz {}');
  438. $this->generator->setClassMapAuthoritative(true);
  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/ClassMapBar.php',
  444. 'ClassMapBaz' => $this->vendorDir.'/c/c/foo/ClassMapBaz.php',
  445. 'ClassMapFoo' => $this->vendorDir.'/a/a/src/ClassMapFoo.php',
  446. ),
  447. include $this->vendorDir.'/composer/autoload_classmap.php'
  448. );
  449. $this->assertAutoloadFiles('classmap8', $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. $autoloadPackage->setIncludePaths(array('/lib', '/src'));
  491. $notAutoloadPackage = new Package('a', '1.0', '1.0');
  492. $autoloadPackages = array();
  493. $autoloadPackages[] = $a = new Package('a/a', '1.0', '1.0');
  494. $autoloadPackages[] = $b = new Package('b/b', '1.0', '1.0');
  495. $autoloadPackages[] = $c = new Package('c/c', '1.0', '1.0');
  496. $a->setAutoload(array('files' => array('test.php')));
  497. $a->setIncludePaths(array('lib1', 'src1'));
  498. $b->setAutoload(array('files' => array('test2.php')));
  499. $b->setIncludePaths(array('lib2'));
  500. $c->setAutoload(array('files' => array('test3.php', 'foo/bar/test4.php')));
  501. $c->setIncludePaths(array('lib3'));
  502. $c->setTargetDir('foo/bar');
  503. $notAutoloadPackages = array();
  504. $notAutoloadPackages[] = $a = new Package('a/a', '1.0', '1.0');
  505. $notAutoloadPackages[] = $b = new Package('b/b', '1.0', '1.0');
  506. $notAutoloadPackages[] = $c = new Package('c/c', '1.0', '1.0');
  507. $this->repository->expects($this->at(0))
  508. ->method('getCanonicalPackages')
  509. ->will($this->returnValue($autoloadPackages));
  510. $this->repository->expects($this->at(1))
  511. ->method('getCanonicalPackages')
  512. ->will($this->returnValue($notAutoloadPackages));
  513. $this->repository->expects($this->at(2))
  514. ->method('getCanonicalPackages')
  515. ->will($this->returnValue($notAutoloadPackages));
  516. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a');
  517. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b');
  518. $this->fs->ensureDirectoryExists($this->vendorDir.'/c/c/foo/bar');
  519. file_put_contents($this->vendorDir.'/a/a/test.php', '<?php function testFilesAutoloadGeneration1() {}');
  520. file_put_contents($this->vendorDir.'/b/b/test2.php', '<?php function testFilesAutoloadGeneration2() {}');
  521. file_put_contents($this->vendorDir.'/c/c/foo/bar/test3.php', '<?php function testFilesAutoloadGeneration3() {}');
  522. file_put_contents($this->vendorDir.'/c/c/foo/bar/test4.php', '<?php function testFilesAutoloadGeneration4() {}');
  523. file_put_contents($this->workingDir.'/root.php', '<?php function testFilesAutoloadGenerationRoot() {}');
  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_with_include_paths.php', $this->vendorDir.'/composer/autoload_real.php');
  527. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_files_functions.php', $this->vendorDir.'/composer/autoload_files.php');
  528. $this->assertFileEquals(__DIR__.'/Fixtures/include_paths_functions.php', $this->vendorDir.'/composer/include_paths.php');
  529. $this->generator->dump($this->config, $this->repository, $autoloadPackage, $this->im, 'composer', false, 'FilesAutoload');
  530. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php');
  531. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_functions_with_include_paths.php', $this->vendorDir.'/composer/autoload_real.php');
  532. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_files_functions_with_removed_extra.php', $this->vendorDir.'/composer/autoload_files.php');
  533. $this->assertFileEquals(__DIR__.'/Fixtures/include_paths_functions_with_removed_extra.php', $this->vendorDir.'/composer/include_paths.php');
  534. $this->generator->dump($this->config, $this->repository, $notAutoloadPackage, $this->im, 'composer', false, 'FilesAutoload');
  535. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php');
  536. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_functions_with_removed_include_paths_and_autolad_files.php', $this->vendorDir.'/composer/autoload_real.php');
  537. $this->assertFileNotExists($this->vendorDir.'/composer/autoload_files.php');
  538. $this->assertFileNotExists($this->vendorDir.'/composer/include_paths.php');
  539. }
  540. public function testFilesAutoloadOrderByDependencies()
  541. {
  542. $package = new Package('a', '1.0', '1.0');
  543. $package->setAutoload(array('files' => array('root.php')));
  544. $package->setRequires(array(new Link('a', 'z/foo')));
  545. $package->setRequires(array(new Link('a', 'd/d')));
  546. $package->setRequires(array(new Link('a', 'e/e')));
  547. $packages = array();
  548. $packages[] = $z = new Package('z/foo', '1.0', '1.0');
  549. $packages[] = $b = new Package('b/bar', '1.0', '1.0');
  550. $packages[] = $d = new Package('d/d', '1.0', '1.0');
  551. $packages[] = $c = new Package('c/lorem', '1.0', '1.0');
  552. $packages[] = $e = new Package('e/e', '1.0', '1.0');
  553. $z->setAutoload(array('files' => array('testA.php')));
  554. $z->setRequires(array(new Link('z/foo', 'c/lorem')));
  555. $b->setAutoload(array('files' => array('testB.php')));
  556. $b->setRequires(array(new Link('b/bar', 'c/lorem'), new Link('b/bar', 'd/d')));
  557. $c->setAutoload(array('files' => array('testC.php')));
  558. $d->setAutoload(array('files' => array('testD.php')));
  559. $d->setRequires(array(new Link('d/d', 'c/lorem')));
  560. $e->setAutoload(array('files' => array('testE.php')));
  561. $e->setRequires(array(new Link('e/e', 'c/lorem')));
  562. $this->repository->expects($this->once())
  563. ->method('getCanonicalPackages')
  564. ->will($this->returnValue($packages));
  565. $this->fs->ensureDirectoryExists($this->vendorDir . '/z/foo');
  566. $this->fs->ensureDirectoryExists($this->vendorDir . '/b/bar');
  567. $this->fs->ensureDirectoryExists($this->vendorDir . '/c/lorem');
  568. $this->fs->ensureDirectoryExists($this->vendorDir . '/d/d');
  569. $this->fs->ensureDirectoryExists($this->vendorDir . '/e/e');
  570. file_put_contents($this->vendorDir . '/z/foo/testA.php', '<?php function testFilesAutoloadOrderByDependency1() {}');
  571. file_put_contents($this->vendorDir . '/b/bar/testB.php', '<?php function testFilesAutoloadOrderByDependency2() {}');
  572. file_put_contents($this->vendorDir . '/c/lorem/testC.php', '<?php function testFilesAutoloadOrderByDependency3() {}');
  573. file_put_contents($this->vendorDir . '/d/d/testD.php', '<?php function testFilesAutoloadOrderByDependency4() {}');
  574. file_put_contents($this->vendorDir . '/e/e/testE.php', '<?php function testFilesAutoloadOrderByDependency5() {}');
  575. file_put_contents($this->workingDir . '/root.php', '<?php function testFilesAutoloadOrderByDependencyRoot() {}');
  576. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'FilesAutoloadOrder');
  577. $this->assertFileEquals(__DIR__ . '/Fixtures/autoload_functions_by_dependency.php', $this->vendorDir . '/autoload.php');
  578. $this->assertFileEquals(__DIR__ . '/Fixtures/autoload_real_files_by_dependency.php', $this->vendorDir . '/composer/autoload_real.php');
  579. require $this->vendorDir . '/autoload.php';
  580. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency1'));
  581. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency2'));
  582. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency3'));
  583. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency4'));
  584. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency5'));
  585. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependencyRoot'));
  586. }
  587. /**
  588. * Test that PSR-0 and PSR-4 mappings are processed in the correct order for
  589. * autoloading and for classmap generation:
  590. * - The main package has priority over other packages.
  591. * - Longer namespaces have priority over shorter namespaces.
  592. */
  593. public function testOverrideVendorsAutoloading()
  594. {
  595. $mainPackage = new Package('z', '1.0', '1.0');
  596. $mainPackage->setAutoload(array(
  597. 'psr-0' => array('A\\B' => $this->workingDir.'/lib'),
  598. 'classmap' => array($this->workingDir.'/src'),
  599. ));
  600. $mainPackage->setRequires(array(new Link('z', 'a/a')));
  601. $packages = array();
  602. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  603. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  604. $a->setAutoload(array(
  605. 'psr-0' => array('A' => 'src/', 'A\\B' => 'lib/'),
  606. 'classmap' => array('classmap'),
  607. ));
  608. $b->setAutoload(array(
  609. 'psr-0' => array('B\\Sub\\Name' => 'src/'),
  610. ));
  611. $this->repository->expects($this->once())
  612. ->method('getCanonicalPackages')
  613. ->will($this->returnValue($packages));
  614. $this->fs->ensureDirectoryExists($this->workingDir.'/lib/A/B');
  615. $this->fs->ensureDirectoryExists($this->workingDir.'/src/');
  616. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  617. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/classmap');
  618. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  619. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib/A/B');
  620. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  621. // Define the classes A\B\C and Foo\Bar in the main package.
  622. file_put_contents($this->workingDir.'/lib/A/B/C.php', '<?php namespace A\\B; class C {}');
  623. file_put_contents($this->workingDir.'/src/classes.php', '<?php namespace Foo; class Bar {}');
  624. // Define the same two classes in the package a/a.
  625. file_put_contents($this->vendorDir.'/a/a/lib/A/B/C.php', '<?php namespace A\\B; class C {}');
  626. file_put_contents($this->vendorDir.'/a/a/classmap/classes.php', '<?php namespace Foo; class Bar {}');
  627. $expectedNamespace = <<<EOF
  628. <?php
  629. // autoload_namespaces.php @generated by Composer
  630. \$vendorDir = dirname(dirname(__FILE__));
  631. \$baseDir = dirname(\$vendorDir);
  632. return array(
  633. 'B\\\\Sub\\\\Name' => array(\$vendorDir . '/b/b/src'),
  634. 'A\\\\B' => array(\$baseDir . '/lib', \$vendorDir . '/a/a/lib'),
  635. 'A' => array(\$vendorDir . '/a/a/src'),
  636. );
  637. EOF;
  638. // autoload_psr4.php is expected to be empty in this example.
  639. $expectedPsr4 = <<<EOF
  640. <?php
  641. // autoload_psr4.php @generated by Composer
  642. \$vendorDir = dirname(dirname(__FILE__));
  643. \$baseDir = dirname(\$vendorDir);
  644. return array(
  645. );
  646. EOF;
  647. $expectedClassmap = <<<EOF
  648. <?php
  649. // autoload_classmap.php @generated by Composer
  650. \$vendorDir = dirname(dirname(__FILE__));
  651. \$baseDir = dirname(\$vendorDir);
  652. return array(
  653. 'A\\\\B\\\\C' => \$baseDir . '/lib/A/B/C.php',
  654. 'Foo\\\\Bar' => \$baseDir . '/src/classes.php',
  655. );
  656. EOF;
  657. $this->generator->dump($this->config, $this->repository, $mainPackage, $this->im, 'composer', true, '_9');
  658. $this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php'));
  659. $this->assertEquals($expectedPsr4, file_get_contents($this->vendorDir.'/composer/autoload_psr4.php'));
  660. $this->assertEquals($expectedClassmap, file_get_contents($this->vendorDir.'/composer/autoload_classmap.php'));
  661. }
  662. public function testIncludePathFileGeneration()
  663. {
  664. $package = new Package('a', '1.0', '1.0');
  665. $packages = array();
  666. $a = new Package("a/a", "1.0", "1.0");
  667. $a->setIncludePaths(array("lib/"));
  668. $b = new Package("b/b", "1.0", "1.0");
  669. $b->setIncludePaths(array("library"));
  670. $c = new Package("c", "1.0", "1.0");
  671. $c->setIncludePaths(array("library"));
  672. $packages[] = $a;
  673. $packages[] = $b;
  674. $packages[] = $c;
  675. $this->repository->expects($this->once())
  676. ->method("getCanonicalPackages")
  677. ->will($this->returnValue($packages));
  678. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  679. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_10');
  680. $this->assertFileEquals(__DIR__.'/Fixtures/include_paths.php', $this->vendorDir.'/composer/include_paths.php');
  681. $this->assertEquals(
  682. array(
  683. $this->vendorDir."/a/a/lib",
  684. $this->vendorDir."/b/b/library",
  685. $this->vendorDir."/c/library",
  686. ),
  687. require $this->vendorDir."/composer/include_paths.php"
  688. );
  689. }
  690. public function testIncludePathsArePrependedInAutoloadFile()
  691. {
  692. $package = new Package('a', '1.0', '1.0');
  693. $packages = array();
  694. $a = new Package("a/a", "1.0", "1.0");
  695. $a->setIncludePaths(array("lib/"));
  696. $packages[] = $a;
  697. $this->repository->expects($this->once())
  698. ->method("getCanonicalPackages")
  699. ->will($this->returnValue($packages));
  700. mkdir($this->vendorDir."/composer", 0777, true);
  701. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_11');
  702. $oldIncludePath = get_include_path();
  703. require $this->vendorDir."/autoload.php";
  704. $this->assertEquals(
  705. $this->vendorDir."/a/a/lib".PATH_SEPARATOR.$oldIncludePath,
  706. get_include_path()
  707. );
  708. set_include_path($oldIncludePath);
  709. }
  710. public function testIncludePathsInMainPackage()
  711. {
  712. $package = new Package('a', '1.0', '1.0');
  713. $package->setIncludePaths(array('/lib', '/src'));
  714. $packages = array($a = new Package("a/a", "1.0", "1.0"));
  715. $a->setIncludePaths(array("lib/"));
  716. $this->repository->expects($this->once())
  717. ->method("getCanonicalPackages")
  718. ->will($this->returnValue($packages));
  719. mkdir($this->vendorDir."/composer", 0777, true);
  720. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_12');
  721. $oldIncludePath = get_include_path();
  722. require $this->vendorDir."/autoload.php";
  723. $this->assertEquals(
  724. $this->workingDir."/lib".PATH_SEPARATOR.$this->workingDir."/src".PATH_SEPARATOR.$this->vendorDir."/a/a/lib".PATH_SEPARATOR.$oldIncludePath,
  725. get_include_path()
  726. );
  727. set_include_path($oldIncludePath);
  728. }
  729. public function testIncludePathFileWithoutPathsIsSkipped()
  730. {
  731. $package = new Package('a', '1.0', '1.0');
  732. $packages = array();
  733. $a = new Package("a/a", "1.0", "1.0");
  734. $packages[] = $a;
  735. $this->repository->expects($this->once())
  736. ->method("getCanonicalPackages")
  737. ->will($this->returnValue($packages));
  738. mkdir($this->vendorDir."/composer", 0777, true);
  739. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_12');
  740. $this->assertFalse(file_exists($this->vendorDir."/composer/include_paths.php"));
  741. }
  742. public function testPreAndPostEventsAreDispatchedDuringAutoloadDump()
  743. {
  744. $this->eventDispatcher
  745. ->expects($this->at(0))
  746. ->method('dispatchScript')
  747. ->with(ScriptEvents::PRE_AUTOLOAD_DUMP, false);
  748. $this->eventDispatcher
  749. ->expects($this->at(1))
  750. ->method('dispatchScript')
  751. ->with(ScriptEvents::POST_AUTOLOAD_DUMP, false);
  752. $package = new Package('a', '1.0', '1.0');
  753. $package->setAutoload(array('psr-0' => array('foo/bar/non/existing/')));
  754. $this->repository->expects($this->once())
  755. ->method('getCanonicalPackages')
  756. ->will($this->returnValue(array()));
  757. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_8');
  758. }
  759. public function testUseGlobalIncludePath()
  760. {
  761. $package = new Package('a', '1.0', '1.0');
  762. $package->setAutoload(array(
  763. 'psr-0' => array('Main\\Foo' => '', 'Main\\Bar' => ''),
  764. ));
  765. $package->setTargetDir('Main/Foo/');
  766. $this->repository->expects($this->once())
  767. ->method('getCanonicalPackages')
  768. ->will($this->returnValue(array()));
  769. $this->configValueMap['use-include-path'] = true;
  770. $this->fs->ensureDirectoryExists($this->vendorDir.'/a');
  771. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'IncludePath');
  772. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_include_path.php', $this->vendorDir.'/composer/autoload_real.php');
  773. }
  774. public function testVendorDirExcludedFromWorkingDir()
  775. {
  776. $workingDir = $this->vendorDir.'/working-dir';
  777. $vendorDir = $workingDir.'/../vendor';
  778. $this->fs->ensureDirectoryExists($workingDir);
  779. chdir($workingDir);
  780. $package = new Package('a', '1.0', '1.0');
  781. $package->setAutoload(array(
  782. 'psr-0' => array('Foo' => 'src'),
  783. 'psr-4' => array('Acme\Foo\\' => 'src-psr4'),
  784. 'classmap' => array('classmap'),
  785. 'files' => array('test.php'),
  786. ));
  787. $vendorPackage = new Package('b/b', '1.0', '1.0');
  788. $vendorPackage->setAutoload(array(
  789. 'psr-0' => array('Bar' => 'lib'),
  790. 'psr-4' => array('Acme\Bar\\' => 'lib-psr4'),
  791. 'classmap' => array('classmaps'),
  792. 'files' => array('bootstrap.php'),
  793. ));
  794. $this->repository->expects($this->once())
  795. ->method('getCanonicalPackages')
  796. ->will($this->returnValue(array($vendorPackage)));
  797. $im = $this->getMockBuilder('Composer\Installer\InstallationManager')
  798. ->disableOriginalConstructor()
  799. ->getMock();
  800. $im->expects($this->any())
  801. ->method('getInstallPath')
  802. ->will($this->returnCallback(function ($package) use ($vendorDir) {
  803. $targetDir = $package->getTargetDir();
  804. return $vendorDir.'/'.$package->getName() . ($targetDir ? '/'.$targetDir : '');
  805. }));
  806. $this->fs->ensureDirectoryExists($workingDir.'/src/Foo');
  807. $this->fs->ensureDirectoryExists($workingDir.'/classmap');
  808. $this->fs->ensureDirectoryExists($vendorDir.'/composer');
  809. $this->fs->ensureDirectoryExists($vendorDir.'/b/b/lib/Bar');
  810. $this->fs->ensureDirectoryExists($vendorDir.'/b/b/classmaps');
  811. file_put_contents($workingDir.'/src/Foo/Bar.php', '<?php namespace Foo; class Bar {}');
  812. file_put_contents($workingDir.'/classmap/classes.php', '<?php namespace Foo; class Foo {}');
  813. file_put_contents($workingDir.'/test.php', '<?php class Foo {}');
  814. file_put_contents($vendorDir.'/b/b/lib/Bar/Foo.php', '<?php namespace Bar; class Foo {}');
  815. file_put_contents($vendorDir.'/b/b/classmaps/classes.php', '<?php namespace Bar; class Bar {}');
  816. file_put_contents($vendorDir.'/b/b/bootstrap.php', '<?php class Bar {}');
  817. $oldVendorDir = $this->vendorDir;
  818. $this->vendorDir = $vendorDir;
  819. $this->generator->dump($this->config, $this->repository, $package, $im, 'composer', true, '_13');
  820. $this->vendorDir = $oldVendorDir;
  821. $expectedNamespace = <<<'EOF'
  822. <?php
  823. // autoload_namespaces.php @generated by Composer
  824. $vendorDir = dirname(dirname(__FILE__));
  825. $baseDir = dirname($vendorDir).'/working-dir';
  826. return array(
  827. 'Foo' => array($baseDir . '/src'),
  828. 'Bar' => array($vendorDir . '/b/b/lib'),
  829. );
  830. EOF;
  831. $expectedPsr4 = <<<'EOF'
  832. <?php
  833. // autoload_psr4.php @generated by Composer
  834. $vendorDir = dirname(dirname(__FILE__));
  835. $baseDir = dirname($vendorDir).'/working-dir';
  836. return array(
  837. 'Acme\\Foo\\' => array($baseDir . '/src-psr4'),
  838. 'Acme\\Bar\\' => array($vendorDir . '/b/b/lib-psr4'),
  839. );
  840. EOF;
  841. $expectedClassmap = <<<'EOF'
  842. <?php
  843. // autoload_classmap.php @generated by Composer
  844. $vendorDir = dirname(dirname(__FILE__));
  845. $baseDir = dirname($vendorDir).'/working-dir';
  846. return array(
  847. 'Bar\\Bar' => $vendorDir . '/b/b/classmaps/classes.php',
  848. 'Bar\\Foo' => $vendorDir . '/b/b/lib/Bar/Foo.php',
  849. 'Foo\\Bar' => $baseDir . '/src/Foo/Bar.php',
  850. 'Foo\\Foo' => $baseDir . '/classmap/classes.php',
  851. );
  852. EOF;
  853. $this->assertEquals($expectedNamespace, file_get_contents($vendorDir.'/composer/autoload_namespaces.php'));
  854. $this->assertEquals($expectedPsr4, file_get_contents($vendorDir.'/composer/autoload_psr4.php'));
  855. $this->assertEquals($expectedClassmap, file_get_contents($vendorDir.'/composer/autoload_classmap.php'));
  856. $this->assertContains("\n \$vendorDir . '/b/b/bootstrap.php',\n", file_get_contents($vendorDir.'/composer/autoload_files.php'));
  857. $this->assertContains("\n \$baseDir . '/test.php',\n", file_get_contents($vendorDir.'/composer/autoload_files.php'));
  858. }
  859. public function testUpLevelRelativePaths()
  860. {
  861. $workingDir = $this->workingDir.'/working-dir';
  862. mkdir($workingDir, 0777, true);
  863. chdir($workingDir);
  864. $package = new Package('a', '1.0', '1.0');
  865. $package->setAutoload(array(
  866. 'psr-0' => array('Foo' => '../path/../src'),
  867. 'psr-4' => array('Acme\Foo\\' => '../path/../src-psr4'),
  868. 'classmap' => array('../classmap'),
  869. 'files' => array('../test.php'),
  870. 'exclude-from-classmap' => array('./../classmap/excluded'),
  871. ));
  872. $this->repository->expects($this->once())
  873. ->method('getCanonicalPackages')
  874. ->will($this->returnValue(array()));
  875. $this->fs->ensureDirectoryExists($this->workingDir.'/src/Foo');
  876. $this->fs->ensureDirectoryExists($this->workingDir.'/classmap/excluded');
  877. file_put_contents($this->workingDir.'/src/Foo/Bar.php', '<?php namespace Foo; class Bar {}');
  878. file_put_contents($this->workingDir.'/classmap/classes.php', '<?php namespace Foo; class Foo {}');
  879. file_put_contents($this->workingDir.'/classmap/excluded/classes.php', '<?php namespace Foo; class Boo {}');
  880. file_put_contents($this->workingDir.'/test.php', '<?php class Foo {}');
  881. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_14');
  882. $expectedNamespace = <<<'EOF'
  883. <?php
  884. // autoload_namespaces.php @generated by Composer
  885. $vendorDir = dirname(dirname(__FILE__));
  886. $baseDir = dirname($vendorDir).'/working-dir';
  887. return array(
  888. 'Foo' => array($baseDir . '/../src'),
  889. );
  890. EOF;
  891. $expectedPsr4 = <<<'EOF'
  892. <?php
  893. // autoload_psr4.php @generated by Composer
  894. $vendorDir = dirname(dirname(__FILE__));
  895. $baseDir = dirname($vendorDir).'/working-dir';
  896. return array(
  897. 'Acme\\Foo\\' => array($baseDir . '/../src-psr4'),
  898. );
  899. EOF;
  900. $expectedClassmap = <<<'EOF'
  901. <?php
  902. // autoload_classmap.php @generated by Composer
  903. $vendorDir = dirname(dirname(__FILE__));
  904. $baseDir = dirname($vendorDir).'/working-dir';
  905. return array(
  906. 'Foo\\Bar' => $baseDir . '/../src/Foo/Bar.php',
  907. 'Foo\\Foo' => $baseDir . '/../classmap/classes.php',
  908. );
  909. EOF;
  910. $this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php'));
  911. $this->assertEquals($expectedPsr4, file_get_contents($this->vendorDir.'/composer/autoload_psr4.php'));
  912. $this->assertEquals($expectedClassmap, file_get_contents($this->vendorDir.'/composer/autoload_classmap.php'));
  913. $this->assertContains("\n \$baseDir . '/../test.php',\n", file_get_contents($this->vendorDir.'/composer/autoload_files.php'));
  914. }
  915. public function testEmptyPaths()
  916. {
  917. $package = new Package('a', '1.0', '1.0');
  918. $package->setAutoload(array(
  919. 'psr-0' => array('Foo' => ''),
  920. 'psr-4' => array('Acme\Foo\\' => ''),
  921. 'classmap' => array(''),
  922. ));
  923. $this->repository->expects($this->once())
  924. ->method('getCanonicalPackages')
  925. ->will($this->returnValue(array()));
  926. $this->fs->ensureDirectoryExists($this->workingDir.'/Foo');
  927. file_put_contents($this->workingDir.'/Foo/Bar.php', '<?php namespace Foo; class Bar {}');
  928. file_put_contents($this->workingDir.'/class.php', '<?php namespace Classmap; class Foo {}');
  929. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_15');
  930. $expectedNamespace = <<<'EOF'
  931. <?php
  932. // autoload_namespaces.php @generated by Composer
  933. $vendorDir = dirname(dirname(__FILE__));
  934. $baseDir = dirname($vendorDir);
  935. return array(
  936. 'Foo' => array($baseDir . '/'),
  937. );
  938. EOF;
  939. $expectedPsr4 = <<<'EOF'
  940. <?php
  941. // autoload_psr4.php @generated by Composer
  942. $vendorDir = dirname(dirname(__FILE__));
  943. $baseDir = dirname($vendorDir);
  944. return array(
  945. 'Acme\\Foo\\' => array($baseDir . '/'),
  946. );
  947. EOF;
  948. $expectedClassmap = <<<'EOF'
  949. <?php
  950. // autoload_classmap.php @generated by Composer
  951. $vendorDir = dirname(dirname(__FILE__));
  952. $baseDir = dirname($vendorDir);
  953. return array(
  954. 'Classmap\\Foo' => $baseDir . '/class.php',
  955. 'Foo\\Bar' => $baseDir . '/Foo/Bar.php',
  956. );
  957. EOF;
  958. $this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php'));
  959. $this->assertEquals($expectedPsr4, file_get_contents($this->vendorDir.'/composer/autoload_psr4.php'));
  960. $this->assertEquals($expectedClassmap, file_get_contents($this->vendorDir.'/composer/autoload_classmap.php'));
  961. }
  962. public function testVendorSubstringPath()
  963. {
  964. $package = new Package('a', '1.0', '1.0');
  965. $package->setAutoload(array(
  966. 'psr-0' => array('Foo' => 'composer-test-autoload-src/src'),
  967. 'psr-4' => array('Acme\Foo\\' => 'composer-test-autoload-src/src-psr4'),
  968. ));
  969. $this->repository->expects($this->once())
  970. ->method('getCanonicalPackages')
  971. ->will($this->returnValue(array()));
  972. $this->fs->ensureDirectoryExists($this->vendorDir.'/a');
  973. $expectedNamespace = <<<'EOF'
  974. <?php
  975. // autoload_namespaces.php @generated by Composer
  976. $vendorDir = dirname(dirname(__FILE__));
  977. $baseDir = dirname($vendorDir);
  978. return array(
  979. 'Foo' => array($baseDir . '/composer-test-autoload-src/src'),
  980. );
  981. EOF;
  982. $expectedPsr4 = <<<'EOF'
  983. <?php
  984. // autoload_psr4.php @generated by Composer
  985. $vendorDir = dirname(dirname(__FILE__));
  986. $baseDir = dirname($vendorDir);
  987. return array(
  988. 'Acme\\Foo\\' => array($baseDir . '/composer-test-autoload-src/src-psr4'),
  989. );
  990. EOF;
  991. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'VendorSubstring');
  992. $this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php'));
  993. $this->assertEquals($expectedPsr4, file_get_contents($this->vendorDir.'/composer/autoload_psr4.php'));
  994. }
  995. public function testExcludeFromClassmap()
  996. {
  997. $package = new Package('a', '1.0', '1.0');
  998. $package->setAutoload(array(
  999. 'psr-0' => array(
  1000. 'Main' => 'src/',
  1001. 'Lala' => array('src/', 'lib/'),
  1002. ),
  1003. 'psr-4' => array(
  1004. 'Acme\Fruit\\' => 'src-fruit/',
  1005. 'Acme\Cake\\' => array('src-cake/', 'lib-cake/'),
  1006. ),
  1007. 'classmap' => array('composersrc/'),
  1008. 'exclude-from-classmap' => array(
  1009. '/composersrc/excludedTests/',
  1010. '/composersrc/ClassToExclude.php',
  1011. '/composersrc/*/excluded/excsubpath',
  1012. '**/excsubpath',
  1013. ),
  1014. ));
  1015. $this->repository->expects($this->once())
  1016. ->method('getCanonicalPackages')
  1017. ->will($this->returnValue(array()));
  1018. $this->fs->ensureDirectoryExists($this->workingDir.'/composer');
  1019. $this->fs->ensureDirectoryExists($this->workingDir.'/src/Lala/Test');
  1020. $this->fs->ensureDirectoryExists($this->workingDir.'/lib');
  1021. file_put_contents($this->workingDir.'/src/Lala/ClassMapMain.php', '<?php namespace Lala; class ClassMapMain {}');
  1022. file_put_contents($this->workingDir.'/src/Lala/Test/ClassMapMainTest.php', '<?php namespace Lala\Test; class ClassMapMainTest {}');
  1023. $this->fs->ensureDirectoryExists($this->workingDir.'/src-fruit');
  1024. $this->fs->ensureDirectoryExists($this->workingDir.'/src-cake');
  1025. $this->fs->ensureDirectoryExists($this->workingDir.'/lib-cake');
  1026. file_put_contents($this->workingDir.'/src-cake/ClassMapBar.php', '<?php namespace Acme\Cake; class ClassMapBar {}');
  1027. $this->fs->ensureDirectoryExists($this->workingDir.'/composersrc');
  1028. $this->fs->ensureDirectoryExists($this->workingDir.'/composersrc/tests');
  1029. file_put_contents($this->workingDir.'/composersrc/foo.php', '<?php class ClassMapFoo {}');
  1030. // this classes should not be found in the classmap
  1031. $this->fs->ensureDirectoryExists($this->workingDir.'/composersrc/excludedTests');
  1032. file_put_contents($this->workingDir.'/composersrc/excludedTests/bar.php', '<?php class ClassExcludeMapFoo {}');
  1033. file_put_contents($this->workingDir.'/composersrc/ClassToExclude.php', '<?php class ClassClassToExclude {}');
  1034. $this->fs->ensureDirectoryExists($this->workingDir.'/composersrc/long/excluded/excsubpath');
  1035. file_put_contents($this->workingDir.'/composersrc/long/excluded/excsubpath/foo.php', '<?php class ClassExcludeMapFoo2 {}');
  1036. file_put_contents($this->workingDir.'/composersrc/long/excluded/excsubpath/bar.php', '<?php class ClassExcludeMapBar {}');
  1037. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_1');
  1038. // Assert that autoload_classmap.php was correctly generated.
  1039. $this->assertAutoloadFiles('classmap', $this->vendorDir.'/composer', 'classmap');
  1040. }
  1041. private function assertAutoloadFiles($name, $dir, $type = 'namespaces')
  1042. {
  1043. $a = __DIR__.'/Fixtures/autoload_'.$name.'.php';
  1044. $b = $dir.'/autoload_'.$type.'.php';
  1045. $this->assertFileEquals($a, $b);
  1046. }
  1047. public static function assertFileEquals($expected, $actual, $message = '', $canonicalize = false, $ignoreCase = false)
  1048. {
  1049. return self::assertEquals(
  1050. file_get_contents($expected),
  1051. file_get_contents($actual),
  1052. $message ?: $expected.' equals '.$actual,
  1053. 0,
  1054. 10,
  1055. $canonicalize,
  1056. $ignoreCase
  1057. );
  1058. }
  1059. public static function assertEquals($expected, $actual, $message = '', $delta = 0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false)
  1060. {
  1061. return parent::assertEquals(str_replace("\r", '', $expected), str_replace("\r", '', $actual), $message, $delta, $maxDepth, $canonicalize, $ignoreCase);
  1062. }
  1063. }