123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- <?php
- class sfTesterForm extends sfTester
- {
- protected
- $form = null;
-
- public function __construct(sfTestFunctionalBase $browser, $tester)
- {
- parent::__construct($browser, $tester);
- $this->browser->addListener('template.filter_parameters', array($this, 'filterTemplateParameters'));
- }
-
- public function prepare()
- {
- $this->form = null;
- }
-
- public function initialize()
- {
- if (is_null($this->form))
- {
- $action = $this->browser->getContext()->getActionStack()->getLastEntry()->getActionInstance();
- foreach ($action->getVarHolder()->getAll() as $name => $value)
- {
- if ($value instanceof sfForm && $value->isBound())
- {
- $this->form = $value;
- break;
- }
- }
- }
- }
-
- public function getForm()
- {
- return $this->form;
- }
-
- public function hasErrors($value = true)
- {
- if (is_null($this->form))
- {
- throw new LogicException('no form has been submitted.');
- }
- if (is_int($value))
- {
- $this->tester->is(count($this->form->getErrorSchema()), $value, sprintf('the submitted form has "%s" errors.', $value));
- }
- else
- {
- $this->tester->is($this->form->hasErrors(), $value, sprintf('the submitted form %s.', ($value) ? 'has some errors' : 'is valid'));
- }
- return $this->getObjectToReturn();
- }
-
- public function hasGlobalError($value = true)
- {
- return $this->isError(null, $value);
- }
-
- public function isError($field, $value = true)
- {
- if (is_null($this->form))
- {
- throw new LogicException('no form has been submitted.');
- }
- if (is_null($field))
- {
- $error = new sfValidatorErrorSchema(new sfValidatorPass(), $this->form->getGlobalErrors());
- }
- else
- {
- $error = $this->form[$field]->getError();
- }
- if (false === $value)
- {
- $this->tester->ok(!$error || 0 == count($error), sprintf('the submitted form has no "%s" error.', $field));
- }
- else if (true === $value)
- {
- $this->tester->ok($error && count($error) > 0, sprintf('the submitted form has a "%s" error.', $field));
- }
- else if (is_int($value))
- {
- $this->tester->ok($error && count($error) == $value, sprintf('the submitted form has %s "%s" error(s).', $value, $field));
- }
- else if (preg_match('/^(!)?([^a-zA-Z0-9\\\\]).+?\\2[ims]?$/', $value, $match))
- {
- if (!$error)
- {
- $this->tester->fail(sprintf('the submitted form has a "%s" error.', $field));
- }
- else
- {
- if ($match[1] == '!')
- {
- $this->tester->unlike($error->getCode(), substr($value, 1), sprintf('the submitted form has a "%s" error that does not match "%s".', $field, $value));
- }
- else
- {
- $this->tester->like($error->getCode(), $value, sprintf('the submitted form has a "%s" error that matches "%s".', $field, $value));
- }
- }
- }
- else
- {
- if (!$error)
- {
- $this->tester->fail(sprintf('the submitted form has a "%s" error (%s).', $field, $value));
- }
- else
- {
- $this->tester->is($error->getCode(), $value, sprintf('the submitted form has a "%s" error (%s).', $field, $value));
- }
- }
- return $this->getObjectToReturn();
- }
-
- public function debug()
- {
- if (is_null($this->form))
- {
- throw new LogicException('no form has been submitted.');
- }
- print $this->tester->error('Form debug');
- print sprintf("Submitted values: %s\n", str_replace("\n", '', var_export($this->form->getTaintedValues(), true)));
- print sprintf("Errors: %s\n", $this->form->getErrorSchema());
- exit(1);
- }
-
- public function filterTemplateParameters(sfEvent $event, $parameters)
- {
- if (!isset($parameters['sf_type']))
- {
- return $parameters;
- }
- if ('action' == $parameters['sf_type'])
- {
- foreach ($parameters as $key => $value)
- {
- if ($value instanceof sfForm && $value->isBound())
- {
- $this->form = $value;
- break;
- }
- }
- }
- return $parameters;
- }
- }
|