ClassMapGeneratorTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. use Composer\Util\Filesystem;
  14. class ClassMapGeneratorTest extends \PHPUnit_Framework_TestCase
  15. {
  16. /**
  17. * @dataProvider getTestCreateMapTests
  18. */
  19. public function testCreateMap($directory, $expected)
  20. {
  21. $this->assertEqualsNormalized($expected, ClassMapGenerator::createMap($directory));
  22. }
  23. public function getTestCreateMapTests()
  24. {
  25. $data = array(
  26. array(__DIR__.'/Fixtures/Namespaced', array(
  27. 'Namespaced\\Bar' => realpath(__DIR__).'/Fixtures/Namespaced/Bar.inc',
  28. 'Namespaced\\Foo' => realpath(__DIR__).'/Fixtures/Namespaced/Foo.php',
  29. 'Namespaced\\Baz' => realpath(__DIR__).'/Fixtures/Namespaced/Baz.php',
  30. )),
  31. array(__DIR__.'/Fixtures/beta/NamespaceCollision', array(
  32. 'NamespaceCollision\\A\\B\\Bar' => realpath(__DIR__).'/Fixtures/beta/NamespaceCollision/A/B/Bar.php',
  33. 'NamespaceCollision\\A\\B\\Foo' => realpath(__DIR__).'/Fixtures/beta/NamespaceCollision/A/B/Foo.php',
  34. )),
  35. array(__DIR__.'/Fixtures/Pearlike', array(
  36. 'Pearlike_Foo' => realpath(__DIR__).'/Fixtures/Pearlike/Foo.php',
  37. 'Pearlike_Bar' => realpath(__DIR__).'/Fixtures/Pearlike/Bar.php',
  38. 'Pearlike_Baz' => realpath(__DIR__).'/Fixtures/Pearlike/Baz.php',
  39. )),
  40. array(__DIR__.'/Fixtures/classmap', array(
  41. 'Foo\\Bar\\A' => realpath(__DIR__).'/Fixtures/classmap/sameNsMultipleClasses.php',
  42. 'Foo\\Bar\\B' => realpath(__DIR__).'/Fixtures/classmap/sameNsMultipleClasses.php',
  43. 'Alpha\\A' => realpath(__DIR__).'/Fixtures/classmap/multipleNs.php',
  44. 'Alpha\\B' => realpath(__DIR__).'/Fixtures/classmap/multipleNs.php',
  45. 'A' => realpath(__DIR__).'/Fixtures/classmap/multipleNs.php',
  46. 'Be\\ta\\A' => realpath(__DIR__).'/Fixtures/classmap/multipleNs.php',
  47. 'Be\\ta\\B' => realpath(__DIR__).'/Fixtures/classmap/multipleNs.php',
  48. 'ClassMap\\SomeInterface' => realpath(__DIR__).'/Fixtures/classmap/SomeInterface.php',
  49. 'ClassMap\\SomeParent' => realpath(__DIR__).'/Fixtures/classmap/SomeParent.php',
  50. 'ClassMap\\SomeClass' => realpath(__DIR__).'/Fixtures/classmap/SomeClass.php',
  51. 'ClassMap\\LongString' => realpath(__DIR__).'/Fixtures/classmap/LongString.php',
  52. 'Foo\\LargeClass' => realpath(__DIR__).'/Fixtures/classmap/LargeClass.php',
  53. 'Foo\\LargeGap' => realpath(__DIR__).'/Fixtures/classmap/LargeGap.php',
  54. 'Foo\\MissingSpace' => realpath(__DIR__).'/Fixtures/classmap/MissingSpace.php',
  55. 'Foo\\StripNoise' => realpath(__DIR__).'/Fixtures/classmap/StripNoise.php',
  56. 'Foo\\SlashedA' => realpath(__DIR__).'/Fixtures/classmap/BackslashLineEndingString.php',
  57. 'Foo\\SlashedB' => realpath(__DIR__).'/Fixtures/classmap/BackslashLineEndingString.php',
  58. 'Unicode\\↑\\↑' => realpath(__DIR__).'/Fixtures/classmap/Unicode.php',
  59. )),
  60. array(__DIR__.'/Fixtures/template', array()),
  61. );
  62. if (PHP_VERSION_ID >= 50400) {
  63. $data[] = array(__DIR__.'/Fixtures/php5.4', array(
  64. 'TFoo' => __DIR__.'/Fixtures/php5.4/traits.php',
  65. 'CFoo' => __DIR__.'/Fixtures/php5.4/traits.php',
  66. 'Foo\\TBar' => __DIR__.'/Fixtures/php5.4/traits.php',
  67. 'Foo\\IBar' => __DIR__.'/Fixtures/php5.4/traits.php',
  68. 'Foo\\TFooBar' => __DIR__.'/Fixtures/php5.4/traits.php',
  69. 'Foo\\CBar' => __DIR__.'/Fixtures/php5.4/traits.php',
  70. ));
  71. }
  72. if (defined('HHVM_VERSION') && version_compare(HHVM_VERSION, '3.3', '>=')) {
  73. $data[] = array(__DIR__.'/Fixtures/hhvm3.3', array(
  74. 'FooEnum' => __DIR__.'/Fixtures/hhvm3.3/HackEnum.php',
  75. 'Foo\BarEnum' => __DIR__.'/Fixtures/hhvm3.3/NamespacedHackEnum.php',
  76. 'GenericsClass' => __DIR__.'/Fixtures/hhvm3.3/Generics.php',
  77. ));
  78. }
  79. return $data;
  80. }
  81. public function testCreateMapFinderSupport()
  82. {
  83. $this->checkIfFinderIsAvailable();
  84. $finder = new Finder();
  85. $finder->files()->in(__DIR__ . '/Fixtures/beta/NamespaceCollision');
  86. $this->assertEqualsNormalized(array(
  87. 'NamespaceCollision\\A\\B\\Bar' => realpath(__DIR__).'/Fixtures/beta/NamespaceCollision/A/B/Bar.php',
  88. 'NamespaceCollision\\A\\B\\Foo' => realpath(__DIR__).'/Fixtures/beta/NamespaceCollision/A/B/Foo.php',
  89. ), ClassMapGenerator::createMap($finder));
  90. }
  91. /**
  92. * @expectedException \RuntimeException
  93. * @expectedExceptionMessage Could not scan for classes inside
  94. */
  95. public function testFindClassesThrowsWhenFileDoesNotExist()
  96. {
  97. $r = new \ReflectionClass('Composer\\Autoload\\ClassMapGenerator');
  98. $find = $r->getMethod('findClasses');
  99. $find->setAccessible(true);
  100. $find->invoke(null, __DIR__.'/no-file');
  101. }
  102. public function testAmbiguousReference()
  103. {
  104. $this->checkIfFinderIsAvailable();
  105. $tempDir = sys_get_temp_dir().'/ComposerTestAmbiguousRefs';
  106. if (!is_dir($tempDir.'/other')) {
  107. mkdir($tempDir.'/other', 0777, true);
  108. }
  109. $finder = new Finder();
  110. $finder->files()->in($tempDir);
  111. $io = $this->getMockBuilder('Composer\IO\ConsoleIO')
  112. ->disableOriginalConstructor()
  113. ->getMock();
  114. file_put_contents($tempDir.'/A.php', "<?php\nclass A {}");
  115. file_put_contents($tempDir.'/other/A.php', "<?php\nclass A {}");
  116. $a = realpath($tempDir.'/A.php');
  117. $b = realpath($tempDir.'/other/A.php');
  118. $msg = '';
  119. $io->expects($this->once())
  120. ->method('writeError')
  121. ->will($this->returnCallback(function ($text) use (&$msg) {
  122. $msg = $text;
  123. }));
  124. $messages = array(
  125. '<warning>Warning: Ambiguous class resolution, "A" was found in both "'.$a.'" and "'.$b.'", the first will be used.</warning>',
  126. '<warning>Warning: Ambiguous class resolution, "A" was found in both "'.$b.'" and "'.$a.'", the first will be used.</warning>',
  127. );
  128. ClassMapGenerator::createMap($finder, null, $io);
  129. $this->assertTrue(in_array($msg, $messages, true), $msg.' not found in expected messages ('.var_export($messages, true).')');
  130. $fs = new Filesystem();
  131. $fs->removeDirectory($tempDir);
  132. }
  133. /**
  134. * If one file has a class or interface defined more than once,
  135. * an ambiguous reference warning should not be produced
  136. */
  137. public function testUnambiguousReference()
  138. {
  139. $tempDir = sys_get_temp_dir().'/ComposerTestUnambiguousRefs';
  140. if (!is_dir($tempDir)) {
  141. mkdir($tempDir, 0777, true);
  142. }
  143. file_put_contents($tempDir.'/A.php', "<?php\nclass A {}");
  144. file_put_contents(
  145. $tempDir.'/B.php',
  146. "<?php
  147. if (true) {
  148. interface B {}
  149. } else {
  150. interface B extends Iterator {}
  151. }
  152. "
  153. );
  154. foreach (array('test', 'fixture', 'example') as $keyword) {
  155. if (!is_dir($tempDir.'/'.$keyword)) {
  156. mkdir($tempDir.'/'.$keyword, 0777, true);
  157. }
  158. file_put_contents($tempDir.'/'.$keyword.'/A.php', "<?php\nclass A {}");
  159. }
  160. $io = $this->getMockBuilder('Composer\IO\ConsoleIO')
  161. ->disableOriginalConstructor()
  162. ->getMock();
  163. $io->expects($this->never())
  164. ->method('write');
  165. ClassMapGenerator::createMap($tempDir, null, $io);
  166. $fs = new Filesystem();
  167. $fs->removeDirectory($tempDir);
  168. }
  169. /**
  170. * @expectedException \RuntimeException
  171. * @expectedExceptionMessage Could not scan for classes inside
  172. */
  173. public function testCreateMapThrowsWhenDirectoryDoesNotExist()
  174. {
  175. ClassMapGenerator::createMap(__DIR__.'/no-file.no-foler');
  176. }
  177. protected function assertEqualsNormalized($expected, $actual, $message = null)
  178. {
  179. foreach ($expected as $ns => $path) {
  180. $expected[$ns] = strtr($path, '\\', '/');
  181. }
  182. foreach ($actual as $ns => $path) {
  183. $actual[$ns] = strtr($path, '\\', '/');
  184. }
  185. $this->assertEquals($expected, $actual, $message);
  186. }
  187. private function checkIfFinderIsAvailable()
  188. {
  189. if (!class_exists('Symfony\\Component\\Finder\\Finder')) {
  190. $this->markTestSkipped('Finder component is not available');
  191. }
  192. }
  193. }