ClassMapGeneratorTest.php 11 KB

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