Skip to content

Instantly share code, notes, and snippets.

@ikwattro
Created October 2, 2012 19:25
Show Gist options
  • Save ikwattro/3822724 to your computer and use it in GitHub Desktop.
Save ikwattro/3822724 to your computer and use it in GitHub Desktop.
<?php
namespace Lunetics\TimezoneBundle\Validator;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
class Timezone extends Constraint
{
public $message = 'The timezone "%string%" is not a valid timezone';
}
<?php
/**
* This file is part of the MfCoreBundle package.
*
* <https://github.com/lunetics/>
*
* For the full copyright and license information, please view the LICENSE
* file that is distributed with this source code.
*/
namespace Lunetics\TimezoneBundle\Validator;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
/**
* Timezone Validator Class
*
* @author Matthias Breddin <[email protected]>
*/
class TimezoneValidator extends ConstraintValidator
{
/**
* Validates the Timezone
*
* @param string $timezone The timezone string
* @param Timezone|Constraint $constraint timezone Constraint
*/
public function validate($timezone, Constraint $constraint)
{
if (!in_array($timezone, \DateTimeZone::listIdentifiers())) {
$this->context->addViolation($constraint->message, array('%string%' => $timezone));
}
}
}
<?php
namespace Lunetics\TimezoneBundle\Tests\Validator;
use Lunetics\TimezoneBundle\Validator\Timezone;
use Lunetics\TimezoneBundle\Validator\TimezoneValidator;
/**
* Test for the Timezone Validator
*/
class TimezoneValidatorTest extends \PHPUnit_Framework_TestCase
{
protected $context;
/**
* Setup
*/
public function setUp()
{
$this->context = $this->getContext();
}
/**
* Provider for Valid Timezones
*
* @return array
*/
public function validTimezones()
{
return array(
array('Europe/Berlin'),
array('UTC'),
array('America/New_York')
);
}
/**
* Test if Timezone is valid
*
* @param string $timezone
*
* @dataProvider validTimezones
*/
public function testTimezoneIsValid($timezone)
{
$constraint = new Timezone();
$this->context->expects($this->never())
->method('addViolation');
$this->getTimezoneValidator()->validate($timezone, $constraint);
}
/**
* Provider for Invalid Timezones
*
* @return array
*/
public function invalidTimezones()
{
return array(
array('GMT'),
array('NONE'),
array('EST')
);
}
/**
* Test if Timezone is invalid
*
* @param string $timezone
*
* @dataProvider invalidTimezones
*/
public function testTimezoneIsInvalid($timezone)
{
$constraint = new Timezone();
$this->context->expects($this->once())
->method('addViolation')
->with($this->equalTo($constraint->message), $this->equalTo(array('%string%' => $timezone)));
$this->getTimezoneValidator()->validate($timezone, $constraint);
}
/**
* Returns an Executioncontext
*
* @return \PHPUnit_Framework_MockObject_MockObject
*/
private function getContext()
{
return $this->getMockBuilder('Symfony\Component\Validator\ExecutionContext')->disableOriginalConstructor()->getMock();
}
/**
* Returns the TimezoneValidator
*
* @return \Lunetics\TimezoneBundle\Validator\TimezoneValidator
*/
private function getTimezoneValidator()
{
$validator = new TimezoneValidator();
$validator->initialize($this->context);
return $validator;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment