AutoloadGeneratorTest.php 48 KB

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