PhpMatcherDumperTest.php 3.1 KB

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