AutoloadGeneratorTest.php 25 KB

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