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,43 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2013 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Form\Constraint;
use Symfony\Component\Validator\Constraint;
use Alchemy\Geonames\Connector;
use Alchemy\Geonames\Exception\TransportException;
use Alchemy\Geonames\Exception\NotFoundException;
class Geoname extends Constraint
{
private $connector;
private $message;
public function __construct(Connector $connector)
{
$this->message = _('This place does not seem to exist.');
$this->connector = $connector;
parent::__construct();
}
public function isValid($geonameid)
{
try {
$this->connector->geoname($geonameid);
} catch (TransportException $e) {
return true;
} catch (NotFoundException $e) {
return false;
}
return true;
}
}

View File

@@ -0,0 +1,28 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2013 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Form\Constraint;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class GeonameValidator extends ConstraintValidator
{
/**
* {@inheritDoc}
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint->isValid($value)) {
$this->context->addViolation(_('This place does not seem to exist.'));
}
}
}

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();
}
}