AutoloadGeneratorTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 testVendorsAutoloading()
  118. {
  119. $package = new MemoryPackage('a', '1.0', '1.0');
  120. $packages = array();
  121. $packages[] = $a = new MemoryPackage('a/a', '1.0', '1.0');
  122. $packages[] = $b = new MemoryPackage('b/b', '1.0', '1.0');
  123. $a->setAutoload(array('psr-0' => array('A' => 'src/', 'A\\B' => 'lib/')));
  124. $b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/')));
  125. $this->repository->expects($this->once())
  126. ->method('getPackages')
  127. ->will($this->returnValue($packages));
  128. mkdir($this->vendorDir.'/.composer', 0777, true);
  129. $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/.composer');
  130. $this->assertAutoloadFiles('vendors', $this->vendorDir.'/.composer');
  131. $this->assertTrue(file_exists($this->vendorDir.'/.composer/autoload_classmap.php'), "ClassMap file needs to be generated, even if empty.");
  132. }
  133. public function testVendorsClassMapAutoloading()
  134. {
  135. $package = new MemoryPackage('a', '1.0', '1.0');
  136. $packages = array();
  137. $packages[] = $a = new MemoryPackage('a/a', '1.0', '1.0');
  138. $packages[] = $b = new MemoryPackage('b/b', '1.0', '1.0');
  139. $a->setAutoload(array('classmap' => array('src/')));
  140. $b->setAutoload(array('classmap' => array('src/', 'lib/')));
  141. $this->repository->expects($this->once())
  142. ->method('getPackages')
  143. ->will($this->returnValue($packages));
  144. @mkdir($this->vendorDir.'/.composer', 0777, true);
  145. mkdir($this->vendorDir.'/a/a/src', 0777, true);
  146. mkdir($this->vendorDir.'/b/b/src', 0777, true);
  147. mkdir($this->vendorDir.'/b/b/lib', 0777, true);
  148. file_put_contents($this->vendorDir.'/a/a/src/a.php', '<?php class ClassMapFoo {}');
  149. file_put_contents($this->vendorDir.'/b/b/src/b.php', '<?php class ClassMapBar {}');
  150. file_put_contents($this->vendorDir.'/b/b/lib/c.php', '<?php class ClassMapBaz {}');
  151. $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/.composer');
  152. $this->assertTrue(file_exists($this->vendorDir.'/.composer/autoload_classmap.php'), "ClassMap file needs to be generated.");
  153. $this->assertEquals(
  154. array(
  155. 'ClassMapFoo' => $this->workingDir.'/composer-test-autoload/a/a/src/a.php',
  156. 'ClassMapBar' => $this->workingDir.'/composer-test-autoload/b/b/src/b.php',
  157. 'ClassMapBaz' => $this->workingDir.'/composer-test-autoload/b/b/lib/c.php',
  158. ),
  159. include ($this->vendorDir.'/.composer/autoload_classmap.php')
  160. );
  161. $this->assertAutoloadFiles('classmap4', $this->vendorDir.'/.composer', 'classmap');
  162. }
  163. public function testClassMapAutoloadingEmptyDirAndExactFile()
  164. {
  165. $package = new MemoryPackage('a', '1.0', '1.0');
  166. $packages = array();
  167. $packages[] = $a = new MemoryPackage('a/a', '1.0', '1.0');
  168. $packages[] = $b = new MemoryPackage('b/b', '1.0', '1.0');
  169. $packages[] = $c = new MemoryPackage('c/c', '1.0', '1.0');
  170. $a->setAutoload(array('classmap' => array('')));
  171. $b->setAutoload(array('classmap' => array('test.php')));
  172. $c->setAutoload(array('classmap' => array('./')));
  173. $this->repository->expects($this->once())
  174. ->method('getPackages')
  175. ->will($this->returnValue($packages));
  176. @mkdir($this->vendorDir.'/.composer', 0777, true);
  177. mkdir($this->vendorDir.'/a/a/src', 0777, true);
  178. mkdir($this->vendorDir.'/b/b', 0777, true);
  179. mkdir($this->vendorDir.'/c/c/foo', 0777, true);
  180. file_put_contents($this->vendorDir.'/a/a/src/a.php', '<?php class ClassMapFoo {}');
  181. file_put_contents($this->vendorDir.'/b/b/test.php', '<?php class ClassMapBar {}');
  182. file_put_contents($this->vendorDir.'/c/c/foo/test.php', '<?php class ClassMapBaz {}');
  183. $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/.composer');
  184. $this->assertTrue(file_exists($this->vendorDir.'/.composer/autoload_classmap.php'), "ClassMap file needs to be generated.");
  185. $this->assertEquals(
  186. array(
  187. 'ClassMapFoo' => $this->workingDir.'/composer-test-autoload/a/a/src/a.php',
  188. 'ClassMapBar' => $this->workingDir.'/composer-test-autoload/b/b/test.php',
  189. 'ClassMapBaz' => $this->workingDir.'/composer-test-autoload/c/c/foo/test.php',
  190. ),
  191. include ($this->vendorDir.'/.composer/autoload_classmap.php')
  192. );
  193. $this->assertAutoloadFiles('classmap5', $this->vendorDir.'/.composer', 'classmap');
  194. }
  195. public function testOverrideVendorsAutoloading()
  196. {
  197. $package = new MemoryPackage('a', '1.0', '1.0');
  198. $package->setAutoload(array('psr-0' => array('A\\B' => '/home/deveuser/local-packages/a-a/lib')));
  199. $packages = array();
  200. $packages[] = $a = new MemoryPackage('a/a', '1.0', '1.0');
  201. $packages[] = $b = new MemoryPackage('b/b', '1.0', '1.0');
  202. $a->setAutoload(array('psr-0' => array('A' => 'src/', 'A\\B' => 'lib/')));
  203. $b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/')));
  204. $this->repository->expects($this->once())
  205. ->method('getPackages')
  206. ->will($this->returnValue($packages));
  207. mkdir($this->vendorDir.'/.composer', 0777, true);
  208. $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/.composer');
  209. $this->assertAutoloadFiles('override_vendors', $this->vendorDir.'/.composer');
  210. }
  211. public function testIncludePathFileGeneration()
  212. {
  213. $package = new MemoryPackage('a', '1.0', '1.0');
  214. $packages = array();
  215. $a = new MemoryPackage("a/a", "1.0", "1.0");
  216. $a->setIncludePaths(array("lib/"));
  217. $b = new MemoryPackage("b/b", "1.0", "1.0");
  218. $b->setIncludePaths(array("library"));
  219. $packages[] = $a;
  220. $packages[] = $b;
  221. $this->repository->expects($this->once())
  222. ->method("getPackages")
  223. ->will($this->returnValue($packages));
  224. mkdir($this->vendorDir."/.composer", 0777, true);
  225. $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir."/.composer");
  226. $this->assertEquals(
  227. array(
  228. $this->vendorDir."/a/a/lib/",
  229. $this->vendorDir."/b/b/library"
  230. ),
  231. require($this->vendorDir."/.composer/include_paths.php")
  232. );
  233. }
  234. public function testIncludePathsAreAppendedInAutoloadFile()
  235. {
  236. $package = new MemoryPackage('a', '1.0', '1.0');
  237. $packages = array();
  238. $a = new MemoryPackage("a/a", "1.0", "1.0");
  239. $a->setIncludePaths(array("lib/"));
  240. $packages[] = $a;
  241. $this->repository->expects($this->once())
  242. ->method("getPackages")
  243. ->will($this->returnValue($packages));
  244. mkdir($this->vendorDir."/.composer", 0777, true);
  245. $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir."/.composer");
  246. $oldIncludePath = get_include_path();
  247. require($this->vendorDir."/.composer/autoload.php");
  248. $this->assertEquals(
  249. $oldIncludePath.PATH_SEPARATOR.$this->vendorDir."/a/a/lib/",
  250. get_include_path()
  251. );
  252. set_include_path($oldIncludePath);
  253. }
  254. public function testIncludePathFileWithoutPathsIsSkipped()
  255. {
  256. $package = new MemoryPackage('a', '1.0', '1.0');
  257. $packages = array();
  258. $a = new MemoryPackage("a/a", "1.0", "1.0");
  259. $packages[] = $a;
  260. $this->repository->expects($this->once())
  261. ->method("getPackages")
  262. ->will($this->returnValue($packages));
  263. mkdir($this->vendorDir."/.composer", 0777, true);
  264. $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir."/.composer");
  265. $this->assertFalse(file_exists($this->vendorDir."/.composer/include_paths.php"));
  266. }
  267. private function createClassFile($basedir)
  268. {
  269. if (!is_dir($basedir.'/.composersrc')) {
  270. mkdir($basedir.'/.composersrc', 0777, true);
  271. }
  272. file_put_contents($basedir.'/.composersrc/foo.php', '<?php class ClassMapFoo {}');
  273. }
  274. private function assertAutoloadFiles($name, $dir, $type = 'namespaces')
  275. {
  276. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_'.$name.'.php', $dir.'/autoload_'.$type.'.php');
  277. }
  278. }