PageRenderTime 34ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/composer/composer/tests/Composer/Test/Autoload/ClassMapGeneratorTest.php

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