AutoloadGeneratorTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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\Package;
  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 Package('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 Package('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 Package('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 Package('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 testMainPackageAutoloadingWithTargetDirAndNoPsr()
  135. {
  136. $package = new Package('a', '1.0', '1.0');
  137. $package->setAutoload(array(
  138. 'classmap' => array('composersrc/'),
  139. ));
  140. $package->setTargetDir('Main/Foo/');
  141. $this->repository->expects($this->once())
  142. ->method('getPackages')
  143. ->will($this->returnValue(array()));
  144. $this->vendorDir .= '/subdir';
  145. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  146. $this->fs->ensureDirectoryExists($this->workingDir.'/src');
  147. $this->createClassFile($this->workingDir);
  148. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'TargetDirNoPsr');
  149. $this->assertAutoloadFiles('classmap2', $this->vendorDir.'/composer', 'classmap');
  150. }
  151. public function testVendorsAutoloading()
  152. {
  153. $package = new Package('a', '1.0', '1.0');
  154. $packages = array();
  155. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  156. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  157. $packages[] = $c = new AliasPackage($b, '1.2', '1.2');
  158. $a->setAutoload(array('psr-0' => array('A' => 'src/', 'A\\B' => 'lib/')));
  159. $b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/')));
  160. $this->repository->expects($this->once())
  161. ->method('getPackages')
  162. ->will($this->returnValue($packages));
  163. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  164. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  165. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib');
  166. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  167. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_5');
  168. $this->assertAutoloadFiles('vendors', $this->vendorDir.'/composer');
  169. $this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated, even if empty.");
  170. }
  171. public function testVendorsClassMapAutoloading()
  172. {
  173. $package = new Package('a', '1.0', '1.0');
  174. $packages = array();
  175. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  176. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  177. $a->setAutoload(array('classmap' => array('src/')));
  178. $b->setAutoload(array('classmap' => array('src/', 'lib/')));
  179. $this->repository->expects($this->once())
  180. ->method('getPackages')
  181. ->will($this->returnValue($packages));
  182. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  183. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  184. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  185. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/lib');
  186. file_put_contents($this->vendorDir.'/a/a/src/a.php', '<?php class ClassMapFoo {}');
  187. file_put_contents($this->vendorDir.'/b/b/src/b.php', '<?php class ClassMapBar {}');
  188. file_put_contents($this->vendorDir.'/b/b/lib/c.php', '<?php class ClassMapBaz {}');
  189. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_6');
  190. $this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated.");
  191. $this->assertEquals(
  192. array(
  193. 'ClassMapFoo' => $this->workingDir.'/composer-test-autoload/a/a/src/a.php',
  194. 'ClassMapBar' => $this->workingDir.'/composer-test-autoload/b/b/src/b.php',
  195. 'ClassMapBaz' => $this->workingDir.'/composer-test-autoload/b/b/lib/c.php',
  196. ),
  197. include ($this->vendorDir.'/composer/autoload_classmap.php')
  198. );
  199. $this->assertAutoloadFiles('classmap4', $this->vendorDir.'/composer', 'classmap');
  200. }
  201. public function testClassMapAutoloadingEmptyDirAndExactFile()
  202. {
  203. $package = new Package('a', '1.0', '1.0');
  204. $packages = array();
  205. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  206. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  207. $packages[] = $c = new Package('c/c', '1.0', '1.0');
  208. $a->setAutoload(array('classmap' => array('')));
  209. $b->setAutoload(array('classmap' => array('test.php')));
  210. $c->setAutoload(array('classmap' => array('./')));
  211. $this->repository->expects($this->once())
  212. ->method('getPackages')
  213. ->will($this->returnValue($packages));
  214. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  215. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  216. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b');
  217. $this->fs->ensureDirectoryExists($this->vendorDir.'/c/c/foo');
  218. file_put_contents($this->vendorDir.'/a/a/src/a.php', '<?php class ClassMapFoo {}');
  219. file_put_contents($this->vendorDir.'/b/b/test.php', '<?php class ClassMapBar {}');
  220. file_put_contents($this->vendorDir.'/c/c/foo/test.php', '<?php class ClassMapBaz {}');
  221. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_7');
  222. $this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated.");
  223. $this->assertEquals(
  224. array(
  225. 'ClassMapFoo' => $this->workingDir.'/composer-test-autoload/a/a/src/a.php',
  226. 'ClassMapBar' => $this->workingDir.'/composer-test-autoload/b/b/test.php',
  227. 'ClassMapBaz' => $this->workingDir.'/composer-test-autoload/c/c/foo/test.php',
  228. ),
  229. include ($this->vendorDir.'/composer/autoload_classmap.php')
  230. );
  231. $this->assertAutoloadFiles('classmap5', $this->vendorDir.'/composer', 'classmap');
  232. }
  233. public function testFilesAutoloadGeneration()
  234. {
  235. $package = new Package('a', '1.0', '1.0');
  236. $package->setAutoload(array('files' => array('root.php')));
  237. $packages = array();
  238. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  239. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  240. $a->setAutoload(array('files' => array('test.php')));
  241. $b->setAutoload(array('files' => array('test2.php')));
  242. $this->repository->expects($this->once())
  243. ->method('getPackages')
  244. ->will($this->returnValue($packages));
  245. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a');
  246. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b');
  247. file_put_contents($this->vendorDir.'/a/a/test.php', '<?php function testFilesAutoloadGeneration1() {}');
  248. file_put_contents($this->vendorDir.'/b/b/test2.php', '<?php function testFilesAutoloadGeneration2() {}');
  249. file_put_contents($this->workingDir.'/root.php', '<?php function testFilesAutoloadGenerationRoot() {}');
  250. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'FilesAutoload');
  251. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php');
  252. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_functions.php', $this->vendorDir.'/composer/autoload_realFilesAutoload.php');
  253. // suppress the class loader to avoid fatals if the class is redefined
  254. file_put_contents($this->vendorDir.'/composer/ClassLoader.php', '');
  255. include $this->vendorDir . '/autoload.php';
  256. $this->assertTrue(function_exists('testFilesAutoloadGeneration1'));
  257. $this->assertTrue(function_exists('testFilesAutoloadGeneration2'));
  258. $this->assertTrue(function_exists('testFilesAutoloadGenerationRoot'));
  259. }
  260. public function testOverrideVendorsAutoloading()
  261. {
  262. $package = new Package('a', '1.0', '1.0');
  263. $package->setAutoload(array('psr-0' => array('A\\B' => $this->workingDir.'/lib')));
  264. $packages = array();
  265. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  266. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  267. $a->setAutoload(array('psr-0' => array('A' => 'src/', 'A\\B' => 'lib/')));
  268. $b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/')));
  269. $this->repository->expects($this->once())
  270. ->method('getPackages')
  271. ->will($this->returnValue($packages));
  272. $this->fs->ensureDirectoryExists($this->workingDir.'/lib/A/B');
  273. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  274. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  275. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib/A/B');
  276. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  277. file_put_contents($this->workingDir.'/lib/A/B/C.php', '<?php namespace A\\B; class C {}');
  278. file_put_contents($this->vendorDir.'/a/a/lib/A/B/C.php', '<?php namespace A\\B; class C {}');
  279. $workDir = strtr($this->workingDir, '\\', '/');
  280. $expectedNamespace = <<<EOF
  281. <?php
  282. // autoload_namespaces.php generated by Composer
  283. \$vendorDir = dirname(__DIR__);
  284. \$baseDir = dirname(\$vendorDir);
  285. return array(
  286. 'B\\\\Sub\\\\Name' => \$vendorDir . '/b/b/src/',
  287. 'A\\\\B' => array('$workDir/lib', \$vendorDir . '/a/a/lib/'),
  288. 'A' => \$vendorDir . '/a/a/src/',
  289. );
  290. EOF;
  291. $expectedClassmap = <<<EOF
  292. <?php
  293. // autoload_classmap.php generated by Composer
  294. \$vendorDir = dirname(__DIR__);
  295. \$baseDir = dirname(\$vendorDir);
  296. return array(
  297. 'A\\\\B\\\\C' => \$baseDir . '/lib/A/B/C.php',
  298. );
  299. EOF;
  300. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_9');
  301. $this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php'));
  302. $this->assertEquals($expectedClassmap, file_get_contents($this->vendorDir.'/composer/autoload_classmap.php'));
  303. }
  304. public function testIncludePathFileGeneration()
  305. {
  306. $package = new Package('a', '1.0', '1.0');
  307. $packages = array();
  308. $a = new Package("a/a", "1.0", "1.0");
  309. $a->setIncludePaths(array("lib/"));
  310. $b = new Package("b/b", "1.0", "1.0");
  311. $b->setIncludePaths(array("library"));
  312. $c = new Package("c", "1.0", "1.0");
  313. $c->setIncludePaths(array("library"));
  314. $packages[] = $a;
  315. $packages[] = $b;
  316. $packages[] = $c;
  317. $this->repository->expects($this->once())
  318. ->method("getPackages")
  319. ->will($this->returnValue($packages));
  320. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  321. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_10');
  322. $this->assertFileEquals(__DIR__.'/Fixtures/include_paths.php', $this->vendorDir.'/composer/include_paths.php');
  323. $this->assertEquals(
  324. array(
  325. $this->vendorDir."/a/a/lib",
  326. $this->vendorDir."/b/b/library",
  327. $this->vendorDir."/c/library",
  328. ),
  329. require($this->vendorDir."/composer/include_paths.php")
  330. );
  331. }
  332. public function testIncludePathsArePrependedInAutoloadFile()
  333. {
  334. $package = new Package('a', '1.0', '1.0');
  335. $packages = array();
  336. $a = new Package("a/a", "1.0", "1.0");
  337. $a->setIncludePaths(array("lib/"));
  338. $packages[] = $a;
  339. $this->repository->expects($this->once())
  340. ->method("getPackages")
  341. ->will($this->returnValue($packages));
  342. mkdir($this->vendorDir."/composer", 0777, true);
  343. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_11');
  344. $oldIncludePath = get_include_path();
  345. // suppress the class loader to avoid fatals if the class is redefined
  346. file_put_contents($this->vendorDir.'/composer/ClassLoader.php', '');
  347. require($this->vendorDir."/autoload.php");
  348. $this->assertEquals(
  349. $this->vendorDir."/a/a/lib".PATH_SEPARATOR.$oldIncludePath,
  350. get_include_path()
  351. );
  352. set_include_path($oldIncludePath);
  353. }
  354. public function testIncludePathFileWithoutPathsIsSkipped()
  355. {
  356. $package = new Package('a', '1.0', '1.0');
  357. $packages = array();
  358. $a = new Package("a/a", "1.0", "1.0");
  359. $packages[] = $a;
  360. $this->repository->expects($this->once())
  361. ->method("getPackages")
  362. ->will($this->returnValue($packages));
  363. mkdir($this->vendorDir."/composer", 0777, true);
  364. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_12');
  365. $this->assertFalse(file_exists($this->vendorDir."/composer/include_paths.php"));
  366. }
  367. private function createClassFile($basedir)
  368. {
  369. if (!is_dir($basedir.'/composersrc')) {
  370. mkdir($basedir.'/composersrc', 0777, true);
  371. }
  372. file_put_contents($basedir.'/composersrc/foo.php', '<?php class ClassMapFoo {}');
  373. }
  374. private function assertAutoloadFiles($name, $dir, $type = 'namespaces')
  375. {
  376. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_'.$name.'.php', $dir.'/autoload_'.$type.'.php');
  377. }
  378. }