AutoloadGeneratorTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. $packages = array();
  220. $packages[] = $a = new MemoryPackage('a/a', '1.0', '1.0');
  221. $packages[] = $b = new MemoryPackage('b/b', '1.0', '1.0');
  222. $a->setAutoload(array('files' => array('test.php')));
  223. $b->setAutoload(array('files' => array('test2.php')));
  224. $this->repository->expects($this->once())
  225. ->method('getPackages')
  226. ->will($this->returnValue($packages));
  227. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a');
  228. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b');
  229. file_put_contents($this->vendorDir.'/a/a/test.php', '<?php function testFilesAutoloadGeneration1() {}');
  230. file_put_contents($this->vendorDir.'/b/b/test2.php', '<?php function testFilesAutoloadGeneration2() {}');
  231. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'FilesAutoload');
  232. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php');
  233. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_functions.php', $this->vendorDir.'/composer/autoload_realFilesAutoload.php');
  234. // suppress the class loader to avoid fatals if the class is redefined
  235. file_put_contents($this->vendorDir.'/composer/ClassLoader.php', '');
  236. include $this->vendorDir . '/autoload.php';
  237. $this->assertTrue(function_exists('testFilesAutoloadGeneration1'));
  238. $this->assertTrue(function_exists('testFilesAutoloadGeneration2'));
  239. }
  240. public function testOverrideVendorsAutoloading()
  241. {
  242. $package = new MemoryPackage('a', '1.0', '1.0');
  243. $package->setAutoload(array('psr-0' => array('A\\B' => $this->workingDir.'/lib')));
  244. $packages = array();
  245. $packages[] = $a = new MemoryPackage('a/a', '1.0', '1.0');
  246. $packages[] = $b = new MemoryPackage('b/b', '1.0', '1.0');
  247. $a->setAutoload(array('psr-0' => array('A' => 'src/', 'A\\B' => 'lib/')));
  248. $b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/')));
  249. $this->repository->expects($this->once())
  250. ->method('getPackages')
  251. ->will($this->returnValue($packages));
  252. $this->fs->ensureDirectoryExists($this->workingDir.'/lib/A/B');
  253. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  254. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  255. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib/A/B');
  256. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  257. file_put_contents($this->workingDir.'/lib/A/B/C.php', '<?php namespace A\\B; class C {}');
  258. file_put_contents($this->vendorDir.'/a/a/lib/A/B/C.php', '<?php namespace A\\B; class C {}');
  259. $workDir = strtr($this->workingDir, '\\', '/');
  260. $expectedNamespace = <<<EOF
  261. <?php
  262. // autoload_namespaces.php generated by Composer
  263. \$vendorDir = dirname(__DIR__);
  264. \$baseDir = dirname(\$vendorDir);
  265. return array(
  266. 'B\\\\Sub\\\\Name' => \$vendorDir . '/b/b/src/',
  267. 'A\\\\B' => array('$workDir/lib', \$vendorDir . '/a/a/lib/'),
  268. 'A' => \$vendorDir . '/a/a/src/',
  269. );
  270. EOF;
  271. $expectedClassmap = <<<EOF
  272. <?php
  273. // autoload_classmap.php generated by Composer
  274. \$vendorDir = dirname(__DIR__);
  275. \$baseDir = dirname(\$vendorDir);
  276. return array(
  277. 'A\\\\B\\\\C' => \$baseDir . '/lib/A/B/C.php',
  278. );
  279. EOF;
  280. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_9');
  281. $this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php'));
  282. $this->assertEquals($expectedClassmap, file_get_contents($this->vendorDir.'/composer/autoload_classmap.php'));
  283. }
  284. public function testIncludePathFileGeneration()
  285. {
  286. $package = new MemoryPackage('a', '1.0', '1.0');
  287. $packages = array();
  288. $a = new MemoryPackage("a/a", "1.0", "1.0");
  289. $a->setIncludePaths(array("lib/"));
  290. $b = new MemoryPackage("b/b", "1.0", "1.0");
  291. $b->setIncludePaths(array("library"));
  292. $c = new MemoryPackage("c", "1.0", "1.0");
  293. $c->setIncludePaths(array("library"));
  294. $packages[] = $a;
  295. $packages[] = $b;
  296. $packages[] = $c;
  297. $this->repository->expects($this->once())
  298. ->method("getPackages")
  299. ->will($this->returnValue($packages));
  300. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  301. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_10');
  302. $this->assertFileEquals(__DIR__.'/Fixtures/include_paths.php', $this->vendorDir.'/composer/include_paths.php');
  303. $this->assertEquals(
  304. array(
  305. $this->vendorDir."/a/a/lib",
  306. $this->vendorDir."/b/b/library",
  307. $this->vendorDir."/c/library",
  308. ),
  309. require($this->vendorDir."/composer/include_paths.php")
  310. );
  311. }
  312. public function testIncludePathsArePrependedInAutoloadFile()
  313. {
  314. $package = new MemoryPackage('a', '1.0', '1.0');
  315. $packages = array();
  316. $a = new MemoryPackage("a/a", "1.0", "1.0");
  317. $a->setIncludePaths(array("lib/"));
  318. $packages[] = $a;
  319. $this->repository->expects($this->once())
  320. ->method("getPackages")
  321. ->will($this->returnValue($packages));
  322. mkdir($this->vendorDir."/composer", 0777, true);
  323. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_11');
  324. $oldIncludePath = get_include_path();
  325. // suppress the class loader to avoid fatals if the class is redefined
  326. file_put_contents($this->vendorDir.'/composer/ClassLoader.php', '');
  327. require($this->vendorDir."/autoload.php");
  328. $this->assertEquals(
  329. $this->vendorDir."/a/a/lib".PATH_SEPARATOR.$oldIncludePath,
  330. get_include_path()
  331. );
  332. set_include_path($oldIncludePath);
  333. }
  334. public function testIncludePathFileWithoutPathsIsSkipped()
  335. {
  336. $package = new MemoryPackage('a', '1.0', '1.0');
  337. $packages = array();
  338. $a = new MemoryPackage("a/a", "1.0", "1.0");
  339. $packages[] = $a;
  340. $this->repository->expects($this->once())
  341. ->method("getPackages")
  342. ->will($this->returnValue($packages));
  343. mkdir($this->vendorDir."/composer", 0777, true);
  344. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_12');
  345. $this->assertFalse(file_exists($this->vendorDir."/composer/include_paths.php"));
  346. }
  347. private function createClassFile($basedir)
  348. {
  349. if (!is_dir($basedir.'/composersrc')) {
  350. mkdir($basedir.'/composersrc', 0777, true);
  351. }
  352. file_put_contents($basedir.'/composersrc/foo.php', '<?php class ClassMapFoo {}');
  353. }
  354. private function assertAutoloadFiles($name, $dir, $type = 'namespaces')
  355. {
  356. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_'.$name.'.php', $dir.'/autoload_'.$type.'.php');
  357. }
  358. }