AutoloadGeneratorTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. $this->generator->dump($this->repo, $package, $this->im, $this->vendorDir.'/.composer');
  74. $this->assertAutoloadFiles('main3', $this->vendorDir.'/.composer');
  75. }
  76. public function testMainPackageAutoloadingAlternativeVendorDir()
  77. {
  78. $package = new MemoryPackage('a', '1.0', '1.0');
  79. $package->setAutoload(array('psr-0' => array('Main' => 'src/', 'Lala' => 'src/')));
  80. $this->repo->expects($this->once())
  81. ->method('getPackages')
  82. ->will($this->returnValue(array()));
  83. $this->vendorDir .= '/subdir';
  84. mkdir($this->vendorDir.'/.composer', 0777, true);
  85. $this->generator->dump($this->repo, $package, $this->im, $this->vendorDir.'/.composer');
  86. $this->assertAutoloadFiles('main2', $this->vendorDir.'/.composer');
  87. }
  88. public function testVendorsAutoloading()
  89. {
  90. $package = new MemoryPackage('a', '1.0', '1.0');
  91. $packages = array();
  92. $packages[] = $a = new MemoryPackage('a/a', '1.0', '1.0');
  93. $packages[] = $b = new MemoryPackage('b/b', '1.0', '1.0');
  94. $a->setAutoload(array('psr-0' => array('A' => 'src/', 'A\\B' => 'lib/')));
  95. $b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/')));
  96. $this->repo->expects($this->once())
  97. ->method('getPackages')
  98. ->will($this->returnValue($packages));
  99. mkdir($this->vendorDir.'/.composer', 0777, true);
  100. $this->generator->dump($this->repo, $package, $this->im, $this->vendorDir.'/.composer');
  101. $this->assertAutoloadFiles('vendors', $this->vendorDir.'/.composer');
  102. }
  103. public function testOverrideVendorsAutoloading()
  104. {
  105. $package = new MemoryPackage('a', '1.0', '1.0');
  106. $package->setAutoload(array('psr-0' => array('A\\B' => '/home/deveuser/local-packages/a-a/lib')));
  107. $packages = array();
  108. $packages[] = $a = new MemoryPackage('a/a', '1.0', '1.0');
  109. $packages[] = $b = new MemoryPackage('b/b', '1.0', '1.0');
  110. $a->setAutoload(array('psr-0' => array('A' => 'src/', 'A\\B' => 'lib/')));
  111. $b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/')));
  112. $this->repo->expects($this->once())
  113. ->method('getPackages')
  114. ->will($this->returnValue($packages));
  115. mkdir($this->vendorDir.'/.composer', 0777, true);
  116. $this->generator->dump($this->repo, $package, $this->im, $this->vendorDir.'/.composer');
  117. $this->assertAutoloadFiles('override_vendors', $this->vendorDir.'/.composer');
  118. }
  119. private function assertAutoloadFiles($name, $dir)
  120. {
  121. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_'.$name.'.php', $dir.'/autoload_namespaces.php');
  122. }
  123. }