AutoloadGeneratorTest.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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\EventDispatcher\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->assertFileEquals(__DIR__.'/Fixtures/autoload_files_target_dir.php', $this->vendorDir.'/composer/autoload_files.php');
  158. $this->assertAutoloadFiles('classmap6', $this->vendorDir.'/composer', 'classmap');
  159. }
  160. public function testVendorsAutoloading()
  161. {
  162. $package = new Package('a', '1.0', '1.0');
  163. $packages = array();
  164. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  165. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  166. $packages[] = $c = new AliasPackage($b, '1.2', '1.2');
  167. $a->setAutoload(array('psr-0' => array('A' => 'src/', 'A\\B' => 'lib/')));
  168. $b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/')));
  169. $this->repository->expects($this->once())
  170. ->method('getCanonicalPackages')
  171. ->will($this->returnValue($packages));
  172. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  173. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  174. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib');
  175. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  176. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_5');
  177. $this->assertAutoloadFiles('vendors', $this->vendorDir.'/composer');
  178. $this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated, even if empty.");
  179. }
  180. public function testPSR0ToClassMapIgnoresNonExistingDir()
  181. {
  182. $package = new Package('a', '1.0', '1.0');
  183. $package->setAutoload(array('psr-0' => array('foo/bar/non/existing/')));
  184. $this->repository->expects($this->once())
  185. ->method('getCanonicalPackages')
  186. ->will($this->returnValue(array()));
  187. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_8');
  188. $this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated.");
  189. $this->assertEquals(
  190. array(),
  191. include $this->vendorDir.'/composer/autoload_classmap.php'
  192. );
  193. }
  194. public function testVendorsClassMapAutoloading()
  195. {
  196. $package = new Package('a', '1.0', '1.0');
  197. $packages = array();
  198. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  199. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  200. $a->setAutoload(array('classmap' => array('src/')));
  201. $b->setAutoload(array('classmap' => array('src/', 'lib/')));
  202. $this->repository->expects($this->once())
  203. ->method('getCanonicalPackages')
  204. ->will($this->returnValue($packages));
  205. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  206. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  207. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  208. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/lib');
  209. file_put_contents($this->vendorDir.'/a/a/src/a.php', '<?php class ClassMapFoo {}');
  210. file_put_contents($this->vendorDir.'/b/b/src/b.php', '<?php class ClassMapBar {}');
  211. file_put_contents($this->vendorDir.'/b/b/lib/c.php', '<?php class ClassMapBaz {}');
  212. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_6');
  213. $this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated.");
  214. $this->assertEquals(
  215. array(
  216. 'ClassMapBar' => $this->vendorDir.'/b/b/src/b.php',
  217. 'ClassMapBaz' => $this->vendorDir.'/b/b/lib/c.php',
  218. 'ClassMapFoo' => $this->vendorDir.'/a/a/src/a.php',
  219. ),
  220. include $this->vendorDir.'/composer/autoload_classmap.php'
  221. );
  222. $this->assertAutoloadFiles('classmap4', $this->vendorDir.'/composer', 'classmap');
  223. }
  224. public function testVendorsClassMapAutoloadingWithTargetDir()
  225. {
  226. $package = new Package('a', '1.0', '1.0');
  227. $packages = array();
  228. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  229. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  230. $a->setAutoload(array('classmap' => array('target/src/', 'lib/')));
  231. $a->setTargetDir('target');
  232. $b->setAutoload(array('classmap' => array('src/')));
  233. $this->repository->expects($this->once())
  234. ->method('getCanonicalPackages')
  235. ->will($this->returnValue($packages));
  236. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  237. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/target/src');
  238. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/target/lib');
  239. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  240. file_put_contents($this->vendorDir.'/a/a/target/src/a.php', '<?php class ClassMapFoo {}');
  241. file_put_contents($this->vendorDir.'/a/a/target/lib/b.php', '<?php class ClassMapBar {}');
  242. file_put_contents($this->vendorDir.'/b/b/src/c.php', '<?php class ClassMapBaz {}');
  243. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_6');
  244. $this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated.");
  245. $this->assertEquals(
  246. array(
  247. 'ClassMapBar' => $this->vendorDir.'/a/a/target/lib/b.php',
  248. 'ClassMapBaz' => $this->vendorDir.'/b/b/src/c.php',
  249. 'ClassMapFoo' => $this->vendorDir.'/a/a/target/src/a.php',
  250. ),
  251. include $this->vendorDir.'/composer/autoload_classmap.php'
  252. );
  253. }
  254. public function testClassMapAutoloadingEmptyDirAndExactFile()
  255. {
  256. $package = new Package('a', '1.0', '1.0');
  257. $packages = array();
  258. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  259. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  260. $packages[] = $c = new Package('c/c', '1.0', '1.0');
  261. $a->setAutoload(array('classmap' => array('')));
  262. $b->setAutoload(array('classmap' => array('test.php')));
  263. $c->setAutoload(array('classmap' => array('./')));
  264. $this->repository->expects($this->once())
  265. ->method('getCanonicalPackages')
  266. ->will($this->returnValue($packages));
  267. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  268. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  269. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b');
  270. $this->fs->ensureDirectoryExists($this->vendorDir.'/c/c/foo');
  271. file_put_contents($this->vendorDir.'/a/a/src/a.php', '<?php class ClassMapFoo {}');
  272. file_put_contents($this->vendorDir.'/b/b/test.php', '<?php class ClassMapBar {}');
  273. file_put_contents($this->vendorDir.'/c/c/foo/test.php', '<?php class ClassMapBaz {}');
  274. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_7');
  275. $this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated.");
  276. $this->assertEquals(
  277. array(
  278. 'ClassMapBar' => $this->vendorDir.'/b/b/test.php',
  279. 'ClassMapBaz' => $this->vendorDir.'/c/c/foo/test.php',
  280. 'ClassMapFoo' => $this->vendorDir.'/a/a/src/a.php',
  281. ),
  282. include $this->vendorDir.'/composer/autoload_classmap.php'
  283. );
  284. $this->assertAutoloadFiles('classmap5', $this->vendorDir.'/composer', 'classmap');
  285. }
  286. public function testFilesAutoloadGeneration()
  287. {
  288. $package = new Package('a', '1.0', '1.0');
  289. $package->setAutoload(array('files' => array('root.php')));
  290. $packages = array();
  291. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  292. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  293. $packages[] = $c = new Package('c/c', '1.0', '1.0');
  294. $a->setAutoload(array('files' => array('test.php')));
  295. $b->setAutoload(array('files' => array('test2.php')));
  296. $c->setAutoload(array('files' => array('test3.php', 'foo/bar/test4.php')));
  297. $c->setTargetDir('foo/bar');
  298. $this->repository->expects($this->once())
  299. ->method('getCanonicalPackages')
  300. ->will($this->returnValue($packages));
  301. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a');
  302. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b');
  303. $this->fs->ensureDirectoryExists($this->vendorDir.'/c/c/foo/bar');
  304. file_put_contents($this->vendorDir.'/a/a/test.php', '<?php function testFilesAutoloadGeneration1() {}');
  305. file_put_contents($this->vendorDir.'/b/b/test2.php', '<?php function testFilesAutoloadGeneration2() {}');
  306. file_put_contents($this->vendorDir.'/c/c/foo/bar/test3.php', '<?php function testFilesAutoloadGeneration3() {}');
  307. file_put_contents($this->vendorDir.'/c/c/foo/bar/test4.php', '<?php function testFilesAutoloadGeneration4() {}');
  308. file_put_contents($this->workingDir.'/root.php', '<?php function testFilesAutoloadGenerationRoot() {}');
  309. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'FilesAutoload');
  310. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php');
  311. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_functions.php', $this->vendorDir.'/composer/autoload_real.php');
  312. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_files_functions.php', $this->vendorDir.'/composer/autoload_files.php');
  313. include $this->vendorDir . '/autoload.php';
  314. $this->assertTrue(function_exists('testFilesAutoloadGeneration1'));
  315. $this->assertTrue(function_exists('testFilesAutoloadGeneration2'));
  316. $this->assertTrue(function_exists('testFilesAutoloadGeneration3'));
  317. $this->assertTrue(function_exists('testFilesAutoloadGeneration4'));
  318. $this->assertTrue(function_exists('testFilesAutoloadGenerationRoot'));
  319. }
  320. public function testFilesAutoloadOrderByDependencies()
  321. {
  322. $package = new Package('a', '1.0', '1.0');
  323. $package->setAutoload(array('files' => array('root.php')));
  324. $package->setRequires(array(new Link('a', 'z/foo')));
  325. $package->setRequires(array(new Link('a', 'd/d')));
  326. $package->setRequires(array(new Link('a', 'e/e')));
  327. $packages = array();
  328. $packages[] = $z = new Package('z/foo', '1.0', '1.0');
  329. $packages[] = $b = new Package('b/bar', '1.0', '1.0');
  330. $packages[] = $d = new Package('d/d', '1.0', '1.0');
  331. $packages[] = $c = new Package('c/lorem', '1.0', '1.0');
  332. $packages[] = $e = new Package('e/e', '1.0', '1.0');
  333. $z->setAutoload(array('files' => array('testA.php')));
  334. $z->setRequires(array(new Link('z/foo', 'c/lorem')));
  335. $b->setAutoload(array('files' => array('testB.php')));
  336. $b->setRequires(array(new Link('b/bar', 'c/lorem'), new Link('b/bar', 'd/d')));
  337. $c->setAutoload(array('files' => array('testC.php')));
  338. $d->setAutoload(array('files' => array('testD.php')));
  339. $d->setRequires(array(new Link('d/d', 'c/lorem')));
  340. $e->setAutoload(array('files' => array('testE.php')));
  341. $e->setRequires(array(new Link('e/e', 'c/lorem')));
  342. $this->repository->expects($this->once())
  343. ->method('getCanonicalPackages')
  344. ->will($this->returnValue($packages));
  345. $this->fs->ensureDirectoryExists($this->vendorDir . '/z/foo');
  346. $this->fs->ensureDirectoryExists($this->vendorDir . '/b/bar');
  347. $this->fs->ensureDirectoryExists($this->vendorDir . '/c/lorem');
  348. $this->fs->ensureDirectoryExists($this->vendorDir . '/d/d');
  349. $this->fs->ensureDirectoryExists($this->vendorDir . '/e/e');
  350. file_put_contents($this->vendorDir . '/z/foo/testA.php', '<?php function testFilesAutoloadOrderByDependency1() {}');
  351. file_put_contents($this->vendorDir . '/b/bar/testB.php', '<?php function testFilesAutoloadOrderByDependency2() {}');
  352. file_put_contents($this->vendorDir . '/c/lorem/testC.php', '<?php function testFilesAutoloadOrderByDependency3() {}');
  353. file_put_contents($this->vendorDir . '/d/d/testD.php', '<?php function testFilesAutoloadOrderByDependency4() {}');
  354. file_put_contents($this->vendorDir . '/e/e/testE.php', '<?php function testFilesAutoloadOrderByDependency5() {}');
  355. file_put_contents($this->workingDir . '/root.php', '<?php function testFilesAutoloadOrderByDependencyRoot() {}');
  356. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'FilesAutoloadOrder');
  357. $this->assertFileEquals(__DIR__ . '/Fixtures/autoload_functions_by_dependency.php', $this->vendorDir . '/autoload.php');
  358. $this->assertFileEquals(__DIR__ . '/Fixtures/autoload_real_files_by_dependency.php', $this->vendorDir . '/composer/autoload_real.php');
  359. require $this->vendorDir . '/autoload.php';
  360. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency1'));
  361. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency2'));
  362. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency3'));
  363. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency4'));
  364. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency5'));
  365. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependencyRoot'));
  366. }
  367. public function testOverrideVendorsAutoloading()
  368. {
  369. $package = new Package('z', '1.0', '1.0');
  370. $package->setAutoload(array('psr-0' => array('A\\B' => $this->workingDir.'/lib'), 'classmap' => array($this->workingDir.'/src')));
  371. $package->setRequires(array(new Link('z', 'a/a')));
  372. $packages = array();
  373. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  374. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  375. $a->setAutoload(array('psr-0' => array('A' => 'src/', 'A\\B' => 'lib/'), 'classmap' => array('classmap')));
  376. $b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/')));
  377. $this->repository->expects($this->once())
  378. ->method('getCanonicalPackages')
  379. ->will($this->returnValue($packages));
  380. $this->fs->ensureDirectoryExists($this->workingDir.'/lib/A/B');
  381. $this->fs->ensureDirectoryExists($this->workingDir.'/src/');
  382. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  383. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/classmap');
  384. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  385. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib/A/B');
  386. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  387. file_put_contents($this->workingDir.'/lib/A/B/C.php', '<?php namespace A\\B; class C {}');
  388. file_put_contents($this->workingDir.'/src/classes.php', '<?php namespace Foo; class Bar {}');
  389. file_put_contents($this->vendorDir.'/a/a/lib/A/B/C.php', '<?php namespace A\\B; class C {}');
  390. file_put_contents($this->vendorDir.'/a/a/classmap/classes.php', '<?php namespace Foo; class Bar {}');
  391. $expectedNamespace = <<<EOF
  392. <?php
  393. // autoload_namespaces.php generated by Composer
  394. \$vendorDir = dirname(dirname(__FILE__));
  395. \$baseDir = dirname(\$vendorDir);
  396. return array(
  397. 'B\\\\Sub\\\\Name' => array(\$vendorDir . '/b/b/src'),
  398. 'A\\\\B' => array(\$baseDir . '/lib', \$vendorDir . '/a/a/lib'),
  399. 'A' => array(\$vendorDir . '/a/a/src'),
  400. );
  401. EOF;
  402. $expectedClassmap = <<<EOF
  403. <?php
  404. // autoload_classmap.php generated by Composer
  405. \$vendorDir = dirname(dirname(__FILE__));
  406. \$baseDir = dirname(\$vendorDir);
  407. return array(
  408. 'A\\\\B\\\\C' => \$baseDir . '/lib/A/B/C.php',
  409. 'Foo\\\\Bar' => \$baseDir . '/src/classes.php',
  410. );
  411. EOF;
  412. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_9');
  413. $this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php'));
  414. $this->assertEquals($expectedClassmap, file_get_contents($this->vendorDir.'/composer/autoload_classmap.php'));
  415. }
  416. public function testIncludePathFileGeneration()
  417. {
  418. $package = new Package('a', '1.0', '1.0');
  419. $packages = array();
  420. $a = new Package("a/a", "1.0", "1.0");
  421. $a->setIncludePaths(array("lib/"));
  422. $b = new Package("b/b", "1.0", "1.0");
  423. $b->setIncludePaths(array("library"));
  424. $c = new Package("c", "1.0", "1.0");
  425. $c->setIncludePaths(array("library"));
  426. $packages[] = $a;
  427. $packages[] = $b;
  428. $packages[] = $c;
  429. $this->repository->expects($this->once())
  430. ->method("getCanonicalPackages")
  431. ->will($this->returnValue($packages));
  432. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  433. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_10');
  434. $this->assertFileEquals(__DIR__.'/Fixtures/include_paths.php', $this->vendorDir.'/composer/include_paths.php');
  435. $this->assertEquals(
  436. array(
  437. $this->vendorDir."/a/a/lib",
  438. $this->vendorDir."/b/b/library",
  439. $this->vendorDir."/c/library",
  440. ),
  441. require $this->vendorDir."/composer/include_paths.php"
  442. );
  443. }
  444. public function testIncludePathsArePrependedInAutoloadFile()
  445. {
  446. $package = new Package('a', '1.0', '1.0');
  447. $packages = array();
  448. $a = new Package("a/a", "1.0", "1.0");
  449. $a->setIncludePaths(array("lib/"));
  450. $packages[] = $a;
  451. $this->repository->expects($this->once())
  452. ->method("getCanonicalPackages")
  453. ->will($this->returnValue($packages));
  454. mkdir($this->vendorDir."/composer", 0777, true);
  455. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_11');
  456. $oldIncludePath = get_include_path();
  457. require $this->vendorDir."/autoload.php";
  458. $this->assertEquals(
  459. $this->vendorDir."/a/a/lib".PATH_SEPARATOR.$oldIncludePath,
  460. get_include_path()
  461. );
  462. set_include_path($oldIncludePath);
  463. }
  464. public function testIncludePathsInMainPackage()
  465. {
  466. $package = new Package('a', '1.0', '1.0');
  467. $package->setIncludePaths(array('/lib', '/src'));
  468. $packages = array($a = new Package("a/a", "1.0", "1.0"));
  469. $a->setIncludePaths(array("lib/"));
  470. $this->repository->expects($this->once())
  471. ->method("getCanonicalPackages")
  472. ->will($this->returnValue($packages));
  473. mkdir($this->vendorDir."/composer", 0777, true);
  474. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_12');
  475. $oldIncludePath = get_include_path();
  476. require $this->vendorDir."/autoload.php";
  477. $this->assertEquals(
  478. $this->workingDir."/lib".PATH_SEPARATOR.$this->workingDir."/src".PATH_SEPARATOR.$this->vendorDir."/a/a/lib".PATH_SEPARATOR.$oldIncludePath,
  479. get_include_path()
  480. );
  481. set_include_path($oldIncludePath);
  482. }
  483. public function testIncludePathFileWithoutPathsIsSkipped()
  484. {
  485. $package = new Package('a', '1.0', '1.0');
  486. $packages = array();
  487. $a = new Package("a/a", "1.0", "1.0");
  488. $packages[] = $a;
  489. $this->repository->expects($this->once())
  490. ->method("getCanonicalPackages")
  491. ->will($this->returnValue($packages));
  492. mkdir($this->vendorDir."/composer", 0777, true);
  493. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_12');
  494. $this->assertFalse(file_exists($this->vendorDir."/composer/include_paths.php"));
  495. }
  496. public function testPreAndPostEventsAreDispatchedDuringAutoloadDump()
  497. {
  498. $this->eventDispatcher
  499. ->expects($this->at(0))
  500. ->method('dispatchScript')
  501. ->with(ScriptEvents::PRE_AUTOLOAD_DUMP, false);
  502. $this->eventDispatcher
  503. ->expects($this->at(1))
  504. ->method('dispatchScript')
  505. ->with(ScriptEvents::POST_AUTOLOAD_DUMP, false);
  506. $package = new Package('a', '1.0', '1.0');
  507. $package->setAutoload(array('psr-0' => array('foo/bar/non/existing/')));
  508. $this->repository->expects($this->once())
  509. ->method('getCanonicalPackages')
  510. ->will($this->returnValue(array()));
  511. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_8');
  512. }
  513. public function testUseGlobalIncludePath()
  514. {
  515. $package = new Package('a', '1.0', '1.0');
  516. $package->setAutoload(array(
  517. 'psr-0' => array('Main\\Foo' => '', 'Main\\Bar' => ''),
  518. ));
  519. $package->setTargetDir('Main/Foo/');
  520. $this->repository->expects($this->once())
  521. ->method('getCanonicalPackages')
  522. ->will($this->returnValue(array()));
  523. $this->config->expects($this->at(2))
  524. ->method('get')
  525. ->with($this->equalTo('use-include-path'))
  526. ->will($this->returnValue(true));
  527. $this->fs->ensureDirectoryExists($this->vendorDir.'/a');
  528. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'IncludePath');
  529. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_include_path.php', $this->vendorDir.'/composer/autoload_real.php');
  530. }
  531. public function testVendorDirExcludedFromWorkingDir()
  532. {
  533. $workingDir = $this->vendorDir.'/working-dir';
  534. $vendorDir = $workingDir.'/../vendor';
  535. $this->fs->ensureDirectoryExists($workingDir);
  536. chdir($workingDir);
  537. $package = new Package('a', '1.0', '1.0');
  538. $package->setAutoload(array(
  539. 'psr-0' => array('Foo' => 'src'),
  540. 'classmap' => array('classmap'),
  541. 'files' => array('test.php'),
  542. ));
  543. $vendorPackage = new Package('b/b', '1.0', '1.0');
  544. $vendorPackage->setAutoload(array(
  545. 'psr-0' => array('Bar' => 'lib'),
  546. 'classmap' => array('classmaps'),
  547. 'files' => array('bootstrap.php'),
  548. ));
  549. $this->repository->expects($this->once())
  550. ->method('getCanonicalPackages')
  551. ->will($this->returnValue(array($vendorPackage)));
  552. $im = $this->getMockBuilder('Composer\Installer\InstallationManager')
  553. ->disableOriginalConstructor()
  554. ->getMock();
  555. $im->expects($this->any())
  556. ->method('getInstallPath')
  557. ->will($this->returnCallback(function ($package) use ($vendorDir) {
  558. $targetDir = $package->getTargetDir();
  559. return $vendorDir.'/'.$package->getName() . ($targetDir ? '/'.$targetDir : '');
  560. }));
  561. $this->fs->ensureDirectoryExists($workingDir.'/src/Foo');
  562. $this->fs->ensureDirectoryExists($workingDir.'/classmap');
  563. $this->fs->ensureDirectoryExists($vendorDir.'/composer');
  564. $this->fs->ensureDirectoryExists($vendorDir.'/b/b/lib/Bar');
  565. $this->fs->ensureDirectoryExists($vendorDir.'/b/b/classmaps');
  566. file_put_contents($workingDir.'/src/Foo/Bar.php', '<?php namespace Foo; class Bar {}');
  567. file_put_contents($workingDir.'/classmap/classes.php', '<?php namespace Foo; class Foo {}');
  568. file_put_contents($workingDir.'/test.php', '<?php class Foo {}');
  569. file_put_contents($vendorDir.'/b/b/lib/Bar/Foo.php', '<?php namespace Bar; class Foo {}');
  570. file_put_contents($vendorDir.'/b/b/classmaps/classes.php', '<?php namespace Bar; class Bar {}');
  571. file_put_contents($vendorDir.'/b/b/bootstrap.php', '<?php class Bar {}');
  572. $oldVendorDir = $this->vendorDir;
  573. $this->vendorDir = $vendorDir;
  574. $this->generator->dump($this->config, $this->repository, $package, $im, 'composer', true, '_13');
  575. $this->vendorDir = $oldVendorDir;
  576. $expectedNamespace = <<<'EOF'
  577. <?php
  578. // autoload_namespaces.php generated by Composer
  579. $vendorDir = dirname(dirname(__FILE__));
  580. $baseDir = dirname($vendorDir).'/working-dir';
  581. return array(
  582. 'Foo' => array($baseDir . '/src'),
  583. 'Bar' => array($vendorDir . '/b/b/lib'),
  584. );
  585. EOF;
  586. $expectedClassmap = <<<'EOF'
  587. <?php
  588. // autoload_classmap.php generated by Composer
  589. $vendorDir = dirname(dirname(__FILE__));
  590. $baseDir = dirname($vendorDir).'/working-dir';
  591. return array(
  592. 'Bar\\Bar' => $vendorDir . '/b/b/classmaps/classes.php',
  593. 'Bar\\Foo' => $vendorDir . '/b/b/lib/Bar/Foo.php',
  594. 'Foo\\Bar' => $baseDir . '/src/Foo/Bar.php',
  595. 'Foo\\Foo' => $baseDir . '/classmap/classes.php',
  596. );
  597. EOF;
  598. $this->assertEquals($expectedNamespace, file_get_contents($vendorDir.'/composer/autoload_namespaces.php'));
  599. $this->assertEquals($expectedClassmap, file_get_contents($vendorDir.'/composer/autoload_classmap.php'));
  600. $this->assertContains("\n \$vendorDir . '/b/b/bootstrap.php',\n", file_get_contents($vendorDir.'/composer/autoload_files.php'));
  601. $this->assertContains("\n \$baseDir . '/test.php',\n", file_get_contents($vendorDir.'/composer/autoload_files.php'));
  602. }
  603. public function testUpLevelRelativePaths()
  604. {
  605. $workingDir = $this->workingDir.'/working-dir';
  606. mkdir($workingDir, 0777, true);
  607. chdir($workingDir);
  608. $package = new Package('a', '1.0', '1.0');
  609. $package->setAutoload(array(
  610. 'psr-0' => array('Foo' => '../path/../src'),
  611. 'classmap' => array('../classmap'),
  612. 'files' => array('../test.php'),
  613. ));
  614. $this->repository->expects($this->once())
  615. ->method('getCanonicalPackages')
  616. ->will($this->returnValue(array()));
  617. $this->fs->ensureDirectoryExists($this->workingDir.'/src/Foo');
  618. $this->fs->ensureDirectoryExists($this->workingDir.'/classmap');
  619. file_put_contents($this->workingDir.'/src/Foo/Bar.php', '<?php namespace Foo; class Bar {}');
  620. file_put_contents($this->workingDir.'/classmap/classes.php', '<?php namespace Foo; class Foo {}');
  621. file_put_contents($this->workingDir.'/test.php', '<?php class Foo {}');
  622. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_14');
  623. $expectedNamespace = <<<'EOF'
  624. <?php
  625. // autoload_namespaces.php generated by Composer
  626. $vendorDir = dirname(dirname(__FILE__));
  627. $baseDir = dirname($vendorDir).'/working-dir';
  628. return array(
  629. 'Foo' => array($baseDir . '/../src'),
  630. );
  631. EOF;
  632. $expectedClassmap = <<<'EOF'
  633. <?php
  634. // autoload_classmap.php generated by Composer
  635. $vendorDir = dirname(dirname(__FILE__));
  636. $baseDir = dirname($vendorDir).'/working-dir';
  637. return array(
  638. 'Foo\\Bar' => $baseDir . '/../src/Foo/Bar.php',
  639. 'Foo\\Foo' => $baseDir . '/../classmap/classes.php',
  640. );
  641. EOF;
  642. $this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php'));
  643. $this->assertEquals($expectedClassmap, file_get_contents($this->vendorDir.'/composer/autoload_classmap.php'));
  644. $this->assertContains("\n \$baseDir . '/../test.php',\n", file_get_contents($this->vendorDir.'/composer/autoload_files.php'));
  645. }
  646. public function testEmptyPaths()
  647. {
  648. $package = new Package('a', '1.0', '1.0');
  649. $package->setAutoload(array(
  650. 'psr-0' => array('Foo' => ''),
  651. 'classmap' => array(''),
  652. ));
  653. $this->repository->expects($this->once())
  654. ->method('getCanonicalPackages')
  655. ->will($this->returnValue(array()));
  656. $this->fs->ensureDirectoryExists($this->workingDir.'/Foo');
  657. file_put_contents($this->workingDir.'/Foo/Bar.php', '<?php namespace Foo; class Bar {}');
  658. file_put_contents($this->workingDir.'/class.php', '<?php namespace Classmap; class Foo {}');
  659. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_15');
  660. $expectedNamespace = <<<'EOF'
  661. <?php
  662. // autoload_namespaces.php generated by Composer
  663. $vendorDir = dirname(dirname(__FILE__));
  664. $baseDir = dirname($vendorDir);
  665. return array(
  666. 'Foo' => array($baseDir . '/'),
  667. );
  668. EOF;
  669. $expectedClassmap = <<<'EOF'
  670. <?php
  671. // autoload_classmap.php generated by Composer
  672. $vendorDir = dirname(dirname(__FILE__));
  673. $baseDir = dirname($vendorDir);
  674. return array(
  675. 'Classmap\\Foo' => $baseDir . '/class.php',
  676. 'Foo\\Bar' => $baseDir . '/Foo/Bar.php',
  677. );
  678. EOF;
  679. $this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php'));
  680. $this->assertEquals($expectedClassmap, file_get_contents($this->vendorDir.'/composer/autoload_classmap.php'));
  681. }
  682. private function assertAutoloadFiles($name, $dir, $type = 'namespaces')
  683. {
  684. $a = __DIR__.'/Fixtures/autoload_'.$name.'.php';
  685. $b = $dir.'/autoload_'.$type.'.php';
  686. $this->assertFileEquals($a, $b);
  687. }
  688. }