diff --git a/lib/Alchemy/Phrasea/Form/Constraint/Geoname.php b/lib/Alchemy/Phrasea/Form/Constraint/Geoname.php new file mode 100644 index 0000000000..ace2f3529d --- /dev/null +++ b/lib/Alchemy/Phrasea/Form/Constraint/Geoname.php @@ -0,0 +1,43 @@ +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; + } +} diff --git a/lib/Alchemy/Phrasea/Form/Constraint/GeonameValidator.php b/lib/Alchemy/Phrasea/Form/Constraint/GeonameValidator.php new file mode 100644 index 0000000000..899f0c66fe --- /dev/null +++ b/lib/Alchemy/Phrasea/Form/Constraint/GeonameValidator.php @@ -0,0 +1,28 @@ +isValid($value)) { + $this->context->addViolation(_('This place does not seem to exist.')); + } + } +} diff --git a/tests/Alchemy/Tests/Phrasea/Form/Constraint/GeonameTest.php b/tests/Alchemy/Tests/Phrasea/Form/Constraint/GeonameTest.php new file mode 100644 index 0000000000..c7ec707b9e --- /dev/null +++ b/tests/Alchemy/Tests/Phrasea/Form/Constraint/GeonameTest.php @@ -0,0 +1,54 @@ +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(); + } +} diff --git a/tests/Alchemy/Tests/Phrasea/Form/Constraint/GeonameValidatorTest.php b/tests/Alchemy/Tests/Phrasea/Form/Constraint/GeonameValidatorTest.php new file mode 100644 index 0000000000..288681bb0b --- /dev/null +++ b/tests/Alchemy/Tests/Phrasea/Form/Constraint/GeonameValidatorTest.php @@ -0,0 +1,51 @@ +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(); + } +}