AutoloadGeneratorTest.php 57 KB

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