AutoloadGeneratorTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Test\Autoload;
  12. use Composer\Autoload\AutoloadGenerator;
  13. use Composer\Util\Filesystem;
  14. use Composer\Package\MemoryPackage;
  15. use Composer\Test\TestCase;
  16. class AutoloadGeneratorTest extends TestCase
  17. {
  18. public $vendorDir;
  19. private $workingDir;
  20. private $im;
  21. private $repository;
  22. private $generator;
  23. private $fs;
  24. protected function setUp()
  25. {
  26. $this->fs = new Filesystem;
  27. $that = $this;
  28. $this->workingDir = realpath(sys_get_temp_dir());
  29. $this->vendorDir = $this->workingDir.DIRECTORY_SEPARATOR.'composer-test-autoload';
  30. $this->ensureDirectoryExistsAndClear($this->vendorDir);
  31. $this->dir = getcwd();
  32. chdir($this->workingDir);
  33. $this->im = $this->getMockBuilder('Composer\Installer\InstallationManager')
  34. ->disableOriginalConstructor()
  35. ->getMock();
  36. $this->im->expects($this->any())
  37. ->method('getInstallPath')
  38. ->will($this->returnCallback(function ($package) use ($that) {
  39. return $that->vendorDir.'/'.$package->getName();
  40. }));
  41. $this->im->expects($this->any())
  42. ->method('getVendorPath')
  43. ->will($this->returnCallback(function () use ($that) {
  44. return $that->vendorDir;
  45. }));
  46. $this->repository = $this->getMock('Composer\Repository\RepositoryInterface');
  47. $this->generator = new AutoloadGenerator();
  48. }
  49. protected function tearDown()
  50. {
  51. if ($this->vendorDir === $this->workingDir) {
  52. if (is_dir($this->workingDir.'/.composer')) {
  53. $this->fs->removeDirectory($this->workingDir.'/.composer');
  54. }
  55. } elseif (is_dir($this->vendorDir)) {
  56. $this->fs->removeDirectory($this->vendorDir);
  57. }
  58. chdir($this->dir);
  59. }
  60. public function testMainPackageAutoloading()
  61. {
  62. $package = new MemoryPackage('a', '1.0', '1.0');
  63. $package->setAutoload(array('psr-0' => array('Main' => 'src/', 'Lala' => 'src/')));
  64. $this->repository->expects($this->once())
  65. ->method('getPackages')
  66. ->will($this->returnValue(array()));
  67. mkdir($this->vendorDir.'/.composer');
  68. $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/.composer');
  69. $this->assertAutoloadFiles('main', $this->vendorDir.'/.composer');
  70. }
  71. public function testVendorDirSameAsWorkingDir()
  72. {
  73. $this->vendorDir = $this->workingDir;
  74. $package = new MemoryPackage('a', '1.0', '1.0');
  75. $package->setAutoload(array('psr-0' => array('Main' => 'src/', 'Lala' => 'src/')));
  76. $this->repository->expects($this->once())
  77. ->method('getPackages')
  78. ->will($this->returnValue(array()));
  79. if (!is_dir($this->vendorDir.'/.composer')) {
  80. mkdir($this->vendorDir.'/.composer', 0777, true);
  81. }
  82. $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/.composer');
  83. $this->assertAutoloadFiles('main3', $this->vendorDir.'/.composer');
  84. }
  85. public function testMainPackageAutoloadingAlternativeVendorDir()
  86. {
  87. $package = new MemoryPackage('a', '1.0', '1.0');
  88. $package->setAutoload(array('psr-0' => array('Main' => 'src/', 'Lala' => 'src/')));
  89. $this->repository->expects($this->once())
  90. ->method('getPackages')
  91. ->will($this->returnValue(array()));
  92. $this->vendorDir .= '/subdir';
  93. mkdir($this->vendorDir.'/.composer', 0777, true);
  94. $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/.composer');
  95. $this->assertAutoloadFiles('main2', $this->vendorDir.'/.composer');
  96. }
  97. public function testVendorsAutoloading()
  98. {
  99. $package = new MemoryPackage('a', '1.0', '1.0');
  100. $packages = array();
  101. $packages[] = $a = new MemoryPackage('a/a', '1.0', '1.0');
  102. $packages[] = $b = new MemoryPackage('b/b', '1.0', '1.0');
  103. $a->setAutoload(array('psr-0' => array('A' => 'src/', 'A\\B' => 'lib/')));
  104. $b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/')));
  105. $this->repository->expects($this->once())
  106. ->method('getPackages')
  107. ->will($this->returnValue($packages));
  108. mkdir($this->vendorDir.'/.composer', 0777, true);
  109. $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/.composer');
  110. $this->assertAutoloadFiles('vendors', $this->vendorDir.'/.composer');
  111. $this->assertTrue(file_exists($this->vendorDir.'/.composer/autoload_classmap.php'), "ClassMap file needs to be generated, even if empty.");
  112. }
  113. public function testVendorsClassMapAutoloading()
  114. {
  115. $package = new MemoryPackage('a', '1.0', '1.0');
  116. $packages = array();
  117. $packages[] = $a = new MemoryPackage('a/a', '1.0', '1.0');
  118. $packages[] = $b = new MemoryPackage('b/b', '1.0', '1.0');
  119. $a->setAutoload(array('classmap' => array('src/')));
  120. $b->setAutoload(array('classmap' => array('src/', 'lib/')));
  121. $this->repository->expects($this->once())
  122. ->method('getPackages')
  123. ->will($this->returnValue($packages));
  124. @mkdir($this->vendorDir.'/.composer', 0777, true);
  125. mkdir($this->vendorDir.'/a/a/src', 0777, true);
  126. mkdir($this->vendorDir.'/b/b/src', 0777, true);
  127. mkdir($this->vendorDir.'/b/b/lib', 0777, true);
  128. file_put_contents($this->vendorDir.'/a/a/src/a.php', '<?php class ClassMapFoo {}');
  129. file_put_contents($this->vendorDir.'/b/b/src/b.php', '<?php class ClassMapBar {}');
  130. file_put_contents($this->vendorDir.'/b/b/lib/c.php', '<?php class ClassMapBaz {}');
  131. $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/.composer');
  132. $this->assertTrue(file_exists($this->vendorDir.'/.composer/autoload_classmap.php'), "ClassMap file needs to be generated.");
  133. $this->assertEquals(
  134. array(
  135. 'ClassMapFoo' => $this->workingDir.'/composer-test-autoload/a/a/src/a.php',
  136. 'ClassMapBar' => $this->workingDir.'/composer-test-autoload/b/b/src/b.php',
  137. 'ClassMapBaz' => $this->workingDir.'/composer-test-autoload/b/b/lib/c.php',
  138. ),
  139. include ($this->vendorDir.'/.composer/autoload_classmap.php')
  140. );
  141. }
  142. public function testOverrideVendorsAutoloading()
  143. {
  144. $package = new MemoryPackage('a', '1.0', '1.0');
  145. $package->setAutoload(array('psr-0' => array('A\\B' => '/home/deveuser/local-packages/a-a/lib')));
  146. $packages = array();
  147. $packages[] = $a = new MemoryPackage('a/a', '1.0', '1.0');
  148. $packages[] = $b = new MemoryPackage('b/b', '1.0', '1.0');
  149. $a->setAutoload(array('psr-0' => array('A' => 'src/', 'A\\B' => 'lib/')));
  150. $b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/')));
  151. $this->repository->expects($this->once())
  152. ->method('getPackages')
  153. ->will($this->returnValue($packages));
  154. mkdir($this->vendorDir.'/.composer', 0777, true);
  155. $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/.composer');
  156. $this->assertAutoloadFiles('override_vendors', $this->vendorDir.'/.composer');
  157. }
  158. private function assertAutoloadFiles($name, $dir)
  159. {
  160. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_'.$name.'.php', $dir.'/autoload_namespaces.php');
  161. }
  162. }