AutoloadGeneratorTest.php 29 KB

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