AutoloadGeneratorTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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\Util\Filesystem;
  14. use Composer\Package\AliasPackage;
  15. use Composer\Package\MemoryPackage;
  16. use Composer\Test\TestCase;
  17. class AutoloadGeneratorTest extends TestCase
  18. {
  19. public $vendorDir;
  20. private $config;
  21. private $workingDir;
  22. private $im;
  23. private $repository;
  24. private $generator;
  25. private $fs;
  26. protected function setUp()
  27. {
  28. $this->fs = new Filesystem;
  29. $that = $this;
  30. $this->workingDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'cmptest';
  31. $this->fs->ensureDirectoryExists($this->workingDir);
  32. $this->vendorDir = $this->workingDir.DIRECTORY_SEPARATOR.'composer-test-autoload';
  33. $this->ensureDirectoryExistsAndClear($this->vendorDir);
  34. $this->config = $this->getMock('Composer\Config');
  35. $this->config->expects($this->any())
  36. ->method('get')
  37. ->with($this->equalTo('vendor-dir'))
  38. ->will($this->returnCallback(function () use ($that) {
  39. return $that->vendorDir;
  40. }));
  41. $this->dir = getcwd();
  42. chdir($this->workingDir);
  43. $this->im = $this->getMockBuilder('Composer\Installer\InstallationManager')
  44. ->disableOriginalConstructor()
  45. ->getMock();
  46. $this->im->expects($this->any())
  47. ->method('getInstallPath')
  48. ->will($this->returnCallback(function ($package) use ($that) {
  49. return $that->vendorDir.'/'.$package->getName();
  50. }));
  51. $this->repository = $this->getMock('Composer\Repository\RepositoryInterface');
  52. $this->generator = new AutoloadGenerator();
  53. }
  54. protected function tearDown()
  55. {
  56. chdir($this->dir);
  57. if (is_dir($this->workingDir)) {
  58. $this->fs->removeDirectory($this->workingDir);
  59. }
  60. if (is_dir($this->vendorDir)) {
  61. $this->fs->removeDirectory($this->vendorDir);
  62. }
  63. }
  64. public function testMainPackageAutoloading()
  65. {
  66. $package = new MemoryPackage('a', '1.0', '1.0');
  67. $package->setAutoload(array(
  68. 'psr-0' => array('Main' => 'src/', 'Lala' => array('src/', 'lib/')),
  69. 'classmap' => array('composersrc/'),
  70. ));
  71. $this->repository->expects($this->once())
  72. ->method('getPackages')
  73. ->will($this->returnValue(array()));
  74. $this->fs->ensureDirectoryExists($this->workingDir.'/composer');
  75. $this->fs->ensureDirectoryExists($this->workingDir.'/src');
  76. $this->fs->ensureDirectoryExists($this->workingDir.'/lib');
  77. $this->createClassFile($this->workingDir);
  78. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_1');
  79. $this->assertAutoloadFiles('main', $this->vendorDir.'/composer');
  80. $this->assertAutoloadFiles('classmap', $this->vendorDir.'/composer', 'classmap');
  81. }
  82. public function testVendorDirSameAsWorkingDir()
  83. {
  84. $this->vendorDir = $this->workingDir;
  85. $package = new MemoryPackage('a', '1.0', '1.0');
  86. $package->setAutoload(array(
  87. 'psr-0' => array('Main' => 'src/', 'Lala' => 'src/'),
  88. 'classmap' => array('composersrc/'),
  89. ));
  90. $this->repository->expects($this->once())
  91. ->method('getPackages')
  92. ->will($this->returnValue(array()));
  93. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  94. $this->fs->ensureDirectoryExists($this->vendorDir.'/src/Main');
  95. file_put_contents($this->vendorDir.'/src/Main/Foo.php', '<?php namespace Main; class Foo {}');
  96. $this->createClassFile($this->vendorDir);
  97. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_2');
  98. $this->assertAutoloadFiles('main3', $this->vendorDir.'/composer');
  99. $this->assertAutoloadFiles('classmap3', $this->vendorDir.'/composer', 'classmap');
  100. }
  101. public function testMainPackageAutoloadingAlternativeVendorDir()
  102. {
  103. $package = new MemoryPackage('a', '1.0', '1.0');
  104. $package->setAutoload(array(
  105. 'psr-0' => array('Main' => 'src/', 'Lala' => 'src/'),
  106. 'classmap' => array('composersrc/'),
  107. ));
  108. $this->repository->expects($this->once())
  109. ->method('getPackages')
  110. ->will($this->returnValue(array()));
  111. $this->vendorDir .= '/subdir';
  112. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  113. $this->fs->ensureDirectoryExists($this->workingDir.'/src');
  114. $this->createClassFile($this->workingDir);
  115. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_3');
  116. $this->assertAutoloadFiles('main2', $this->vendorDir.'/composer');
  117. $this->assertAutoloadFiles('classmap2', $this->vendorDir.'/composer', 'classmap');
  118. }
  119. public function testMainPackageAutoloadingWithTargetDir()
  120. {
  121. $package = new MemoryPackage('a', '1.0', '1.0');
  122. $package->setAutoload(array(
  123. 'psr-0' => array('Main\\Foo' => '', 'Main\\Bar' => ''),
  124. ));
  125. $package->setTargetDir('Main/Foo/');
  126. $this->repository->expects($this->once())
  127. ->method('getPackages')
  128. ->will($this->returnValue(array()));
  129. $this->fs->ensureDirectoryExists($this->vendorDir.'/a');
  130. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'TargetDir');
  131. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_target_dir.php', $this->vendorDir.'/autoload.php');
  132. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_target_dir.php', $this->vendorDir.'/composer/autoload_realTargetDir.php');
  133. }
  134. public function testVendorsAutoloading()
  135. {
  136. $package = new MemoryPackage('a', '1.0', '1.0');
  137. $packages = array();
  138. $packages[] = $a = new MemoryPackage('a/a', '1.0', '1.0');
  139. $packages[] = $b = new MemoryPackage('b/b', '1.0', '1.0');
  140. $packages[] = $c = new AliasPackage($b, '1.2', '1.2');
  141. $a->setAutoload(array('psr-0' => array('A' => 'src/', 'A\\B' => 'lib/')));
  142. $b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/')));
  143. $this->repository->expects($this->once())
  144. ->method('getPackages')
  145. ->will($this->returnValue($packages));
  146. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  147. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  148. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib');
  149. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  150. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_5');
  151. $this->assertAutoloadFiles('vendors', $this->vendorDir.'/composer');
  152. $this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated, even if empty.");
  153. }
  154. public function testVendorsClassMapAutoloading()
  155. {
  156. $package = new MemoryPackage('a', '1.0', '1.0');
  157. $packages = array();
  158. $packages[] = $a = new MemoryPackage('a/a', '1.0', '1.0');
  159. $packages[] = $b = new MemoryPackage('b/b', '1.0', '1.0');
  160. $a->setAutoload(array('classmap' => array('src/')));
  161. $b->setAutoload(array('classmap' => array('src/', 'lib/')));
  162. $this->repository->expects($this->once())
  163. ->method('getPackages')
  164. ->will($this->returnValue($packages));
  165. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  166. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  167. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  168. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/lib');
  169. file_put_contents($this->vendorDir.'/a/a/src/a.php', '<?php class ClassMapFoo {}');
  170. file_put_contents($this->vendorDir.'/b/b/src/b.php', '<?php class ClassMapBar {}');
  171. file_put_contents($this->vendorDir.'/b/b/lib/c.php', '<?php class ClassMapBaz {}');
  172. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_6');
  173. $this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated.");
  174. $this->assertEquals(
  175. array(
  176. 'ClassMapFoo' => $this->workingDir.'/composer-test-autoload/a/a/src/a.php',
  177. 'ClassMapBar' => $this->workingDir.'/composer-test-autoload/b/b/src/b.php',
  178. 'ClassMapBaz' => $this->workingDir.'/composer-test-autoload/b/b/lib/c.php',
  179. ),
  180. include ($this->vendorDir.'/composer/autoload_classmap.php')
  181. );
  182. $this->assertAutoloadFiles('classmap4', $this->vendorDir.'/composer', 'classmap');
  183. }
  184. public function testClassMapAutoloadingEmptyDirAndExactFile()
  185. {
  186. $package = new MemoryPackage('a', '1.0', '1.0');
  187. $packages = array();
  188. $packages[] = $a = new MemoryPackage('a/a', '1.0', '1.0');
  189. $packages[] = $b = new MemoryPackage('b/b', '1.0', '1.0');
  190. $packages[] = $c = new MemoryPackage('c/c', '1.0', '1.0');
  191. $a->setAutoload(array('classmap' => array('')));
  192. $b->setAutoload(array('classmap' => array('test.php')));
  193. $c->setAutoload(array('classmap' => array('./')));
  194. $this->repository->expects($this->once())
  195. ->method('getPackages')
  196. ->will($this->returnValue($packages));
  197. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  198. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  199. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b');
  200. $this->fs->ensureDirectoryExists($this->vendorDir.'/c/c/foo');
  201. file_put_contents($this->vendorDir.'/a/a/src/a.php', '<?php class ClassMapFoo {}');
  202. file_put_contents($this->vendorDir.'/b/b/test.php', '<?php class ClassMapBar {}');
  203. file_put_contents($this->vendorDir.'/c/c/foo/test.php', '<?php class ClassMapBaz {}');
  204. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_7');
  205. $this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated.");
  206. $this->assertEquals(
  207. array(
  208. 'ClassMapFoo' => $this->workingDir.'/composer-test-autoload/a/a/src/a.php',
  209. 'ClassMapBar' => $this->workingDir.'/composer-test-autoload/b/b/test.php',
  210. 'ClassMapBaz' => $this->workingDir.'/composer-test-autoload/c/c/foo/test.php',
  211. ),
  212. include ($this->vendorDir.'/composer/autoload_classmap.php')
  213. );
  214. $this->assertAutoloadFiles('classmap5', $this->vendorDir.'/composer', 'classmap');
  215. }
  216. public function testFilesAutoloadGeneration()
  217. {
  218. $package = new MemoryPackage('a', '1.0', '1.0');
  219. $package->setAutoload(array('files' => array('root.php')));
  220. $packages = array();
  221. $packages[] = $a = new MemoryPackage('a/a', '1.0', '1.0');
  222. $packages[] = $b = new MemoryPackage('b/b', '1.0', '1.0');
  223. $a->setAutoload(array('files' => array('test.php')));
  224. $b->setAutoload(array('files' => array('test2.php')));
  225. $this->repository->expects($this->once())
  226. ->method('getPackages')
  227. ->will($this->returnValue($packages));
  228. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a');
  229. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b');
  230. file_put_contents($this->vendorDir.'/a/a/test.php', '<?php function testFilesAutoloadGeneration1() {}');
  231. file_put_contents($this->vendorDir.'/b/b/test2.php', '<?php function testFilesAutoloadGeneration2() {}');
  232. file_put_contents($this->workingDir.'/root.php', '<?php function testFilesAutoloadGenerationRoot() {}');
  233. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'FilesAutoload');
  234. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php');
  235. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_functions.php', $this->vendorDir.'/composer/autoload_realFilesAutoload.php');
  236. // suppress the class loader to avoid fatals if the class is redefined
  237. file_put_contents($this->vendorDir.'/composer/ClassLoader.php', '');
  238. include $this->vendorDir . '/autoload.php';
  239. $this->assertTrue(function_exists('testFilesAutoloadGeneration1'));
  240. $this->assertTrue(function_exists('testFilesAutoloadGeneration2'));
  241. $this->assertTrue(function_exists('testFilesAutoloadGenerationRoot'));
  242. }
  243. public function testOverrideVendorsAutoloading()
  244. {
  245. $package = new MemoryPackage('a', '1.0', '1.0');
  246. $package->setAutoload(array('psr-0' => array('A\\B' => $this->workingDir.'/lib')));
  247. $packages = array();
  248. $packages[] = $a = new MemoryPackage('a/a', '1.0', '1.0');
  249. $packages[] = $b = new MemoryPackage('b/b', '1.0', '1.0');
  250. $a->setAutoload(array('psr-0' => array('A' => 'src/', 'A\\B' => 'lib/')));
  251. $b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/')));
  252. $this->repository->expects($this->once())
  253. ->method('getPackages')
  254. ->will($this->returnValue($packages));
  255. $this->fs->ensureDirectoryExists($this->workingDir.'/lib/A/B');
  256. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  257. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  258. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib/A/B');
  259. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  260. file_put_contents($this->workingDir.'/lib/A/B/C.php', '<?php namespace A\\B; class C {}');
  261. file_put_contents($this->vendorDir.'/a/a/lib/A/B/C.php', '<?php namespace A\\B; class C {}');
  262. $workDir = strtr($this->workingDir, '\\', '/');
  263. $expectedNamespace = <<<EOF
  264. <?php
  265. // autoload_namespaces.php generated by Composer
  266. \$vendorDir = dirname(__DIR__);
  267. \$baseDir = dirname(\$vendorDir);
  268. return array(
  269. 'B\\\\Sub\\\\Name' => \$vendorDir . '/b/b/src/',
  270. 'A\\\\B' => array('$workDir/lib', \$vendorDir . '/a/a/lib/'),
  271. 'A' => \$vendorDir . '/a/a/src/',
  272. );
  273. EOF;
  274. $expectedClassmap = <<<EOF
  275. <?php
  276. // autoload_classmap.php generated by Composer
  277. \$vendorDir = dirname(__DIR__);
  278. \$baseDir = dirname(\$vendorDir);
  279. return array(
  280. 'A\\\\B\\\\C' => \$baseDir . '/lib/A/B/C.php',
  281. );
  282. EOF;
  283. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_9');
  284. $this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php'));
  285. $this->assertEquals($expectedClassmap, file_get_contents($this->vendorDir.'/composer/autoload_classmap.php'));
  286. }
  287. public function testIncludePathFileGeneration()
  288. {
  289. $package = new MemoryPackage('a', '1.0', '1.0');
  290. $packages = array();
  291. $a = new MemoryPackage("a/a", "1.0", "1.0");
  292. $a->setIncludePaths(array("lib/"));
  293. $b = new MemoryPackage("b/b", "1.0", "1.0");
  294. $b->setIncludePaths(array("library"));
  295. $c = new MemoryPackage("c", "1.0", "1.0");
  296. $c->setIncludePaths(array("library"));
  297. $packages[] = $a;
  298. $packages[] = $b;
  299. $packages[] = $c;
  300. $this->repository->expects($this->once())
  301. ->method("getPackages")
  302. ->will($this->returnValue($packages));
  303. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  304. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_10');
  305. $this->assertFileEquals(__DIR__.'/Fixtures/include_paths.php', $this->vendorDir.'/composer/include_paths.php');
  306. $this->assertEquals(
  307. array(
  308. $this->vendorDir."/a/a/lib",
  309. $this->vendorDir."/b/b/library",
  310. $this->vendorDir."/c/library",
  311. ),
  312. require($this->vendorDir."/composer/include_paths.php")
  313. );
  314. }
  315. public function testIncludePathsArePrependedInAutoloadFile()
  316. {
  317. $package = new MemoryPackage('a', '1.0', '1.0');
  318. $packages = array();
  319. $a = new MemoryPackage("a/a", "1.0", "1.0");
  320. $a->setIncludePaths(array("lib/"));
  321. $packages[] = $a;
  322. $this->repository->expects($this->once())
  323. ->method("getPackages")
  324. ->will($this->returnValue($packages));
  325. mkdir($this->vendorDir."/composer", 0777, true);
  326. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_11');
  327. $oldIncludePath = get_include_path();
  328. // suppress the class loader to avoid fatals if the class is redefined
  329. file_put_contents($this->vendorDir.'/composer/ClassLoader.php', '');
  330. require($this->vendorDir."/autoload.php");
  331. $this->assertEquals(
  332. $this->vendorDir."/a/a/lib".PATH_SEPARATOR.$oldIncludePath,
  333. get_include_path()
  334. );
  335. set_include_path($oldIncludePath);
  336. }
  337. public function testIncludePathFileWithoutPathsIsSkipped()
  338. {
  339. $package = new MemoryPackage('a', '1.0', '1.0');
  340. $packages = array();
  341. $a = new MemoryPackage("a/a", "1.0", "1.0");
  342. $packages[] = $a;
  343. $this->repository->expects($this->once())
  344. ->method("getPackages")
  345. ->will($this->returnValue($packages));
  346. mkdir($this->vendorDir."/composer", 0777, true);
  347. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_12');
  348. $this->assertFalse(file_exists($this->vendorDir."/composer/include_paths.php"));
  349. }
  350. private function createClassFile($basedir)
  351. {
  352. if (!is_dir($basedir.'/composersrc')) {
  353. mkdir($basedir.'/composersrc', 0777, true);
  354. }
  355. file_put_contents($basedir.'/composersrc/foo.php', '<?php class ClassMapFoo {}');
  356. }
  357. private function assertAutoloadFiles($name, $dir, $type = 'namespaces')
  358. {
  359. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_'.$name.'.php', $dir.'/autoload_'.$type.'.php');
  360. }
  361. }