2010-06-24 09:40:05 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Symfony\Tests\Components\Validator;
|
|
|
|
|
|
|
|
require_once __DIR__.'/Fixtures/ConstraintA.php';
|
|
|
|
require_once __DIR__.'/Fixtures/ConstraintB.php';
|
|
|
|
require_once __DIR__.'/Fixtures/ConstraintC.php';
|
|
|
|
|
|
|
|
use Symfony\Tests\Components\Validator\Fixtures\ConstraintA;
|
|
|
|
use Symfony\Tests\Components\Validator\Fixtures\ConstraintB;
|
|
|
|
use Symfony\Tests\Components\Validator\Fixtures\ConstraintC;
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
$this->setExpectedException('Symfony\Components\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-06-24 10:24:08 +01:00
|
|
|
$this->setExpectedException('Symfony\Components\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
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
$this->setExpectedException('Symfony\Components\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()
|
|
|
|
{
|
|
|
|
$this->setExpectedException('Symfony\Components\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
|
|
|
|
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-06-24 09:40:05 +01:00
|
|
|
}
|