/src/Symfony/Component/Routing/Tests/Generator/Dumper/PhpGeneratorDumperTest.php

https://github.com/deviantintegral/symfony · PHP · 177 lines · 111 code · 40 blank · 26 comment · 1 complexity · 958ccbbff045f191fc80652986260c44 MD5 · raw file

  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\Generator\Dumper;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  13. use Symfony\Component\Routing\RouteCollection;
  14. use Symfony\Component\Routing\Route;
  15. use Symfony\Component\Routing\Generator\Dumper\PhpGeneratorDumper;
  16. use Symfony\Component\Routing\RequestContext;
  17. class PhpGeneratorDumperTest extends TestCase
  18. {
  19. /**
  20. * @var RouteCollection
  21. */
  22. private $routeCollection;
  23. /**
  24. * @var PhpGeneratorDumper
  25. */
  26. private $generatorDumper;
  27. /**
  28. * @var string
  29. */
  30. private $testTmpFilepath;
  31. /**
  32. * @var string
  33. */
  34. private $largeTestTmpFilepath;
  35. protected function setUp()
  36. {
  37. parent::setUp();
  38. $this->routeCollection = new RouteCollection();
  39. $this->generatorDumper = new PhpGeneratorDumper($this->routeCollection);
  40. $this->testTmpFilepath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'php_generator.'.$this->getName().'.php';
  41. $this->largeTestTmpFilepath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'php_generator.'.$this->getName().'.large.php';
  42. @unlink($this->testTmpFilepath);
  43. @unlink($this->largeTestTmpFilepath);
  44. }
  45. protected function tearDown()
  46. {
  47. parent::tearDown();
  48. @unlink($this->testTmpFilepath);
  49. $this->routeCollection = null;
  50. $this->generatorDumper = null;
  51. $this->testTmpFilepath = null;
  52. }
  53. public function testDumpWithRoutes()
  54. {
  55. $this->routeCollection->add('Test', new Route('/testing/{foo}'));
  56. $this->routeCollection->add('Test2', new Route('/testing2'));
  57. file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump());
  58. include $this->testTmpFilepath;
  59. $projectUrlGenerator = new \ProjectUrlGenerator(new RequestContext('/app.php'));
  60. $absoluteUrlWithParameter = $projectUrlGenerator->generate('Test', array('foo' => 'bar'), UrlGeneratorInterface::ABSOLUTE_URL);
  61. $absoluteUrlWithoutParameter = $projectUrlGenerator->generate('Test2', array(), UrlGeneratorInterface::ABSOLUTE_URL);
  62. $relativeUrlWithParameter = $projectUrlGenerator->generate('Test', array('foo' => 'bar'), UrlGeneratorInterface::ABSOLUTE_PATH);
  63. $relativeUrlWithoutParameter = $projectUrlGenerator->generate('Test2', array(), UrlGeneratorInterface::ABSOLUTE_PATH);
  64. $this->assertEquals('http://localhost/app.php/testing/bar', $absoluteUrlWithParameter);
  65. $this->assertEquals('http://localhost/app.php/testing2', $absoluteUrlWithoutParameter);
  66. $this->assertEquals('/app.php/testing/bar', $relativeUrlWithParameter);
  67. $this->assertEquals('/app.php/testing2', $relativeUrlWithoutParameter);
  68. }
  69. public function testDumpWithTooManyRoutes()
  70. {
  71. $this->routeCollection->add('Test', new Route('/testing/{foo}'));
  72. for ($i = 0; $i < 32769; ++$i) {
  73. $this->routeCollection->add('route_'.$i, new Route('/route_'.$i));
  74. }
  75. $this->routeCollection->add('Test2', new Route('/testing2'));
  76. file_put_contents($this->largeTestTmpFilepath, $this->generatorDumper->dump(array(
  77. 'class' => 'ProjectLargeUrlGenerator',
  78. )));
  79. $this->routeCollection = $this->generatorDumper = null;
  80. include $this->largeTestTmpFilepath;
  81. $projectUrlGenerator = new \ProjectLargeUrlGenerator(new RequestContext('/app.php'));
  82. $absoluteUrlWithParameter = $projectUrlGenerator->generate('Test', array('foo' => 'bar'), UrlGeneratorInterface::ABSOLUTE_URL);
  83. $absoluteUrlWithoutParameter = $projectUrlGenerator->generate('Test2', array(), UrlGeneratorInterface::ABSOLUTE_URL);
  84. $relativeUrlWithParameter = $projectUrlGenerator->generate('Test', array('foo' => 'bar'), UrlGeneratorInterface::ABSOLUTE_PATH);
  85. $relativeUrlWithoutParameter = $projectUrlGenerator->generate('Test2', array(), UrlGeneratorInterface::ABSOLUTE_PATH);
  86. $this->assertEquals('http://localhost/app.php/testing/bar', $absoluteUrlWithParameter);
  87. $this->assertEquals('http://localhost/app.php/testing2', $absoluteUrlWithoutParameter);
  88. $this->assertEquals('/app.php/testing/bar', $relativeUrlWithParameter);
  89. $this->assertEquals('/app.php/testing2', $relativeUrlWithoutParameter);
  90. }
  91. /**
  92. * @expectedException \InvalidArgumentException
  93. */
  94. public function testDumpWithoutRoutes()
  95. {
  96. file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump(array('class' => 'WithoutRoutesUrlGenerator')));
  97. include $this->testTmpFilepath;
  98. $projectUrlGenerator = new \WithoutRoutesUrlGenerator(new RequestContext('/app.php'));
  99. $projectUrlGenerator->generate('Test', array());
  100. }
  101. /**
  102. * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
  103. */
  104. public function testGenerateNonExistingRoute()
  105. {
  106. $this->routeCollection->add('Test', new Route('/test'));
  107. file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump(array('class' => 'NonExistingRoutesUrlGenerator')));
  108. include $this->testTmpFilepath;
  109. $projectUrlGenerator = new \NonExistingRoutesUrlGenerator(new RequestContext());
  110. $url = $projectUrlGenerator->generate('NonExisting', array());
  111. }
  112. public function testDumpForRouteWithDefaults()
  113. {
  114. $this->routeCollection->add('Test', new Route('/testing/{foo}', array('foo' => 'bar')));
  115. file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump(array('class' => 'DefaultRoutesUrlGenerator')));
  116. include $this->testTmpFilepath;
  117. $projectUrlGenerator = new \DefaultRoutesUrlGenerator(new RequestContext());
  118. $url = $projectUrlGenerator->generate('Test', array());
  119. $this->assertEquals('/testing', $url);
  120. }
  121. public function testDumpWithSchemeRequirement()
  122. {
  123. $this->routeCollection->add('Test1', new Route('/testing', array(), array(), array(), '', array('ftp', 'https')));
  124. file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump(array('class' => 'SchemeUrlGenerator')));
  125. include $this->testTmpFilepath;
  126. $projectUrlGenerator = new \SchemeUrlGenerator(new RequestContext('/app.php'));
  127. $absoluteUrl = $projectUrlGenerator->generate('Test1', array(), UrlGeneratorInterface::ABSOLUTE_URL);
  128. $relativeUrl = $projectUrlGenerator->generate('Test1', array(), UrlGeneratorInterface::ABSOLUTE_PATH);
  129. $this->assertEquals('ftp://localhost/app.php/testing', $absoluteUrl);
  130. $this->assertEquals('ftp://localhost/app.php/testing', $relativeUrl);
  131. $projectUrlGenerator = new \SchemeUrlGenerator(new RequestContext('/app.php', 'GET', 'localhost', 'https'));
  132. $absoluteUrl = $projectUrlGenerator->generate('Test1', array(), UrlGeneratorInterface::ABSOLUTE_URL);
  133. $relativeUrl = $projectUrlGenerator->generate('Test1', array(), UrlGeneratorInterface::ABSOLUTE_PATH);
  134. $this->assertEquals('https://localhost/app.php/testing', $absoluteUrl);
  135. $this->assertEquals('/app.php/testing', $relativeUrl);
  136. }
  137. }