AutoloadGeneratorTest.php 45 KB

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