ClassMapGeneratorTest.php 9.4 KB

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