ClassMapGeneratorTest.php 9.2 KB

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