AutoloadGeneratorTest.php 57 KB

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