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