AutoloadGeneratorTest.php 69 KB

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