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