AutoloadGeneratorTest.php 56 KB

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