Development

#1665: dateTest.php

You must first sign up to be able to contribute.

Ticket #1665: dateTest.php

File dateTest.php, 5.1 kB (added by bladus, 2 years ago)
Line 
1 <?php
2
3 require_once(dirname(__FILE__).'/../bootstrap/unit.php');
4 require_once($_test_dir.'/unit/sfValidatorTestHelper.class.php');
5
6 class sfContext
7 {
8   public $request = null;
9
10   function getRequest()
11   {
12     return $this->request;
13   }
14 }
15
16 class sfRequest
17 {
18   public $parameters = array();
19
20   function getParameter($key)
21   {
22     return $this->parameters[$key];
23   }
24 }
25
26 $t = new lime_test(69, new lime_output_color());
27
28 $context = new sfContext();
29 $request = new sfRequest();
30 $context->request = $request;
31 $v = new sfDateValidator();
32 $v->initialize($context);
33
34 // ->execute()
35 $t->diag('->execute()');
36
37 $date = '12.04.2007';
38 $error = null;
39 $t->ok($v->execute($date, $error), '->execute() returns true if you don\'t define any parameter');
40
41 $date = 'not a date';
42 $error = null;
43 $t->ok(!$v->execute($date, $error), '->execute() returns "date_error" if value is not a date');
44 $t->is($error, 'Invalid date', '->execute() changes "$error" with a default message if it returns false');
45
46 foreach (array('>', '>=', '<=', '<', '!=', '==') as $operator)
47 {
48   $t->ok($v->initialize($context, array('operator' => $operator)), sprintf('->execute() can take "%s" as a operator argument', $operator));
49 }
50
51 try
52 {
53   $v->initialize($context, array('operator' => 'another'));
54   $t->fail('->initialize() throws an sfValidatorException if "operator" is invalid');
55 }
56 catch (sfValidatorException $e)
57 {
58   $t->pass('->initialize() throws an sfValidatorException if "operator" is invalid');
59 }
60
61
62 $h = new sfValidatorTestHelper($context, $t);
63
64 // min
65 $t->diag('->execute() - min parameter');
66 $h->launchTests($v, '15.01.2000', true, 'min', null, array('min' => '10.01.2000'));
67 $h->launchTests($v, '10.01.2000', true, 'min', null, array('min' => '10.01.2000'));
68 $h->launchTests($v, '01.01.2000', false, 'min', 'min_error', array('min' => '10.01.2000'));
69
70 // check exception if min is invalid
71 try
72 {
73   $v->initialize($context, array('min' => 'another'));
74   $t->fail('->initialize() throws an sfValidatorException if "min" is invalid');
75 }
76 catch (sfValidatorException $e)
77 {
78   $t->pass('->initialize() throws an sfValidatorException if "min" is invalid');
79 }
80
81 // max
82 $t->diag('->execute() - max parameter');
83 $h->launchTests($v, '01.01.2000', true, 'max', null, array('max' => '10.01.2000'));
84 $h->launchTests($v, '10.01.2000', true, 'max', null, array('max' => '10.01.2000'));
85 $h->launchTests($v, '15.01.2000', false, 'max', 'max_error', array('max' => '10.01.2000'));
86
87 // check exception if max is invalid
88 try
89 {
90   $v->initialize($context, array('max' => 'another'));
91   $t->fail('->initialize() throws an sfValidatorException if "max" is invalid');
92 }
93 catch (sfValidatorException $e)
94 {
95   $t->pass('->initialize() throws an sfValidatorException if "max" is invalid');
96 }
97
98 // compare (default for "operator" is "==")
99 $t->diag('->execute() - compare parameter');
100 $request->parameters = array('value2' => '10.01.2000');
101 // another form field
102 $options = array('compare' => 'value2');
103 $h->launchTests($v, '10.01.2000', true, 'compare', null, $options);
104 $h->launchTests($v, '10.10.2000', false, 'compare', 'compare_error', $options);
105 // strtotime/relative values
106 $options = array('compare' => 'tomorrow');
107 $h->launchTests($v, 'tomorrow', true, 'compare', null, $options);
108 $h->launchTests($v, '10.01.2000', false, 'compare', 'compare_error', $options);
109 // invalid compare parameter
110 $options = array('compare' => 'another');
111 $h->launchTests($v, '10.01.2000', false, 'compare', 'compare_error', $options);
112
113 // ">" operator
114 $t->diag('->execute() - ">" operator');
115 $h->launchTests($v, '15.01.2000', true, 'operator', null, array('operator' => '>', 'compare' => '10.01.2000'));
116 $h->launchTests($v, '10.01.2000', false, 'operator', 'compare_error', array('operator' => '>', 'compare' => '10.01.2000'));
117
118 // ">=" operator
119 $t->diag('->execute() - ">=" operator');
120 $h->launchTests($v, '10.01.2000', true, 'operator', null, array('operator' => '>=', 'compare' => '10.01.2000'));
121 $h->launchTests($v, '01.01.2000', false, 'operator', 'compare_error', array('operator' => '>=', 'compare' => '10.01.2000'));
122
123 // "!=" operator
124 $t->diag('->execute() - "!=" operator');
125 $h->launchTests($v, '15.01.2000', true, 'operator', null, array('operator' => '!=', 'compare' => '10.01.2000'));
126 $h->launchTests($v, '10.01.2000', false, 'operator', 'compare_error', array('operator' => '!=', 'compare' => '10.01.2000'));
127
128 // "==" operator
129 $t->diag('->execute() - "==" operator');
130 $h->launchTests($v, '10.01.2000', true, 'operator', null, array('operator' => '==', 'compare' => '10.01.2000'));
131 $h->launchTests($v, '01.01.2000', false, 'operator', 'compare_error', array('operator' => '==', 'compare' => '10.01.2000'));
132
133 // "<" operator
134 $t->diag('->execute() - "<" operator');
135 $h->launchTests($v, '01.01.2000', true, 'operator', null, array('operator' => '<', 'compare' => '10.01.2000'));
136 $h->launchTests($v, '10.01.2000', false, 'operator', 'compare_error', array('operator' => '<', 'compare' => '10.01.2000'));
137
138 // "<=" operator
139 $t->diag('->execute() - "<=" operator');
140 $h->launchTests($v, '10.01.2000', true, 'operator', null, array('operator' => '<=', 'compare' => '10.01.2000'));
141 $h->launchTests($v, '15.01.2000', false, 'operator', 'compare_error', array('operator' => '<=', 'compare' => '10.01.2000'));
142