ClassMapGeneratorTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /*
  3. * This file was copied from the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Composer\Test\Autoload;
  11. use Composer\Autoload\ClassMapGenerator;
  12. use Symfony\Component\Finder\Finder;
  13. class ClassMapGeneratorTest extends \PHPUnit_Framework_TestCase
  14. {
  15. /**
  16. * @dataProvider getTestCreateMapTests
  17. */
  18. public function testCreateMap($directory, $expected)
  19. {
  20. $this->assertEqualsNormalized($expected, ClassMapGenerator::createMap($directory));
  21. }
  22. public function getTestCreateMapTests()
  23. {
  24. $data = array(
  25. array(__DIR__.'/Fixtures/Namespaced', array(
  26. 'Namespaced\\Bar' => realpath(__DIR__).'/Fixtures/Namespaced/Bar.inc',
  27. 'Namespaced\\Foo' => realpath(__DIR__).'/Fixtures/Namespaced/Foo.php',
  28. 'Namespaced\\Baz' => realpath(__DIR__).'/Fixtures/Namespaced/Baz.php',
  29. )),
  30. array(__DIR__.'/Fixtures/beta/NamespaceCollision', array(
  31. 'NamespaceCollision\\A\\B\\Bar' => realpath(__DIR__).'/Fixtures/beta/NamespaceCollision/A/B/Bar.php',
  32. 'NamespaceCollision\\A\\B\\Foo' => realpath(__DIR__).'/Fixtures/beta/NamespaceCollision/A/B/Foo.php',
  33. )),
  34. array(__DIR__.'/Fixtures/Pearlike', array(
  35. 'Pearlike_Foo' => realpath(__DIR__).'/Fixtures/Pearlike/Foo.php',
  36. 'Pearlike_Bar' => realpath(__DIR__).'/Fixtures/Pearlike/Bar.php',
  37. 'Pearlike_Baz' => realpath(__DIR__).'/Fixtures/Pearlike/Baz.php',
  38. )),
  39. array(__DIR__.'/Fixtures/classmap', array(
  40. 'Foo\\Bar\\A' => realpath(__DIR__).'/Fixtures/classmap/sameNsMultipleClasses.php',
  41. 'Foo\\Bar\\B' => realpath(__DIR__).'/Fixtures/classmap/sameNsMultipleClasses.php',
  42. 'Alpha\\A' => realpath(__DIR__).'/Fixtures/classmap/multipleNs.php',
  43. 'Alpha\\B' => realpath(__DIR__).'/Fixtures/classmap/multipleNs.php',
  44. 'A' => realpath(__DIR__).'/Fixtures/classmap/multipleNs.php',
  45. 'Be\\ta\\A' => realpath(__DIR__).'/Fixtures/classmap/multipleNs.php',
  46. 'Be\\ta\\B' => realpath(__DIR__).'/Fixtures/classmap/multipleNs.php',
  47. 'ClassMap\\SomeInterface' => realpath(__DIR__).'/Fixtures/classmap/SomeInterface.php',
  48. 'ClassMap\\SomeParent' => realpath(__DIR__).'/Fixtures/classmap/SomeParent.php',
  49. 'ClassMap\\SomeClass' => realpath(__DIR__).'/Fixtures/classmap/SomeClass.php',
  50. 'Foo\\LargeClass' => realpath(__DIR__).'/Fixtures/classmap/LargeClass.php',
  51. 'Foo\\LargeGap' => realpath(__DIR__).'/Fixtures/classmap/LargeGap.php',
  52. 'Foo\\MissingSpace' => realpath(__DIR__).'/Fixtures/classmap/MissingSpace.php',
  53. 'Foo\\StripNoise' => realpath(__DIR__).'/Fixtures/classmap/StripNoise.php',
  54. 'Foo\\SlashedA' => realpath(__DIR__).'/Fixtures/classmap/BackslashLineEndingString.php',
  55. 'Foo\\SlashedB' => realpath(__DIR__).'/Fixtures/classmap/BackslashLineEndingString.php',
  56. 'Unicode\\↑\\↑' => realpath(__DIR__).'/Fixtures/classmap/Unicode.php',
  57. )),
  58. array(__DIR__.'/Fixtures/template', array()),
  59. );
  60. if (version_compare(PHP_VERSION, '5.4', '>=')) {
  61. $data[] = array(__DIR__.'/Fixtures/php5.4', array(
  62. 'TFoo' => __DIR__.'/Fixtures/php5.4/traits.php',
  63. 'CFoo' => __DIR__.'/Fixtures/php5.4/traits.php',
  64. 'Foo\\TBar' => __DIR__.'/Fixtures/php5.4/traits.php',
  65. 'Foo\\IBar' => __DIR__.'/Fixtures/php5.4/traits.php',
  66. 'Foo\\TFooBar' => __DIR__.'/Fixtures/php5.4/traits.php',
  67. 'Foo\\CBar' => __DIR__.'/Fixtures/php5.4/traits.php',
  68. ));
  69. }
  70. return $data;
  71. }
  72. public function testCreateMapFinderSupport()
  73. {
  74. $this->checkIfFinderIsAvailable();
  75. $finder = new Finder();
  76. $finder->files()->in(__DIR__ . '/Fixtures/beta/NamespaceCollision');
  77. $this->assertEqualsNormalized(array(
  78. 'NamespaceCollision\\A\\B\\Bar' => realpath(__DIR__).'/Fixtures/beta/NamespaceCollision/A/B/Bar.php',
  79. 'NamespaceCollision\\A\\B\\Foo' => realpath(__DIR__).'/Fixtures/beta/NamespaceCollision/A/B/Foo.php',
  80. ), ClassMapGenerator::createMap($finder));
  81. }
  82. /**
  83. * @expectedException \RuntimeException
  84. * @expectedExceptionMessage Could not scan for classes inside
  85. */
  86. public function testFindClassesThrowsWhenFileDoesNotExist()
  87. {
  88. $r = new \ReflectionClass('Composer\\Autoload\\ClassMapGenerator');
  89. $find = $r->getMethod('findClasses');
  90. $find->setAccessible(true);
  91. $find->invoke(null, __DIR__.'/no-file');
  92. }
  93. public function testAmbiguousReference()
  94. {
  95. $this->checkIfFinderIsAvailable();
  96. $finder = new Finder();
  97. $finder->files()->in(__DIR__ . '/Fixtures/Ambiguous');
  98. $io = $this->getMockBuilder('Composer\IO\ConsoleIO')
  99. ->disableOriginalConstructor()
  100. ->getMock();
  101. $a = realpath(__DIR__.'/Fixtures/Ambiguous/A.php');
  102. $b = realpath(__DIR__.'/Fixtures/Ambiguous/other/A.php');
  103. $msg = '';
  104. $io->expects($this->once())
  105. ->method('write')
  106. ->will($this->returnCallback(function ($text) use (&$msg) {
  107. $msg = $text;
  108. }));
  109. $messages = array(
  110. '<warning>Warning: Ambiguous class resolution, "A" was found in both "'.$a.'" and "'.$b.'", the first will be used.</warning>',
  111. '<warning>Warning: Ambiguous class resolution, "A" was found in both "'.$b.'" and "'.$a.'", the first will be used.</warning>',
  112. );
  113. ClassMapGenerator::createMap($finder, null, $io);
  114. $this->assertTrue(in_array($msg, $messages, true), $msg.' not found in expected messages ('.var_export($messages, true).')');
  115. }
  116. /**
  117. * If one file has a class or interface defined more than once,
  118. * an ambiguous reference warning should not be produced
  119. */
  120. public function testUnambiguousReference()
  121. {
  122. $this->checkIfFinderIsAvailable();
  123. $finder = new Finder();
  124. $finder->files()->in(__DIR__ . '/Fixtures/Unambiguous');
  125. $io = $this->getMockBuilder('Composer\IO\ConsoleIO')
  126. ->disableOriginalConstructor()
  127. ->getMock();
  128. $io->expects($this->never())
  129. ->method('write');
  130. ClassMapGenerator::createMap($finder, null, $io);
  131. }
  132. /**
  133. * @expectedException \RuntimeException
  134. * @expectedExceptionMessage Could not scan for classes inside
  135. */
  136. public function testCreateMapThrowsWhenDirectoryDoesNotExist()
  137. {
  138. ClassMapGenerator::createMap(__DIR__.'/no-file.no-foler');
  139. }
  140. protected function assertEqualsNormalized($expected, $actual, $message = null)
  141. {
  142. foreach ($expected as $ns => $path) {
  143. $expected[$ns] = strtr($path, '\\', '/');
  144. }
  145. foreach ($actual as $ns => $path) {
  146. $actual[$ns] = strtr($path, '\\', '/');
  147. }
  148. $this->assertEquals($expected, $actual, $message);
  149. }
  150. private function checkIfFinderIsAvailable()
  151. {
  152. if (!class_exists('Symfony\\Component\\Finder\\Finder')) {
  153. $this->markTestSkipped('Finder component is not available');
  154. }
  155. }
  156. }