AutoloadGeneratorTest.php 45 KB

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