AutoloadGeneratorTest.php 5.0 KB

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