AutoloadGeneratorTest.php 48 KB

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