AutoloadGeneratorTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 = 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->getMockBuilder('Composer\Repository\RepositoryInterface')
  48. ->disableOriginalConstructor()
  49. ->getMock();
  50. $this->generator = new AutoloadGenerator();
  51. }
  52. protected function tearDown()
  53. {
  54. chdir($this->dir);
  55. }
  56. public function testMainPackageAutoloading()
  57. {
  58. $package = new MemoryPackage('a', '1.0', '1.0');
  59. $package->setAutoload(array('psr-0' => array('Main' => 'src/', 'Lala' => 'src/')));
  60. $this->repo->expects($this->once())
  61. ->method('getPackages')
  62. ->will($this->returnValue(array()));
  63. mkdir($this->vendorDir.'/.composer');
  64. $this->generator->dump($this->repo, $package, $this->im, $this->vendorDir.'/.composer');
  65. $this->assertAutoloadFiles('main', $this->vendorDir.'/.composer');
  66. }
  67. public function testMainPackageAutoloadingAlternativeVendorDir()
  68. {
  69. $package = new MemoryPackage('a', '1.0', '1.0');
  70. $package->setAutoload(array('psr-0' => array('Main' => 'src/', 'Lala' => 'src/')));
  71. $this->repo->expects($this->once())
  72. ->method('getPackages')
  73. ->will($this->returnValue(array()));
  74. $this->vendorDir .= '/subdir';
  75. mkdir($this->vendorDir.'/.composer', 0777, true);
  76. $this->generator->dump($this->repo, $package, $this->im, $this->vendorDir.'/.composer');
  77. $this->assertAutoloadFiles('main2', $this->vendorDir.'/.composer');
  78. }
  79. public function testVendorsAutoloading()
  80. {
  81. $package = new MemoryPackage('a', '1.0', '1.0');
  82. $packages = array();
  83. $packages[] = $a = new MemoryPackage('a/a', '1.0', '1.0');
  84. $packages[] = $b = new MemoryPackage('b/b', '1.0', '1.0');
  85. $a->setAutoload(array('psr-0' => array('A' => 'src/', 'A\\B' => 'lib/')));
  86. $b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/')));
  87. $this->repo->expects($this->once())
  88. ->method('getPackages')
  89. ->will($this->returnValue($packages));
  90. mkdir($this->vendorDir.'/.composer', 0777, true);
  91. $this->generator->dump($this->repo, $package, $this->im, $this->vendorDir.'/.composer');
  92. $this->assertAutoloadFiles('vendors', $this->vendorDir.'/.composer');
  93. }
  94. private function assertAutoloadFiles($name, $dir)
  95. {
  96. $this->assertEquals(file_get_contents(__DIR__.'/Fixtures/autoload_'.$name.'.php'), file_get_contents($dir.'/autoload_namespaces.php'));
  97. }
  98. }