sfTesterRequest.class.php 3.6 KB

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