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) 2004-2006 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
class CrudBrowser extends sfTestBrowser
12
{
13
  protected
14
    $urlPrefix = 'article',
15
    $singularName = 'Article',
16
    $pluralName = 'Articles',
17
    $projectDir = '';
18
 
19
  public function setup($options)
20
  {
21
    $this->projectDir = dirname(__FILE__).'/../fixtures';
22
    $this->cleanup();
23
 
24
    chdir($this->projectDir);
25
    $task = new sfPropelGenerateModuleTask(new sfEventDispatcher(), new sfFormatter());
26
    $options[] = 'env=test';
27
    $options[] = 'singular='.$this->singularName;
28
    $options[] = 'plural='.$this->pluralName;
29
    $options[] = '--non-verbose-templates';
30
    $task->run(array('crud', 'article', 'Article'), $options);
31
 
32
    require_once($this->projectDir.'/config/ProjectConfiguration.class.php');
33
    sfContext::createInstance(ProjectConfiguration::getApplicationConfiguration('crud', 'test', true, $this->projectDir));
34
 
35
    return $options;
36
  }
37
 
38
  public function teardown()
39
  {
40
    $this->cleanup();
41
 
42
    return $this;
43
  }
44
 
45
  public function browse($options)
46
  {
47
    $options = $this->setup($options);
48
 
49
    // list page
50
    $this->
51
      info('list page')->
52
      get('/'.$this->urlPrefix)->
53
      with('request')->begin()->
54
        isParameter('module', $this->urlPrefix)->
55
        isParameter('action', 'index')->
56
      end()->
57
      with('response')->begin()->
58
        isStatusCode(200)->
59
 
60
        checkElement('h1', $this->pluralName.' List')->
61
 
62
        checkElement('table thead tr th:nth(0)', 'Id')->
63
        checkElement('table thead tr th:nth(1)', 'Title')->
64
        checkElement('table thead tr th:nth(2)', 'Body')->
65
        checkElement('table thead tr th:nth(3)', 'Online')->
66
        checkElement('table thead tr th:nth(4)', 'Excerpt')->
67
        checkElement('table thead tr th:nth(5)', 'Category')->
68
        checkElement('table thead tr th:nth(6)', 'Created at')->
69
        checkElement('table thead tr th:nth(7)', 'End date')->
70
        checkElement('table thead tr th:nth(8)', 'Book')->
71
 
72
        checkElement('table tbody tr td:nth(0)', '1')->
73
        checkElement('table tbody tr td:nth(1)', 'foo title')->
74
        checkElement('table tbody tr td:nth(2)', 'bar body')->
75
        checkElement('table tbody tr td:nth(3)', '1')->
76
        checkElement('table tbody tr td:nth(4)', 'foo excerpt')->
77
        checkElement('table tbody tr td:nth(5)', '1')->
78
        checkElement('table tbody tr td:nth(6)', '/^\d{4}\-\d{2}\-\d{2} \d{2}\:\d{2}\:\d{2}$/')->
79
        checkElement('table tbody tr td:nth(7)', '')->
80
        checkElement('table tbody tr td:nth(8)', '')->
81
        checkElement(sprintf('a[href*="/%s/new"]', $this->urlPrefix))->
82
        checkElement(sprintf('tbody a[href*="/%s/1%s"]', $this->urlPrefix, in_array('with-show', $options) ? '' : '/edit'))->
83
        checkElement(sprintf('tbody a[href*="/%s/2%s"]', $this->urlPrefix, in_array('with-show', $options) ? '' : '/edit'))->
84
      end()
85
    ;
86
 
87
    // create page
88
    $this->
89
      info('create page')->
90
      click('New')->
91
      with('request')->begin()->
92
        isParameter('module', $this->urlPrefix)->
93
        isParameter('action', 'new')->
94
        isParameter('id', null)->
95
      end()->
96
      with('response')->begin()->
97
        isStatusCode(200)->
98
        checkElement('h1', 'New '.$this->singularName)->
99
        checkElement(sprintf('a[href*="/%s"]', $this->urlPrefix), 'Back to list')->
100
        checkElement(sprintf('a[href*="/%s/"]', $this->urlPrefix), false)->
101
      end()->
102
      checkFormValues(array(
103
        'title'               => '',
104
        'body'                => '',
105
        'Online'              => false,
106
        'category_id'         => 0,
107
        'end_date'            => array('year' => '', 'month' => '', 'day' => '', 'hour' => '', 'minute' => ''),
108
        'book_id'             => 0,
109
        'author_article_list' => array(),
110
      ))
111
    ;
112
 
113
    // save
114
    $this->
115
      info('save')->
116
      saveValues($options, array(
117
        'title'               => 'my real title',
118
        'body'                => 'my real body',
119
        'Online'              => true,
120
        'category_id'         => 2,
121
        'end_date'            => array('year' => '', 'month' => '', 'day' => '', 'hour' => '', 'minute' => ''),
122
        'book_id'             => null,
123
        'author_article_list' => array(1, 2),
124
      ), 3, true)
125
    ;
126
 
127
    // go back to the list
128
    $this->
129
      info('go back to the list')->
130
      click('Back to list')->
131
      with('request')->begin()->
132
        isParameter('module', $this->urlPrefix)->
133
        isParameter('action', 'index')->
134
      end()->
135
      with('response')->isStatusCode(200)
136
    ;
137
 
138
    // edit page
139
    $this->info('edit page');
140
    if (!in_array('with-show', $options) && ($options['with-show'] === true))
141
    {
142
      $this->click('3');
143
    }
144
    else
145
    {
146
      $this->get(sprintf('/%s/3/edit', $this->urlPrefix));
147
    }
148
 
149
    $this->
150
      with('request')->begin()->
151
        isParameter('module', $this->urlPrefix)->
152
        isParameter('action', 'edit')->
153
        isParameter('id', 3)->
154
      end()->
155
      with('response')->begin()->
156
        isStatusCode(200)->
157
        checkElement('h1', 'Edit '.$this->singularName)->
158
        checkElement(sprintf('a[href*="/%s"]', $this->urlPrefix), 'Back to list')->
159
        checkElement(sprintf('a[href*="/%s/3"]', $this->urlPrefix), 'Delete')->
160
        checkElement(sprintf('a[href*="/%s/3"][onclick*="confirm"]', $this->urlPrefix))->
161
        checkElement('table tbody th:nth(0)', 'Title')->
162
        checkElement('table tbody th:nth(1)', 'Body')->
163
        checkElement('table tbody th:nth(2)', 'Online')->
164
        checkElement('table tbody th:nth(3)', 'Excerpt')->
165
        checkElement('table tbody th:nth(4)', 'Category')->
166
        checkElement('table tbody th:nth(5)', 'Created at')->
167
        checkElement('table tbody th:nth(6)', 'End date')->
168
        checkElement('table tbody th:nth(7)', 'Book')->
169
        checkElement('table tbody th:nth(8)', 'Author article list')->
170
        checkElement('table tbody td select[id="article_category_id"][name="article[category_id]"] option', 2)->
171
        checkElement('table tbody td select[id="article_book_id"][name="article[book_id]"] option', 2)->
172
      end()
173
    ;
174
 
175
    // save / validation
176
    $values = array(
177
      'id'                  => 1009299,
178
      'title'               => '',
179
      'body'                => 'my body',
180
      'Online'              => true,
181
      'excerpt'             => 'my excerpt',
182
      'category_id'         => null,
183
      'end_date'            => array('year' => 0, 'month' => 0, 'day' => 15, 'hour' => '10', 'minute' => '20'),
184
      'book_id'             => 149999,
185
      'author_article_list' => array(0, 5),
186
    );
187
 
188
    $this->
189
      info('save / validation')->
190
      click('Save', array('article' => $values))->
191
      with('request')->begin()->
192
        isParameter('module', $this->urlPrefix)->
193
        isParameter('action', 'update')->
194
      end()->
195
      checkFormValues(array_merge($values, array(
196
        'end_date' => array('year' => null, 'month' => null, 'day' => 15, 'hour' => '10', 'minute' => '20')))
197
      )->
198
      with('response')->begin()->
199
        isStatusCode(200)->
200
        checkElement('ul[class="error_list"] li:contains("Required.")', 2)->
201
        checkElement('ul[class="error_list"] li:contains("Invalid.")', 4)->
202
      end()
203
    ;
204
 
205
    // save
206
    $this->
207
      info('save')->
208
      saveValues($options, array(
209
        'id'                  => 3,
210
        'title'               => 'my title',
211
        'body'                => 'my body',
212
        'Online'              => false,
213
        'category_id'         => 1,
214
        'end_date'            => array('year' => date('Y'), 'month' => 10, 'day' => 15, 'hour' => '10', 'minute' => '20'),
215
        'book_id'             => 1,
216
        'author_article_list' => array(1, 3),
217
      ), 3, false)
218
    ;
219
 
220
    // go back to the list
221
    $this->
222
      info('go back to the list')->
223
      click('Back to list')->
224
      with('request')->begin()->
225
        isParameter('module', $this->urlPrefix)->
226
        isParameter('action', 'index')->
227
      end()->
228
      with('response')->isStatusCode(200)
229
    ;
230
 
231
    // delete
232
    $this->
233
      info('delete')->
234
      get(sprintf('/%s/3/edit', $this->urlPrefix))->
235
      click('Delete', array(), array('method' => 'delete', '_with_csrf' => true))->
236
      with('request')->begin()->
237
        isParameter('module', $this->urlPrefix)->
238
        isParameter('action', 'delete')->
239
      end()->
240
      with('response')->begin()->
241
        isRedirected()->
242
        followRedirect()->
243
      end()->
244
      with('request')->begin()->
245
        isParameter('module', $this->urlPrefix)->
246
        isParameter('action', 'index')->
247
      end()->
248
      with('response')->isStatusCode(200)->
249
 
250
      get(sprintf('/%s/3/edit', $this->urlPrefix))->
251
      with('response')->isStatusCode(404)
252
    ;
253
 
254
    if (in_array('with-show', $options))
255
    {
256
      // show page
257
      $this->
258
        info('show page')->
259
        get(sprintf('/%s/2', $this->urlPrefix))->
260
        with('request')->begin()->
261
          isParameter('module', $this->urlPrefix)->
262
          isParameter('action', 'show')->
263
          isParameter('id', 2)->
264
        end()->
265
        with('response')->begin()->
266
          isStatusCode(200)->
267
          checkElement(sprintf('a[href*="/%s/2%s"]', $this->urlPrefix, in_array('with-show', $options) ? '' : '/edit'), 'Edit')->
268
          checkElement(sprintf('a[href*="/%s"]', $this->urlPrefix), 'List', array('position' => 1))->
269
          checkElement('body table tbody tr:nth(0)', '/Id\:\s+2/')->
270
          checkElement('body table tbody tr:nth(1)', '/Title\:\s+foo foo title/')->
271
          checkElement('body table tbody tr:nth(2)', '/Body\:\s+bar bar body/')->
272
          checkElement('body table tbody tr:nth(3)', '/Online\:\s+/')->
273
          checkElement('body table tbody tr:nth(4)', '/Excerpt\:\s+foo excerpt/')->
274
          checkElement('body table tbody tr:nth(5)', '/Category\:\s+2/')->
275
          checkElement('body table tbody tr:nth(6)', '/Created at\:\s+[0-9\-\:\s]+/')->
276
          checkElement('body table tbody tr:nth(7)', '/End date\:\s+[0-9\-\:\s]+/')->
277
          checkElement('body table tbody tr:nth(8)', '/Book\:\s+/')->
278
        end()
279
      ;
280
    }
281
    else
282
    {
283
      $this->get(sprintf('/%s/show/id/2', $this->urlPrefix))->with('response')->isStatusCode(404);
284
    }
285
 
286
    $this->teardown();
287
 
288
    return $this;
289
  }
290
 
291
  public function saveValues($options, $values, $id, $creation)
292
  {
293
    $this->
294
      click('Save', array('article' => $values))->
295
      with('request')->begin()->
296
        isParameter('module', $this->urlPrefix)->
297
        isParameter('action', $creation ? 'create' : 'update')->
298
      end()->
299
      with('response')->begin()->
300
        isRedirected()->
301
        followRedirect()->
302
      end()->
303
      with('response')->begin()->
304
        isStatusCode(200)->
305
      end()->
306
      with('request')->begin()->
307
        isParameter('module', $this->urlPrefix)->
308
        isParameter('action', 'edit')->
309
        isParameter('id', $id)->
310
      end()->
311
      checkFormValues($values)
312
    ;
313
 
314
    return $this;
315
  }
316
 
317
  public function checkFormValues(array $values)
318
  {
319
    return $this->with('response')->begin()->
320
      checkElement(sprintf('table tbody td input[id="article_title"][name="article[title]"][value="%s"]', $values['title']))->
321
 
322
      checkElement('table tbody td textarea[id="article_body"][name="article[body]"]', $values['body'])->
323
 
324
      checkElement(sprintf('table tbody td input[id="article_Online"][name="article[Online]"][type="checkbox"]%s', $values['Online'] ? '[checked="checked"]' : ''))->
325
 
326
      checkElement(sprintf('table tbody td select[id="article_category_id"][name="article[category_id]"] option[value="1"]%s', $values['category_id'] == 1 ? '[selected="selected"]' : ''), 'Category 1')->
327
      checkElement(sprintf('table tbody td select[id="article_category_id"][name="article[category_id]"] option[value="2"]%s', $values['category_id'] == 2 ? '[selected="selected"]' : ''), 'Category 2')->
328
 
329
      checkElement(sprintf('table tbody td select[id="article_book_id"][name="article[book_id]"] option[value=""]%s', $values['book_id'] == '' ? '[selected="selected"]' : ''), '')->
330
      checkElement(sprintf('table tbody td select[id="article_book_id"][name="article[book_id]"] option[value="1"]%s', $values['book_id'] == 1 ? '[selected="selected"]' : ''), 'The definitive guide to symfony')->
331
 
332
      checkElement(sprintf('table tbody td select[id="article_author_article_list"][name="article[author_article_list][]"] option[value="1"]%s', in_array(1, $values['author_article_list']) ? '[selected="selected"]' : ''), 'Fabien')->
333
      checkElement(sprintf('table tbody td select[id="article_author_article_list"][name="article[author_article_list][]"] option[value="2"]%s', in_array(2, $values['author_article_list']) ? '[selected="selected"]' : ''), 'Thomas')->
334
      checkElement(sprintf('table tbody td select[id="article_author_article_list"][name="article[author_article_list][]"] option[value="3"]%s', in_array(3, $values['author_article_list']) ? '[selected="selected"]' : ''), 'Hélène')->
335
 
336
      checkElement('table tbody td select[id="article_end_date_year"][name="article[end_date][year]"] option[selected="selected"]', (string) $values['end_date']['year'])->
337
      checkElement('table tbody td select[id="article_end_date_month"][name="article[end_date][month]"] option[selected="selected"]', (string) $values['end_date']['month'])->
338
      checkElement('table tbody td select[id="article_end_date_day"][name="article[end_date][day]"] option[selected="selected"]', (string) $values['end_date']['day'])->
339
      checkElement('table tbody td select[id="article_end_date_hour"][name="article[end_date][hour]"] option[selected="selected"]', (string) $values['end_date']['hour'])->
340
      checkElement('table tbody td select[id="article_end_date_minute"][name="article[end_date][minute]"] option[selected="selected"]', (string) $values['end_date']['minute'])->
341
    end();
342
  }
343
 
344
  protected function clearDirectory($dir)
345
  {
346
    sfToolkit::clearDirectory($dir);
347
    if (is_dir($dir))
348
    {
349
      rmdir($dir);
350
    }
351
  }
352
 
353
  protected function cleanup()
354
  {
355
    $this->clearDirectory(sprintf($this->projectDir.'/apps/crud/modules/%s', $this->urlPrefix));
356
    $this->clearDirectory(sprintf($this->projectDir.'/cache/crud/test/modules/auto%s', ucfirst($this->urlPrefix)));
357
    $this->clearDirectory($this->projectDir.'/test/functional/crud');
358
  }
359
}