PhpGeneratorDumperTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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\Dumper\PhpGeneratorDumper;
  13. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  14. use Symfony\Component\Routing\RequestContext;
  15. use Symfony\Component\Routing\Route;
  16. use Symfony\Component\Routing\RouteCollection;
  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. if (\defined('HHVM_VERSION_ID')) {
  72. $this->markTestSkipped('HHVM consumes too much memory on this test.');
  73. }
  74. $this->routeCollection->add('Test', new Route('/testing/{foo}'));
  75. for ($i = 0; $i < 32769; ++$i) {
  76. $this->routeCollection->add('route_'.$i, new Route('/route_'.$i));
  77. }
  78. $this->routeCollection->add('Test2', new Route('/testing2'));
  79. file_put_contents($this->largeTestTmpFilepath, $this->generatorDumper->dump(array(
  80. 'class' => 'ProjectLargeUrlGenerator',
  81. )));
  82. $this->routeCollection = $this->generatorDumper = null;
  83. include $this->largeTestTmpFilepath;
  84. $projectUrlGenerator = new \ProjectLargeUrlGenerator(new RequestContext('/app.php'));
  85. $absoluteUrlWithParameter = $projectUrlGenerator->generate('Test', array('foo' => 'bar'), UrlGeneratorInterface::ABSOLUTE_URL);
  86. $absoluteUrlWithoutParameter = $projectUrlGenerator->generate('Test2', array(), UrlGeneratorInterface::ABSOLUTE_URL);
  87. $relativeUrlWithParameter = $projectUrlGenerator->generate('Test', array('foo' => 'bar'), UrlGeneratorInterface::ABSOLUTE_PATH);
  88. $relativeUrlWithoutParameter = $projectUrlGenerator->generate('Test2', array(), UrlGeneratorInterface::ABSOLUTE_PATH);
  89. $this->assertEquals('http://localhost/app.php/testing/bar', $absoluteUrlWithParameter);
  90. $this->assertEquals('http://localhost/app.php/testing2', $absoluteUrlWithoutParameter);
  91. $this->assertEquals('/app.php/testing/bar', $relativeUrlWithParameter);
  92. $this->assertEquals('/app.php/testing2', $relativeUrlWithoutParameter);
  93. }
  94. /**
  95. * @expectedException \InvalidArgumentException
  96. */
  97. public function testDumpWithoutRoutes()
  98. {
  99. file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump(array('class' => 'WithoutRoutesUrlGenerator')));
  100. include $this->testTmpFilepath;
  101. $projectUrlGenerator = new \WithoutRoutesUrlGenerator(new RequestContext('/app.php'));
  102. $projectUrlGenerator->generate('Test', array());
  103. }
  104. /**
  105. * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
  106. */
  107. public function testGenerateNonExistingRoute()
  108. {
  109. $this->routeCollection->add('Test', new Route('/test'));
  110. file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump(array('class' => 'NonExistingRoutesUrlGenerator')));
  111. include $this->testTmpFilepath;
  112. $projectUrlGenerator = new \NonExistingRoutesUrlGenerator(new RequestContext());
  113. $url = $projectUrlGenerator->generate('NonExisting', array());
  114. }
  115. public function testDumpForRouteWithDefaults()
  116. {
  117. $this->routeCollection->add('Test', new Route('/testing/{foo}', array('foo' => 'bar')));
  118. file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump(array('class' => 'DefaultRoutesUrlGenerator')));
  119. include $this->testTmpFilepath;
  120. $projectUrlGenerator = new \DefaultRoutesUrlGenerator(new RequestContext());
  121. $url = $projectUrlGenerator->generate('Test', array());
  122. $this->assertEquals('/testing', $url);
  123. }
  124. public function testDumpWithSchemeRequirement()
  125. {
  126. $this->routeCollection->add('Test1', new Route('/testing', array(), array(), array(), '', array('ftp', 'https')));
  127. file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump(array('class' => 'SchemeUrlGenerator')));
  128. include $this->testTmpFilepath;
  129. $projectUrlGenerator = new \SchemeUrlGenerator(new RequestContext('/app.php'));
  130. $absoluteUrl = $projectUrlGenerator->generate('Test1', array(), UrlGeneratorInterface::ABSOLUTE_URL);
  131. $relativeUrl = $projectUrlGenerator->generate('Test1', array(), UrlGeneratorInterface::ABSOLUTE_PATH);
  132. $this->assertEquals('ftp://localhost/app.php/testing', $absoluteUrl);
  133. $this->assertEquals('ftp://localhost/app.php/testing', $relativeUrl);
  134. $projectUrlGenerator = new \SchemeUrlGenerator(new RequestContext('/app.php', 'GET', 'localhost', 'https'));
  135. $absoluteUrl = $projectUrlGenerator->generate('Test1', array(), UrlGeneratorInterface::ABSOLUTE_URL);
  136. $relativeUrl = $projectUrlGenerator->generate('Test1', array(), UrlGeneratorInterface::ABSOLUTE_PATH);
  137. $this->assertEquals('https://localhost/app.php/testing', $absoluteUrl);
  138. $this->assertEquals('/app.php/testing', $relativeUrl);
  139. }
  140. }