PageRenderTime 55ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php

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