ClassMapGeneratorTest.php 10 KB

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