PhpMatcherDumperTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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\Tests\Component\Routing;
  11. use Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper;
  12. use Symfony\Component\Routing\Route;
  13. use Symfony\Component\Routing\RouteCollection;
  14. use Symfony\Component\Routing\RequestContext;
  15. class PhpMatcherDumperTest extends \PHPUnit_Framework_TestCase
  16. {
  17. public function testDump()
  18. {
  19. $collection = new RouteCollection();
  20. // defaults and requirements
  21. $collection->add('foo', new Route(
  22. '/foo/{bar}',
  23. array('def' => 'test'),
  24. array('bar' => 'baz|symfony')
  25. ));
  26. // method requirement
  27. $collection->add('bar', new Route(
  28. '/bar/{foo}',
  29. array(),
  30. array('_method' => 'GET|head')
  31. ));
  32. // simple
  33. $collection->add('baz', new Route(
  34. '/test/baz'
  35. ));
  36. // simple with extension
  37. $collection->add('baz2', new Route(
  38. '/test/baz.html'
  39. ));
  40. // trailing slash
  41. $collection->add('baz3', new Route(
  42. '/test/baz3/'
  43. ));
  44. // trailing slash with variable
  45. $collection->add('baz4', new Route(
  46. '/test/{foo}/'
  47. ));
  48. // trailing slash and method
  49. $collection->add('baz5', new Route(
  50. '/test/{foo}/',
  51. array(),
  52. array('_method' => 'post')
  53. ));
  54. // complex name
  55. $collection->add('baz.baz6', new Route(
  56. '/test/{foo}/',
  57. array(),
  58. array('_method' => 'put')
  59. ));
  60. // defaults without variable
  61. $collection->add('foofoo', new Route(
  62. '/foofoo',
  63. array('def' => 'test')
  64. ));
  65. $dumper = new PhpMatcherDumper($collection, new RequestContext());
  66. $this->assertStringEqualsFile(__DIR__.'/../../Fixtures/dumper/url_matcher1.php', $dumper->dump(), '->dump() dumps basic routes to the correct PHP file.');
  67. // force HTTPS redirection
  68. $collection->add('secure', new Route(
  69. '/secure',
  70. array(),
  71. array('_scheme' => 'https')
  72. ));
  73. // force HTTP redirection
  74. $collection->add('nonsecure', new Route(
  75. '/nonsecure',
  76. array(),
  77. array('_scheme' => 'http')
  78. ));
  79. $this->assertStringEqualsFile(__DIR__.'/../../Fixtures/dumper/url_matcher2.php', $dumper->dump(array('base_class' => 'Symfony\Tests\Component\Routing\Fixtures\RedirectableUrlMatcher')), '->dump() dumps basic routes to the correct PHP file.');
  80. }
  81. /**
  82. * @expectedException \LogicException
  83. */
  84. public function testDumpWhenSchemeIsUsedWithoutAProperDumper()
  85. {
  86. $collection = new RouteCollection();
  87. $collection->add('secure', new Route(
  88. '/secure',
  89. array(),
  90. array('_scheme' => 'https')
  91. ));
  92. $dumper = new PhpMatcherDumper($collection, new RequestContext());
  93. $dumper->dump();
  94. }
  95. }