sfTesterForm.class.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. * sfTesterForm implements tests for forms submitted by the user.
  11. *
  12. * @package symfony
  13. * @subpackage test
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfTesterForm.class.php 13481 2008-11-29 14:35:33Z dwhittle $
  16. */
  17. class sfTesterForm extends sfTester
  18. {
  19. protected
  20. $form = null;
  21. /**
  22. * Constructor.
  23. *
  24. * @param sfTestFunctionalBase $browser A browser
  25. * @param lime_test $tester A tester object
  26. */
  27. public function __construct(sfTestFunctionalBase $browser, $tester)
  28. {
  29. parent::__construct($browser, $tester);
  30. $this->browser->addListener('template.filter_parameters', array($this, 'filterTemplateParameters'));
  31. }
  32. /**
  33. * Prepares the tester.
  34. */
  35. public function prepare()
  36. {
  37. $this->form = null;
  38. }
  39. /**
  40. * Initiliazes the tester.
  41. */
  42. public function initialize()
  43. {
  44. if (is_null($this->form))
  45. {
  46. $action = $this->browser->getContext()->getActionStack()->getLastEntry()->getActionInstance();
  47. foreach ($action->getVarHolder()->getAll() as $name => $value)
  48. {
  49. if ($value instanceof sfForm && $value->isBound())
  50. {
  51. $this->form = $value;
  52. break;
  53. }
  54. }
  55. }
  56. }
  57. /**
  58. * Returns the current form.
  59. *
  60. * @return sfForm The current sfForm form instance
  61. */
  62. public function getForm()
  63. {
  64. return $this->form;
  65. }
  66. /**
  67. * Tests if the submitted form has some error.
  68. *
  69. * @param Boolean|integer $value Whether to check if the form has error or not, or the number of errors
  70. *
  71. * @return sfTestFunctionalBase|sfTester
  72. */
  73. public function hasErrors($value = true)
  74. {
  75. if (is_null($this->form))
  76. {
  77. throw new LogicException('no form has been submitted.');
  78. }
  79. if (is_int($value))
  80. {
  81. $this->tester->is(count($this->form->getErrorSchema()), $value, sprintf('the submitted form has "%s" errors.', $value));
  82. }
  83. else
  84. {
  85. $this->tester->is($this->form->hasErrors(), $value, sprintf('the submitted form %s.', ($value) ? 'has some errors' : 'is valid'));
  86. }
  87. return $this->getObjectToReturn();
  88. }
  89. /**
  90. * Tests if the submitted form has a specific error.
  91. *
  92. * @param string $field The field name to check for an error (null for global errors)
  93. * @param mixed $message The error message or the number of errors for the field (optional)
  94. *
  95. * @return sfTestFunctionalBase|sfTester
  96. */
  97. public function hasGlobalError($value = true)
  98. {
  99. return $this->isError(null, $value);
  100. }
  101. /**
  102. * Tests if the submitted form has a specific error.
  103. *
  104. * @param string $field The field name to check for an error (null for global errors)
  105. * @param mixed $message The error message or the number of errors for the field (optional)
  106. *
  107. * @return sfTestFunctionalBase|sfTester
  108. */
  109. public function isError($field, $value = true)
  110. {
  111. if (is_null($this->form))
  112. {
  113. throw new LogicException('no form has been submitted.');
  114. }
  115. if (is_null($field))
  116. {
  117. $error = new sfValidatorErrorSchema(new sfValidatorPass(), $this->form->getGlobalErrors());
  118. }
  119. else
  120. {
  121. $error = $this->form[$field]->getError();
  122. }
  123. if (false === $value)
  124. {
  125. $this->tester->ok(!$error || 0 == count($error), sprintf('the submitted form has no "%s" error.', $field));
  126. }
  127. else if (true === $value)
  128. {
  129. $this->tester->ok($error && count($error) > 0, sprintf('the submitted form has a "%s" error.', $field));
  130. }
  131. else if (is_int($value))
  132. {
  133. $this->tester->ok($error && count($error) == $value, sprintf('the submitted form has %s "%s" error(s).', $value, $field));
  134. }
  135. else if (preg_match('/^(!)?([^a-zA-Z0-9\\\\]).+?\\2[ims]?$/', $value, $match))
  136. {
  137. if (!$error)
  138. {
  139. $this->tester->fail(sprintf('the submitted form has a "%s" error.', $field));
  140. }
  141. else
  142. {
  143. if ($match[1] == '!')
  144. {
  145. $this->tester->unlike($error->getCode(), substr($value, 1), sprintf('the submitted form has a "%s" error that does not match "%s".', $field, $value));
  146. }
  147. else
  148. {
  149. $this->tester->like($error->getCode(), $value, sprintf('the submitted form has a "%s" error that matches "%s".', $field, $value));
  150. }
  151. }
  152. }
  153. else
  154. {
  155. if (!$error)
  156. {
  157. $this->tester->fail(sprintf('the submitted form has a "%s" error (%s).', $field, $value));
  158. }
  159. else
  160. {
  161. $this->tester->is($error->getCode(), $value, sprintf('the submitted form has a "%s" error (%s).', $field, $value));
  162. }
  163. }
  164. return $this->getObjectToReturn();
  165. }
  166. /**
  167. * Outputs some debug information about the current submitted form.
  168. */
  169. public function debug()
  170. {
  171. if (is_null($this->form))
  172. {
  173. throw new LogicException('no form has been submitted.');
  174. }
  175. print $this->tester->error('Form debug');
  176. print sprintf("Submitted values: %s\n", str_replace("\n", '', var_export($this->form->getTaintedValues(), true)));
  177. print sprintf("Errors: %s\n", $this->form->getErrorSchema());
  178. exit(1);
  179. }
  180. /**
  181. * Listens to the template.filter_parameters event to get the submitted form object.
  182. *
  183. * @param sfEvent $event The event
  184. * @param array $parameters An array of parameters passed to the template
  185. *
  186. * @return array The array of parameters passed to the template
  187. */
  188. public function filterTemplateParameters(sfEvent $event, $parameters)
  189. {
  190. if (!isset($parameters['sf_type']))
  191. {
  192. return $parameters;
  193. }
  194. if ('action' == $parameters['sf_type'])
  195. {
  196. foreach ($parameters as $key => $value)
  197. {
  198. if ($value instanceof sfForm && $value->isBound())
  199. {
  200. $this->form = $value;
  201. break;
  202. }
  203. }
  204. }
  205. return $parameters;
  206. }
  207. }