2011-05-08 22:52:34 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Tests\Component\Validator;
|
|
|
|
|
|
|
|
use Symfony\Component\Validator\ExecutionContext;
|
|
|
|
|
|
|
|
class ExecutionContextTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
protected $walker;
|
|
|
|
protected $metadataFactory;
|
|
|
|
protected $context;
|
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
$this->walker = $this->getMock('Symfony\Component\Validator\GraphWalker', array(), array(), '', false);
|
|
|
|
$this->metadataFactory = $this->getMock('Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface');
|
|
|
|
$this->context = new ExecutionContext('Root', $this->walker, $this->metadataFactory);
|
|
|
|
}
|
|
|
|
|
2011-06-14 09:40:42 +01:00
|
|
|
protected function tearDown()
|
|
|
|
{
|
|
|
|
$this->walker = null;
|
|
|
|
$this->metadataFactory = null;
|
|
|
|
$this->context = null;
|
|
|
|
}
|
|
|
|
|
2011-05-08 22:52:34 +01:00
|
|
|
public function testClone()
|
|
|
|
{
|
|
|
|
$clone = clone $this->context;
|
|
|
|
|
|
|
|
$this->assertNotSame($this->context, $clone);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddViolation()
|
|
|
|
{
|
2012-01-18 13:42:47 +00:00
|
|
|
$this->assertCount(0, $this->context->getViolations());
|
2011-05-08 22:52:34 +01:00
|
|
|
$this->context->addViolation('', array(), '');
|
|
|
|
|
2012-01-18 13:42:47 +00:00
|
|
|
$this->assertCount(1, $this->context->getViolations());
|
2011-05-08 22:52:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetViolations()
|
|
|
|
{
|
|
|
|
$this->context->addViolation('', array(), '');
|
|
|
|
|
2011-06-14 09:40:42 +01:00
|
|
|
$violations = $this->context->getViolations();
|
|
|
|
|
2012-01-18 13:42:47 +00:00
|
|
|
$this->assertCount(1, $violations);
|
2011-06-14 09:40:42 +01:00
|
|
|
$this->assertInstanceOf('Symfony\Component\Validator\ConstraintViolationList', $violations);
|
|
|
|
|
|
|
|
$this->assertInstanceOf('ArrayIterator', $violations->getIterator());
|
|
|
|
|
|
|
|
$this->assertTrue(isset($violations[0]));
|
|
|
|
$this->assertFalse(isset($violations[1]));
|
|
|
|
|
|
|
|
$violations[] = 'fake';
|
|
|
|
$this->assertEquals('fake', $violations[1]);
|
|
|
|
$this->assertTrue(isset($violations[1]));
|
|
|
|
|
|
|
|
unset($violations[1]);
|
|
|
|
$this->assertFalse(isset($violations[1]));
|
|
|
|
|
|
|
|
$violations[0] = 'fake';
|
|
|
|
$this->assertEquals('fake', $violations[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testViolationsMerge()
|
|
|
|
{
|
|
|
|
$this->context->addViolation('Message 1', array(), '');
|
|
|
|
$this->context->addViolation('Message 2', array(), '');
|
|
|
|
|
|
|
|
$violations1 = $this->context->getViolations();
|
|
|
|
|
|
|
|
$this->context->addViolation('', array(), '');
|
|
|
|
|
|
|
|
$violations2 = $this->context->getViolations();
|
|
|
|
unset($violations2[1]);
|
|
|
|
|
|
|
|
$violations1->addAll($violations2);
|
|
|
|
|
|
|
|
$this->assertEmpty($violations1[2]->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testViolationsAsString()
|
|
|
|
{
|
|
|
|
$this->context->addViolation('Message 1', array(), '');
|
|
|
|
$this->context->addViolation('Message 2', array(), '');
|
|
|
|
|
|
|
|
$violations = $this->context->getViolations();
|
|
|
|
|
|
|
|
$expected = <<<EOF
|
|
|
|
Root.:
|
|
|
|
Message 1
|
|
|
|
Root.:
|
|
|
|
Message 2
|
|
|
|
|
|
|
|
EOF;
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $violations->__toString());
|
2011-05-08 22:52:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetRoot()
|
|
|
|
{
|
|
|
|
$this->assertEquals('Root', $this->context->getRoot());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetGetPropertyPath()
|
|
|
|
{
|
|
|
|
$this->context->setPropertyPath('property_path');
|
|
|
|
|
|
|
|
$this->assertEquals('property_path', $this->context->getPropertyPath());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetGetCurrentClass()
|
|
|
|
{
|
|
|
|
$this->context->setCurrentClass('current_class');
|
|
|
|
|
|
|
|
$this->assertEquals('current_class', $this->context->getCurrentClass());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetGetCurrentProperty()
|
|
|
|
{
|
|
|
|
$this->context->setCurrentProperty('current_property');
|
|
|
|
|
|
|
|
$this->assertEquals('current_property', $this->context->getCurrentProperty());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetGetGroup()
|
|
|
|
{
|
|
|
|
$this->context->setGroup('group');
|
|
|
|
|
|
|
|
$this->assertEquals('group', $this->context->getGroup());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetGraphWalker()
|
|
|
|
{
|
|
|
|
$this->assertSame($this->walker, $this->context->getGraphWalker());
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
'Symfony\Component\Validator\GraphWalker',
|
|
|
|
$this->context->getGraphWalker()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetMetadataFactory()
|
|
|
|
{
|
|
|
|
$this->assertSame($this->metadataFactory, $this->context->getMetadataFactory());
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
'Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface',
|
|
|
|
$this->context->getMetadataFactory()
|
|
|
|
);
|
|
|
|
}
|
2011-06-08 18:56:59 +01:00
|
|
|
}
|