ClassMapGeneratorTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. class ClassMapGeneratorTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @dataProvider getTestCreateMapTests
  16. */
  17. public function testCreateMap($directory, $expected)
  18. {
  19. $this->assertEqualsNormalized($expected, ClassMapGenerator::createMap($directory));
  20. }
  21. public function getTestCreateMapTests()
  22. {
  23. $data = array(
  24. array(__DIR__.'/Fixtures/Namespaced', array(
  25. 'Namespaced\\Bar' => realpath(__DIR__).'/Fixtures/Namespaced/Bar.inc',
  26. 'Namespaced\\Foo' => realpath(__DIR__).'/Fixtures/Namespaced/Foo.php',
  27. 'Namespaced\\Baz' => realpath(__DIR__).'/Fixtures/Namespaced/Baz.php',
  28. )),
  29. array(__DIR__.'/Fixtures/beta/NamespaceCollision', array(
  30. 'NamespaceCollision\\A\\B\\Bar' => realpath(__DIR__).'/Fixtures/beta/NamespaceCollision/A/B/Bar.php',
  31. 'NamespaceCollision\\A\\B\\Foo' => realpath(__DIR__).'/Fixtures/beta/NamespaceCollision/A/B/Foo.php',
  32. )),
  33. array(__DIR__.'/Fixtures/Pearlike', array(
  34. 'Pearlike_Foo' => realpath(__DIR__).'/Fixtures/Pearlike/Foo.php',
  35. 'Pearlike_Bar' => realpath(__DIR__).'/Fixtures/Pearlike/Bar.php',
  36. 'Pearlike_Baz' => realpath(__DIR__).'/Fixtures/Pearlike/Baz.php',
  37. )),
  38. array(__DIR__.'/Fixtures/classmap', array(
  39. 'Foo\\Bar\\A' => realpath(__DIR__).'/Fixtures/classmap/sameNsMultipleClasses.php',
  40. 'Foo\\Bar\\B' => realpath(__DIR__).'/Fixtures/classmap/sameNsMultipleClasses.php',
  41. 'Alpha\\A' => realpath(__DIR__).'/Fixtures/classmap/multipleNs.php',
  42. 'Alpha\\B' => realpath(__DIR__).'/Fixtures/classmap/multipleNs.php',
  43. 'A' => realpath(__DIR__).'/Fixtures/classmap/multipleNs.php',
  44. 'Be\\ta\\A' => realpath(__DIR__).'/Fixtures/classmap/multipleNs.php',
  45. 'Be\\ta\\B' => realpath(__DIR__).'/Fixtures/classmap/multipleNs.php',
  46. 'ClassMap\\SomeInterface' => realpath(__DIR__).'/Fixtures/classmap/SomeInterface.php',
  47. 'ClassMap\\SomeParent' => realpath(__DIR__).'/Fixtures/classmap/SomeParent.php',
  48. 'ClassMap\\SomeClass' => realpath(__DIR__).'/Fixtures/classmap/SomeClass.php',
  49. 'Foo\\LargeClass' => realpath(__DIR__).'/Fixtures/classmap/LargeClass.php',
  50. 'Foo\\LargeGap' => realpath(__DIR__).'/Fixtures/classmap/LargeGap.php',
  51. 'Foo\\MissingSpace' => realpath(__DIR__).'/Fixtures/classmap/MissingSpace.php',
  52. 'Foo\\StripNoise' => realpath(__DIR__).'/Fixtures/classmap/StripNoise.php',
  53. 'Foo\\SlashedA' => realpath(__DIR__).'/Fixtures/classmap/BackslashLineEndingString.php',
  54. 'Foo\\SlashedB' => realpath(__DIR__).'/Fixtures/classmap/BackslashLineEndingString.php',
  55. 'Unicode\\↑\\↑' => realpath(__DIR__).'/Fixtures/classmap/Unicode.php',
  56. )),
  57. array(__DIR__.'/Fixtures/template', array()),
  58. );
  59. if (version_compare(PHP_VERSION, '5.4', '>=')) {
  60. $data[] = array(__DIR__.'/Fixtures/php5.4', array(
  61. 'TFoo' => __DIR__.'/Fixtures/php5.4/traits.php',
  62. 'CFoo' => __DIR__.'/Fixtures/php5.4/traits.php',
  63. 'Foo\\TBar' => __DIR__.'/Fixtures/php5.4/traits.php',
  64. 'Foo\\IBar' => __DIR__.'/Fixtures/php5.4/traits.php',
  65. 'Foo\\TFooBar' => __DIR__.'/Fixtures/php5.4/traits.php',
  66. 'Foo\\CBar' => __DIR__.'/Fixtures/php5.4/traits.php',
  67. ));
  68. }
  69. return $data;
  70. }
  71. public function testCreateMapFinderSupport()
  72. {
  73. if (!class_exists('Symfony\\Component\\Finder\\Finder')) {
  74. $this->markTestSkipped('Finder component is not available');
  75. }
  76. $finder = new \Symfony\Component\Finder\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. /**
  95. * @expectedException \RuntimeException
  96. * @expectedExceptionMessage Could not scan for classes inside
  97. */
  98. public function testCreateMapThrowsWhenDirectoryDoesNotExist()
  99. {
  100. ClassMapGenerator::createMap(__DIR__.'/no-file.no-foler');
  101. }
  102. protected function assertEqualsNormalized($expected, $actual, $message = null)
  103. {
  104. foreach ($expected as $ns => $path) {
  105. $expected[$ns] = strtr($path, '\\', '/');
  106. }
  107. foreach ($actual as $ns => $path) {
  108. $actual[$ns] = strtr($path, '\\', '/');
  109. }
  110. $this->assertEquals($expected, $actual, $message);
  111. }
  112. }