Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/6311' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Aug 7, 2014
171 parents bc6b82e + e240ab3 + 44b0196 + 4cf13fa + 26a1d46 + 712a04b + 4dbc11a + c99b627 + 740abb6 + 9b5478d + 91a23e2 + 8649d44 + 3f353b0 + d340adb + 1e0e8f4 + 5c4289e + fb94cca + 2a3844c + 906bbcf + 3d9b8bb + 84844ae + 62fc651 + b0a3dd4 + 0dca0ef + 910bbbf + e574b9b + f30ec7d + 1fa84de + 41b8543 + 9e2c9d5 + 913f51c + 39924f3 + c2a11e1 + 61b9322 + 413a38b + e51b2b8 + 20e328b + 6437ec0 + e9b8476 + 95e54a0 + 7ea3aed + df6a706 + a82fc82 + 7c2a059 + 4fefb53 + 599ee3a + ea3fc65 + f6c04c2 + 6591e3d + a4f76e3 + 33c7531 + 2d59782 + 8152327 + e56ce9b + db9b18f + b88635a + a262823 + b79aef6 + c2284e4 + 70193eb + 96acf77 + 9675426 + 5f02411 + 0dafea7 + 15dc674 + 4a2447d + e6eb7f3 + e9499c5 + 272b15f + 11c7758 + 6f0e918 + 5f4980a + ecca95a + 88b5971 + ecb8d13 + 9de1c08 + 44aad17 + 13269e9 + 654cdb6 + dc708db + 380ffba + ff67e7f + fe2e025 + 95f0efa + 68cc4b3 + bf13b96 + 8870381 + 56480f4 + 1fa90d7 + 5c7fe1f + abe9bc1 + a33cacd + cdd7d9f + 6a261b1 + 5e594c4 + 01c1241 + 30d1dd2 + d4af079 + c9aa9b4 + 10f47ca + ef20fa1 + 2187ae2 + e7f3993 + db93d37 + aa57612 + 4af81d8 + 2f90998 + 3013404 + 69d83fe + f383ca9 + 1b26f48 + 054f09d + 0c86829 + f22d81a + e7ebffe + 72a7a54 + cc09223 + ab99f58 + 2c69e37 + b6ccfbc + b92a5da + 773a133 + 9ee28ff + 5865e20 + 63c7303 + 73371d0 + b96f402 + b36e36b + 60fa081 + a1d27a6 + 43e9240 + 9e59ae6 + be1ce44 + 5a6465d + 7e455b4 + 83d837e + 28bc01e + 215be48 + efcc8e0 + 5192ae6 + 7e1ba0f + dec8ccf + 94afc0f + 8c3ea5f + d680762 + 9092473 + 041fc63 + ea6499d + 1f59300 + a75142b + f592cc2 + f523aef + 2d12221 + 34ad758 + 23cc229 + 1fa15a0 + 6b74fc7 + 1472e82 + d816ccc + 7b26586 + 08d26c4 + a5cc444 + fe92acf + 6bd67b2 + 371ba41 + 2dc7154 + 2902dd7 + aecaa35 + 63cc2ee + fe40c59 + 02c8dbd commit 514fc95
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 53 deletions.
48 changes: 22 additions & 26 deletions src/Validator/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@

