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->assertNotContains('$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->assertContains('$loader->setClassMapAuthoritative(true);', file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
  450. }
  451. public function testFilesAutoloadGeneration()
  452. {
  453. $package = new Package('a', '1.0', '1.0');
  454. $package->setAutoload(array('files' => array('root.php')));
  455. $packages = array();
  456. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  457. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  458. $packages[] = $c = new Package('c/c', '1.0', '1.0');
  459. $a->setAutoload(array('files' => array('test.php')));
  460. $b->setAutoload(array('files' => array('test2.php')));
  461. $c->setAutoload(array('files' => array('test3.php', 'foo/bar/test4.php')));
  462. $c->setTargetDir('foo/bar');
  463. $this->repository->expects($this->once())
  464. ->method('getCanonicalPackages')
  465. ->will($this->returnValue($packages));
  466. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a');
  467. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b');
  468. $this->fs->ensureDirectoryExists($this->vendorDir.'/c/c/foo/bar');
  469. file_put_contents($this->vendorDir.'/a/a/test.php', '<?php function testFilesAutoloadGeneration1() {}');
  470. file_put_contents($this->vendorDir.'/b/b/test2.php', '<?php function testFilesAutoloadGeneration2() {}');
  471. file_put_contents($this->vendorDir.'/c/c/foo/bar/test3.php', '<?php function testFilesAutoloadGeneration3() {}');
  472. file_put_contents($this->vendorDir.'/c/c/foo/bar/test4.php', '<?php function testFilesAutoloadGeneration4() {}');
  473. file_put_contents($this->workingDir.'/root.php', '<?php function testFilesAutoloadGenerationRoot() {}');
  474. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'FilesAutoload');
  475. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php');
  476. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_functions.php', $this->vendorDir.'/composer/autoload_real.php');
  477. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_files_functions.php', $this->vendorDir.'/composer/autoload_files.php');
  478. include $this->vendorDir . '/autoload.php';
  479. $this->assertTrue(function_exists('testFilesAutoloadGeneration1'));
  480. $this->assertTrue(function_exists('testFilesAutoloadGeneration2'));
  481. $this->assertTrue(function_exists('testFilesAutoloadGeneration3'));
  482. $this->assertTrue(function_exists('testFilesAutoloadGeneration4'));
  483. $this->assertTrue(function_exists('testFilesAutoloadGenerationRoot'));
  484. }
  485. public function testFilesAutoloadOrderByDependencies()
  486. {
  487. $package = new Package('a', '1.0', '1.0');
  488. $package->setAutoload(array('files' => array('root.php')));
  489. $package->setRequires(array(new Link('a', 'z/foo')));
  490. $package->setRequires(array(new Link('a', 'd/d')));
  491. $package->setRequires(array(new Link('a', 'e/e')));
  492. $packages = array();
  493. $packages[] = $z = new Package('z/foo', '1.0', '1.0');
  494. $packages[] = $b = new Package('b/bar', '1.0', '1.0');
  495. $packages[] = $d = new Package('d/d', '1.0', '1.0');
  496. $packages[] = $c = new Package('c/lorem', '1.0', '1.0');
  497. $packages[] = $e = new Package('e/e', '1.0', '1.0');
  498. $z->setAutoload(array('files' => array('testA.php')));
  499. $z->setRequires(array(new Link('z/foo', 'c/lorem')));
  500. $b->setAutoload(array('files' => array('testB.php')));
  501. $b->setRequires(array(new Link('b/bar', 'c/lorem'), new Link('b/bar', 'd/d')));
  502. $c->setAutoload(array('files' => array('testC.php')));
  503. $d->setAutoload(array('files' => array('testD.php')));
  504. $d->setRequires(array(new Link('d/d', 'c/lorem')));
  505. $e->setAutoload(array('files' => array('testE.php')));
  506. $e->setRequires(array(new Link('e/e', 'c/lorem')));
  507. $this->repository->expects($this->once())
  508. ->method('getCanonicalPackages')
  509. ->will($this->returnValue($packages));
  510. $this->fs->ensureDirectoryExists($this->vendorDir . '/z/foo');
  511. $this->fs->ensureDirectoryExists($this->vendorDir . '/b/bar');
  512. $this->fs->ensureDirectoryExists($this->vendorDir . '/c/lorem');
  513. $this->fs->ensureDirectoryExists($this->vendorDir . '/d/d');
  514. $this->fs->ensureDirectoryExists($this->vendorDir . '/e/e');
  515. file_put_contents($this->vendorDir . '/z/foo/testA.php', '<?php function testFilesAutoloadOrderByDependency1() {}');
  516. file_put_contents($this->vendorDir . '/b/bar/testB.php', '<?php function testFilesAutoloadOrderByDependency2() {}');
  517. file_put_contents($this->vendorDir . '/c/lorem/testC.php', '<?php function testFilesAutoloadOrderByDependency3() {}');
  518. file_put_contents($this->vendorDir . '/d/d/testD.php', '<?php function testFilesAutoloadOrderByDependency4() {}');
  519. file_put_contents($this->vendorDir . '/e/e/testE.php', '<?php function testFilesAutoloadOrderByDependency5() {}');
  520. file_put_contents($this->workingDir . '/root.php', '<?php function testFilesAutoloadOrderByDependencyRoot() {}');
  521. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'FilesAutoloadOrder');
  522. $this->assertFileEquals(__DIR__ . '/Fixtures/autoload_functions_by_dependency.php', $this->vendorDir . '/autoload.php');
  523. $this->assertFileEquals(__DIR__ . '/Fixtures/autoload_real_files_by_dependency.php', $this->vendorDir . '/composer/autoload_real.php');
  524. require $this->vendorDir . '/autoload.php';
  525. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency1'));
  526. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency2'));
  527. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency3'));
  528. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency4'));
  529. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency5'));
  530. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependencyRoot'));
  531. }
  532. /**
  533. * Test that PSR-0 and PSR-4 mappings are processed in the correct order for
  534. * autoloading and for classmap generation:
  535. * - The main package has priority over other packages.
  536. * - Longer namespaces have priority over shorter namespaces.
  537. */
  538. public function testOverrideVendorsAutoloading()
  539. {
  540. $mainPackage = new Package('z', '1.0', '1.0');
  541. $mainPackage->setAutoload(array(
  542. 'psr-0' => array('A\\B' => $this->workingDir.'/lib'),
  543. 'classmap' => array($this->workingDir.'/src')
  544. ));
  545. $mainPackage->setRequires(array(new Link('z', 'a/a')));
  546. $packages = array();
  547. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  548. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  549. $a->setAutoload(array(
  550. 'psr-0' => array('A' => 'src/', 'A\\B' => 'lib/'),
  551. 'classmap' => array('classmap'),
  552. ));
  553. $b->setAutoload(array(
  554. 'psr-0' => array('B\\Sub\\Name' => 'src/'),
  555. ));
  556. $this->repository->expects($this->once())
  557. ->method('getCanonicalPackages')
  558. ->will($this->returnValue($packages));
  559. $this->fs->ensureDirectoryExists($this->workingDir.'/lib/A/B');
  560. $this->fs->ensureDirectoryExists($this->workingDir.'/src/');
  561. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  562. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/classmap');
  563. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  564. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib/A/B');
  565. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  566. // Define the classes A\B\C and Foo\Bar in the main package.
  567. file_put_contents($this->workingDir.'/lib/A/B/C.php', '<?php namespace A\\B; class C {}');
  568. file_put_contents($this->workingDir.'/src/classes.php', '<?php namespace Foo; class Bar {}');
  569. // Define the same two classes in the package a/a.
  570. file_put_contents($this->vendorDir.'/a/a/lib/A/B/C.php', '<?php namespace A\\B; class C {}');
  571. file_put_contents($this->vendorDir.'/a/a/classmap/classes.php', '<?php namespace Foo; class Bar {}');
  572. $expectedNamespace = <<<EOF
  573. <?php
  574. // autoload_namespaces.php @generated by Composer
  575. \$vendorDir = dirname(dirname(__FILE__));
  576. \$baseDir = dirname(\$vendorDir);
  577. return array(
  578. 'B\\\\Sub\\\\Name' => array(\$vendorDir . '/b/b/src'),
  579. 'A\\\\B' => array(\$baseDir . '/lib', \$vendorDir . '/a/a/lib'),
  580. 'A' => array(\$vendorDir . '/a/a/src'),
  581. );
  582. EOF;
  583. // autoload_psr4.php is expected to be empty in this example.
  584. $expectedPsr4 = <<<EOF
  585. <?php
  586. // autoload_psr4.php @generated by Composer
  587. \$vendorDir = dirname(dirname(__FILE__));
  588. \$baseDir = dirname(\$vendorDir);
  589. return array(
  590. );
  591. EOF;
  592. $expectedClassmap = <<<EOF
  593. <?php
  594. // autoload_classmap.php @generated by Composer
  595. \$vendorDir = dirname(dirname(__FILE__));
  596. \$baseDir = dirname(\$vendorDir);
  597. return array(
  598. 'A\\\\B\\\\C' => \$baseDir . '/lib/A/B/C.php',
  599. 'Foo\\\\Bar' => \$baseDir . '/src/classes.php',
  600. );
  601. EOF;
  602. $this->generator->dump($this->config, $this->repository, $mainPackage, $this->im, 'composer', true, '_9');
  603. $this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php'));
  604. $this->assertEquals($expectedPsr4, file_get_contents($this->vendorDir.'/composer/autoload_psr4.php'));
  605. $this->assertEquals($expectedClassmap, file_get_contents($this->vendorDir.'/composer/autoload_classmap.php'));
  606. }
  607. public function testIncludePathFileGeneration()
  608. {
  609. $package = new Package('a', '1.0', '1.0');
  610. $packages = array();
  611. $a = new Package("a/a", "1.0", "1.0");
  612. $a->setIncludePaths(array("lib/"));
  613. $b = new Package("b/b", "1.0", "1.0");
  614. $b->setIncludePaths(array("library"));
  615. $c = new Package("c", "1.0", "1.0");
  616. $c->setIncludePaths(array("library"));
  617. $packages[] = $a;
  618. $packages[] = $b;
  619. $packages[] = $c;
  620. $this->repository->expects($this->once())
  621. ->method("getCanonicalPackages")
  622. ->will($this->returnValue($packages));
  623. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  624. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_10');
  625. $this->assertFileEquals(__DIR__.'/Fixtures/include_paths.php', $this->vendorDir.'/composer/include_paths.php');
  626. $this->assertEquals(
  627. array(
  628. $this->vendorDir."/a/a/lib",
  629. $this->vendorDir."/b/b/library",
  630. $this->vendorDir."/c/library",
  631. ),
  632. require $this->vendorDir."/composer/include_paths.php"
  633. );
  634. }
  635. public function testIncludePathsArePrependedInAutoloadFile()
  636. {
  637. $package = new Package('a', '1.0', '1.0');
  638. $packages = array();
  639. $a = new Package("a/a", "1.0", "1.0");
  640. $a->setIncludePaths(array("lib/"));
  641. $packages[] = $a;
  642. $this->repository->expects($this->once())
  643. ->method("getCanonicalPackages")
  644. ->will($this->returnValue($packages));
  645. mkdir($this->vendorDir."/composer", 0777, true);
  646. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_11');
  647. $oldIncludePath = get_include_path();
  648. require $this->vendorDir."/autoload.php";
  649. $this->assertEquals(
  650. $this->vendorDir."/a/a/lib".PATH_SEPARATOR.$oldIncludePath,
  651. get_include_path()
  652. );
  653. set_include_path($oldIncludePath);
  654. }
  655. public function testIncludePathsInMainPackage()
  656. {
  657. $package = new Package('a', '1.0', '1.0');
  658. $package->setIncludePaths(array('/lib', '/src'));
  659. $packages = array($a = new Package("a/a", "1.0", "1.0"));
  660. $a->setIncludePaths(array("lib/"));
  661. $this->repository->expects($this->once())
  662. ->method("getCanonicalPackages")
  663. ->will($this->returnValue($packages));
  664. mkdir($this->vendorDir."/composer", 0777, true);
  665. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_12');
  666. $oldIncludePath = get_include_path();
  667. require $this->vendorDir."/autoload.php";
  668. $this->assertEquals(
  669. $this->workingDir."/lib".PATH_SEPARATOR.$this->workingDir."/src".PATH_SEPARATOR.$this->vendorDir."/a/a/lib".PATH_SEPARATOR.$oldIncludePath,
  670. get_include_path()
  671. );
  672. set_include_path($oldIncludePath);
  673. }
  674. public function testIncludePathFileWithoutPathsIsSkipped()
  675. {
  676. $package = new Package('a', '1.0', '1.0');
  677. $packages = array();
  678. $a = new Package("a/a", "1.0", "1.0");
  679. $packages[] = $a;
  680. $this->repository->expects($this->once())
  681. ->method("getCanonicalPackages")
  682. ->will($this->returnValue($packages));
  683. mkdir($this->vendorDir."/composer", 0777, true);
  684. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_12');
  685. $this->assertFalse(file_exists($this->vendorDir."/composer/include_paths.php"));
  686. }
  687. public function testPreAndPostEventsAreDispatchedDuringAutoloadDump()
  688. {
  689. $this->eventDispatcher
  690. ->expects($this->at(0))
  691. ->method('dispatchScript')
  692. ->with(ScriptEvents::PRE_AUTOLOAD_DUMP, false);
  693. $this->eventDispatcher
  694. ->expects($this->at(1))
  695. ->method('dispatchScript')
  696. ->with(ScriptEvents::POST_AUTOLOAD_DUMP, false);
  697. $package = new Package('a', '1.0', '1.0');
  698. $package->setAutoload(array('psr-0' => array('foo/bar/non/existing/')));
  699. $this->repository->expects($this->once())
  700. ->method('getCanonicalPackages')
  701. ->will($this->returnValue(array()));
  702. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_8');
  703. }
  704. public function testUseGlobalIncludePath()
  705. {
  706. $package = new Package('a', '1.0', '1.0');
  707. $package->setAutoload(array(
  708. 'psr-0' => array('Main\\Foo' => '', 'Main\\Bar' => ''),
  709. ));
  710. $package->setTargetDir('Main/Foo/');
  711. $this->repository->expects($this->once())
  712. ->method('getCanonicalPackages')
  713. ->will($this->returnValue(array()));
  714. $this->configValueMap['use-include-path'] = true;
  715. $this->fs->ensureDirectoryExists($this->vendorDir.'/a');
  716. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'IncludePath');
  717. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_include_path.php', $this->vendorDir.'/composer/autoload_real.php');
  718. }
  719. public function testVendorDirExcludedFromWorkingDir()
  720. {
  721. $workingDir = $this->vendorDir.'/working-dir';
  722. $vendorDir = $workingDir.'/../vendor';
  723. $this->fs->ensureDirectoryExists($workingDir);
  724. chdir($workingDir);
  725. $package = new Package('a', '1.0', '1.0');
  726. $package->setAutoload(array(
  727. 'psr-0' => array('Foo' => 'src'),
  728. 'psr-4' => array('Acme\Foo\\' => 'src-psr4'),
  729. 'classmap' => array('classmap'),
  730. 'files' => array('test.php'),
  731. ));
  732. $vendorPackage = new Package('b/b', '1.0', '1.0');
  733. $vendorPackage->setAutoload(array(
  734. 'psr-0' => array('Bar' => 'lib'),
  735. 'psr-4' => array('Acme\Bar\\' => 'lib-psr4'),
  736. 'classmap' => array('classmaps'),
  737. 'files' => array('bootstrap.php'),
  738. ));
  739. $this->repository->expects($this->once())
  740. ->method('getCanonicalPackages')
  741. ->will($this->returnValue(array($vendorPackage)));
  742. $im = $this->getMockBuilder('Composer\Installer\InstallationManager')
  743. ->disableOriginalConstructor()
  744. ->getMock();
  745. $im->expects($this->any())
  746. ->method('getInstallPath')
  747. ->will($this->returnCallback(function ($package) use ($vendorDir) {
  748. $targetDir = $package->getTargetDir();
  749. return $vendorDir.'/'.$package->getName() . ($targetDir ? '/'.$targetDir : '');
  750. }));
  751. $this->fs->ensureDirectoryExists($workingDir.'/src/Foo');
  752. $this->fs->ensureDirectoryExists($workingDir.'/classmap');
  753. $this->fs->ensureDirectoryExists($vendorDir.'/composer');
  754. $this->fs->ensureDirectoryExists($vendorDir.'/b/b/lib/Bar');
  755. $this->fs->ensureDirectoryExists($vendorDir.'/b/b/classmaps');
  756. file_put_contents($workingDir.'/src/Foo/Bar.php', '<?php namespace Foo; class Bar {}');
  757. file_put_contents($workingDir.'/classmap/classes.php', '<?php namespace Foo; class Foo {}');
  758. file_put_contents($workingDir.'/test.php', '<?php class Foo {}');
  759. file_put_contents($vendorDir.'/b/b/lib/Bar/Foo.php', '<?php namespace Bar; class Foo {}');
  760. file_put_contents($vendorDir.'/b/b/classmaps/classes.php', '<?php namespace Bar; class Bar {}');
  761. file_put_contents($vendorDir.'/b/b/bootstrap.php', '<?php class Bar {}');
  762. $oldVendorDir = $this->vendorDir;
  763. $this->vendorDir = $vendorDir;
  764. $this->generator->dump($this->config, $this->repository, $package, $im, 'composer', true, '_13');
  765. $this->vendorDir = $oldVendorDir;
  766. $expectedNamespace = <<<'EOF'
  767. <?php
  768. // autoload_namespaces.php @generated by Composer
  769. $vendorDir = dirname(dirname(__FILE__));
  770. $baseDir = dirname($vendorDir).'/working-dir';
  771. return array(
  772. 'Foo' => array($baseDir . '/src'),
  773. 'Bar' => array($vendorDir . '/b/b/lib'),
  774. );
  775. EOF;
  776. $expectedPsr4 = <<<'EOF'
  777. <?php
  778. // autoload_psr4.php @generated by Composer
  779. $vendorDir = dirname(dirname(__FILE__));
  780. $baseDir = dirname($vendorDir).'/working-dir';
  781. return array(
  782. 'Acme\\Foo\\' => array($baseDir . '/src-psr4'),
  783. 'Acme\\Bar\\' => array($vendorDir . '/b/b/lib-psr4'),
  784. );
  785. EOF;
  786. $expectedClassmap = <<<'EOF'
  787. <?php
  788. // autoload_classmap.php @generated by Composer
  789. $vendorDir = dirname(dirname(__FILE__));
  790. $baseDir = dirname($vendorDir).'/working-dir';
  791. return array(
  792. 'Bar\\Bar' => $vendorDir . '/b/b/classmaps/classes.php',
  793. 'Bar\\Foo' => $vendorDir . '/b/b/lib/Bar/Foo.php',
  794. 'Foo\\Bar' => $baseDir . '/src/Foo/Bar.php',
  795. 'Foo\\Foo' => $baseDir . '/classmap/classes.php',
  796. );
  797. EOF;
  798. $this->assertEquals($expectedNamespace, file_get_contents($vendorDir.'/composer/autoload_namespaces.php'));
  799. $this->assertEquals($expectedPsr4, file_get_contents($vendorDir.'/composer/autoload_psr4.php'));
  800. $this->assertEquals($expectedClassmap, file_get_contents($vendorDir.'/composer/autoload_classmap.php'));
  801. $this->assertContains("\n \$vendorDir . '/b/b/bootstrap.php',\n", file_get_contents($vendorDir.'/composer/autoload_files.php'));
  802. $this->assertContains("\n \$baseDir . '/test.php',\n", file_get_contents($vendorDir.'/composer/autoload_files.php'));
  803. }
  804. public function testUpLevelRelativePaths()
  805. {
  806. $workingDir = $this->workingDir.'/working-dir';
  807. mkdir($workingDir, 0777, true);
  808. chdir($workingDir);
  809. $package = new Package('a', '1.0', '1.0');
  810. $package->setAutoload(array(
  811. 'psr-0' => array('Foo' => '../path/../src'),
  812. 'psr-4' => array('Acme\Foo\\' => '../path/../src-psr4'),
  813. 'classmap' => array('../classmap'),
  814. 'files' => array('../test.php'),
  815. ));
  816. $this->repository->expects($this->once())
  817. ->method('getCanonicalPackages')
  818. ->will($this->returnValue(array()));
  819. $this->fs->ensureDirectoryExists($this->workingDir.'/src/Foo');
  820. $this->fs->ensureDirectoryExists($this->workingDir.'/classmap');
  821. file_put_contents($this->workingDir.'/src/Foo/Bar.php', '<?php namespace Foo; class Bar {}');
  822. file_put_contents($this->workingDir.'/classmap/classes.php', '<?php namespace Foo; class Foo {}');
  823. file_put_contents($this->workingDir.'/test.php', '<?php class Foo {}');
  824. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_14');
  825. $expectedNamespace = <<<'EOF'
  826. <?php
  827. // autoload_namespaces.php @generated by Composer
  828. $vendorDir = dirname(dirname(__FILE__));
  829. $baseDir = dirname($vendorDir).'/working-dir';
  830. return array(
  831. 'Foo' => array($baseDir . '/../src'),
  832. );
  833. EOF;
  834. $expectedPsr4 = <<<'EOF'
  835. <?php
  836. // autoload_psr4.php @generated by Composer
  837. $vendorDir = dirname(dirname(__FILE__));
  838. $baseDir = dirname($vendorDir).'/working-dir';
  839. return array(
  840. 'Acme\\Foo\\' => array($baseDir . '/../src-psr4'),
  841. );
  842. EOF;
  843. $expectedClassmap = <<<'EOF'
  844. <?php
  845. // autoload_classmap.php @generated by Composer
  846. $vendorDir = dirname(dirname(__FILE__));
  847. $baseDir = dirname($vendorDir).'/working-dir';
  848. return array(
  849. 'Foo\\Bar' => $baseDir . '/../src/Foo/Bar.php',
  850. 'Foo\\Foo' => $baseDir . '/../classmap/classes.php',
  851. );
  852. EOF;
  853. $this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php'));
  854. $this->assertEquals($expectedPsr4, file_get_contents($this->vendorDir.'/composer/autoload_psr4.php'));
  855. $this->assertEquals($expectedClassmap, file_get_contents($this->vendorDir.'/composer/autoload_classmap.php'));
  856. $this->assertContains("\n \$baseDir . '/../test.php',\n", file_get_contents($this->vendorDir.'/composer/autoload_files.php'));
  857. }
  858. public function testEmptyPaths()
  859. {
  860. $package = new Package('a', '1.0', '1.0');
  861. $package->setAutoload(array(
  862. 'psr-0' => array('Foo' => ''),
  863. 'psr-4' => array('Acme\Foo\\' => ''),
  864. 'classmap' => array(''),
  865. ));
  866. $this->repository->expects($this->once())
  867. ->method('getCanonicalPackages')
  868. ->will($this->returnValue(array()));
  869. $this->fs->ensureDirectoryExists($this->workingDir.'/Foo');
  870. file_put_contents($this->workingDir.'/Foo/Bar.php', '<?php namespace Foo; class Bar {}');
  871. file_put_contents($this->workingDir.'/class.php', '<?php namespace Classmap; class Foo {}');
  872. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_15');
  873. $expectedNamespace = <<<'EOF'
  874. <?php
  875. // autoload_namespaces.php @generated by Composer
  876. $vendorDir = dirname(dirname(__FILE__));
  877. $baseDir = dirname($vendorDir);
  878. return array(
  879. 'Foo' => array($baseDir . '/'),
  880. );
  881. EOF;
  882. $expectedPsr4 = <<<'EOF'
  883. <?php
  884. // autoload_psr4.php @generated by Composer
  885. $vendorDir = dirname(dirname(__FILE__));
  886. $baseDir = dirname($vendorDir);
  887. return array(
  888. 'Acme\\Foo\\' => array($baseDir . '/'),
  889. );
  890. EOF;
  891. $expectedClassmap = <<<'EOF'
  892. <?php
  893. // autoload_classmap.php @generated by Composer
  894. $vendorDir = dirname(dirname(__FILE__));
  895. $baseDir = dirname($vendorDir);
  896. return array(
  897. 'Classmap\\Foo' => $baseDir . '/class.php',
  898. 'Foo\\Bar' => $baseDir . '/Foo/Bar.php',
  899. );
  900. EOF;
  901. $this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php'));
  902. $this->assertEquals($expectedPsr4, file_get_contents($this->vendorDir.'/composer/autoload_psr4.php'));
  903. $this->assertEquals($expectedClassmap, file_get_contents($this->vendorDir.'/composer/autoload_classmap.php'));
  904. }
  905. public function testVendorSubstringPath()
  906. {
  907. $package = new Package('a', '1.0', '1.0');
  908. $package->setAutoload(array(
  909. 'psr-0' => array('Foo' => 'composer-test-autoload-src/src'),
  910. 'psr-4' => array('Acme\Foo\\' => 'composer-test-autoload-src/src-psr4'),
  911. ));
  912. $this->repository->expects($this->once())
  913. ->method('getCanonicalPackages')
  914. ->will($this->returnValue(array()));
  915. $this->fs->ensureDirectoryExists($this->vendorDir.'/a');
  916. $expectedNamespace = <<<'EOF'
  917. <?php
  918. // autoload_namespaces.php @generated by Composer
  919. $vendorDir = dirname(dirname(__FILE__));
  920. $baseDir = dirname($vendorDir);
  921. return array(
  922. 'Foo' => array($baseDir . '/composer-test-autoload-src/src'),
  923. );
  924. EOF;
  925. $expectedPsr4 = <<<'EOF'
  926. <?php
  927. // autoload_psr4.php @generated by Composer
  928. $vendorDir = dirname(dirname(__FILE__));
  929. $baseDir = dirname($vendorDir);
  930. return array(
  931. 'Acme\\Foo\\' => array($baseDir . '/composer-test-autoload-src/src-psr4'),
  932. );
  933. EOF;
  934. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'VendorSubstring');
  935. $this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php'));
  936. $this->assertEquals($expectedPsr4, file_get_contents($this->vendorDir.'/composer/autoload_psr4.php'));
  937. }
  938. private function assertAutoloadFiles($name, $dir, $type = 'namespaces')
  939. {
  940. $a = __DIR__.'/Fixtures/autoload_'.$name.'.php';
  941. $b = $dir.'/autoload_'.$type.'.php';
  942. $this->assertFileEquals($a, $b);
  943. }
  944. }