ClassMapGeneratorTest.php 8.6 KB

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