-
Notifications
You must be signed in to change notification settings - Fork 791
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/serializer-transformer' into ser…
…ializer-integration Conflicts: DependencyInjection/FOSElasticaExtension.php Resources/config/config.xml
- Loading branch information
Showing
7 changed files
with
351 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
namespace FOS\ElasticaBundle\Persister; | ||
|
||
use Elastica\Type; | ||
use FOS\ElasticaBundle\Transformer\ModelToElasticaTransformerInterface; | ||
|
||
/** | ||
* Inserts, replaces and deletes single objects in an elastica type, making use | ||
* of elastica's serializer support to convert objects in to elastica documents. | ||
* Accepts domain model objects and passes them directly to elastica | ||
* | ||
* @author Lea Haensenberber <lea.haensenberger@gmail.com> | ||
*/ | ||
class ObjectSerializerPersister extends ObjectPersister | ||
{ | ||
public function __construct(Type $type, ModelToElasticaTransformerInterface $transformer, $objectClass) | ||
{ | ||
$this->type = $type; | ||
$this->transformer = $transformer; | ||
$this->objectClass = $objectClass; | ||
} | ||
|
||
/** | ||
* Insert one object into the type | ||
* The object will be transformed to an elastica document | ||
* | ||
* @param object $object | ||
*/ | ||
public function insertOne($object) | ||
{ | ||
$document = $this->transformToElasticaDocument($object); | ||
$this->type->addObject($object, $document); | ||
} | ||
|
||
/** | ||
* Replaces one object in the type | ||
* | ||
* @param object $object | ||
* @return null | ||
*/ | ||
public function replaceOne($object) | ||
{ | ||
$document = $this->transformToElasticaDocument($object); | ||
$this->type->deleteById($document->getId()); | ||
$this->type->addObject($object, $document); | ||
} | ||
|
||
/** | ||
* Deletes one object in the type | ||
* | ||
* @param object $object | ||
* @return null | ||
**/ | ||
public function deleteOne($object) | ||
{ | ||
$document = $this->transformToElasticaDocument($object); | ||
$this->type->deleteById($document->getId()); | ||
} | ||
|
||
/** | ||
* Deletes one object in the type by id | ||
* | ||
* @param mixed $id | ||
* | ||
* @return null | ||
**/ | ||
public function deleteById($id) | ||
{ | ||
$this->type->deleteById($id); | ||
} | ||
|
||
|
||
/** | ||
* Inserts an array of objects in the type | ||
* | ||
* @param array $objects array of domain model objects | ||
**/ | ||
public function insertMany(array $objects) | ||
{ | ||
foreach ($objects as $object) { | ||
$this->insertOne($object); | ||
} | ||
} | ||
|
||
/** | ||
* Transforms an object to an elastica document | ||
* with just the identifier set | ||
* | ||
* @param object $object | ||
* @return Document the elastica document | ||
*/ | ||
public function transformToElasticaDocument($object) | ||
{ | ||
return $this->transformer->transform($object, array()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
|
||
namespace FOS\ElasticaBundle\Tests\ObjectSerializerPersister; | ||
|
||
use FOS\ElasticaBundle\Persister\ObjectPersister; | ||
use FOS\ElasticaBundle\Persister\ObjectSerializerPersister; | ||
use FOS\ElasticaBundle\Transformer\ModelToElasticaAutoTransformer; | ||
use FOS\ElasticaBundle\Transformer\ModelToElasticaIdentifierTransformer; | ||
use Symfony\Component\PropertyAccess\PropertyAccess; | ||
|
||
class POPO | ||
{ | ||
public $id = 123; | ||
public $name = 'popoName'; | ||
|
||
public function getId() | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getName() | ||
{ | ||
return $this->name; | ||
} | ||
} | ||
|
||
class ObjectSerializerPersisterTest 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('addObject'); | ||
|
||
$objectPersister = new ObjectSerializerPersister($typeMock, $transformer, 'SomeClass'); | ||
$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('addObject'); | ||
|
||
$objectPersister = new ObjectSerializerPersister($typeMock, $transformer, 'SomeClass'); | ||
$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('addObject'); | ||
|
||
$objectPersister = new ObjectSerializerPersister($typeMock, $transformer, 'SomeClass'); | ||
$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->exactly(2)) | ||
->method('addObject'); | ||
$typeMock->expects($this->never()) | ||
->method('addObjects'); | ||
|
||
$objectPersister = new ObjectSerializerPersister($typeMock, $transformer, 'SomeClass'); | ||
$objectPersister->insertMany(array(new POPO(), new POPO())); | ||
} | ||
|
||
/** | ||
* @return ModelToElasticaIdentifierTransformer | ||
*/ | ||
private function getTransformer() | ||
{ | ||
$transformer = new ModelToElasticaIdentifierTransformer(); | ||
$transformer->setPropertyAccessor(PropertyAccess::getPropertyAccessor()); | ||
|
||
return $transformer; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.