AutoloadGeneratorTest.php 39 KB

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