ClassMapGeneratorTest.php 8.9 KB

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