AutoloadGeneratorTest.php 66 KB

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