Add geoname validation constraint

This commit is contained in:
Romain Neutron
2013-07-02 15:01:44 +02:00
parent 4720a46b66
commit 8ca7aa4fac
4 changed files with 176 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
<?php
namespace Alchemy\Tests\Phrasea\Form\Constraint;
use Alchemy\Phrasea\Form\Constraint\Geoname;
use Alchemy\Geonames\Geoname as GeonameResult;
use Alchemy\Geonames\Exception\NotFoundException;
use Alchemy\Geonames\Exception\TransportException;
class GeonameTest extends \PhraseanetPHPUnitAbstract
{
public function testAValidGeonameIsValid()
{
$connector = $this->getConnectorMock();
$connector->expects($this->once())
->method('geoname')
->with(123456)
->will($this->returnValue(new GeonameResult(array())));
$constraint = new Geoname($connector);
$this->assertTrue($constraint->isValid(123456));
}
public function testATransportErrorIsIgnored()
{
$connector = $this->getConnectorMock();
$connector->expects($this->once())
->method('geoname')
->with(123456)
->will($this->throwException(new TransportException()));
$constraint = new Geoname($connector);
$this->assertTrue($constraint->isValid(123456));
}
public function testAResourceNotFoundReturnFalse()
{
$connector = $this->getConnectorMock();
$connector->expects($this->once())
->method('geoname')
->with(123456)
->will($this->throwException(new NotFoundException()));
$constraint = new Geoname($connector);
$this->assertFalse($constraint->isValid(123456));
}
private function getConnectorMock()
{
return $this->getMockBuilder('Alchemy\Geonames\Connector')
->disableOriginalConstructor()
->getMock();
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace Alchemy\Tests\Phrasea\Form\Constraint;
use Alchemy\Phrasea\Form\Constraint\GeonameValidator;
class GeonameValidatorTest extends \PhraseanetPHPUnitAbstract
{
/**
* @dataProvider provideData
*/
public function testValidate($valid)
{
$context = $this->getMock('Symfony\Component\Validator\ExecutionContextInterface');
$builder = $context
->expects($this->exactly($valid ? 0 : 1))
->method('addViolation');
if (!$valid) {
$builder->with($this->isType('string'));
}
$validator = new GeonameValidator();
$validator->initialize($context);
$constraint = $this->getConstraint();
$constraint
->expects($this->once())
->method('isValid')
->with(123456)
->will($this->returnValue($valid));
$validator->validate(123456, $constraint);
}
public function provideData()
{
return array(
array(true),
array(false),
);
}
private function getConstraint()
{
return $this
->getMockBuilder('Alchemy\Phrasea\Form\Constraint\Geoname')
->disableOriginalConstructor()
->getMock();
}
}