DumperPrefixCollectionTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /*
  3. * This file is part of 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 Symfony\Component\Routing\Tests\Matcher\Dumper;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Routing\Matcher\Dumper\DumperCollection;
  13. use Symfony\Component\Routing\Matcher\Dumper\DumperPrefixCollection;
  14. use Symfony\Component\Routing\Matcher\Dumper\DumperRoute;
  15. use Symfony\Component\Routing\Route;
  16. class DumperPrefixCollectionTest extends TestCase
  17. {
  18. public function testAddPrefixRoute()
  19. {
  20. $coll = new DumperPrefixCollection();
  21. $coll->setPrefix('');
  22. $route = new DumperRoute('bar', new Route('/foo/bar'));
  23. $coll = $coll->addPrefixRoute($route);
  24. $route = new DumperRoute('bar2', new Route('/foo/bar'));
  25. $coll = $coll->addPrefixRoute($route);
  26. $route = new DumperRoute('qux', new Route('/foo/qux'));
  27. $coll = $coll->addPrefixRoute($route);
  28. $route = new DumperRoute('bar3', new Route('/foo/bar'));
  29. $coll = $coll->addPrefixRoute($route);
  30. $route = new DumperRoute('bar4', new Route(''));
  31. $result = $coll->addPrefixRoute($route);
  32. $expect = <<<'EOF'
  33. |-coll /
  34. | |-coll /f
  35. | | |-coll /fo
  36. | | | |-coll /foo
  37. | | | | |-coll /foo/
  38. | | | | | |-coll /foo/b
  39. | | | | | | |-coll /foo/ba
  40. | | | | | | | |-coll /foo/bar
  41. | | | | | | | | |-route bar /foo/bar
  42. | | | | | | | | |-route bar2 /foo/bar
  43. | | | | | |-coll /foo/q
  44. | | | | | | |-coll /foo/qu
  45. | | | | | | | |-coll /foo/qux
  46. | | | | | | | | |-route qux /foo/qux
  47. | | | | | |-coll /foo/b
  48. | | | | | | |-coll /foo/ba
  49. | | | | | | | |-coll /foo/bar
  50. | | | | | | | | |-route bar3 /foo/bar
  51. | |-route bar4 /
  52. EOF;
  53. $this->assertSame($expect, $this->collectionToString($result->getRoot(), ' '));
  54. }
  55. public function testMergeSlashNodes()
  56. {
  57. $coll = new DumperPrefixCollection();
  58. $coll->setPrefix('');
  59. $route = new DumperRoute('bar', new Route('/foo/bar'));
  60. $coll = $coll->addPrefixRoute($route);
  61. $route = new DumperRoute('bar2', new Route('/foo/bar'));
  62. $coll = $coll->addPrefixRoute($route);
  63. $route = new DumperRoute('qux', new Route('/foo/qux'));
  64. $coll = $coll->addPrefixRoute($route);
  65. $route = new DumperRoute('bar3', new Route('/foo/bar'));
  66. $result = $coll->addPrefixRoute($route);
  67. $result->getRoot()->mergeSlashNodes();
  68. $expect = <<<'EOF'
  69. |-coll /f
  70. | |-coll /fo
  71. | | |-coll /foo
  72. | | | |-coll /foo/b
  73. | | | | |-coll /foo/ba
  74. | | | | | |-coll /foo/bar
  75. | | | | | | |-route bar /foo/bar
  76. | | | | | | |-route bar2 /foo/bar
  77. | | | |-coll /foo/q
  78. | | | | |-coll /foo/qu
  79. | | | | | |-coll /foo/qux
  80. | | | | | | |-route qux /foo/qux
  81. | | | |-coll /foo/b
  82. | | | | |-coll /foo/ba
  83. | | | | | |-coll /foo/bar
  84. | | | | | | |-route bar3 /foo/bar
  85. EOF;
  86. $this->assertSame($expect, $this->collectionToString($result->getRoot(), ' '));
  87. }
  88. private function collectionToString(DumperCollection $collection, $prefix)
  89. {
  90. $string = '';
  91. foreach ($collection as $route) {
  92. if ($route instanceof DumperCollection) {
  93. $string .= sprintf("%s|-coll %s\n", $prefix, $route->getPrefix());
  94. $string .= $this->collectionToString($route, $prefix.'| ');
  95. } else {
  96. $string .= sprintf("%s|-route %s %s\n", $prefix, $route->getName(), $route->getRoute()->getPath());
  97. }
  98. }
  99. return $string;
  100. }
  101. }