AutoloadGeneratorTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 testMainPackageAutoloadingAlternativeVendorDir()
  66. {
  67. $package = new MemoryPackage('a', '1.0', '1.0');
  68. $package->setAutoload(array('psr-0' => array('Main' => 'src/', 'Lala' => 'src/')));
  69. $this->repo->expects($this->once())
  70. ->method('getPackages')
  71. ->will($this->returnValue(array()));
  72. $this->vendorDir .= '/subdir';
  73. mkdir($this->vendorDir.'/.composer', 0777, true);
  74. $this->generator->dump($this->repo, $package, $this->im, $this->vendorDir.'/.composer');
  75. $this->assertAutoloadFiles('main2', $this->vendorDir.'/.composer');
  76. }
  77. public function testVendorsAutoloading()
  78. {
  79. $package = new MemoryPackage('a', '1.0', '1.0');
  80. $packages = array();
  81. $packages[] = $a = new MemoryPackage('a/a', '1.0', '1.0');
  82. $packages[] = $b = new MemoryPackage('b/b', '1.0', '1.0');
  83. $a->setAutoload(array('psr-0' => array('A' => 'src/', 'A\\B' => 'lib/')));
  84. $b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/')));
  85. $this->repo->expects($this->once())
  86. ->method('getPackages')
  87. ->will($this->returnValue($packages));
  88. mkdir($this->vendorDir.'/.composer', 0777, true);
  89. $this->generator->dump($this->repo, $package, $this->im, $this->vendorDir.'/.composer');
  90. $this->assertAutoloadFiles('vendors', $this->vendorDir.'/.composer');
  91. }
  92. private function assertAutoloadFiles($name, $dir)
  93. {
  94. $this->assertFileEquals(__DIR__.'/Fixtures/autoload_'.$name.'.php', $dir.'/autoload_namespaces.php');
  95. }
  96. }