AutoloadGeneratorTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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. $packages = array();
  279. $packages[] = $z = new Package('z/foo', '1.0', '1.0');
  280. $packages[] = $b = new Package('b/bar', '1.0', '1.0');
  281. $packages[] = $c = new Package('c/lorem', '1.0', '1.0');
  282. $packages[] = $d = new Package('d/d', '1.0', '1.0');
  283. $packages[] = $e = new Package('e/e', '1.0', '1.0');
  284. $z->setAutoload(array('files' => array('testA.php')));
  285. $z->setRequires(array(new Link('z/foo', 'c/lorem')));
  286. $b->setAutoload(array('files' => array('testB.php')));
  287. $b->setRequires(array(new Link('b/bar', 'c/lorem')));
  288. $c->setAutoload(array('files' => array('testC.php')));
  289. $d->setAutoload(array('files' => array('testD.php')));
  290. $e->setAutoload(array('files' => array('testE.php')));
  291. $e->setRequires(array(new Link('e/e', 'c/lorem')));
  292. $this->repository->expects($this->once())
  293. ->method('getPackages')
  294. ->will($this->returnValue($packages));
  295. $this->fs->ensureDirectoryExists($this->vendorDir . '/z/foo');
  296. $this->fs->ensureDirectoryExists($this->vendorDir . '/b/bar');
  297. $this->fs->ensureDirectoryExists($this->vendorDir . '/c/lorem');
  298. $this->fs->ensureDirectoryExists($this->vendorDir . '/d/d');
  299. $this->fs->ensureDirectoryExists($this->vendorDir . '/e/e');
  300. file_put_contents($this->vendorDir . '/z/foo/testA.php', '<?php function testFilesAutoloadOrderByDependency1() {}');
  301. file_put_contents($this->vendorDir . '/b/bar/testB.php', '<?php function testFilesAutoloadOrderByDependency2() {}');
  302. file_put_contents($this->vendorDir . '/c/lorem/testC.php', '<?php function testFilesAutoloadOrderByDependency3() {}');
  303. file_put_contents($this->vendorDir . '/d/d/testD.php', '<?php function testFilesAutoloadOrderByDependency4() {}');
  304. file_put_contents($this->vendorDir . '/e/e/testE.php', '<?php function testFilesAutoloadOrderByDependency5() {}');
  305. file_put_contents($this->workingDir . '/root.php', '<?php function testFilesAutoloadOrderByDependencyRoot() {}');
  306. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'FilesAutoloadOrder');
  307. $this->assertFileEquals(__DIR__ . '/Fixtures/autoload_functions_by_dependency.php', $this->vendorDir . '/autoload.php');
  308. $this->assertFileEquals(__DIR__ . '/Fixtures/autoload_real_files_by_dependency.php', $this->vendorDir . '/composer/autoload_real.php');
  309. require $this->vendorDir . '/autoload.php';
  310. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency1'));
  311. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency2'));
  312. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency3'));
  313. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency4'));
  314. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency5'));
  315. $this->assertTrue(function_exists('testFilesAutoloadOrderByDependencyRoot'));
  316. }
  317. public function testOverrideVendorsAutoloading()
  318. {
  319. $package = new Package('z', '1.0', '1.0');
  320. $package->setAutoload(array('psr-0' => array('A\\B' => $this->workingDir.'/lib'), 'classmap' => array($this->workingDir.'/src')));
  321. $package->setRequires(array(new Link('z', 'a/a')));
  322. $packages = array();
  323. $packages[] = $a = new Package('a/a', '1.0', '1.0');
  324. $packages[] = $b = new Package('b/b', '1.0', '1.0');
  325. $a->setAutoload(array('psr-0' => array('A' => 'src/', 'A\\B' => 'lib/'), 'classmap' => array('classmap')));
  326. $b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/')));
  327. $this->repository->expects($this->once())
  328. ->method('getPackages')
  329. ->will($this->returnValue($packages));
  330. $this->fs->ensureDirectoryExists($this->workingDir.'/lib/A/B');
  331. $this->fs->ensureDirectoryExists($this->workingDir.'/src/');
  332. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  333. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/classmap');
  334. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
  335. $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib/A/B');
  336. $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
  337. file_put_contents($this->workingDir.'/lib/A/B/C.php', '<?php namespace A\\B; class C {}');
  338. file_put_contents($this->workingDir.'/src/classes.php', '<?php namespace Foo; class Bar {}');
  339. file_put_contents($this->vendorDir.'/a/a/lib/A/B/C.php', '<?php namespace A\\B; class C {}');
  340. file_put_contents($this->vendorDir.'/a/a/classmap/classes.php', '<?php namespace Foo; class Bar {}');
  341. $workDir = strtr($this->workingDir, '\\', '/');
  342. $expectedNamespace = <<<EOF
  343. <?php
  344. // autoload_namespaces.php generated by Composer
  345. \$vendorDir = dirname(__DIR__);
  346. \$baseDir = dirname(\$vendorDir);
  347. return array(
  348. 'B\\\\Sub\\\\Name' => \$vendorDir . '/b/b/src/',
  349. 'A\\\\B' => array('$workDir/lib', \$vendorDir . '/a/a/lib/'),
  350. 'A' => \$vendorDir . '/a/a/src/',
  351. );
  352. EOF;
  353. $expectedClassmap = <<<EOF
  354. <?php
  355. // autoload_classmap.php generated by Composer
  356. \$vendorDir = dirname(__DIR__);
  357. \$baseDir = dirname(\$vendorDir);
  358. return array(
  359. 'A\\\\B\\\\C' => \$baseDir . '/lib/A/B/C.php',
  360. 'Foo\\\\Bar' => \$baseDir . '/src/classes.php',
  361. );
  362. EOF;
  363. $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_9');
  364. $this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php'));
  365. $this->assertEquals($expectedClassmap, file_get_contents($this->vendorDir.'/composer/autoload_classmap.php'));
  366. }
  367. public function testIncludePathFileGeneration()
  368. {
  369. $package = new Package('a', '1.0', '1.0');
  370. $packages = array();
  371. $a = new Package("a/a", "1.0", "1.0");
  372. $a->setIncludePaths(array("lib/"));
  373. $b = new Package("b/b", "1.0", "1.0");
  374. $b->setIncludePaths(array("library"));
  375. $c = new Package("c", "1.0", "1.0");
  376. $c->setIncludePaths(array("library"));
  377. $packages[] = $a;
  378. $packages[] = $b;
  379. $packages[] = $c;
  380. $this->repository->expects($this->once())
  381. ->method("getPackages")
  382. ->will($this->returnValue($packages));
  383. $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
  384. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_10');
  385. $this->assertFileEquals(__DIR__.'/Fixtures/include_paths.php', $this->vendorDir.'/composer/include_paths.php');
  386. $this->assertEquals(
  387. array(
  388. $this->vendorDir."/a/a/lib",
  389. $this->vendorDir."/b/b/library",
  390. $this->vendorDir."/c/library",
  391. ),
  392. require $this->vendorDir."/composer/include_paths.php"
  393. );
  394. }
  395. public function testIncludePathsArePrependedInAutoloadFile()
  396. {
  397. $package = new Package('a', '1.0', '1.0');
  398. $packages = array();
  399. $a = new Package("a/a", "1.0", "1.0");
  400. $a->setIncludePaths(array("lib/"));
  401. $packages[] = $a;
  402. $this->repository->expects($this->once())
  403. ->method("getPackages")
  404. ->will($this->returnValue($packages));
  405. mkdir($this->vendorDir."/composer", 0777, true);
  406. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_11');
  407. $oldIncludePath = get_include_path();
  408. require $this->vendorDir."/autoload.php";
  409. $this->assertEquals(
  410. $this->vendorDir."/a/a/lib".PATH_SEPARATOR.$oldIncludePath,
  411. get_include_path()
  412. );
  413. set_include_path($oldIncludePath);
  414. }
  415. public function testIncludePathFileWithoutPathsIsSkipped()
  416. {
  417. $package = new Package('a', '1.0', '1.0');
  418. $packages = array();
  419. $a = new Package("a/a", "1.0", "1.0");
  420. $packages[] = $a;
  421. $this->repository->expects($this->once())
  422. ->method("getPackages")
  423. ->will($this->returnValue($packages));
  424. mkdir($this->vendorDir."/composer", 0777, true);
  425. $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_12');
  426. $this->assertFalse(file_exists($this->vendorDir."/composer/include_paths.php"));
  427. }
  428. private function createClassFile($basedir)
  429. {
  430. if (!is_dir($basedir.'/composersrc')) {
  431. mkdir($basedir.'/composersrc', 0777, true);
  432. }
  433. file_put_contents($basedir.'/composersrc/foo.php', '<?php class ClassMapFoo {}');
  434. }
  435. private function assertAutoloadFiles($name, $dir, $type = 'namespaces')
  436. {
  437. $a = __DIR__.'/Fixtures/autoload_'.$name.'.php';
  438. $b = $dir.'/autoload_'.$type.'.php';
  439. $this->assertEquals(
  440. str_replace('%vendorDir%', basename($this->vendorDir), file_get_contents($a)),
  441. file_get_contents($b),
  442. $a .' does not equal '. $b
  443. );
  444. }
  445. private function normalizePaths($paths)
  446. {
  447. if (!is_array($paths)) {
  448. return strtr($paths, '\\', '/');
  449. }
  450. foreach ($paths as $key => $path) {
  451. $paths[$key] = strtr($path, '\\', '/');
  452. }
  453. return $paths;
  454. }
  455. }