Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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