/** * Returns false if there is another object with the same field values but other identifiers. * * @access public * @param mixed $value * @param array $context ,default is null * @return boolean is valid or not */ public function isValid($value, $context = null) { if (count($this->fields) > 1) { $value = $context; } return parent::isValid($value, $context); }
public function testCanFetchIdentifierFromObjectContext() { $context = new stdClass(); $context->id = 'identifier'; $match = new stdClass(); $classMetadata = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata'); $classMetadata->expects($this->at(0))->method('getIdentifierValues')->with($context)->will($this->returnValue(array('id' => 'identifier'))); $classMetadata->expects($this->at(1))->method('getIdentifierValues')->with($match)->will($this->returnValue(array('id' => 'identifier'))); $objectManager = $this->getMock('Doctrine\\Common\\Persistence\\ObjectManager'); $objectManager->expects($this->any())->method('getClassMetadata')->with('stdClass')->will($this->returnValue($classMetadata)); $repository = $this->getMock('Doctrine\\Common\\Persistence\\ObjectRepository'); $repository->expects($this->any())->method('getClassName')->will($this->returnValue('stdClass')); $repository->expects($this->once())->method('findOneBy')->with(array('matchKey' => 'matchValue'))->will($this->returnValue($match)); $validator = new UniqueObject(array('object_repository' => $repository, 'object_manager' => $objectManager, 'fields' => 'matchKey', 'use_context' => true)); $this->assertTrue($validator->isValid('matchValue', $context)); }
public function testErrorMessageIsStringInsteadArray() { $match = new stdClass(); $classMetadata = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata'); $classMetadata->expects($this->once())->method('getIdentifierFieldNames')->will($this->returnValue(array('id'))); $classMetadata->expects($this->once())->method('getIdentifierValues')->with($match)->will($this->returnValue(array('id' => 'identifier'))); $objectManager = $this->getMock('Doctrine\\Common\\Persistence\\ObjectManager'); $objectManager->expects($this->any())->method('getClassMetadata')->with('stdClass')->will($this->returnValue($classMetadata)); $repository = $this->getMock('Doctrine\\Common\\Persistence\\ObjectRepository'); $repository->expects($this->any())->method('getClassName')->will($this->returnValue('stdClass')); $repository->expects($this->once())->method('findOneBy')->with(array('matchKey' => 'matchValue'))->will($this->returnValue($match)); $validator = new UniqueObject(array('object_repository' => $repository, 'object_manager' => $objectManager, 'fields' => 'matchKey', 'use_context' => true)); $this->assertFalse($validator->isValid('matchValue', array('matchKey' => 'matchValue', 'id' => 'another identifier'))); $messageTemplates = $validator->getMessageTemplates(); $expectedMessage = str_replace('%value%', 'matchValue', $messageTemplates[UniqueObject::ERROR_OBJECT_NOT_UNIQUE]); $messages = $validator->getMessages(); $receivedMessage = $messages[UniqueObject::ERROR_OBJECT_NOT_UNIQUE]; $this->assertTrue($expectedMessage == $receivedMessage); }
/** * @expectedException Zend\Validator\Exception\RuntimeException * @expectedExceptionMessage Expected context to contain id */ public function testThrowsAnExceptionOnMissingIdentifierInContext() { $match = new stdClass(); $classMetadata = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata'); $classMetadata->expects($this->once())->method('getIdentifierFieldNames')->will($this->returnValue(array('id'))); $objectManager = $this->getMock('Doctrine\\Common\\Persistence\\ObjectManager'); $objectManager->expects($this->any())->method('getClassMetadata')->with('stdClass')->will($this->returnValue($classMetadata)); $repository = $this->getMock('Doctrine\\Common\\Persistence\\ObjectRepository'); $repository->expects($this->any())->method('getClassName')->will($this->returnValue('stdClass')); $repository->expects($this->once())->method('findOneBy')->with(array('matchKey' => 'matchValue'))->will($this->returnValue($match)); $validator = new UniqueObject(array('object_repository' => $repository, 'object_manager' => $objectManager, 'fields' => 'matchKey')); $validator->isValid('matchValue', array()); }