AutoloadGeneratorTest.php 58 KB

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