AutoloadGeneratorTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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\Installer;
  12. use Composer\Autoload\AutoloadGenerator;
  13. use Composer\Downloader\Util\Filesystem;
  14. use Composer\Package\MemoryPackage;
  15. class AutoloadGeneratorTest extends \PHPUnit_Framework_TestCase
  16. {
  17. public $vendorDir;
  18. private $workingDir;
  19. private $im;
  20. private $repository;
  21. private $generator;
  22. protected function setUp()
  23. {
  24. $fs = new Filesystem;
  25. $that = $this;
  26. $this->workingDir = realpath(sys_get_temp_dir());
  27. $this->vendorDir = $this->workingDir.DIRECTORY_SEPARATOR.'composer-test-autoload';
  28. if (is_dir($this->vendorDir)) {
  29. $fs->removeDirectory($this->vendorDir);
  30. }
  31. mkdir($this->vendorDir);
  32. $this->dir = getcwd();
  33. chdir($this->workingDir);
  34. $this->im = $this->getMockBuilder('Composer\Installer\InstallationManager')
  35. ->disableOriginalConstructor()
  36. ->getMock();
  37. $this->im->expects($this->any())
  38. ->method('getInstallPath')
  39. ->will($this->returnCallback(function ($package) use ($that) {
  40. return $that->vendorDir.'/'.$package->getName();
  41. }));
  42. $this->im->expects($this->any())
  43. ->method('getVendorPath')
  44. ->will($this->returnCallback(function () use ($that) {
  45. return $that->vendorDir;
  46. }));
  47. $this->repo = $this->getMock('Composer\Repository\RepositoryInterface');
  48. $this->generator = new AutoloadGenerator();
  49. }
  50. protected function tearDown()
  51. {
  52. chdir($this->dir);
  53. }
  54. public function testMainPackageAutoloading()
  55. {
  56. $package = new MemoryPackage('a', '1.0', '1.0');
  57. $package->setAutoload(array('psr-0' => array('Main' => 'src/', 'Lala' => 'src/')));
  58. $this->repo->expects($this->once())
  59. ->method('getPackages')
  60. ->will($this->returnValue(array()));
  61. mkdir($this->vendorDir.'/.composer');
  62. $this->generator->dump($this->repo, $package, $this->im, $this->vendorDir.'/.composer');
  63. $this->assertAutoloadFiles('main', $this->vendorDir.'/.composer');
  64. }
  65. public function testVendorDirSameAsWorkingDir()
  66. {
  67. $this->vendorDir = $this->workingDir;
  68. $package = new MemoryPackage('a', '1.0', '1.0');
  69. $package->setAutoload(array('psr-0' => array('Main' => 'src/', 'Lala' => 'src/')));
  70. $this->repo->expects($this->once())
  71. ->method('getPackages')
  72. ->will($this->returnValue(array()));
  73. if (!is_dir($this->vendorDir.'/.composer')) {
  74. mkdir($this->vendorDir.'/.composer', 0777, true);
  75. }
  76. $this->generator->dump($this->repo, $package, $this->im, $this->vendorDir.'/.composer');
  77. $this->assertAutoloadFiles('main3', $this->vendorDir.'/.composer');
  78. }
  79. public function testMainPackageAutoloadingAlternativeVendorDir()
  80. {
  81. $package = new MemoryPackage('a', '1.0', '1.0');
  82. $package->setAutoload(array('psr-0' => array('Main' => 'src/', 'Lala' => 'src/')));
  83. $this->repo->expects($this->once())
  84. ->method('getPackages')
  85. ->will($this->returnValue(array()));
  86. $this->vendorDir .= '/subdir';
  87. mkdir($this->vendorDir.'/.composer', 0777, true);
  88. $this->generator->dump($this->repo, $package, $this->im, $this->vendorDir.'/.composer');
  89. $this->assertAutoloadFiles('main2', $this->vendorDir.'/.composer');
  90. }
  91. public function testVendorsAutoloading()
  92. {
  93. $package = new MemoryPackage('a', '1.0', '1.0');
  94. $packages = array();
  95. $packages[] = $a = new MemoryPackage('a/a', '1.0', '1.0');
  96. $packages[] = $b = new MemoryPackage('b/b', '1.0', '1.0');
  97. $a->setAutoload(array('psr-0' => array('A' => 'src/', 'A\\B' => 'lib/')));
  98. $b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/')));
  99. $this->repo->expects($this->once())
  100. ->method('getPackages')
  101. ->will($this->returnValue($packages));
  102. mkdir($this->vendorDir.'/.composer', 0777, true);
  103. $this->generator->dump($this->repo, $package, $this->im, $this->vendorDir.'/.composer');
  104. $this->assertAutoloadFiles('vendors', $this->vendorDir.'/.composer');
  105. }
  106. public function testOverrideVendorsAutoloading()
  107. {
  108. $package = new MemoryPackage('a', '1.0', '1.0');
  109. $package->setAutoload(array('psr-0' => array('A\\B' => '/home/deveuser/local-packages/a-a/lib')));
  110. $packages = array();
  111. $packages[] = $a = new MemoryPackage('a/a', '1.0', '1.0');
  112. $packages[] = $b = new MemoryPackage('b/b', '1.0', '1.0');
  113. $a->setAutoload(array('psr-0' => array('A' => 'src/', 'A\\B' => 'lib/')));
  114. $b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/')));
  115. $this->repo->expects($this->once())
  116. ->method('getPackages')
  117. ->will($this->returnValue($packages));
  118. mkdir($this->vendorDir.'/.composer', 0777, true);
  119. $this->generator->dump($this->repo, $package, $this->im, $this->vendorDir.'/.composer');
  120. $this->assertAutoloadFiles('override_vendors', $this->vendorDir.'/.composer');
  121. }
  122. private function assertAutoloadFiles($name, $dir)
  123. {
  124. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_'.$name.'.php', $dir.'/autoload_namespaces.php');
  125. }
  126. }