RequestContextTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Routing\RequestContext;
  14. class RequestContextTest extends TestCase
  15. {
  16. public function testConstruct()
  17. {
  18. $requestContext = new RequestContext(
  19. 'foo',
  20. 'post',
  21. 'foo.bar',
  22. 'HTTPS',
  23. 8080,
  24. 444,
  25. '/baz',
  26. 'bar=foobar'
  27. );
  28. $this->assertEquals('foo', $requestContext->getBaseUrl());
  29. $this->assertEquals('POST', $requestContext->getMethod());
  30. $this->assertEquals('foo.bar', $requestContext->getHost());
  31. $this->assertEquals('https', $requestContext->getScheme());
  32. $this->assertSame(8080, $requestContext->getHttpPort());
  33. $this->assertSame(444, $requestContext->getHttpsPort());
  34. $this->assertEquals('/baz', $requestContext->getPathInfo());
  35. $this->assertEquals('bar=foobar', $requestContext->getQueryString());
  36. }
  37. public function testFromRequest()
  38. {
  39. $request = Request::create('https://test.com:444/foo?bar=baz');
  40. $requestContext = new RequestContext();
  41. $requestContext->setHttpPort(123);
  42. $requestContext->fromRequest($request);
  43. $this->assertEquals('', $requestContext->getBaseUrl());
  44. $this->assertEquals('GET', $requestContext->getMethod());
  45. $this->assertEquals('test.com', $requestContext->getHost());
  46. $this->assertEquals('https', $requestContext->getScheme());
  47. $this->assertEquals('/foo', $requestContext->getPathInfo());
  48. $this->assertEquals('bar=baz', $requestContext->getQueryString());
  49. $this->assertSame(123, $requestContext->getHttpPort());
  50. $this->assertSame(444, $requestContext->getHttpsPort());
  51. $request = Request::create('http://test.com:8080/foo?bar=baz');
  52. $requestContext = new RequestContext();
  53. $requestContext->setHttpsPort(567);
  54. $requestContext->fromRequest($request);
  55. $this->assertSame(8080, $requestContext->getHttpPort());
  56. $this->assertSame(567, $requestContext->getHttpsPort());
  57. }
  58. public function testGetParameters()
  59. {
  60. $requestContext = new RequestContext();
  61. $this->assertEquals(array(), $requestContext->getParameters());
  62. $requestContext->setParameters(array('foo' => 'bar'));
  63. $this->assertEquals(array('foo' => 'bar'), $requestContext->getParameters());
  64. }
  65. public function testHasParameter()
  66. {
  67. $requestContext = new RequestContext();
  68. $requestContext->setParameters(array('foo' => 'bar'));
  69. $this->assertTrue($requestContext->hasParameter('foo'));
  70. $this->assertFalse($requestContext->hasParameter('baz'));
  71. }
  72. public function testGetParameter()
  73. {
  74. $requestContext = new RequestContext();
  75. $requestContext->setParameters(array('foo' => 'bar'));
  76. $this->assertEquals('bar', $requestContext->getParameter('foo'));
  77. $this->assertNull($requestContext->getParameter('baz'));
  78. }
  79. public function testSetParameter()
  80. {
  81. $requestContext = new RequestContext();
  82. $requestContext->setParameter('foo', 'bar');
  83. $this->assertEquals('bar', $requestContext->getParameter('foo'));
  84. }
  85. public function testMethod()
  86. {
  87. $requestContext = new RequestContext();
  88. $requestContext->setMethod('post');
  89. $this->assertSame('POST', $requestContext->getMethod());
  90. }
  91. public function testScheme()
  92. {
  93. $requestContext = new RequestContext();
  94. $requestContext->setScheme('HTTPS');
  95. $this->assertSame('https', $requestContext->getScheme());
  96. }
  97. public function testHost()
  98. {
  99. $requestContext = new RequestContext();
  100. $requestContext->setHost('eXampLe.com');
  101. $this->assertSame('example.com', $requestContext->getHost());
  102. }
  103. public function testQueryString()
  104. {
  105. $requestContext = new RequestContext();
  106. $requestContext->setQueryString(null);
  107. $this->assertSame('', $requestContext->getQueryString());
  108. }
  109. public function testPort()
  110. {
  111. $requestContext = new RequestContext();
  112. $requestContext->setHttpPort('123');
  113. $requestContext->setHttpsPort('456');
  114. $this->assertSame(123, $requestContext->getHttpPort());
  115. $this->assertSame(456, $requestContext->getHttpsPort());
  116. }
  117. public function testFluentInterface()
  118. {
  119. $requestContext = new RequestContext();
  120. $this->assertSame($requestContext, $requestContext->setBaseUrl('/app.php'));
  121. $this->assertSame($requestContext, $requestContext->setPathInfo('/index'));
  122. $this->assertSame($requestContext, $requestContext->setMethod('POST'));
  123. $this->assertSame($requestContext, $requestContext->setScheme('https'));
  124. $this->assertSame($requestContext, $requestContext->setHost('example.com'));
  125. $this->assertSame($requestContext, $requestContext->setQueryString('foo=bar'));
  126. $this->assertSame($requestContext, $requestContext->setHttpPort(80));
  127. $this->assertSame($requestContext, $requestContext->setHttpsPort(443));
  128. $this->assertSame($requestContext, $requestContext->setParameters(array()));
  129. $this->assertSame($requestContext, $requestContext->setParameter('foo', 'bar'));
  130. }
  131. }