AutoloadGeneratorTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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\Test\TestCase;
  18. class AutoloadGeneratorTest extends TestCase
  19. {
  20. public $vendorDir;
  21. private $config;
  22. private $workingDir;
  23. private $im;
  24. private $repository;
  25. private $generator;
  26. private $fs;
  27. protected function setUp()
  28. {
  29. $this->fs = new Filesystem;
  30. $that = $this;
  31. $this->workingDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'cmptest';
  32. $this->fs->ensureDirectoryExists($this->workingDir);
  33. $this->vendorDir = $this->workingDir.DIRECTORY_SEPARATOR.'composer-test-autoload-'.md5(uniqid('', true));
  34. $this->ensureDirectoryExistsAndClear($this->vendorDir);
  35. $this->config = $this->getMock('Composer\Config');
  36. $this->config->expects($this->at(0))
  37. ->method('get')
  38. ->with($this->equalTo('vendor-dir'))
  39. ->will($this->returnCallback(function () use ($that) {
  40. return $that->vendorDir;
  41. }));
  42. $this->config->expects($this->at(1))
  43. ->method('get')
  44. ->with($this->equalTo('vendor-dir'))
  45. ->will($this->returnCallback(function () use ($that) {
  46. return $that->vendorDir;
  47. }));
  48. $this->dir = getcwd();
  49. chdir($this->workingDir);
  50. $this->im = $this->getMockBuilder('Composer\Installer\InstallationManager')
  51. ->disableOriginalConstructor()
  52. ->getMock();
  53. $this->im->expects($this->any())
  54. ->method('getInstallPath')
  55. ->will($this->returnCallback(function ($package) use ($that) {
  56. $targetDir = $package->getTargetDir();
  57. return $that->vendorDir.'/'.$package->getName() . ($targetDir ? '/'.$targetDir : '');
  58. }));
  59. $this->repository = $this->getMock('Composer\Repository\RepositoryInterface');
  60. $this->generator = new AutoloadGenerator();
  61. }
  62. protected function tearDown()
  63. {
  64. chdir($this->dir);
  65. if (is_dir($this->workingDir)) {
  66. $this->fs->removeDirectory($this->workingDir);
  67. }
  68. if (is_dir($this->vendorDir)) {
  69. $this->fs->removeDirectory($this->vendorDir);
  70. }
  71. }
  72. public function testMainPackageAutoloading()
  73. {
  74. $package = new Package('a', '1.0', '1.0');
  75. $package->setAutoload(array(
  76. 'psr-0' => array('Main' => 'src/', 'Lala' => array('src/', 'lib/')),
  77. 'classmap' => array('composersrc/'),
  78. ));
  79. $this->repository->expects($this->once())
  80. ->method('getPackages')
  81. ->will($this->returnValue(array()));
  82. $this->fs->ensureDirectoryExists($this->workingDir.'/composer');
  83. $this->fs->ensureDirectoryExists($this->workingDir.'/src');
  84. $this->fs->ensureDirectoryExists($this->workingDir.'/lib');
  85. $this->createClassFile($this->workingDir);
  86. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_1');
  87. $this->assertAutoloadFiles('main', $this->vendorDir.'/composer');
  88. $this->assertAutoloadFiles('classmap', $this->vendorDir.'/composer', 'classmap');
  89. }
  90. public function testVendorDirSameAsWorkingDir()
  91. {
  92. $this->vendorDir = $this->workingDir;
  93. $package = new Package('a', '1.0', '1.0');
  94. $package->setAutoload(array(
  95. 'psr-0' => array('Main' => 'src/', 'Lala' => 'src/'),
  96. 'classmap' => array('composersrc/'),
  97. ));
  98. $this->repository->expects($this->once())
  99. ->method('getPackages')
  100. ->will($this->returnValue(array()));
  101. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  102. $this->fs->ensureDirectoryExists($this->vendorDir.'/src/Main');
  103. file_put_contents($this->vendorDir.'/src/Main/Foo.php', '<?php namespace Main; class Foo {}');
  104. $this->createClassFile($this->vendorDir);
  105. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_2');
  106. $this->assertAutoloadFiles('main3', $this->vendorDir.'/composer');
  107. $this->assertAutoloadFiles('classmap3', $this->vendorDir.'/composer', 'classmap');
  108. }
  109. public function testMainPackageAutoloadingAlternativeVendorDir()
  110. {
  111. $package = new Package('a', '1.0', '1.0');
  112. $package->setAutoload(array(
  113. 'psr-0' => array('Main' => 'src/', 'Lala' => 'src/'),
  114. 'classmap' => array('composersrc/'),
  115. ));
  116. $this->repository->expects($this->once())
  117. ->method('getPackages')
  118. ->will($this->returnValue(array()));
  119. $this->vendorDir .= '/subdir';
  120. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  121. $this->fs->ensureDirectoryExists($this->workingDir.'/src');
  122. $this->createClassFile($this->workingDir);
  123. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_3');
  124. $this->assertAutoloadFiles('main2', $this->vendorDir.'/composer');
  125. $this->assertAutoloadFiles('classmap2', $this->vendorDir.'/composer', 'classmap');
  126. }
  127. public function testMainPackageAutoloadingWithTargetDir()
  128. {
  129. $package = new Package('a', '1.0', '1.0');
  130. $package->setAutoload(array(
  131. 'psr-0' => array('Main\\Foo' => '', 'Main\\Bar' => ''),
  132. ));
  133. $package->setTargetDir('Main/Foo/');
  134. $this->repository->expects($this->once())
  135. ->method('getPackages')
  136. ->will($this->returnValue(array()));
  137. $this->fs->ensureDirectoryExists($this->vendorDir.'/a');
  138. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'TargetDir');
  139. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_target_dir.php', $this->vendorDir.'/autoload.php');
  140. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_target_dir.php', $this->vendorDir.'/composer/autoload_real.php');
  141. }
  142. public function testMainPackageAutoloadingWithTargetDirAndClassmap()
  143. {
  144. $package = new Package('a', '1.0', '1.0');
  145. $package->setAutoload(array(
  146. 'classmap' => array('Main/Foo/composersrc/'),
  147. ));
  148. $package->setTargetDir('Main/Foo/');
  149. $this->repository->expects($this->once())
  150. ->method('getPackages')
  151. ->will($this->returnValue(array()));
  152. $this->vendorDir .= '/subdir';
  153. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  154. $this->fs->ensureDirectoryExists($this->workingDir.'/src');
  155. $this->createClassFile($this->workingDir);
  156. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'TargetDirNoPsr');
  157. $this->assertAutoloadFiles('classmap2', $this->vendorDir.'/composer', 'classmap');
  158. }
  159. public function testVendorsAutoloading()
  160. {
  161. $package = new Package('a', '1.0', '1.0');
  162. $packages = array();
  163. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  164. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  165. $packages[] = $c = new AliasPackage($b, '1.2', '1.2');
  166. $a->setAutoload(array('psr-0' => array('A' => 'src/', 'A\\B' => 'lib/')));
  167. $b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/')));
  168. $this->repository->expects($this->once())
  169. ->method('getPackages')
  170. ->will($this->returnValue($packages));
  171. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  172. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  173. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib');
  174. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  175. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_5');
  176. $this->assertAutoloadFiles('vendors', $this->vendorDir.'/composer');
  177. $this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated, even if empty.");
  178. }
  179. public function testPSR0ToClassMapIgnoresNonExistingDir()
  180. {
  181. $package = new Package('a', '1.0', '1.0');
  182. $package->setAutoload(array('psr-0' => array('foo/bar/non/existing/')));
  183. $this->repository->expects($this->once())
  184. ->method('getPackages')
  185. ->will($this->returnValue(array()));
  186. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_8');
  187. $this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated.");
  188. $this->assertEquals(
  189. array(),
  190. include $this->vendorDir.'/composer/autoload_classmap.php'
  191. );
  192. }
  193. public function testVendorsClassMapAutoloading()
  194. {
  195. $package = new Package('a', '1.0', '1.0');
  196. $packages = array();
  197. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  198. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  199. $a->setAutoload(array('classmap' => array('src/')));
  200. $b->setAutoload(array('classmap' => array('src/', 'lib/')));
  201. $this->repository->expects($this->once())
  202. ->method('getPackages')
  203. ->will($this->returnValue($packages));
  204. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  205. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  206. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  207. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/lib');
  208. file_put_contents($this->vendorDir.'/a/a/src/a.php', '<?php class ClassMapFoo {}');
  209. file_put_contents($this->vendorDir.'/b/b/src/b.php', '<?php class ClassMapBar {}');
  210. file_put_contents($this->vendorDir.'/b/b/lib/c.php', '<?php class ClassMapBaz {}');
  211. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_6');
  212. $this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated.");
  213. $this->assertEquals(
  214. $this->normalizePaths(array(
  215. 'ClassMapBar' => $this->vendorDir.'/b/b/src/b.php',
  216. 'ClassMapBaz' => $this->vendorDir.'/b/b/lib/c.php',
  217. 'ClassMapFoo' => $this->vendorDir.'/a/a/src/a.php',
  218. )),
  219. $this->normalizePaths(include $this->vendorDir.'/composer/autoload_classmap.php')
  220. );
  221. $this->assertAutoloadFiles('classmap4', $this->vendorDir.'/composer', 'classmap');
  222. }
  223. public function testClassMapAutoloadingEmptyDirAndExactFile()
  224. {
  225. $package = new Package('a', '1.0', '1.0');
  226. $packages = array();
  227. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  228. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  229. $packages[] = $c = new Package('c/c', '1.0', '1.0');
  230. $a->setAutoload(array('classmap' => array('')));
  231. $b->setAutoload(array('classmap' => array('test.php')));
  232. $c->setAutoload(array('classmap' => array('./')));
  233. $this->repository->expects($this->once())
  234. ->method('getPackages')
  235. ->will($this->returnValue($packages));
  236. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  237. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  238. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b');
  239. $this->fs->ensureDirectoryExists($this->vendorDir.'/c/c/foo');
  240. file_put_contents($this->vendorDir.'/a/a/src/a.php', '<?php class ClassMapFoo {}');
  241. file_put_contents($this->vendorDir.'/b/b/test.php', '<?php class ClassMapBar {}');
  242. file_put_contents($this->vendorDir.'/c/c/foo/test.php', '<?php class ClassMapBaz {}');
  243. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_7');
  244. $this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated.");
  245. $this->assertEquals(
  246. $this->normalizePaths(array(
  247. 'ClassMapBar' => $this->vendorDir.'/b/b/test.php',
  248. 'ClassMapBaz' => $this->vendorDir.'/c/c/foo/test.php',
  249. 'ClassMapFoo' => $this->vendorDir.'/a/a/src/a.php',
  250. )),
  251. $this->normalizePaths(include $this->vendorDir.'/composer/autoload_classmap.php')
  252. );
  253. $this->assertAutoloadFiles('classmap5', $this->vendorDir.'/composer', 'classmap');
  254. }
  255. public function testFilesAutoloadGeneration()
  256. {
  257. $package = new Package('a', '1.0', '1.0');
  258. $package->setAutoload(array('files' => array('root.php')));
  259. $packages = array();
  260. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  261. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  262. $packages[] = $c = new Package('c/c', '1.0', '1.0');
  263. $a->setAutoload(array('files' => array('test.php')));
  264. $b->setAutoload(array('files' => array('test2.php')));
  265. $c->setAutoload(array('files' => array('test3.php', 'foo/bar/test4.php')));
  266. $c->setTargetDir('foo/bar');
  267. $this->repository->expects($this->once())
  268. ->method('getPackages')
  269. ->will($this->returnValue($packages));
  270. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a');
  271. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b');
  272. $this->fs->ensureDirectoryExists($this->vendorDir.'/c/c/foo/bar');
  273. file_put_contents($this->vendorDir.'/a/a/test.php', '<?php function testFilesAutoloadGeneration1() {}');
  274. file_put_contents($this->vendorDir.'/b/b/test2.php', '<?php function testFilesAutoloadGeneration2() {}');
  275. file_put_contents($this->vendorDir.'/c/c/foo/bar/test3.php', '<?php function testFilesAutoloadGeneration3() {}');
  276. file_put_contents($this->vendorDir.'/c/c/foo/bar/test4.php', '<?php function testFilesAutoloadGeneration4() {}');
  277. file_put_contents($this->workingDir.'/root.php', '<?php function testFilesAutoloadGenerationRoot() {}');
  278. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'FilesAutoload');
  279. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php');
  280. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_functions.php', $this->vendorDir.'/composer/autoload_real.php');
  281. include $this->vendorDir . '/autoload.php';
  282. $this->assertTrue(function_exists('testFilesAutoloadGeneration1'));
  283. $this->assertTrue(function_exists('testFilesAutoloadGeneration2'));
  284. $this->assertTrue(function_exists('testFilesAutoloadGeneration3'));
  285. $this->assertTrue(function_exists('testFilesAutoloadGeneration4'));
  286. $this->assertTrue(function_exists('testFilesAutoloadGenerationRoot'));
  287. }
  288. public function testFilesAutoloadOrderByDependencies()
  289. {
  290. $package = new Package('a', '1.0', '1.0');
  291. $package->setAutoload(array('files' => array('root.php')));
  292. $package->setRequires(array(new Link('a', 'z/foo')));
  293. $package->setRequires(array(new Link('a', 'd/d')));
  294. $package->setRequires(array(new Link('a', 'e/e')));
  295. $packages = array();
  296. $packages[] = $z = new Package('z/foo', '1.0', '1.0');
  297. $packages[] = $b = new Package('b/bar', '1.0', '1.0');
  298. $packages[] = $d = new Package('d/d', '1.0', '1.0');
  299. $packages[] = $c = new Package('c/lorem', '1.0', '1.0');
  300. $packages[] = $e = new Package('e/e', '1.0', '1.0');
  301. $z->setAutoload(array('files' => array('testA.php')));
  302. $z->setRequires(array(new Link('z/foo', 'c/lorem')));
  303. $b->setAutoload(array('files' => array('testB.php')));
  304. $b->setRequires(array(new Link('b/bar', 'c/lorem'), new Link('b/bar', 'd/d')));
  305. $c->setAutoload(array('files' => array('testC.php')));
  306. $d->setAutoload(array('files' => array('testD.php')));
  307. $d->setRequires(array(new Link('d/d', 'c/lorem')));
  308. $e->setAutoload(array('files' => array('testE.php')));
  309. $e->setRequires(array(new Link('e/e', 'c/lorem')));
  310. $this->repository->expects($this->once())
  311. ->method('getPackages')
  312. ->will($this->returnValue($packages));
  313. $this->fs->ensureDirectoryExists($this->vendorDir . '/z/foo');
  314. $this->fs->ensureDirectoryExists($this->vendorDir . '/b/bar');
  315. $this->fs->ensureDirectoryExists($this->vendorDir . '/c/lorem');
  316. $this->fs->ensureDirectoryExists($this->vendorDir . '/d/d');
  317. $this->fs->ensureDirectoryExists($this->vendorDir . '/e/e');
  318. file_put_contents($this->vendorDir . '/z/foo/testA.php', '<?php function testFilesAutoloadOrderByDependency1() {}');
  319. file_put_contents($this->vendorDir . '/b/bar/testB.php', '<?php function testFilesAutoloadOrderByDependency2() {}');
  320. file_put_contents($this->vendorDir . '/c/lorem/testC.php', '<?php function testFilesAutoloadOrderByDependency3() {}');
  321. file_put_contents($this->vendorDir . '/d/d/testD.php', '<?php function testFilesAutoloadOrderByDependency4() {}');
  322. file_put_contents($this->vendorDir . '/e/e/testE.php', '<?php function testFilesAutoloadOrderByDependency5() {}');
  323. file_put_contents($this->workingDir . '/root.php', '<?php function testFilesAutoloadOrderByDependencyRoot() {}');
  324. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'FilesAutoloadOrder');
  325. $this->assertFileEquals(__DIR__ . '/Fixtures/autoload_functions_by_dependency.php', $this->vendorDir . '/autoload.php');
  326. $this->assertFileEquals(__DIR__ . '/Fixtures/autoload_real_files_by_dependency.php', $this->vendorDir . '/composer/autoload_real.php');
  327. require $this->vendorDir . '/autoload.php';
  328. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency1'));
  329. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency2'));
  330. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency3'));
  331. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency4'));
  332. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency5'));
  333. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependencyRoot'));
  334. }
  335. public function testOverrideVendorsAutoloading()
  336. {
  337. $package = new Package('z', '1.0', '1.0');
  338. $package->setAutoload(array('psr-0' => array('A\\B' => $this->workingDir.'/lib'), 'classmap' => array($this->workingDir.'/src')));
  339. $package->setRequires(array(new Link('z', 'a/a')));
  340. $packages = array();
  341. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  342. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  343. $a->setAutoload(array('psr-0' => array('A' => 'src/', 'A\\B' => 'lib/'), 'classmap' => array('classmap')));
  344. $b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/')));
  345. $this->repository->expects($this->once())
  346. ->method('getPackages')
  347. ->will($this->returnValue($packages));
  348. $this->fs->ensureDirectoryExists($this->workingDir.'/lib/A/B');
  349. $this->fs->ensureDirectoryExists($this->workingDir.'/src/');
  350. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  351. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/classmap');
  352. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  353. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib/A/B');
  354. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  355. file_put_contents($this->workingDir.'/lib/A/B/C.php', '<?php namespace A\\B; class C {}');
  356. file_put_contents($this->workingDir.'/src/classes.php', '<?php namespace Foo; class Bar {}');
  357. file_put_contents($this->vendorDir.'/a/a/lib/A/B/C.php', '<?php namespace A\\B; class C {}');
  358. file_put_contents($this->vendorDir.'/a/a/classmap/classes.php', '<?php namespace Foo; class Bar {}');
  359. $workDir = strtr($this->workingDir, '\\', '/');
  360. $expectedNamespace = <<<EOF
  361. <?php
  362. // autoload_namespaces.php generated by Composer
  363. \$vendorDir = dirname(__DIR__);
  364. \$baseDir = dirname(\$vendorDir);
  365. return array(
  366. 'B\\\\Sub\\\\Name' => \$vendorDir . '/b/b/src/',
  367. 'A\\\\B' => array('$workDir/lib', \$vendorDir . '/a/a/lib/'),
  368. 'A' => \$vendorDir . '/a/a/src/',
  369. );
  370. EOF;
  371. $expectedClassmap = <<<EOF
  372. <?php
  373. // autoload_classmap.php generated by Composer
  374. \$vendorDir = dirname(__DIR__);
  375. \$baseDir = dirname(\$vendorDir);
  376. return array(
  377. 'A\\\\B\\\\C' => \$baseDir . '/lib/A/B/C.php',
  378. 'Foo\\\\Bar' => \$baseDir . '/src/classes.php',
  379. );
  380. EOF;
  381. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_9');
  382. $this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php'));
  383. $this->assertEquals($expectedClassmap, file_get_contents($this->vendorDir.'/composer/autoload_classmap.php'));
  384. }
  385. public function testIncludePathFileGeneration()
  386. {
  387. $package = new Package('a', '1.0', '1.0');
  388. $packages = array();
  389. $a = new Package("a/a", "1.0", "1.0");
  390. $a->setIncludePaths(array("lib/"));
  391. $b = new Package("b/b", "1.0", "1.0");
  392. $b->setIncludePaths(array("library"));
  393. $c = new Package("c", "1.0", "1.0");
  394. $c->setIncludePaths(array("library"));
  395. $packages[] = $a;
  396. $packages[] = $b;
  397. $packages[] = $c;
  398. $this->repository->expects($this->once())
  399. ->method("getPackages")
  400. ->will($this->returnValue($packages));
  401. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  402. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_10');
  403. $this->assertFileEquals(__DIR__.'/Fixtures/include_paths.php', $this->vendorDir.'/composer/include_paths.php');
  404. $this->assertEquals(
  405. array(
  406. $this->vendorDir."/a/a/lib",
  407. $this->vendorDir."/b/b/library",
  408. $this->vendorDir."/c/library",
  409. ),
  410. require $this->vendorDir."/composer/include_paths.php"
  411. );
  412. }
  413. public function testIncludePathsArePrependedInAutoloadFile()
  414. {
  415. $package = new Package('a', '1.0', '1.0');
  416. $packages = array();
  417. $a = new Package("a/a", "1.0", "1.0");
  418. $a->setIncludePaths(array("lib/"));
  419. $packages[] = $a;
  420. $this->repository->expects($this->once())
  421. ->method("getPackages")
  422. ->will($this->returnValue($packages));
  423. mkdir($this->vendorDir."/composer", 0777, true);
  424. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_11');
  425. $oldIncludePath = get_include_path();
  426. require $this->vendorDir."/autoload.php";
  427. $this->assertEquals(
  428. $this->vendorDir."/a/a/lib".PATH_SEPARATOR.$oldIncludePath,
  429. get_include_path()
  430. );
  431. set_include_path($oldIncludePath);
  432. }
  433. public function testIncludePathsInMainPackage()
  434. {
  435. $package = new Package('a', '1.0', '1.0');
  436. $package->setIncludePaths(array('/lib', '/src'));
  437. $packages = array($a = new Package("a/a", "1.0", "1.0"));
  438. $a->setIncludePaths(array("lib/"));
  439. $this->repository->expects($this->once())
  440. ->method("getPackages")
  441. ->will($this->returnValue($packages));
  442. mkdir($this->vendorDir."/composer", 0777, true);
  443. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_12');
  444. $oldIncludePath = get_include_path();
  445. require $this->vendorDir."/autoload.php";
  446. $this->assertEquals(
  447. $this->workingDir."/lib".PATH_SEPARATOR.$this->workingDir."/src".PATH_SEPARATOR.$this->vendorDir."/a/a/lib".PATH_SEPARATOR.$oldIncludePath,
  448. get_include_path()
  449. );
  450. set_include_path($oldIncludePath);
  451. }
  452. public function testIncludePathFileWithoutPathsIsSkipped()
  453. {
  454. $package = new Package('a', '1.0', '1.0');
  455. $packages = array();
  456. $a = new Package("a/a", "1.0", "1.0");
  457. $packages[] = $a;
  458. $this->repository->expects($this->once())
  459. ->method("getPackages")
  460. ->will($this->returnValue($packages));
  461. mkdir($this->vendorDir."/composer", 0777, true);
  462. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_12');
  463. $this->assertFalse(file_exists($this->vendorDir."/composer/include_paths.php"));
  464. }
  465. public function testUseGlobalIncludePath()
  466. {
  467. $package = new Package('a', '1.0', '1.0');
  468. $package->setAutoload(array(
  469. 'psr-0' => array('Main\\Foo' => '', 'Main\\Bar' => ''),
  470. ));
  471. $package->setTargetDir('Main/Foo/');
  472. $this->repository->expects($this->once())
  473. ->method('getPackages')
  474. ->will($this->returnValue(array()));
  475. $this->config->expects($this->at(2))
  476. ->method('get')
  477. ->with($this->equalTo('use-include-path'))
  478. ->will($this->returnValue(true));
  479. $this->fs->ensureDirectoryExists($this->vendorDir.'/a');
  480. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'IncludePath');
  481. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_include_path.php', $this->vendorDir.'/composer/autoload_real.php');
  482. }
  483. private function createClassFile($basedir)
  484. {
  485. if (!is_dir($basedir.'/composersrc')) {
  486. mkdir($basedir.'/composersrc', 0777, true);
  487. }
  488. file_put_contents($basedir.'/composersrc/foo.php', '<?php class ClassMapFoo {}');
  489. }
  490. private function assertAutoloadFiles($name, $dir, $type = 'namespaces')
  491. {
  492. $a = __DIR__.'/Fixtures/autoload_'.$name.'.php';
  493. $b = $dir.'/autoload_'.$type.'.php';
  494. $this->assertEquals(
  495. str_replace('%vendorDir%', basename($this->vendorDir), file_get_contents($a)),
  496. file_get_contents($b),
  497. $a .' does not equal '. $b
  498. );
  499. }
  500. private function normalizePaths($paths)
  501. {
  502. if (!is_array($paths)) {
  503. return strtr($paths, '\\', '/');
  504. }
  505. foreach ($paths as $key => $path) {
  506. $paths[$key] = strtr($path, '\\', '/');
  507. }
  508. return $paths;
  509. }
  510. }