AutoloadGeneratorTest.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  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. 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. 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. 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. 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. 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. 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. $expectedNamespace = <<<EOF
  390. <?php
  391. // autoload_namespaces.php generated by Composer
  392. \$vendorDir = dirname(dirname(__FILE__));
  393. \$baseDir = dirname(\$vendorDir);
  394. return array(
  395. 'B\\\\Sub\\\\Name' => \$vendorDir . '/b/b/src',
  396. 'A\\\\B' => array(\$baseDir . '/lib', \$vendorDir . '/a/a/lib'),
  397. 'A' => \$vendorDir . '/a/a/src',
  398. );
  399. EOF;
  400. $expectedClassmap = <<<EOF
  401. <?php
  402. // autoload_classmap.php generated by Composer
  403. \$vendorDir = dirname(dirname(__FILE__));
  404. \$baseDir = dirname(\$vendorDir);
  405. return array(
  406. 'A\\\\B\\\\C' => \$baseDir . '/lib/A/B/C.php',
  407. 'Foo\\\\Bar' => \$baseDir . '/src/classes.php',
  408. );
  409. EOF;
  410. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_9');
  411. $this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php'));
  412. $this->assertEquals($expectedClassmap, file_get_contents($this->vendorDir.'/composer/autoload_classmap.php'));
  413. }
  414. public function testIncludePathFileGeneration()
  415. {
  416. $package = new Package('a', '1.0', '1.0');
  417. $packages = array();
  418. $a = new Package("a/a", "1.0", "1.0");
  419. $a->setIncludePaths(array("lib/"));
  420. $b = new Package("b/b", "1.0", "1.0");
  421. $b->setIncludePaths(array("library"));
  422. $c = new Package("c", "1.0", "1.0");
  423. $c->setIncludePaths(array("library"));
  424. $packages[] = $a;
  425. $packages[] = $b;
  426. $packages[] = $c;
  427. $this->repository->expects($this->once())
  428. ->method("getPackages")
  429. ->will($this->returnValue($packages));
  430. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  431. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_10');
  432. $this->assertFileEquals(__DIR__.'/Fixtures/include_paths.php', $this->vendorDir.'/composer/include_paths.php');
  433. $this->assertEquals(
  434. array(
  435. $this->vendorDir."/a/a/lib",
  436. $this->vendorDir."/b/b/library",
  437. $this->vendorDir."/c/library",
  438. ),
  439. require $this->vendorDir."/composer/include_paths.php"
  440. );
  441. }
  442. public function testIncludePathsArePrependedInAutoloadFile()
  443. {
  444. $package = new Package('a', '1.0', '1.0');
  445. $packages = array();
  446. $a = new Package("a/a", "1.0", "1.0");
  447. $a->setIncludePaths(array("lib/"));
  448. $packages[] = $a;
  449. $this->repository->expects($this->once())
  450. ->method("getPackages")
  451. ->will($this->returnValue($packages));
  452. mkdir($this->vendorDir."/composer", 0777, true);
  453. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_11');
  454. $oldIncludePath = get_include_path();
  455. require $this->vendorDir."/autoload.php";
  456. $this->assertEquals(
  457. $this->vendorDir."/a/a/lib".PATH_SEPARATOR.$oldIncludePath,
  458. get_include_path()
  459. );
  460. set_include_path($oldIncludePath);
  461. }
  462. public function testIncludePathsInMainPackage()
  463. {
  464. $package = new Package('a', '1.0', '1.0');
  465. $package->setIncludePaths(array('/lib', '/src'));
  466. $packages = array($a = new Package("a/a", "1.0", "1.0"));
  467. $a->setIncludePaths(array("lib/"));
  468. $this->repository->expects($this->once())
  469. ->method("getPackages")
  470. ->will($this->returnValue($packages));
  471. mkdir($this->vendorDir."/composer", 0777, true);
  472. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_12');
  473. $oldIncludePath = get_include_path();
  474. require $this->vendorDir."/autoload.php";
  475. $this->assertEquals(
  476. $this->workingDir."/lib".PATH_SEPARATOR.$this->workingDir."/src".PATH_SEPARATOR.$this->vendorDir."/a/a/lib".PATH_SEPARATOR.$oldIncludePath,
  477. get_include_path()
  478. );
  479. set_include_path($oldIncludePath);
  480. }
  481. public function testIncludePathFileWithoutPathsIsSkipped()
  482. {
  483. $package = new Package('a', '1.0', '1.0');
  484. $packages = array();
  485. $a = new Package("a/a", "1.0", "1.0");
  486. $packages[] = $a;
  487. $this->repository->expects($this->once())
  488. ->method("getPackages")
  489. ->will($this->returnValue($packages));
  490. mkdir($this->vendorDir."/composer", 0777, true);
  491. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_12');
  492. $this->assertFalse(file_exists($this->vendorDir."/composer/include_paths.php"));
  493. }
  494. public function testEventIsDispatchedAfterAutoloadDump()
  495. {
  496. $this->eventDispatcher
  497. ->expects($this->once())
  498. ->method('dispatch')
  499. ->with(ScriptEvents::POST_AUTOLOAD_DUMP, false);
  500. $package = new Package('a', '1.0', '1.0');
  501. $package->setAutoload(array('psr-0' => array('foo/bar/non/existing/')));
  502. $this->repository->expects($this->once())
  503. ->method('getPackages')
  504. ->will($this->returnValue(array()));
  505. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_8');
  506. }
  507. public function testUseGlobalIncludePath()
  508. {
  509. $package = new Package('a', '1.0', '1.0');
  510. $package->setAutoload(array(
  511. 'psr-0' => array('Main\\Foo' => '', 'Main\\Bar' => ''),
  512. ));
  513. $package->setTargetDir('Main/Foo/');
  514. $this->repository->expects($this->once())
  515. ->method('getPackages')
  516. ->will($this->returnValue(array()));
  517. $this->config->expects($this->at(2))
  518. ->method('get')
  519. ->with($this->equalTo('use-include-path'))
  520. ->will($this->returnValue(true));
  521. $this->fs->ensureDirectoryExists($this->vendorDir.'/a');
  522. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'IncludePath');
  523. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_include_path.php', $this->vendorDir.'/composer/autoload_real.php');
  524. }
  525. public function testVendorDirExcludedFromWorkingDir()
  526. {
  527. $workingDir = $this->vendorDir.'/working-dir';
  528. $vendorDir = $workingDir.'/../vendor';
  529. $this->fs->ensureDirectoryExists($workingDir);
  530. chdir($workingDir);
  531. $package = new Package('a', '1.0', '1.0');
  532. $package->setAutoload(array(
  533. 'psr-0' => array('Foo' => 'src'),
  534. 'classmap' => array('classmap'),
  535. 'files' => array('test.php'),
  536. ));
  537. $vendorPackage = new Package('b/b', '1.0', '1.0');
  538. $vendorPackage->setAutoload(array(
  539. 'psr-0' => array('Bar' => 'lib'),
  540. 'classmap' => array('classmaps'),
  541. 'files' => array('bootstrap.php'),
  542. ));
  543. $this->repository->expects($this->once())
  544. ->method('getPackages')
  545. ->will($this->returnValue(array($vendorPackage)));
  546. $im = $this->getMockBuilder('Composer\Installer\InstallationManager')
  547. ->disableOriginalConstructor()
  548. ->getMock();
  549. $im->expects($this->any())
  550. ->method('getInstallPath')
  551. ->will($this->returnCallback(function ($package) use ($vendorDir) {
  552. $targetDir = $package->getTargetDir();
  553. return $vendorDir.'/'.$package->getName() . ($targetDir ? '/'.$targetDir : '');
  554. }));
  555. $this->fs->ensureDirectoryExists($workingDir.'/src/Foo');
  556. $this->fs->ensureDirectoryExists($workingDir.'/classmap');
  557. $this->fs->ensureDirectoryExists($vendorDir.'/composer');
  558. $this->fs->ensureDirectoryExists($vendorDir.'/b/b/lib/Bar');
  559. $this->fs->ensureDirectoryExists($vendorDir.'/b/b/classmaps');
  560. file_put_contents($workingDir.'/src/Foo/Bar.php', '<?php namespace Foo; class Bar {}');
  561. file_put_contents($workingDir.'/classmap/classes.php', '<?php namespace Foo; class Foo {}');
  562. file_put_contents($workingDir.'/test.php', '<?php class Foo {}');
  563. file_put_contents($vendorDir.'/b/b/lib/Bar/Foo.php', '<?php namespace Bar; class Foo {}');
  564. file_put_contents($vendorDir.'/b/b/classmaps/classes.php', '<?php namespace Bar; class Bar {}');
  565. file_put_contents($vendorDir.'/b/b/bootstrap.php', '<?php class Bar {}');
  566. $oldVendorDir = $this->vendorDir;
  567. $this->vendorDir = $vendorDir;
  568. $this->generator->dump($this->config, $this->repository, $package, $im, 'composer', true, '_13');
  569. $this->vendorDir = $oldVendorDir;
  570. $expectedNamespace = <<<'EOF'
  571. <?php
  572. // autoload_namespaces.php generated by Composer
  573. $vendorDir = dirname(dirname(__FILE__));
  574. $baseDir = dirname($vendorDir).'/working-dir';
  575. return array(
  576. 'Foo' => $baseDir . '/src',
  577. 'Bar' => $vendorDir . '/b/b/lib',
  578. );
  579. EOF;
  580. $expectedClassmap = <<<'EOF'
  581. <?php
  582. // autoload_classmap.php generated by Composer
  583. $vendorDir = dirname(dirname(__FILE__));
  584. $baseDir = dirname($vendorDir).'/working-dir';
  585. return array(
  586. 'Bar\\Bar' => $vendorDir . '/b/b/classmaps/classes.php',
  587. 'Bar\\Foo' => $vendorDir . '/b/b/lib/Bar/Foo.php',
  588. 'Foo\\Bar' => $baseDir . '/src/Foo/Bar.php',
  589. 'Foo\\Foo' => $baseDir . '/classmap/classes.php',
  590. );
  591. EOF;
  592. $this->assertEquals($expectedNamespace, file_get_contents($vendorDir.'/composer/autoload_namespaces.php'));
  593. $this->assertEquals($expectedClassmap, file_get_contents($vendorDir.'/composer/autoload_classmap.php'));
  594. $this->assertContains("require \$vendorDir . '/b/b/bootstrap.php';", file_get_contents($vendorDir.'/composer/autoload_real.php'));
  595. $this->assertContains("require \$baseDir . '/test.php';", file_get_contents($vendorDir.'/composer/autoload_real.php'));
  596. }
  597. public function testUpLevelRelativePaths()
  598. {
  599. $workingDir = $this->workingDir.'/working-dir';
  600. mkdir($workingDir, 0777, true);
  601. chdir($workingDir);
  602. $package = new Package('a', '1.0', '1.0');
  603. $package->setAutoload(array(
  604. 'psr-0' => array('Foo' => '../path/../src'),
  605. 'classmap' => array('../classmap'),
  606. 'files' => array('../test.php'),
  607. ));
  608. $this->repository->expects($this->once())
  609. ->method('getPackages')
  610. ->will($this->returnValue(array()));
  611. $this->fs->ensureDirectoryExists($this->workingDir.'/src/Foo');
  612. $this->fs->ensureDirectoryExists($this->workingDir.'/classmap');
  613. file_put_contents($this->workingDir.'/src/Foo/Bar.php', '<?php namespace Foo; class Bar {}');
  614. file_put_contents($this->workingDir.'/classmap/classes.php', '<?php namespace Foo; class Foo {}');
  615. file_put_contents($this->workingDir.'/test.php', '<?php class Foo {}');
  616. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_14');
  617. $expectedNamespace = <<<'EOF'
  618. <?php
  619. // autoload_namespaces.php generated by Composer
  620. $vendorDir = dirname(dirname(__FILE__));
  621. $baseDir = dirname($vendorDir).'/working-dir';
  622. return array(
  623. 'Foo' => $baseDir . '/../src',
  624. );
  625. EOF;
  626. $expectedClassmap = <<<'EOF'
  627. <?php
  628. // autoload_classmap.php generated by Composer
  629. $vendorDir = dirname(dirname(__FILE__));
  630. $baseDir = dirname($vendorDir).'/working-dir';
  631. return array(
  632. 'Foo\\Bar' => $baseDir . '/../src/Foo/Bar.php',
  633. 'Foo\\Foo' => $baseDir . '/../classmap/classes.php',
  634. );
  635. EOF;
  636. $this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php'));
  637. $this->assertEquals($expectedClassmap, file_get_contents($this->vendorDir.'/composer/autoload_classmap.php'));
  638. $this->assertContains("require \$baseDir . '/../test.php';", file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
  639. }
  640. private function assertAutoloadFiles($name, $dir, $type = 'namespaces')
  641. {
  642. $a = __DIR__.'/Fixtures/autoload_'.$name.'.php';
  643. $b = $dir.'/autoload_'.$type.'.php';
  644. $this->assertFileEquals($a, $b);
  645. }
  646. }