Added Object Persister Test. Made test skipped when elastica library classes are not found.

This commit is contained in:
Leszek 2011-07-28 22:37:54 +02:00
parent c430aeaea1
commit b06c268f05
4 changed files with 239 additions and 2 deletions

View file

@ -8,6 +8,13 @@ use Elastica_Index;
class MappingRegistryTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
if (!class_exists('Elastica_Type') || !class_exists('Elastica_Index')) {
$this->markTestSkipped('The Elastica library classes are not available');
}
}
public function testThatCanApplyMappings()
{
$typeMock = $this->getMockBuilder('Elastica_Type')

View file

@ -0,0 +1,224 @@
<?php
namespace FOQ\ElasticaBundle\Tests\ObjectPersister;
use FOQ\ElasticaBundle\Persister\ObjectPersister;
use FOQ\ElasticaBundle\Transformer\ModelToElasticaAutoTransformer;
class POPO
{
public $id = 123;
public function getId()
{
return $this->id;
}
public function getName()
{
return 'popoName';
}
}
class InvalidObjectPersister extends ObjectPersister
{
protected function transformToElasticaDocument($object)
{
throw new \Exception('Invalid transformation');
}
}
class ObjectPersisterTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
if (!class_exists('Elastica_Type')) {
$this->markTestSkipped('The Elastica library classes are not available');
}
}
public function testThatCanReplaceObject()
{
$modelTransformer = new ModelToElasticaAutoTransformer();
$typeMock = $this->getMockBuilder('Elastica_Type')
->disableOriginalConstructor()
->getMock();
$typeMock->expects($this->once())
->method('deleteById')
->with($this->equalTo(123));
$typeMock->expects($this->once())
->method('addDocument');
$mappingMock = $this->getMockBuilder('FOQ\ElasticaBundle\MappingRegistry')
->disableOriginalConstructor()
->getMock();
$mappingMock->expects($this->once())
->method('getTypeFieldNames')
->will($this->returnValue(array('name')));
$objectPersister = new ObjectPersister($typeMock, $modelTransformer, 'SomeClass', $mappingMock);
$objectPersister->replaceOne(new POPO());
}
/**
* @expectedException Exception
*/
public function testThatErrorIsHandledWhenCannotReplaceObject()
{
$modelTransformer = new ModelToElasticaAutoTransformer();
$mappingMock = $this->getMockBuilder('FOQ\ElasticaBundle\MappingRegistry')
->disableOriginalConstructor()
->getMock();
$typeMock = $this->getMockBuilder('Elastica_Type')
->disableOriginalConstructor()
->getMock();
$typeMock->expects($this->never())
->method('deleteById');
$typeMock->expects($this->never())
->method('addDocument');
$objectPersister = new InvalidObjectPersister($typeMock, $modelTransformer, 'SomeClass', $mappingMock);
$objectPersister->replaceOne(new POPO());
}
public function testThatCanInsertObject()
{
$modelTransformer = new ModelToElasticaAutoTransformer();
$typeMock = $this->getMockBuilder('Elastica_Type')
->disableOriginalConstructor()
->getMock();
$typeMock->expects($this->never())
->method('deleteById');
$typeMock->expects($this->once())
->method('addDocument');
$mappingMock = $this->getMockBuilder('FOQ\ElasticaBundle\MappingRegistry')
->disableOriginalConstructor()
->getMock();
$mappingMock->expects($this->once())
->method('getTypeFieldNames')
->will($this->returnValue(array('name')));
$objectPersister = new ObjectPersister($typeMock, $modelTransformer, 'SomeClass', $mappingMock);
$objectPersister->insertOne(new POPO());
}
/**
* @expectedException Exception
*/
public function testThatErrorIsHandledWhenCannotInsertObject()
{
$modelTransformer = new ModelToElasticaAutoTransformer();
$typeMock = $this->getMockBuilder('Elastica_Type')
->disableOriginalConstructor()
->getMock();
$typeMock->expects($this->never())
->method('deleteById');
$typeMock->expects($this->never())
->method('addDocument');
$objectPersister = new InvalidObjectPersister($typeMock, $modelTransformer, 'SomeClass', $mappingMock);
$objectPersister->insertOne(new POPO());
}
public function testThatCanDeleteObject()
{
$modelTransformer = new ModelToElasticaAutoTransformer();
$typeMock = $this->getMockBuilder('Elastica_Type')
->disableOriginalConstructor()
->getMock();
$typeMock->expects($this->once())
->method('deleteById');
$typeMock->expects($this->never())
->method('addDocument');
$mappingMock = $this->getMockBuilder('FOQ\ElasticaBundle\MappingRegistry')
->disableOriginalConstructor()
->getMock();
$mappingMock->expects($this->any())
->method('getTypeFieldNames')
->will($this->returnValue(array('name')));
$objectPersister = new ObjectPersister($typeMock, $modelTransformer, 'SomeClass', $mappingMock);
$objectPersister->deleteOne(new POPO());
}
/**
* @expectedException Exception
*/
public function testThatErrorIsHandledWhenCannotDeleteObject()
{
$modelTransformer = new ModelToElasticaAutoTransformer();
$typeMock = $this->getMockBuilder('Elastica_Type')
->disableOriginalConstructor()
->getMock();
$typeMock->expects($this->never())
->method('deleteById');
$typeMock->expects($this->never())
->method('addDocument');
$mappingMock = $this->getMockBuilder('FOQ\ElasticaBundle\MappingRegistry')
->disableOriginalConstructor()
->getMock();
$objectPersister = new InvalidObjectPersister($typeMock, $modelTransformer, 'SomeClass', $mappingMock);
$objectPersister->deleteOne(new POPO());
}
public function testThatCanInsertManyObjects()
{
$modelTransformer = new ModelToElasticaAutoTransformer();
$typeMock = $this->getMockBuilder('Elastica_Type')
->disableOriginalConstructor()
->getMock();
$typeMock->expects($this->never())
->method('deleteById');
$typeMock->expects($this->never())
->method('addDocument');
$typeMock->expects($this->once())
->method('addDocuments');
$mappingMock = $this->getMockBuilder('FOQ\ElasticaBundle\MappingRegistry')
->disableOriginalConstructor()
->getMock();
$mappingMock->expects($this->any())
->method('getTypeFieldNames')
->will($this->returnValue(array('name')));
$objectPersister = new ObjectPersister($typeMock, $modelTransformer, 'SomeClass', $mappingMock);
$objectPersister->insertMany(array(new POPO(), new POPO()));
}
/**
* @expectedException Exception
*/
public function testThatErrorIsHandledWhenCannotInsertManyObject()
{
$modelTransformer = new ModelToElasticaAutoTransformer();
$typeMock = $this->getMockBuilder('Elastica_Type')
->disableOriginalConstructor()
->getMock();
$typeMock->expects($this->never())
->method('deleteById');
$typeMock->expects($this->never())
->method('addDocument');
$typeMock->expects($this->never())
->method('addDocuments');
$mappingMock = $this->getMockBuilder('FOQ\ElasticaBundle\MappingRegistry')
->disableOriginalConstructor()
->getMock();
$objectPersister = new InvalidObjectPersister($typeMock, $modelTransformer, 'SomeClass', $mappingMock);
$objectPersister->insertMany(array(new POPO(), new POPO()));
}
}

View file

@ -45,8 +45,7 @@ class ReseterTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
if (!class_exists('Elastica_Exception_Response') || !class_exists('Elastica_Response'))
{
if (!class_exists('Elastica_Exception_Response') || !class_exists('Elastica_Response')) {
$this->markTestSkipped('The Elastica library classes are not available');
}
}

View file

@ -36,6 +36,13 @@ class POPO
class ModelToElasticaAutoTransformerTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
if (!class_exists('Elastica_Document')) {;
$this->markTestSkipped('The Elastica library classes are not available');
}
}
public function testThatCanTransformObject()
{
$transformer = new ModelToElasticaAutoTransformer();