AutoloadGeneratorTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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\MemoryPackage;
  15. use Composer\Test\TestCase;
  16. class AutoloadGeneratorTest extends TestCase
  17. {
  18. public $vendorDir;
  19. private $workingDir;
  20. private $im;
  21. private $repository;
  22. private $generator;
  23. private $fs;
  24. protected function setUp()
  25. {
  26. $this->fs = new Filesystem;
  27. $that = $this;
  28. $this->workingDir = realpath(sys_get_temp_dir());
  29. $this->vendorDir = $this->workingDir.DIRECTORY_SEPARATOR.'composer-test-autoload';
  30. $this->ensureDirectoryExistsAndClear($this->vendorDir);
  31. $this->dir = getcwd();
  32. chdir($this->workingDir);
  33. $this->im = $this->getMockBuilder('Composer\Installer\InstallationManager')
  34. ->disableOriginalConstructor()
  35. ->getMock();
  36. $this->im->expects($this->any())
  37. ->method('getInstallPath')
  38. ->will($this->returnCallback(function ($package) use ($that) {
  39. return $that->vendorDir.'/'.$package->getName();
  40. }));
  41. $this->im->expects($this->any())
  42. ->method('getVendorPath')
  43. ->will($this->returnCallback(function () use ($that) {
  44. return $that->vendorDir;
  45. }));
  46. $this->repository = $this->getMock('Composer\Repository\RepositoryInterface');
  47. $this->generator = new AutoloadGenerator();
  48. }
  49. protected function tearDown()
  50. {
  51. if ($this->vendorDir === $this->workingDir) {
  52. if (is_dir($this->workingDir.'/composer')) {
  53. $this->fs->removeDirectory($this->workingDir.'/composer');
  54. }
  55. } elseif (is_dir($this->vendorDir)) {
  56. $this->fs->removeDirectory($this->vendorDir);
  57. }
  58. if (is_dir($this->workingDir.'/composersrc')) {
  59. $this->fs->removeDirectory($this->workingDir.'/composersrc');
  60. }
  61. chdir($this->dir);
  62. }
  63. public function testMainPackageAutoloading()
  64. {
  65. $package = new MemoryPackage('a', '1.0', '1.0');
  66. $package->setAutoload(array(
  67. 'psr-0' => array('Main' => 'src/', 'Lala' => array('src/', 'lib/')),
  68. 'classmap' => array('composersrc/'),
  69. ));
  70. $this->repository->expects($this->once())
  71. ->method('getPackages')
  72. ->will($this->returnValue(array()));
  73. if (!is_dir($this->vendorDir.'/composer')) {
  74. mkdir($this->vendorDir.'/composer');
  75. }
  76. $this->createClassFile($this->workingDir);
  77. $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/composer');
  78. $this->assertAutoloadFiles('main', $this->vendorDir.'/composer');
  79. $this->assertAutoloadFiles('classmap', $this->vendorDir.'/composer', 'classmap');
  80. }
  81. public function testVendorDirSameAsWorkingDir()
  82. {
  83. $this->vendorDir = $this->workingDir;
  84. $package = new MemoryPackage('a', '1.0', '1.0');
  85. $package->setAutoload(array(
  86. 'psr-0' => array('Main' => 'src/', 'Lala' => 'src/'),
  87. 'classmap' => array('composersrc/'),
  88. ));
  89. $this->repository->expects($this->once())
  90. ->method('getPackages')
  91. ->will($this->returnValue(array()));
  92. if (!is_dir($this->vendorDir.'/composer')) {
  93. mkdir($this->vendorDir.'/composer', 0777, true);
  94. }
  95. $this->createClassFile($this->vendorDir);
  96. $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/composer');
  97. $this->assertAutoloadFiles('main3', $this->vendorDir.'/composer');
  98. $this->assertAutoloadFiles('classmap3', $this->vendorDir.'/composer', 'classmap');
  99. }
  100. public function testMainPackageAutoloadingAlternativeVendorDir()
  101. {
  102. $package = new MemoryPackage('a', '1.0', '1.0');
  103. $package->setAutoload(array(
  104. 'psr-0' => array('Main' => 'src/', 'Lala' => 'src/'),
  105. 'classmap' => array('composersrc/'),
  106. ));
  107. $this->repository->expects($this->once())
  108. ->method('getPackages')
  109. ->will($this->returnValue(array()));
  110. $this->vendorDir .= '/subdir';
  111. mkdir($this->vendorDir.'/composer', 0777, true);
  112. $this->createClassFile($this->workingDir);
  113. $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/composer');
  114. $this->assertAutoloadFiles('main2', $this->vendorDir.'/composer');
  115. $this->assertAutoloadFiles('classmap2', $this->vendorDir.'/composer', 'classmap');
  116. }
  117. public function testMainPackageAutoloadingWithTargetDir()
  118. {
  119. $package = new MemoryPackage('a', '1.0', '1.0');
  120. $package->setAutoload(array(
  121. 'psr-0' => array('Main\\Foo' => '', 'Main\\Bar' => ''),
  122. ));
  123. $package->setTargetDir('Main/Foo/');
  124. $this->repository->expects($this->once())
  125. ->method('getPackages')
  126. ->will($this->returnValue(array()));
  127. $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/composer');
  128. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_target_dir.php', $this->vendorDir.'/autoload.php');
  129. }
  130. public function testVendorsAutoloading()
  131. {
  132. $package = new MemoryPackage('a', '1.0', '1.0');
  133. $packages = array();
  134. $packages[] = $a = new MemoryPackage('a/a', '1.0', '1.0');
  135. $packages[] = $b = new MemoryPackage('b/b', '1.0', '1.0');
  136. $a->setAutoload(array('psr-0' => array('A' => 'src/', 'A\\B' => 'lib/')));
  137. $b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/')));
  138. $this->repository->expects($this->once())
  139. ->method('getPackages')
  140. ->will($this->returnValue($packages));
  141. mkdir($this->vendorDir.'/composer', 0777, true);
  142. $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/composer');
  143. $this->assertAutoloadFiles('vendors', $this->vendorDir.'/composer');
  144. $this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated, even if empty.");
  145. }
  146. public function testVendorsClassMapAutoloading()
  147. {
  148. $package = new MemoryPackage('a', '1.0', '1.0');
  149. $packages = array();
  150. $packages[] = $a = new MemoryPackage('a/a', '1.0', '1.0');
  151. $packages[] = $b = new MemoryPackage('b/b', '1.0', '1.0');
  152. $a->setAutoload(array('classmap' => array('src/')));
  153. $b->setAutoload(array('classmap' => array('src/', 'lib/')));
  154. $this->repository->expects($this->once())
  155. ->method('getPackages')
  156. ->will($this->returnValue($packages));
  157. @mkdir($this->vendorDir.'/composer', 0777, true);
  158. mkdir($this->vendorDir.'/a/a/src', 0777, true);
  159. mkdir($this->vendorDir.'/b/b/src', 0777, true);
  160. mkdir($this->vendorDir.'/b/b/lib', 0777, true);
  161. file_put_contents($this->vendorDir.'/a/a/src/a.php', '<?php class ClassMapFoo {}');
  162. file_put_contents($this->vendorDir.'/b/b/src/b.php', '<?php class ClassMapBar {}');
  163. file_put_contents($this->vendorDir.'/b/b/lib/c.php', '<?php class ClassMapBaz {}');
  164. $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/composer');
  165. $this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated.");
  166. $this->assertEquals(
  167. array(
  168. 'ClassMapFoo' => $this->workingDir.'/composer-test-autoload/a/a/src/a.php',
  169. 'ClassMapBar' => $this->workingDir.'/composer-test-autoload/b/b/src/b.php',
  170. 'ClassMapBaz' => $this->workingDir.'/composer-test-autoload/b/b/lib/c.php',
  171. ),
  172. include ($this->vendorDir.'/composer/autoload_classmap.php')
  173. );
  174. $this->assertAutoloadFiles('classmap4', $this->vendorDir.'/composer', 'classmap');
  175. }
  176. public function testClassMapAutoloadingEmptyDirAndExactFile()
  177. {
  178. $package = new MemoryPackage('a', '1.0', '1.0');
  179. $packages = array();
  180. $packages[] = $a = new MemoryPackage('a/a', '1.0', '1.0');
  181. $packages[] = $b = new MemoryPackage('b/b', '1.0', '1.0');
  182. $packages[] = $c = new MemoryPackage('c/c', '1.0', '1.0');
  183. $a->setAutoload(array('classmap' => array('')));
  184. $b->setAutoload(array('classmap' => array('test.php')));
  185. $c->setAutoload(array('classmap' => array('./')));
  186. $this->repository->expects($this->once())
  187. ->method('getPackages')
  188. ->will($this->returnValue($packages));
  189. @mkdir($this->vendorDir.'/composer', 0777, true);
  190. mkdir($this->vendorDir.'/a/a/src', 0777, true);
  191. mkdir($this->vendorDir.'/b/b', 0777, true);
  192. mkdir($this->vendorDir.'/c/c/foo', 0777, true);
  193. file_put_contents($this->vendorDir.'/a/a/src/a.php', '<?php class ClassMapFoo {}');
  194. file_put_contents($this->vendorDir.'/b/b/test.php', '<?php class ClassMapBar {}');
  195. file_put_contents($this->vendorDir.'/c/c/foo/test.php', '<?php class ClassMapBaz {}');
  196. $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/composer');
  197. $this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated.");
  198. $this->assertEquals(
  199. array(
  200. 'ClassMapFoo' => $this->workingDir.'/composer-test-autoload/a/a/src/a.php',
  201. 'ClassMapBar' => $this->workingDir.'/composer-test-autoload/b/b/test.php',
  202. 'ClassMapBaz' => $this->workingDir.'/composer-test-autoload/c/c/foo/test.php',
  203. ),
  204. include ($this->vendorDir.'/composer/autoload_classmap.php')
  205. );
  206. $this->assertAutoloadFiles('classmap5', $this->vendorDir.'/composer', 'classmap');
  207. }
  208. public function testOverrideVendorsAutoloading()
  209. {
  210. $package = new MemoryPackage('a', '1.0', '1.0');
  211. $package->setAutoload(array('psr-0' => array('A\\B' => '/home/deveuser/local-packages/a-a/lib')));
  212. $packages = array();
  213. $packages[] = $a = new MemoryPackage('a/a', '1.0', '1.0');
  214. $packages[] = $b = new MemoryPackage('b/b', '1.0', '1.0');
  215. $a->setAutoload(array('psr-0' => array('A' => 'src/', 'A\\B' => 'lib/')));
  216. $b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/')));
  217. $this->repository->expects($this->once())
  218. ->method('getPackages')
  219. ->will($this->returnValue($packages));
  220. mkdir($this->vendorDir.'/composer', 0777, true);
  221. $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/composer');
  222. $this->assertAutoloadFiles('override_vendors', $this->vendorDir.'/composer');
  223. }
  224. public function testIncludePathFileGeneration()
  225. {
  226. $package = new MemoryPackage('a', '1.0', '1.0');
  227. $packages = array();
  228. $a = new MemoryPackage("a/a", "1.0", "1.0");
  229. $a->setIncludePaths(array("lib/"));
  230. $b = new MemoryPackage("b/b", "1.0", "1.0");
  231. $b->setIncludePaths(array("library"));
  232. $c = new MemoryPackage("c", "1.0", "1.0");
  233. $c->setIncludePaths(array("library"));
  234. $packages[] = $a;
  235. $packages[] = $b;
  236. $packages[] = $c;
  237. $this->repository->expects($this->once())
  238. ->method("getPackages")
  239. ->will($this->returnValue($packages));
  240. mkdir($this->vendorDir."/composer", 0777, true);
  241. $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir."/composer");
  242. $this->assertFileEquals(__DIR__.'/Fixtures/include_paths.php', $this->vendorDir.'/composer/include_paths.php');
  243. $this->assertEquals(
  244. array(
  245. $this->vendorDir."/a/a/lib",
  246. $this->vendorDir."/b/b/library",
  247. $this->vendorDir."/c/library",
  248. ),
  249. require($this->vendorDir."/composer/include_paths.php")
  250. );
  251. }
  252. public function testIncludePathsAreAppendedInAutoloadFile()
  253. {
  254. $package = new MemoryPackage('a', '1.0', '1.0');
  255. $packages = array();
  256. $a = new MemoryPackage("a/a", "1.0", "1.0");
  257. $a->setIncludePaths(array("lib/"));
  258. $packages[] = $a;
  259. $this->repository->expects($this->once())
  260. ->method("getPackages")
  261. ->will($this->returnValue($packages));
  262. mkdir($this->vendorDir."/composer", 0777, true);
  263. $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir."/composer");
  264. $oldIncludePath = get_include_path();
  265. require($this->vendorDir."/autoload.php");
  266. $this->assertEquals(
  267. $oldIncludePath.PATH_SEPARATOR.$this->vendorDir."/a/a/lib",
  268. get_include_path()
  269. );
  270. set_include_path($oldIncludePath);
  271. }
  272. public function testIncludePathFileWithoutPathsIsSkipped()
  273. {
  274. $package = new MemoryPackage('a', '1.0', '1.0');
  275. $packages = array();
  276. $a = new MemoryPackage("a/a", "1.0", "1.0");
  277. $packages[] = $a;
  278. $this->repository->expects($this->once())
  279. ->method("getPackages")
  280. ->will($this->returnValue($packages));
  281. mkdir($this->vendorDir."/composer", 0777, true);
  282. $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir."/composer");
  283. $this->assertFalse(file_exists($this->vendorDir."/composer/include_paths.php"));
  284. }
  285. private function createClassFile($basedir)
  286. {
  287. if (!is_dir($basedir.'/composersrc')) {
  288. mkdir($basedir.'/composersrc', 0777, true);
  289. }
  290. file_put_contents($basedir.'/composersrc/foo.php', '<?php class ClassMapFoo {}');
  291. }
  292. private function assertAutoloadFiles($name, $dir, $type = 'namespaces')
  293. {
  294. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_'.$name.'.php', $dir.'/autoload_'.$type.'.php');
  295. }
  296. }