AutoloadGeneratorTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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\Package\Link;
  14. use Composer\Util\Filesystem;
  15. use Composer\Package\AliasPackage;
  16. use Composer\Package\Package;
  17. use Composer\Test\TestCase;
  18. class AutoloadGeneratorTest extends TestCase
  19. {
  20. public $vendorDir;
  21. private $config;
  22. private $workingDir;
  23. private $im;
  24. private $repository;
  25. private $generator;
  26. private $fs;
  27. protected function setUp()
  28. {
  29. $this->fs = new Filesystem;
  30. $that = $this;
  31. $this->workingDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'cmptest';
  32. $this->fs->ensureDirectoryExists($this->workingDir);
  33. $this->vendorDir = $this->workingDir.DIRECTORY_SEPARATOR.'composer-test-autoload-'.md5(uniqid('', true));
  34. $this->ensureDirectoryExistsAndClear($this->vendorDir);
  35. $this->config = $this->getMock('Composer\Config');
  36. $this->config->expects($this->at(0))
  37. ->method('get')
  38. ->with($this->equalTo('vendor-dir'))
  39. ->will($this->returnCallback(function () use ($that) {
  40. return $that->vendorDir;
  41. }));
  42. $this->config->expects($this->at(1))
  43. ->method('get')
  44. ->with($this->equalTo('vendor-dir'))
  45. ->will($this->returnCallback(function () use ($that) {
  46. return $that->vendorDir;
  47. }));
  48. $this->dir = getcwd();
  49. chdir($this->workingDir);
  50. $this->im = $this->getMockBuilder('Composer\Installer\InstallationManager')
  51. ->disableOriginalConstructor()
  52. ->getMock();
  53. $this->im->expects($this->any())
  54. ->method('getInstallPath')
  55. ->will($this->returnCallback(function ($package) use ($that) {
  56. return $that->vendorDir.'/'.$package->getName();
  57. }));
  58. $this->repository = $this->getMock('Composer\Repository\RepositoryInterface');
  59. $this->generator = new AutoloadGenerator();
  60. }
  61. protected function tearDown()
  62. {
  63. chdir($this->dir);
  64. if (is_dir($this->workingDir)) {
  65. $this->fs->removeDirectory($this->workingDir);
  66. }
  67. if (is_dir($this->vendorDir)) {
  68. $this->fs->removeDirectory($this->vendorDir);
  69. }
  70. }
  71. public function testMainPackageAutoloading()
  72. {
  73. $package = new Package('a', '1.0', '1.0');
  74. $package->setAutoload(array(
  75. 'psr-0' => array('Main' => 'src/', 'Lala' => array('src/', 'lib/')),
  76. 'classmap' => array('composersrc/'),
  77. ));
  78. $this->repository->expects($this->once())
  79. ->method('getPackages')
  80. ->will($this->returnValue(array()));
  81. $this->fs->ensureDirectoryExists($this->workingDir.'/composer');
  82. $this->fs->ensureDirectoryExists($this->workingDir.'/src');
  83. $this->fs->ensureDirectoryExists($this->workingDir.'/lib');
  84. $this->createClassFile($this->workingDir);
  85. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_1');
  86. $this->assertAutoloadFiles('main', $this->vendorDir.'/composer');
  87. $this->assertAutoloadFiles('classmap', $this->vendorDir.'/composer', 'classmap');
  88. }
  89. public function testVendorDirSameAsWorkingDir()
  90. {
  91. $this->vendorDir = $this->workingDir;
  92. $package = new Package('a', '1.0', '1.0');
  93. $package->setAutoload(array(
  94. 'psr-0' => array('Main' => 'src/', 'Lala' => 'src/'),
  95. 'classmap' => array('composersrc/'),
  96. ));
  97. $this->repository->expects($this->once())
  98. ->method('getPackages')
  99. ->will($this->returnValue(array()));
  100. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  101. $this->fs->ensureDirectoryExists($this->vendorDir.'/src/Main');
  102. file_put_contents($this->vendorDir.'/src/Main/Foo.php', '<?php namespace Main; class Foo {}');
  103. $this->createClassFile($this->vendorDir);
  104. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_2');
  105. $this->assertAutoloadFiles('main3', $this->vendorDir.'/composer');
  106. $this->assertAutoloadFiles('classmap3', $this->vendorDir.'/composer', 'classmap');
  107. }
  108. public function testMainPackageAutoloadingAlternativeVendorDir()
  109. {
  110. $package = new Package('a', '1.0', '1.0');
  111. $package->setAutoload(array(
  112. 'psr-0' => array('Main' => 'src/', 'Lala' => 'src/'),
  113. 'classmap' => array('composersrc/'),
  114. ));
  115. $this->repository->expects($this->once())
  116. ->method('getPackages')
  117. ->will($this->returnValue(array()));
  118. $this->vendorDir .= '/subdir';
  119. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  120. $this->fs->ensureDirectoryExists($this->workingDir.'/src');
  121. $this->createClassFile($this->workingDir);
  122. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_3');
  123. $this->assertAutoloadFiles('main2', $this->vendorDir.'/composer');
  124. $this->assertAutoloadFiles('classmap2', $this->vendorDir.'/composer', 'classmap');
  125. }
  126. public function testMainPackageAutoloadingWithTargetDir()
  127. {
  128. $package = new Package('a', '1.0', '1.0');
  129. $package->setAutoload(array(
  130. 'psr-0' => array('Main\\Foo' => '', 'Main\\Bar' => ''),
  131. ));
  132. $package->setTargetDir('Main/Foo/');
  133. $this->repository->expects($this->once())
  134. ->method('getPackages')
  135. ->will($this->returnValue(array()));
  136. $this->fs->ensureDirectoryExists($this->vendorDir.'/a');
  137. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'TargetDir');
  138. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_target_dir.php', $this->vendorDir.'/autoload.php');
  139. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_target_dir.php', $this->vendorDir.'/composer/autoload_real.php');
  140. }
  141. public function testMainPackageAutoloadingWithTargetDirAndClassmap()
  142. {
  143. $package = new Package('a', '1.0', '1.0');
  144. $package->setAutoload(array(
  145. 'classmap' => array('Main/Foo/composersrc/'),
  146. ));
  147. $package->setTargetDir('Main/Foo/');
  148. $this->repository->expects($this->once())
  149. ->method('getPackages')
  150. ->will($this->returnValue(array()));
  151. $this->vendorDir .= '/subdir';
  152. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  153. $this->fs->ensureDirectoryExists($this->workingDir.'/src');
  154. $this->createClassFile($this->workingDir);
  155. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'TargetDirNoPsr');
  156. $this->assertAutoloadFiles('classmap2', $this->vendorDir.'/composer', 'classmap');
  157. }
  158. public function testVendorsAutoloading()
  159. {
  160. $package = new Package('a', '1.0', '1.0');
  161. $packages = array();
  162. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  163. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  164. $packages[] = $c = new AliasPackage($b, '1.2', '1.2');
  165. $a->setAutoload(array('psr-0' => array('A' => 'src/', 'A\\B' => 'lib/')));
  166. $b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/')));
  167. $this->repository->expects($this->once())
  168. ->method('getPackages')
  169. ->will($this->returnValue($packages));
  170. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  171. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  172. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib');
  173. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  174. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_5');
  175. $this->assertAutoloadFiles('vendors', $this->vendorDir.'/composer');
  176. $this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated, even if empty.");
  177. }
  178. public function testPSR0ToClassMapIgnoresNonExistingDir()
  179. {
  180. $package = new Package('a', '1.0', '1.0');
  181. $package->setAutoload(array('psr-0' => array('foo/bar/non/existing/')));
  182. $this->repository->expects($this->once())
  183. ->method('getPackages')
  184. ->will($this->returnValue(array()));
  185. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_8');
  186. $this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated.");
  187. $this->assertEquals(
  188. array(),
  189. include $this->vendorDir.'/composer/autoload_classmap.php'
  190. );
  191. }
  192. public function testVendorsClassMapAutoloading()
  193. {
  194. $package = new Package('a', '1.0', '1.0');
  195. $packages = array();
  196. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  197. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  198. $a->setAutoload(array('classmap' => array('src/')));
  199. $b->setAutoload(array('classmap' => array('src/', 'lib/')));
  200. $this->repository->expects($this->once())
  201. ->method('getPackages')
  202. ->will($this->returnValue($packages));
  203. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  204. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  205. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  206. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/lib');
  207. file_put_contents($this->vendorDir.'/a/a/src/a.php', '<?php class ClassMapFoo {}');
  208. file_put_contents($this->vendorDir.'/b/b/src/b.php', '<?php class ClassMapBar {}');
  209. file_put_contents($this->vendorDir.'/b/b/lib/c.php', '<?php class ClassMapBaz {}');
  210. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_6');
  211. $this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated.");
  212. $this->assertEquals(
  213. $this->normalizePaths(array(
  214. 'ClassMapBar' => $this->vendorDir.'/b/b/src/b.php',
  215. 'ClassMapBaz' => $this->vendorDir.'/b/b/lib/c.php',
  216. 'ClassMapFoo' => $this->vendorDir.'/a/a/src/a.php',
  217. )),
  218. $this->normalizePaths(include $this->vendorDir.'/composer/autoload_classmap.php')
  219. );
  220. $this->assertAutoloadFiles('classmap4', $this->vendorDir.'/composer', 'classmap');
  221. }
  222. public function testClassMapAutoloadingEmptyDirAndExactFile()
  223. {
  224. $package = new Package('a', '1.0', '1.0');
  225. $packages = array();
  226. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  227. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  228. $packages[] = $c = new Package('c/c', '1.0', '1.0');
  229. $a->setAutoload(array('classmap' => array('')));
  230. $b->setAutoload(array('classmap' => array('test.php')));
  231. $c->setAutoload(array('classmap' => array('./')));
  232. $this->repository->expects($this->once())
  233. ->method('getPackages')
  234. ->will($this->returnValue($packages));
  235. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  236. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  237. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b');
  238. $this->fs->ensureDirectoryExists($this->vendorDir.'/c/c/foo');
  239. file_put_contents($this->vendorDir.'/a/a/src/a.php', '<?php class ClassMapFoo {}');
  240. file_put_contents($this->vendorDir.'/b/b/test.php', '<?php class ClassMapBar {}');
  241. file_put_contents($this->vendorDir.'/c/c/foo/test.php', '<?php class ClassMapBaz {}');
  242. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_7');
  243. $this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated.");
  244. $this->assertEquals(
  245. $this->normalizePaths(array(
  246. 'ClassMapBar' => $this->vendorDir.'/b/b/test.php',
  247. 'ClassMapBaz' => $this->vendorDir.'/c/c/foo/test.php',
  248. 'ClassMapFoo' => $this->vendorDir.'/a/a/src/a.php',
  249. )),
  250. $this->normalizePaths(include $this->vendorDir.'/composer/autoload_classmap.php')
  251. );
  252. $this->assertAutoloadFiles('classmap5', $this->vendorDir.'/composer', 'classmap');
  253. }
  254. public function testFilesAutoloadGeneration()
  255. {
  256. $package = new Package('a', '1.0', '1.0');
  257. $package->setAutoload(array('files' => array('root.php')));
  258. $packages = array();
  259. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  260. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  261. $a->setAutoload(array('files' => array('test.php')));
  262. $b->setAutoload(array('files' => array('test2.php')));
  263. $this->repository->expects($this->once())
  264. ->method('getPackages')
  265. ->will($this->returnValue($packages));
  266. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a');
  267. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b');
  268. file_put_contents($this->vendorDir.'/a/a/test.php', '<?php function testFilesAutoloadGeneration1() {}');
  269. file_put_contents($this->vendorDir.'/b/b/test2.php', '<?php function testFilesAutoloadGeneration2() {}');
  270. file_put_contents($this->workingDir.'/root.php', '<?php function testFilesAutoloadGenerationRoot() {}');
  271. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'FilesAutoload');
  272. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php');
  273. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_functions.php', $this->vendorDir.'/composer/autoload_real.php');
  274. include $this->vendorDir . '/autoload.php';
  275. $this->assertTrue(function_exists('testFilesAutoloadGeneration1'));
  276. $this->assertTrue(function_exists('testFilesAutoloadGeneration2'));
  277. $this->assertTrue(function_exists('testFilesAutoloadGenerationRoot'));
  278. }
  279. public function testFilesAutoloadOrderByDependencies()
  280. {
  281. $package = new Package('a', '1.0', '1.0');
  282. $package->setAutoload(array('files' => array('root.php')));
  283. $package->setRequires(array(new Link('a', 'z/foo')));
  284. $package->setRequires(array(new Link('a', 'd/d')));
  285. $package->setRequires(array(new Link('a', 'e/e')));
  286. $packages = array();
  287. $packages[] = $z = new Package('z/foo', '1.0', '1.0');
  288. $packages[] = $b = new Package('b/bar', '1.0', '1.0');
  289. $packages[] = $d = new Package('d/d', '1.0', '1.0');
  290. $packages[] = $c = new Package('c/lorem', '1.0', '1.0');
  291. $packages[] = $e = new Package('e/e', '1.0', '1.0');
  292. $z->setAutoload(array('files' => array('testA.php')));
  293. $z->setRequires(array(new Link('z/foo', 'c/lorem')));
  294. $b->setAutoload(array('files' => array('testB.php')));
  295. $b->setRequires(array(new Link('b/bar', 'c/lorem'), new Link('b/bar', 'd/d')));
  296. $c->setAutoload(array('files' => array('testC.php')));
  297. $d->setAutoload(array('files' => array('testD.php')));
  298. $d->setRequires(array(new Link('d/d', 'c/lorem')));
  299. $e->setAutoload(array('files' => array('testE.php')));
  300. $e->setRequires(array(new Link('e/e', 'c/lorem')));
  301. $this->repository->expects($this->once())
  302. ->method('getPackages')
  303. ->will($this->returnValue($packages));
  304. $this->fs->ensureDirectoryExists($this->vendorDir . '/z/foo');
  305. $this->fs->ensureDirectoryExists($this->vendorDir . '/b/bar');
  306. $this->fs->ensureDirectoryExists($this->vendorDir . '/c/lorem');
  307. $this->fs->ensureDirectoryExists($this->vendorDir . '/d/d');
  308. $this->fs->ensureDirectoryExists($this->vendorDir . '/e/e');
  309. file_put_contents($this->vendorDir . '/z/foo/testA.php', '<?php function testFilesAutoloadOrderByDependency1() {}');
  310. file_put_contents($this->vendorDir . '/b/bar/testB.php', '<?php function testFilesAutoloadOrderByDependency2() {}');
  311. file_put_contents($this->vendorDir . '/c/lorem/testC.php', '<?php function testFilesAutoloadOrderByDependency3() {}');
  312. file_put_contents($this->vendorDir . '/d/d/testD.php', '<?php function testFilesAutoloadOrderByDependency4() {}');
  313. file_put_contents($this->vendorDir . '/e/e/testE.php', '<?php function testFilesAutoloadOrderByDependency5() {}');
  314. file_put_contents($this->workingDir . '/root.php', '<?php function testFilesAutoloadOrderByDependencyRoot() {}');
  315. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'FilesAutoloadOrder');
  316. $this->assertFileEquals(__DIR__ . '/Fixtures/autoload_functions_by_dependency.php', $this->vendorDir . '/autoload.php');
  317. $this->assertFileEquals(__DIR__ . '/Fixtures/autoload_real_files_by_dependency.php', $this->vendorDir . '/composer/autoload_real.php');
  318. require $this->vendorDir . '/autoload.php';
  319. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency1'));
  320. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency2'));
  321. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency3'));
  322. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency4'));
  323. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency5'));
  324. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependencyRoot'));
  325. }
  326. public function testOverrideVendorsAutoloading()
  327. {
  328. $package = new Package('z', '1.0', '1.0');
  329. $package->setAutoload(array('psr-0' => array('A\\B' => $this->workingDir.'/lib'), 'classmap' => array($this->workingDir.'/src')));
  330. $package->setRequires(array(new Link('z', 'a/a')));
  331. $packages = array();
  332. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  333. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  334. $a->setAutoload(array('psr-0' => array('A' => 'src/', 'A\\B' => 'lib/'), 'classmap' => array('classmap')));
  335. $b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/')));
  336. $this->repository->expects($this->once())
  337. ->method('getPackages')
  338. ->will($this->returnValue($packages));
  339. $this->fs->ensureDirectoryExists($this->workingDir.'/lib/A/B');
  340. $this->fs->ensureDirectoryExists($this->workingDir.'/src/');
  341. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  342. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/classmap');
  343. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  344. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib/A/B');
  345. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  346. file_put_contents($this->workingDir.'/lib/A/B/C.php', '<?php namespace A\\B; class C {}');
  347. file_put_contents($this->workingDir.'/src/classes.php', '<?php namespace Foo; class Bar {}');
  348. file_put_contents($this->vendorDir.'/a/a/lib/A/B/C.php', '<?php namespace A\\B; class C {}');
  349. file_put_contents($this->vendorDir.'/a/a/classmap/classes.php', '<?php namespace Foo; class Bar {}');
  350. $workDir = strtr($this->workingDir, '\\', '/');
  351. $expectedNamespace = <<<EOF
  352. <?php
  353. // autoload_namespaces.php generated by Composer
  354. \$vendorDir = dirname(__DIR__);
  355. \$baseDir = dirname(\$vendorDir);
  356. return array(
  357. 'B\\\\Sub\\\\Name' => \$vendorDir . '/b/b/src/',
  358. 'A\\\\B' => array('$workDir/lib', \$vendorDir . '/a/a/lib/'),
  359. 'A' => \$vendorDir . '/a/a/src/',
  360. );
  361. EOF;
  362. $expectedClassmap = <<<EOF
  363. <?php
  364. // autoload_classmap.php generated by Composer
  365. \$vendorDir = dirname(__DIR__);
  366. \$baseDir = dirname(\$vendorDir);
  367. return array(
  368. 'A\\\\B\\\\C' => \$baseDir . '/lib/A/B/C.php',
  369. 'Foo\\\\Bar' => \$baseDir . '/src/classes.php',
  370. );
  371. EOF;
  372. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_9');
  373. $this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php'));
  374. $this->assertEquals($expectedClassmap, file_get_contents($this->vendorDir.'/composer/autoload_classmap.php'));
  375. }
  376. public function testIncludePathFileGeneration()
  377. {
  378. $package = new Package('a', '1.0', '1.0');
  379. $packages = array();
  380. $a = new Package("a/a", "1.0", "1.0");
  381. $a->setIncludePaths(array("lib/"));
  382. $b = new Package("b/b", "1.0", "1.0");
  383. $b->setIncludePaths(array("library"));
  384. $c = new Package("c", "1.0", "1.0");
  385. $c->setIncludePaths(array("library"));
  386. $packages[] = $a;
  387. $packages[] = $b;
  388. $packages[] = $c;
  389. $this->repository->expects($this->once())
  390. ->method("getPackages")
  391. ->will($this->returnValue($packages));
  392. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  393. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_10');
  394. $this->assertFileEquals(__DIR__.'/Fixtures/include_paths.php', $this->vendorDir.'/composer/include_paths.php');
  395. $this->assertEquals(
  396. array(
  397. $this->vendorDir."/a/a/lib",
  398. $this->vendorDir."/b/b/library",
  399. $this->vendorDir."/c/library",
  400. ),
  401. require $this->vendorDir."/composer/include_paths.php"
  402. );
  403. }
  404. public function testIncludePathsArePrependedInAutoloadFile()
  405. {
  406. $package = new Package('a', '1.0', '1.0');
  407. $packages = array();
  408. $a = new Package("a/a", "1.0", "1.0");
  409. $a->setIncludePaths(array("lib/"));
  410. $packages[] = $a;
  411. $this->repository->expects($this->once())
  412. ->method("getPackages")
  413. ->will($this->returnValue($packages));
  414. mkdir($this->vendorDir."/composer", 0777, true);
  415. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_11');
  416. $oldIncludePath = get_include_path();
  417. require $this->vendorDir."/autoload.php";
  418. $this->assertEquals(
  419. $this->vendorDir."/a/a/lib".PATH_SEPARATOR.$oldIncludePath,
  420. get_include_path()
  421. );
  422. set_include_path($oldIncludePath);
  423. }
  424. public function testIncludePathsInMainPackage()
  425. {
  426. $package = new Package('a', '1.0', '1.0');
  427. $package->setIncludePaths(array('/lib', '/src'));
  428. $packages = array($a = new Package("a/a", "1.0", "1.0"));
  429. $a->setIncludePaths(array("lib/"));
  430. $this->repository->expects($this->once())
  431. ->method("getPackages")
  432. ->will($this->returnValue($packages));
  433. mkdir($this->vendorDir."/composer", 0777, true);
  434. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_12');
  435. $oldIncludePath = get_include_path();
  436. require $this->vendorDir."/autoload.php";
  437. $this->assertEquals(
  438. $this->workingDir."/lib".PATH_SEPARATOR.$this->workingDir."/src".PATH_SEPARATOR.$this->vendorDir."/a/a/lib".PATH_SEPARATOR.$oldIncludePath,
  439. get_include_path()
  440. );
  441. set_include_path($oldIncludePath);
  442. }
  443. public function testIncludePathFileWithoutPathsIsSkipped()
  444. {
  445. $package = new Package('a', '1.0', '1.0');
  446. $packages = array();
  447. $a = new Package("a/a", "1.0", "1.0");
  448. $packages[] = $a;
  449. $this->repository->expects($this->once())
  450. ->method("getPackages")
  451. ->will($this->returnValue($packages));
  452. mkdir($this->vendorDir."/composer", 0777, true);
  453. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_12');
  454. $this->assertFalse(file_exists($this->vendorDir."/composer/include_paths.php"));
  455. }
  456. public function testUseGlobalIncludePath()
  457. {
  458. $package = new Package('a', '1.0', '1.0');
  459. $package->setAutoload(array(
  460. 'psr-0' => array('Main\\Foo' => '', 'Main\\Bar' => ''),
  461. ));
  462. $package->setTargetDir('Main/Foo/');
  463. $this->repository->expects($this->once())
  464. ->method('getPackages')
  465. ->will($this->returnValue(array()));
  466. $this->config->expects($this->at(2))
  467. ->method('get')
  468. ->with($this->equalTo('use-include-path'))
  469. ->will($this->returnValue(true));
  470. $this->fs->ensureDirectoryExists($this->vendorDir.'/a');
  471. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'IncludePath');
  472. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_include_path.php', $this->vendorDir.'/composer/autoload_real.php');
  473. }
  474. private function createClassFile($basedir)
  475. {
  476. if (!is_dir($basedir.'/composersrc')) {
  477. mkdir($basedir.'/composersrc', 0777, true);
  478. }
  479. file_put_contents($basedir.'/composersrc/foo.php', '<?php class ClassMapFoo {}');
  480. }
  481. private function assertAutoloadFiles($name, $dir, $type = 'namespaces')
  482. {
  483. $a = __DIR__.'/Fixtures/autoload_'.$name.'.php';
  484. $b = $dir.'/autoload_'.$type.'.php';
  485. $this->assertEquals(
  486. str_replace('%vendorDir%', basename($this->vendorDir), file_get_contents($a)),
  487. file_get_contents($b),
  488. $a .' does not equal '. $b
  489. );
  490. }
  491. private function normalizePaths($paths)
  492. {
  493. if (!is_array($paths)) {
  494. return strtr($paths, '\\', '/');
  495. }
  496. foreach ($paths as $key => $path) {
  497. $paths[$key] = strtr($path, '\\', '/');
  498. }
  499. return $paths;
  500. }
  501. }