2010-06-24 09:40:05 +01:00
|
|
|
<?php
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-15 13:29:43 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2012-03-28 14:43:52 +01:00
|
|
|
namespace Symfony\Component\Validator\Tests;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2012-03-28 14:43:52 +01:00
|
|
|
use Symfony\Component\Validator\Tests\Fixtures\ClassConstraint;
|
|
|
|
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
|
|
|
|
use Symfony\Component\Validator\Tests\Fixtures\ConstraintB;
|
|
|
|
use Symfony\Component\Validator\Tests\Fixtures\ConstraintC;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
|
|
|
class ConstraintTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testSetProperties()
|
|
|
|
{
|
|
|
|
$constraint = new ConstraintA(array(
|
|
|
|
'property1' => 'foo',
|
|
|
|
'property2' => 'bar',
|
|
|
|
));
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->assertEquals('foo', $constraint->property1);
|
|
|
|
$this->assertEquals('bar', $constraint->property2);
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testSetNotExistingPropertyThrowsException()
|
|
|
|
{
|
2010-08-20 22:09:55 +01:00
|
|
|
$this->setExpectedException('Symfony\Component\Validator\Exception\InvalidOptionsException');
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
new ConstraintA(array(
|
|
|
|
'foo' => 'bar',
|
|
|
|
));
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testMagicPropertiesAreNotAllowed()
|
|
|
|
{
|
|
|
|
$constraint = new ConstraintA();
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
$this->setExpectedException('Symfony\Component\Validator\Exception\InvalidOptionsException');
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$constraint->foo = 'bar';
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2011-06-14 09:40:42 +01:00
|
|
|
public function testInvalidAndRequiredOptionsPassed()
|
|
|
|
{
|
|
|
|
$this->setExpectedException('Symfony\Component\Validator\Exception\InvalidOptionsException');
|
|
|
|
|
|
|
|
new ConstraintC(array(
|
|
|
|
'option1' => 'default',
|
|
|
|
'foo' => 'bar'
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testSetDefaultProperty()
|
|
|
|
{
|
|
|
|
$constraint = new ConstraintA('foo');
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->assertEquals('foo', $constraint->property2);
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testSetDefaultPropertyDoctrineStyle()
|
|
|
|
{
|
|
|
|
$constraint = new ConstraintA(array('value' => 'foo'));
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->assertEquals('foo', $constraint->property2);
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testSetUndefinedDefaultProperty()
|
|
|
|
{
|
2010-08-20 22:09:55 +01:00
|
|
|
$this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
new ConstraintB('foo');
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testRequiredOptionsMustBeDefined()
|
|
|
|
{
|
2010-08-20 22:09:55 +01:00
|
|
|
$this->setExpectedException('Symfony\Component\Validator\Exception\MissingOptionsException');
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
new ConstraintC();
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2011-05-08 22:52:34 +01:00
|
|
|
public function testRequiredOptionsPassed()
|
|
|
|
{
|
|
|
|
new ConstraintC(array('option1' => 'default'));
|
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testGroupsAreConvertedToArray()
|
|
|
|
{
|
|
|
|
$constraint = new ConstraintA(array('groups' => 'Foo'));
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->assertEquals(array('Foo'), $constraint->groups);
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testAddDefaultGroupAddsGroup()
|
|
|
|
{
|
|
|
|
$constraint = new ConstraintA(array('groups' => 'Default'));
|
|
|
|
$constraint->addImplicitGroupName('Foo');
|
|
|
|
$this->assertEquals(array('Default', 'Foo'), $constraint->groups);
|
|
|
|
}
|
2010-07-04 17:08:06 +01:00
|
|
|
|
|
|
|
public function testAllowsSettingZeroRequiredPropertyValue()
|
|
|
|
{
|
|
|
|
$constraint = new ConstraintA(0);
|
|
|
|
$this->assertEquals(0, $constraint->property2);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCanCreateConstraintWithNoDefaultOptionAndEmptyArray()
|
|
|
|
{
|
|
|
|
new ConstraintB(array());
|
|
|
|
}
|
2011-05-25 09:51:35 +01:00
|
|
|
|
|
|
|
public function testGetTargetsCanBeString()
|
|
|
|
{
|
|
|
|
$constraint = new ClassConstraint;
|
|
|
|
|
|
|
|
$this->assertEquals('class', $constraint->getTargets());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetTargetsCanBeArray()
|
|
|
|
{
|
|
|
|
$constraint = new ConstraintA;
|
|
|
|
|
|
|
|
$this->assertEquals(array('property', 'class'), $constraint->getTargets());
|
|
|
|
}
|
2011-06-08 18:56:59 +01:00
|
|
|
}
|