AutoloadGeneratorTest.php 25 KB

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