[ZF-10060] Unittests:
[zend/radio.git] / tests / Zend / Validate / DateTest.php
blob163b5719ec21e495bfda71a154340893c94dde94
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
15 * @category Zend
16 * @package Zend_Validate
17 * @subpackage UnitTests
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id$
23 /**
24 * Test helper
26 require_once dirname(__FILE__) . '/../../TestHelper.php';
28 /**
29 * @see Zend_Validate_Date
31 require_once 'Zend/Validate/Date.php';
33 /**
34 * @category Zend
35 * @package Zend_Validate
36 * @subpackage UnitTests
37 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
38 * @license http://framework.zend.com/license/new-bsd New BSD License
39 * @group Zend_Validate
41 class Zend_Validate_DateTest extends PHPUnit_Framework_TestCase
43 /**
44 * Zend_Validate_Date object
46 * @var Zend_Validate_Date
48 protected $_validator;
50 /**
51 * Whether an error occurred
53 * @var boolean
55 protected $_errorOccurred = false;
57 /**
58 * Creates a new Zend_Validate_Date object for each test method
60 * @return void
62 public function setUp()
64 $this->_validator = new Zend_Validate_Date();
67 /**
68 * Ensures that the validator follows expected behavior
70 * @return void
72 public function testBasic()
74 $valuesExpected = array(
75 '2007-01-01' => true,
76 '2007-02-28' => true,
77 '2007-02-29' => false,
78 '2008-02-29' => true,
79 '2007-02-30' => false,
80 '2007-02-99' => false,
81 '9999-99-99' => false,
82 0 => false,
83 999999999999 => false,
84 'Jan 1 2007' => false,
85 'asdasda' => false,
86 'sdgsdg' => false
88 foreach ($valuesExpected as $input => $result) {
89 $this->assertEquals($result, $this->_validator->isValid($input),
90 "'$input' expected to be " . ($result ? '' : 'in') . 'valid');
94 /**
95 * Ensures that characters trailing an otherwise valid date cause the input to be invalid
97 * @see http://framework.zend.com/issues/browse/ZF-1804
98 * @return void
100 public function testCharactersTrailingInvalid()
102 $dateValid = '2007-08-02';
103 $charactersTrailing = 'something';
104 $this->assertTrue($this->_validator->isValid($dateValid));
105 $this->assertFalse($this->_validator->isValid($dateValid . $charactersTrailing));
109 * Ensures that characters leading an otherwise valid date cause the input to be invalid
111 * @see http://framework.zend.com/issues/browse/ZF-1804
112 * @return void
114 public function testCharactersLeadingInvalid()
116 $dateValid = '2007-08-02';
117 $charactersLeading = 'something';
118 $this->assertTrue($this->_validator->isValid($dateValid));
119 $this->assertFalse($this->_validator->isValid($charactersLeading . $dateValid));
123 * Ensures that getMessages() returns expected default value
125 * @return void
127 public function testGetMessages()
129 $this->assertEquals(array(), $this->_validator->getMessages());
133 * Ensures that the validator can handle different manual dateformats
135 * @see http://framework.zend.com/issues/browse/ZF-2003
136 * @return void
138 public function testUseManualFormat()
140 $this->assertTrue($this->_validator->setFormat('dd.MM.YYYY')->isValid('10.01.2008'));
141 $this->assertEquals('dd.MM.YYYY', $this->_validator->getFormat());
143 $this->assertTrue($this->_validator->setFormat('MM yyyy')->isValid('01 2010'));
144 $this->assertFalse($this->_validator->setFormat('dd/MM/yyyy')->isValid('2008/10/22'));
145 $this->assertTrue($this->_validator->setFormat('dd/MM/yy')->isValid('22/10/08'));
146 $this->assertFalse($this->_validator->setFormat('dd/MM/yy')->isValid('22/10'));
147 set_error_handler(array($this, 'errorHandlerIgnore'));
148 $result = $this->_validator->setFormat('s')->isValid(0);
149 restore_error_handler();
150 if (!$this->_errorOccurred) {
151 $this->assertTrue($result);
152 } else {
153 $this->markTestSkipped('Affected by bug described in ZF-2789');
155 $this->_errorOccurred = false;
159 * Ensures that the validator can handle different dateformats from locale
161 * @see http://framework.zend.com/issues/browse/ZF-2003
162 * @return void
164 public function testUseLocaleFormat()
166 $errorOccurredLocal = false;
167 set_error_handler(array($this, 'errorHandlerIgnore'));
168 $valuesExpected = array(
169 '10.01.2008' => true,
170 '32.02.2008' => false,
171 '20 April 2008' => true,
172 '1 Jul 2008' => true,
173 '2008/20/03' => false,
174 '99/99/2000' => false,
175 0 => false,
176 999999999999 => false,
177 'Jan 1 2007' => false
179 foreach ($valuesExpected as $input => $resultExpected) {
180 $resultActual = $this->_validator->setLocale('de_AT')->isValid($input);
181 if (!$this->_errorOccurred) {
182 $this->assertEquals($resultExpected, $resultActual, "'$input' expected to be "
183 . ($resultExpected ? '' : 'in') . 'valid');
184 } else {
185 $errorOccurredLocal = true;
187 $this->_errorOccurred = false;
189 $this->assertEquals('de_AT', $this->_validator->getLocale());
190 restore_error_handler();
191 if ($errorOccurredLocal) {
192 $this->markTestSkipped('Affected by bug described in ZF-2789');
197 * Ensures that the validator can handle different dateformats from locale
199 * @see http://framework.zend.com/issues/browse/ZF-2003
200 * @return void
202 public function testLocaleContructor()
204 set_error_handler(array($this, 'errorHandlerIgnore'));
205 $valid = new Zend_Validate_Date('dd.MM.YYYY', 'de');
206 $this->assertTrue($valid->isValid('10.April.2008'));
208 restore_error_handler();
212 * @ZF-4352
214 public function testNonStringValidation()
216 $this->assertFalse($this->_validator->isValid(array(1 => 1)));
220 * @ZF-6374
222 public function testUsingApplicationLocale()
224 Zend_Registry::set('Zend_Locale', new Zend_Locale('de'));
225 $valid = new Zend_Validate_Date();
226 $this->assertTrue($valid->isValid('10.April.2008'));
230 * ZF-7630
232 public function testDateObjectVerification()
234 $date = new Zend_Date();
235 $this->assertTrue($this->_validator->isValid($date), "'$date' expected to be valid");
239 * ZF-6457
241 public function testArrayVerification()
243 $date = new Zend_Date();
244 $array = $date->toArray();
245 $this->assertTrue($this->_validator->isValid($array), "array expected to be valid");
249 * Ignores a raised PHP error when in effect, but throws a flag to indicate an error occurred
251 * @param integer $errno
252 * @param string $errstr
253 * @param string $errfile
254 * @param integer $errline
255 * @param array $errcontext
256 * @return void
257 * @see http://framework.zend.com/issues/browse/ZF-2789
259 public function errorHandlerIgnore($errno, $errstr, $errfile, $errline, array $errcontext)
261 $this->_errorOccurred = true;