AutoloadGeneratorTest.php 28 KB

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