AutoloadGeneratorTest.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  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\InstalledRepositoryInterface');
  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('getCanonicalPackages')
  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('getCanonicalPackages')
  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('getCanonicalPackages')
  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('getCanonicalPackages')
  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('getCanonicalPackages')
  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('getCanonicalPackages')
  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('getCanonicalPackages')
  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('getCanonicalPackages')
  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('getCanonicalPackages')
  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('getCanonicalPackages')
  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('getCanonicalPackages')
  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('getCanonicalPackages')
  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' => array(\$vendorDir . '/b/b/src'),
  396. 'A\\\\B' => array(\$baseDir . '/lib', \$vendorDir . '/a/a/lib'),
  397. 'A' => array(\$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("getCanonicalPackages")
  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("getCanonicalPackages")
  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("getCanonicalPackages")
  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("getCanonicalPackages")
  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 testPreAndPostEventsAreDispatchedDuringAutoloadDump()
  495. {
  496. $this->eventDispatcher
  497. ->expects($this->at(0))
  498. ->method('dispatch')
  499. ->with(ScriptEvents::PRE_AUTOLOAD_DUMP, false);
  500. $this->eventDispatcher
  501. ->expects($this->at(1))
  502. ->method('dispatch')
  503. ->with(ScriptEvents::POST_AUTOLOAD_DUMP, false);
  504. $package = new Package('a', '1.0', '1.0');
  505. $package->setAutoload(array('psr-0' => array('foo/bar/non/existing/')));
  506. $this->repository->expects($this->once())
  507. ->method('getCanonicalPackages')
  508. ->will($this->returnValue(array()));
  509. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_8');
  510. }
  511. public function testUseGlobalIncludePath()
  512. {
  513. $package = new Package('a', '1.0', '1.0');
  514. $package->setAutoload(array(
  515. 'psr-0' => array('Main\\Foo' => '', 'Main\\Bar' => ''),
  516. ));
  517. $package->setTargetDir('Main/Foo/');
  518. $this->repository->expects($this->once())
  519. ->method('getCanonicalPackages')
  520. ->will($this->returnValue(array()));
  521. $this->config->expects($this->at(2))
  522. ->method('get')
  523. ->with($this->equalTo('use-include-path'))
  524. ->will($this->returnValue(true));
  525. $this->fs->ensureDirectoryExists($this->vendorDir.'/a');
  526. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'IncludePath');
  527. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_include_path.php', $this->vendorDir.'/composer/autoload_real.php');
  528. }
  529. public function testVendorDirExcludedFromWorkingDir()
  530. {
  531. $workingDir = $this->vendorDir.'/working-dir';
  532. $vendorDir = $workingDir.'/../vendor';
  533. $this->fs->ensureDirectoryExists($workingDir);
  534. chdir($workingDir);
  535. $package = new Package('a', '1.0', '1.0');
  536. $package->setAutoload(array(
  537. 'psr-0' => array('Foo' => 'src'),
  538. 'classmap' => array('classmap'),
  539. 'files' => array('test.php'),
  540. ));
  541. $vendorPackage = new Package('b/b', '1.0', '1.0');
  542. $vendorPackage->setAutoload(array(
  543. 'psr-0' => array('Bar' => 'lib'),
  544. 'classmap' => array('classmaps'),
  545. 'files' => array('bootstrap.php'),
  546. ));
  547. $this->repository->expects($this->once())
  548. ->method('getCanonicalPackages')
  549. ->will($this->returnValue(array($vendorPackage)));
  550. $im = $this->getMockBuilder('Composer\Installer\InstallationManager')
  551. ->disableOriginalConstructor()
  552. ->getMock();
  553. $im->expects($this->any())
  554. ->method('getInstallPath')
  555. ->will($this->returnCallback(function ($package) use ($vendorDir) {
  556. $targetDir = $package->getTargetDir();
  557. return $vendorDir.'/'.$package->getName() . ($targetDir ? '/'.$targetDir : '');
  558. }));
  559. $this->fs->ensureDirectoryExists($workingDir.'/src/Foo');
  560. $this->fs->ensureDirectoryExists($workingDir.'/classmap');
  561. $this->fs->ensureDirectoryExists($vendorDir.'/composer');
  562. $this->fs->ensureDirectoryExists($vendorDir.'/b/b/lib/Bar');
  563. $this->fs->ensureDirectoryExists($vendorDir.'/b/b/classmaps');
  564. file_put_contents($workingDir.'/src/Foo/Bar.php', '<?php namespace Foo; class Bar {}');
  565. file_put_contents($workingDir.'/classmap/classes.php', '<?php namespace Foo; class Foo {}');
  566. file_put_contents($workingDir.'/test.php', '<?php class Foo {}');
  567. file_put_contents($vendorDir.'/b/b/lib/Bar/Foo.php', '<?php namespace Bar; class Foo {}');
  568. file_put_contents($vendorDir.'/b/b/classmaps/classes.php', '<?php namespace Bar; class Bar {}');
  569. file_put_contents($vendorDir.'/b/b/bootstrap.php', '<?php class Bar {}');
  570. $oldVendorDir = $this->vendorDir;
  571. $this->vendorDir = $vendorDir;
  572. $this->generator->dump($this->config, $this->repository, $package, $im, 'composer', true, '_13');
  573. $this->vendorDir = $oldVendorDir;
  574. $expectedNamespace = <<<'EOF'
  575. <?php
  576. // autoload_namespaces.php generated by Composer
  577. $vendorDir = dirname(dirname(__FILE__));
  578. $baseDir = dirname($vendorDir).'/working-dir';
  579. return array(
  580. 'Foo' => array($baseDir . '/src'),
  581. 'Bar' => array($vendorDir . '/b/b/lib'),
  582. );
  583. EOF;
  584. $expectedClassmap = <<<'EOF'
  585. <?php
  586. // autoload_classmap.php generated by Composer
  587. $vendorDir = dirname(dirname(__FILE__));
  588. $baseDir = dirname($vendorDir).'/working-dir';
  589. return array(
  590. 'Bar\\Bar' => $vendorDir . '/b/b/classmaps/classes.php',
  591. 'Bar\\Foo' => $vendorDir . '/b/b/lib/Bar/Foo.php',
  592. 'Foo\\Bar' => $baseDir . '/src/Foo/Bar.php',
  593. 'Foo\\Foo' => $baseDir . '/classmap/classes.php',
  594. );
  595. EOF;
  596. $this->assertEquals($expectedNamespace, file_get_contents($vendorDir.'/composer/autoload_namespaces.php'));
  597. $this->assertEquals($expectedClassmap, file_get_contents($vendorDir.'/composer/autoload_classmap.php'));
  598. $this->assertContains("require \$vendorDir . '/b/b/bootstrap.php';", file_get_contents($vendorDir.'/composer/autoload_real.php'));
  599. $this->assertContains("require \$baseDir . '/test.php';", file_get_contents($vendorDir.'/composer/autoload_real.php'));
  600. }
  601. public function testUpLevelRelativePaths()
  602. {
  603. $workingDir = $this->workingDir.'/working-dir';
  604. mkdir($workingDir, 0777, true);
  605. chdir($workingDir);
  606. $package = new Package('a', '1.0', '1.0');
  607. $package->setAutoload(array(
  608. 'psr-0' => array('Foo' => '../path/../src'),
  609. 'classmap' => array('../classmap'),
  610. 'files' => array('../test.php'),
  611. ));
  612. $this->repository->expects($this->once())
  613. ->method('getCanonicalPackages')
  614. ->will($this->returnValue(array()));
  615. $this->fs->ensureDirectoryExists($this->workingDir.'/src/Foo');
  616. $this->fs->ensureDirectoryExists($this->workingDir.'/classmap');
  617. file_put_contents($this->workingDir.'/src/Foo/Bar.php', '<?php namespace Foo; class Bar {}');
  618. file_put_contents($this->workingDir.'/classmap/classes.php', '<?php namespace Foo; class Foo {}');
  619. file_put_contents($this->workingDir.'/test.php', '<?php class Foo {}');
  620. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_14');
  621. $expectedNamespace = <<<'EOF'
  622. <?php
  623. // autoload_namespaces.php generated by Composer
  624. $vendorDir = dirname(dirname(__FILE__));
  625. $baseDir = dirname($vendorDir).'/working-dir';
  626. return array(
  627. 'Foo' => array($baseDir . '/../src'),
  628. );
  629. EOF;
  630. $expectedClassmap = <<<'EOF'
  631. <?php
  632. // autoload_classmap.php generated by Composer
  633. $vendorDir = dirname(dirname(__FILE__));
  634. $baseDir = dirname($vendorDir).'/working-dir';
  635. return array(
  636. 'Foo\\Bar' => $baseDir . '/../src/Foo/Bar.php',
  637. 'Foo\\Foo' => $baseDir . '/../classmap/classes.php',
  638. );
  639. EOF;
  640. $this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php'));
  641. $this->assertEquals($expectedClassmap, file_get_contents($this->vendorDir.'/composer/autoload_classmap.php'));
  642. $this->assertContains("require \$baseDir . '/../test.php';", file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
  643. }
  644. public function testEmptyPaths()
  645. {
  646. $package = new Package('a', '1.0', '1.0');
  647. $package->setAutoload(array(
  648. 'psr-0' => array('Foo' => ''),
  649. 'classmap' => array(''),
  650. ));
  651. $this->repository->expects($this->once())
  652. ->method('getCanonicalPackages')
  653. ->will($this->returnValue(array()));
  654. $this->fs->ensureDirectoryExists($this->workingDir.'/Foo');
  655. file_put_contents($this->workingDir.'/Foo/Bar.php', '<?php namespace Foo; class Bar {}');
  656. file_put_contents($this->workingDir.'/class.php', '<?php namespace Classmap; class Foo {}');
  657. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_15');
  658. $expectedNamespace = <<<'EOF'
  659. <?php
  660. // autoload_namespaces.php generated by Composer
  661. $vendorDir = dirname(dirname(__FILE__));
  662. $baseDir = dirname($vendorDir);
  663. return array(
  664. 'Foo' => array($baseDir . '/'),
  665. );
  666. EOF;
  667. $expectedClassmap = <<<'EOF'
  668. <?php
  669. // autoload_classmap.php generated by Composer
  670. $vendorDir = dirname(dirname(__FILE__));
  671. $baseDir = dirname($vendorDir);
  672. return array(
  673. 'Classmap\\Foo' => $baseDir . '/class.php',
  674. 'Foo\\Bar' => $baseDir . '/Foo/Bar.php',
  675. );
  676. EOF;
  677. $this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php'));
  678. $this->assertEquals($expectedClassmap, file_get_contents($this->vendorDir.'/composer/autoload_classmap.php'));
  679. }
  680. private function assertAutoloadFiles($name, $dir, $type = 'namespaces')
  681. {
  682. $a = __DIR__.'/Fixtures/autoload_'.$name.'.php';
  683. $b = $dir.'/autoload_'.$type.'.php';
  684. $this->assertFileEquals($a, $b);
  685. }
  686. }