mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-17 15:03:25 +00:00
Add geoname validation constraint
This commit is contained in:
43
lib/Alchemy/Phrasea/Form/Constraint/Geoname.php
Normal file
43
lib/Alchemy/Phrasea/Form/Constraint/Geoname.php
Normal 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;
|
||||
}
|
||||
}
|
28
lib/Alchemy/Phrasea/Form/Constraint/GeonameValidator.php
Normal file
28
lib/Alchemy/Phrasea/Form/Constraint/GeonameValidator.php
Normal 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.'));
|
||||
}
|
||||
}
|
||||
}
|
54
tests/Alchemy/Tests/Phrasea/Form/Constraint/GeonameTest.php
Normal file
54
tests/Alchemy/Tests/Phrasea/Form/Constraint/GeonameTest.php
Normal 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();
|
||||
}
|
||||
}
|
@@ -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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user