RedirectResponseTest.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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\HttpFoundation\Tests;
  11. use \Symfony\Component\HttpFoundation\RedirectResponse;
  12. class RedirectResponseTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testGenerateMetaRedirect()
  15. {
  16. $response = new RedirectResponse('foo.bar');
  17. $this->assertEquals(1, preg_match(
  18. '#<meta http-equiv="refresh" content="\d+;url=foo\.bar" />#',
  19. preg_replace(array('/\s+/', '/\'/'), array(' ', '"'), $response->getContent())
  20. ));
  21. }
  22. /**
  23. * @expectedException \InvalidArgumentException
  24. */
  25. public function testRedirectResponseConstructorNullUrl()
  26. {
  27. $response = new RedirectResponse(null);
  28. }
  29. /**
  30. * @expectedException \InvalidArgumentException
  31. */
  32. public function testRedirectResponseConstructorWrongStatusCode()
  33. {
  34. $response = new RedirectResponse('foo.bar', 404);
  35. }
  36. public function testGenerateLocationHeader()
  37. {
  38. $response = new RedirectResponse('foo.bar');
  39. $this->assertTrue($response->headers->has('Location'));
  40. $this->assertEquals('foo.bar', $response->headers->get('Location'));
  41. }
  42. public function testGetTargetUrl()
  43. {
  44. $response = new RedirectResponse('foo.bar');
  45. $this->assertEquals('foo.bar', $response->getTargetUrl());
  46. }
  47. public function testSetTargetUrl()
  48. {
  49. $response = new RedirectResponse('foo.bar');
  50. $response->setTargetUrl('baz.beep');
  51. $this->assertEquals('baz.beep', $response->getTargetUrl());
  52. }
  53. /**
  54. * @expectedException \InvalidArgumentException
  55. */
  56. public function testSetTargetUrlNull()
  57. {
  58. $response = new RedirectResponse('foo.bar');
  59. $response->setTargetUrl(null);
  60. }
  61. public function testCreate()
  62. {
  63. $response = RedirectResponse::create('foo', 301);
  64. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  65. $this->assertEquals(301, $response->getStatusCode());
  66. }
  67. }