mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-23 09:53:15 +00:00
Add geoname validation constraint
This commit is contained in:
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