FOSElasticaBundle/Tests/Persister/ObjectPersisterTest.php
Jeremy Mikola 2e7d2f2452 Support Symfony 2.2 PropertyAccess with BC for 2.1 (closes #218)
By abstracting the property access, we can easily support Symfony 2.1's Form component and 2.2's PropertyAccess component.

The PropertyAccessor service will be injected into transformers if it is available. Tests were modified to do the same, and expect the appropriate exception depending on which implementation is available.
2013-03-27 17:26:47 -04:00

224 lines
7 KiB
PHP

<?php
namespace FOS\ElasticaBundle\Tests\ObjectPersister;
use FOS\ElasticaBundle\Persister\ObjectPersister;
use FOS\ElasticaBundle\Transformer\ModelToElasticaAutoTransformer;
use Symfony\Component\PropertyAccess\PropertyAccess;
class POPO
{
public $id = 123;
public function getId()
{
return $this->id;
}
public function getName()
{
return 'popoName';
}
}
class InvalidObjectPersister extends ObjectPersister
{
public function transformToElasticaDocument($object)
{
throw new \BadMethodCallException('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()
{
$transformer = $this->getTransformer();
/** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\Elastica_Type */
$typeMock = $this->getMockBuilder('Elastica_Type')
->disableOriginalConstructor()
->getMock();
$typeMock->expects($this->once())
->method('deleteById')
->with($this->equalTo(123));
$typeMock->expects($this->once())
->method('addDocument');
$fields = array('name' => array());
$objectPersister = new ObjectPersister($typeMock, $transformer, 'SomeClass', $fields);
$objectPersister->replaceOne(new POPO());
}
/**
* @expectedException \BadMethodCallException
*/
public function testThatErrorIsHandledWhenCannotReplaceObject()
{
$transformer = $this->getTransformer();
/** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\Elastica_Type */
$typeMock = $this->getMockBuilder('Elastica_Type')
->disableOriginalConstructor()
->getMock();
$typeMock->expects($this->never())
->method('deleteById');
$typeMock->expects($this->never())
->method('addDocument');
$fields = array('name' => array());
$objectPersister = new InvalidObjectPersister($typeMock, $transformer, 'SomeClass', $fields);
$objectPersister->replaceOne(new POPO());
}
public function testThatCanInsertObject()
{
$transformer = $this->getTransformer();
/** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\Elastica_Type */
$typeMock = $this->getMockBuilder('Elastica_Type')
->disableOriginalConstructor()
->getMock();
$typeMock->expects($this->never())
->method('deleteById');
$typeMock->expects($this->once())
->method('addDocument');
$fields = array('name' => array());
$objectPersister = new ObjectPersister($typeMock, $transformer, 'SomeClass', $fields);
$objectPersister->insertOne(new POPO());
}
/**
* @expectedException \BadMethodCallException
*/
public function testThatErrorIsHandledWhenCannotInsertObject()
{
$transformer = $this->getTransformer();
/** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\Elastica_Type */
$typeMock = $this->getMockBuilder('Elastica_Type')
->disableOriginalConstructor()
->getMock();
$typeMock->expects($this->never())
->method('deleteById');
$typeMock->expects($this->never())
->method('addDocument');
$fields = array('name' => array());
$objectPersister = new InvalidObjectPersister($typeMock, $transformer, 'SomeClass', $fields);
$objectPersister->insertOne(new POPO());
}
public function testThatCanDeleteObject()
{
$transformer = $this->getTransformer();
/** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\Elastica_Type */
$typeMock = $this->getMockBuilder('Elastica_Type')
->disableOriginalConstructor()
->getMock();
$typeMock->expects($this->once())
->method('deleteById');
$typeMock->expects($this->never())
->method('addDocument');
$fields = array('name' => array());
$objectPersister = new ObjectPersister($typeMock, $transformer, 'SomeClass', $fields);
$objectPersister->deleteOne(new POPO());
}
/**
* @expectedException \BadMethodCallException
*/
public function testThatErrorIsHandledWhenCannotDeleteObject()
{
$transformer = $this->getTransformer();
/** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\Elastica_Type */
$typeMock = $this->getMockBuilder('Elastica_Type')
->disableOriginalConstructor()
->getMock();
$typeMock->expects($this->never())
->method('deleteById');
$typeMock->expects($this->never())
->method('addDocument');
$fields = array('name' => array());
$objectPersister = new InvalidObjectPersister($typeMock, $transformer, 'SomeClass', $fields);
$objectPersister->deleteOne(new POPO());
}
public function testThatCanInsertManyObjects()
{
$transformer = $this->getTransformer();
/** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\Elastica_Type */
$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');
$fields = array('name' => array());
$objectPersister = new ObjectPersister($typeMock, $transformer, 'SomeClass', $fields);
$objectPersister->insertMany(array(new POPO(), new POPO()));
}
/**
* @expectedException \BadMethodCallException
*/
public function testThatErrorIsHandledWhenCannotInsertManyObject()
{
$transformer = $this->getTransformer();
/** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\Elastica_Type */
$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');
$fields = array('name' => array());
$objectPersister = new InvalidObjectPersister($typeMock, $transformer, 'SomeClass', $fields);
$objectPersister->insertMany(array(new POPO(), new POPO()));
}
/**
* @return ModelToElasticaAutoTransformer
*/
private function getTransformer()
{
$transformer = new ModelToElasticaAutoTransformer();
if (class_exists('Symfony\Component\PropertyAccess\PropertyAccess')) {
$transformer->setPropertyAccessor(PropertyAccess::getPropertyAccessor());
}
return $transformer;
}
}