class DateTime extends AbstractValidator
{
const INVALID = 'datetimeInvalid';
const INVALID_DATETIME = 'datetimeInvalidDateTime';
const INVALID = 'datetimeInvalid';
const INVALID_DATETIME = 'datetimeInvalidDateTime';

/**
*
* @var array
*/
protected $messageTemplates = array(
self::INVALID => "Invalid type given. String expected",
self::INVALID_DATETIME => "The input does not appear to be a valid datetime",
self::INVALID => "Invalid type given. String expected",
self::INVALID_DATETIME => "The input does not appear to be a valid datetime",
);

/**
Expand Down Expand Up @@ -70,7 +71,6 @@ class DateTime extends AbstractValidator

/**
* Is the formatter invalidated
*
* Invalidation occurs when immutable properties are changed
*
* @var bool
Expand All @@ -86,10 +86,9 @@ class DateTime extends AbstractValidator
public function __construct($options = array())
{
if (!extension_loaded('intl')) {
throw new I18nException\ExtensionNotLoadedException(sprintf(
'%s component requires the intl PHP extension',
__NAMESPACE__
));
throw new I18nException\ExtensionNotLoadedException(
sprintf('%s component requires the intl PHP extension', __NAMESPACE__)
);
}

// Delaying initialization until we know ext/intl is available
Expand Down Expand Up @@ -138,7 +137,7 @@ public function getCalendar()
*/
public function setDateType($dateType)
{
$this->dateType = $dateType;
$this->dateType = $dateType;
$this->invalidateFormatter = true;

return $this;
Expand Down Expand Up @@ -185,7 +184,7 @@ public function getPattern()
*/
public function setTimeType($timeType)
{
$this->timeType = $timeType;
$this->timeType = $timeType;
$this->invalidateFormatter = true;

return $this;
Expand Down Expand Up @@ -251,7 +250,7 @@ public function getLocale()
/**
* Returns true if and only if $value is a floating-point value
*
* @param string $value
* @param string $value
* @return bool
* @throws ValidatorException\InvalidArgumentException
*/
Expand All @@ -264,25 +263,16 @@ public function isValid($value)
}

$this->setValue($value);

$formatter = $this->getIntlDateFormatter();

if (intl_is_failure($formatter->getErrorCode())) {
throw new ValidatorException\InvalidArgumentException("Invalid locale string given");
throw new ValidatorException\InvalidArgumentException($formatter->getErrorMessage());
}

$position = 0;
$formatter->parse($value, $position);

if (intl_is_failure($formatter->getErrorCode())) {
$this->error(self::INVALID_DATETIME);

return false;
}
$timestamp = $formatter->parse($value);

if ($position != strlen($value)) {
if (intl_is_failure($formatter->getErrorCode()) || $timestamp === false) {
$this->error(self::INVALID_DATETIME);

return false;
}

Expand All @@ -297,8 +287,14 @@ public function isValid($value)
protected function getIntlDateFormatter()
{
if ($this->formatter == null || $this->invalidateFormatter) {
$this->formatter = new IntlDateFormatter($this->getLocale(), $this->getDateType(), $this->getTimeType(),
$this->getTimezone(), $this->getCalendar(), $this->getPattern());
$this->formatter = new IntlDateFormatter(
$this->getLocale(),
$this->getDateType(),
$this->getTimeType(),
$this->getTimezone(),
$this->getCalendar(),
$this->getPattern()
);

$this->formatter->setLenient(false);

Expand Down
96 changes: 69 additions & 27 deletions test/Validator/DateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,28 @@

namespace ZendTest\I18n\Validator;

use Zend\I18n\Validator\DateTime as DateTimeValidator;
use DateTime;
use IntlDateFormatter;
use Locale;
use PHPUnit_Framework_TestCase;
use PHPUnit_Runner_Version;
use Zend\I18n\Validator\DateTime as DateTimeValidator;

/**
* @group Zend_Validator
*/
class DateTimeTest extends \PHPUnit_Framework_TestCase
class DateTimeTest extends PHPUnit_Framework_TestCase
{
/**
* @var DateTimeValidator
*/
protected $validator;

/**
* @var Locale
*/
protected $locale;

/**
* @var \DateTimeZone
*/
protected $timezone;

public function setUp()
Expand All @@ -35,7 +42,10 @@ public function setUp()
$this->locale = Locale::getDefault();
$this->timezone = date_default_timezone_get();

$this->validator = new DateTimeValidator(array('locale' => 'en', 'timezone'=>'Europe/Amsterdam'));
$this->validator = new DateTimeValidator(array(
'locale' => 'en',
'timezone' => 'Europe/Amsterdam'
));
}

public function tearDown()
Expand All @@ -49,47 +59,80 @@ public function tearDown()
/**
* Ensures that the validator follows expected behavior
*
* @dataProvider basicProvider name of method that provides parameters
* @param string $value that will be tested
* @param boolean $expected expected result of assertion
* @param array $options fed into the validator before validation
* @dataProvider basicProvider name of method that provide the above parameters
* @return void
*/
public function testBasic($value, $expected, $options = array())
{
$this->validator->setOptions($options);

$this->assertEquals($expected, $this->validator->isValid($value),
'Failed expecting ' . $value . ' being ' . ($expected ? 'true' : 'false') .
sprintf(" (locale:%s, dateType: %s, timeType: %s, pattern:%s)", $this->validator->getLocale(),
$this->validator->getDateType(), $this->validator->getTimeType(), $this->validator->getPattern()));
$this->assertEquals(
$expected,
$this->validator->isValid($value),
sprintf('Failed expecting %s being %s', $value, ($expected ? 'true' : 'false'))
. sprintf(
' (locale:%s, dateType: %s, timeType: %s, pattern:%s)',
$this->validator->getLocale(),
$this->validator->getDateType(),
$this->validator->getTimeType(),
$this->validator->getPattern()
)
);
}

public function basicProvider()
{
if (!extension_loaded('intl')) {
if (version_compare(\PHPUnit_Runner_Version::id(), '3.8.0-dev') === 1) {
if (version_compare(PHPUnit_Runner_Version::id(), '3.8.0-dev') === 1) {
$this->markTestSkipped('ext/intl not enabled');
} else {
return array(array());
return array(
array()
);
}
}

return array(
array('May 30, 2013', true, array('locale'=>'en', 'dateType' => \IntlDateFormatter::MEDIUM, 'timeType' => \IntlDateFormatter::NONE)),
array('30.Mai.2013', true, array('locale'=>'de', 'dateType' => \IntlDateFormatter::MEDIUM, 'timeType' => \IntlDateFormatter::NONE)),
array('30 Mei 2013', true, array('locale'=>'nl', 'dateType' => \IntlDateFormatter::MEDIUM, 'timeType' => \IntlDateFormatter::NONE)),
$trueArray = array();
$testingDate = new DateTime();
$testingLocales = array('en', 'de', 'zh-TW', 'ja', 'ar', 'ru', 'si', 'ml-IN', 'hi');
$testingFormats = array(
IntlDateFormatter::FULL,
IntlDateFormatter::LONG,
IntlDateFormatter::MEDIUM,
IntlDateFormatter::SHORT,
IntlDateFormatter::NONE
);

array('May 38, 2013', false, array('locale'=>'en', 'dateType' => \IntlDateFormatter::FULL, 'timeType' => \IntlDateFormatter::NONE)),
array('Dienstag, 28. Mai 2013', true, array('locale'=>'de', 'dateType' => \IntlDateFormatter::FULL, 'timeType' => \IntlDateFormatter::NONE)),
array('Maandag 28 Mei 2013', true, array('locale'=>'nl', 'dateType' => \IntlDateFormatter::FULL, 'timeType' => \IntlDateFormatter::NONE)),
//Loop locales and formats for a more thorough set of "true" test data
foreach ($testingLocales as $locale) {
foreach ($testingFormats as $dateFormat) {
foreach ($testingFormats as $timeFormat) {
if (($timeFormat !== IntlDateFormatter::NONE) || ($dateFormat !== IntlDateFormatter::NONE)) {
$trueArray[] = array(
IntlDateFormatter::create($locale, $dateFormat, $timeFormat)->format($testingDate),
true,
array('locale' => $locale, 'dateType' => $dateFormat, 'timeType' => $timeFormat)
);
}
}
}
}

array('0:00', true, array('locale'=>'nl', 'dateType' => \IntlDateFormatter::NONE, 'timeType' => \IntlDateFormatter::SHORT)),
array('01:01', true, array('locale'=>'nl', 'dateType' => \IntlDateFormatter::NONE, 'timeType' => \IntlDateFormatter::SHORT)),
array('01:01:01', true, array('locale'=>'nl', 'dateType' => \IntlDateFormatter::NONE, 'timeType' => \IntlDateFormatter::MEDIUM)),
array('01:01:01 +2', true, array('locale'=>'nl', 'dateType' => \IntlDateFormatter::NONE, 'timeType' => \IntlDateFormatter::LONG)),
array('03:30:42 am +2', true, array('locale'=>'en', 'dateType' => \IntlDateFormatter::NONE, 'timeType' => \IntlDateFormatter::LONG)),
$falseArray = array(
array(
'May 38, 2013',
false,
array(
'locale' => 'en',
'dateType' => IntlDateFormatter::FULL,
'timeType' => IntlDateFormatter::NONE
)
)
);

return array_merge($trueArray, $falseArray);
}

/**
Expand Down Expand Up @@ -158,5 +201,4 @@ public function testOptionPattern()
$this->assertTrue($this->validator->isValid('02:00'));
$this->assertEquals('hh:mm', $this->validator->getPattern());
}

}

0 comments on commit 514fc95

Please sign in to comment.