AutoloadGeneratorTest.php 25 KB

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