AutoloadGeneratorTest.php 66 KB

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