1. sfTesterRequest.class.php
  2. /** * sfTesterRequest implements tests for the symfony request object. * * @package symfony * @subpackage test * @author Fabien Potencier * @version SVN: $Id: sfTesterRequest.class.php 14497 2009-01-06 17:30:36Z fabien $ */
  3. class sfTesterRequest extends sfTester
  4. {
  5. protected $request;
  6. /**
  7. * Prepares the tester.
  8. */
  9. public function prepare()
  10. {
  11. }
  12. /**
  13. * Initializes the tester.
  14. */
  15. public function initialize()
  16. {
  17. $this->request = $this->browser->getRequest();
  18. }
  19. /**
  20. * Tests whether or not a given key and value exists in the request.
  21. *
  22. * @param string $key
  23. * @param string $value
  24. *
  25. * @return sfTestFunctionalBase|sfTester
  26. */
  27. public function isParameter($key, $value)
  28. {
  29. $this->tester->is($this->request->getParameter($key), $value, sprintf('request parameter "%s" is "%s"', $key, $value));
  30. return $this->getObjectToReturn();
  31. }
  32. /**
  33. * Tests for the request is in the given format.
  34. *
  35. * @param string $format The request format
  36. *
  37. * @return sfTestFunctionalBase|sfTester
  38. */
  39. public function isFormat($format)
  40. {
  41. $this->tester->is($this->request->getRequestFormat(), $format, sprintf('request format is "%s"', $format));
  42. return $this->getObjectToReturn();
  43. }
  44. /**
  45. * Tests if the current HTTP method matches the given one
  46. *
  47. * @param string $method The HTTP method name
  48. *
  49. * @return sfTestFunctionalBase|sfTester
  50. */
  51. public function isMethod($method)
  52. {
  53. $this->tester->ok($this->request->isMethod($method), sprintf('request method is "%s"', strtoupper($method)));
  54. return $this->getObjectToReturn();
  55. }
  56. /**
  57. * Checks if a cookie exists.
  58. *
  59. * @param string $name The cookie name
  60. * @param Boolean $exists Whether the cookie must exist or not
  61. *
  62. * @return sfTestFunctionalBase|sfTester
  63. */
  64. public function hasCookie($name, $exists = true)
  65. {
  66. if (!array_key_exists($name, $_COOKIE))
  67. {
  68. if ($exists)
  69. {
  70. $this->tester->fail(sprintf('cookie "%s" exist.', $name));
  71. }
  72. else
  73. {
  74. $this->tester->pass(sprintf('cookie "%s" does not exist.', $name));
  75. }
  76. return $this->getObjectToReturn();
  77. }
  78. if ($exists)
  79. {
  80. $this->tester->pass(sprintf('cookie "%s" exists.', $name));
  81. }
  82. else
  83. {
  84. $this->tester->fail(sprintf('cookie "%s" does not exist.', $name));
  85. }
  86. return $this->getObjectToReturn();
  87. }
  88. /**
  89. * Checks the value of a cookie.
  90. *
  91. * @param string $name The cookie name
  92. * @param mixed $value The expected value
  93. *
  94. * @return sfTestFunctionalBase|sfTester
  95. */
  96. public function isCookie($name, $value)
  97. {
  98. if (!array_key_exists($name, $_COOKIE))
  99. {
  100. $this->tester->fail(sprintf('cookie "%s" does not exist.', $name));
  101. return $this->getObjectToReturn();
  102. }
  103. if (preg_match('/^(!)?([^a-zA-Z0-9\\\\]).+?\\2[ims]?$/', $value, $match))
  104. {
  105. if ($match[1] == '!')
  106. {
  107. $this->tester->unlike($_COOKIE[$name], substr($value, 1), sprintf('cookie "%s" content does not match regex "%s"', $name, $value));
  108. }
  109. else
  110. {
  111. $this->tester->like($_COOKIE[$name], $value, sprintf('cookie "%s" content matches regex "%s"', $name, $value));
  112. }
  113. }
  114. else
  115. {
  116. $this->tester->is($_COOKIE[$name], $value, sprintf('cookie "%s" content is ok', $name));
  117. }
  118. return $this->getObjectToReturn();
  119. }
  120. }

Debug toolbar