ClassMapGeneratorTest.php 9.2 KB

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