AutoloadGeneratorTest.php 63 KB

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