Skip to content

Commit

Permalink
Merge pull request #255 from FriendsOfSymfony/property-access
Browse files Browse the repository at this point in the history
Support Symfony 2.2 PropertyAccess with BC for 2.1 (closes #218)
  • Loading branch information
jmikola committed Mar 27, 2013
2 parents 91090ed + 2e7d2f2 commit 8146f8e
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 54 deletions.
3 changes: 3 additions & 0 deletions Resources/config/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@

<service id="fos_elastica.model_to_elastica_transformer.prototype.auto" class="FOS\ElasticaBundle\Transformer\ModelToElasticaAutoTransformer" public="false" abstract="true">
<argument /> <!-- options -->
<call method="setPropertyAccessor">
<argument type="service" id="property_accessor" on-invalid="null" />
</call>
</service>

<service id="fos_elastica.elastica_to_model_transformer.collection.prototype" class="%fos_elastica.elastica_to_model_transformer.collection.class%" public="true" abstract="true">
Expand Down
47 changes: 31 additions & 16 deletions Tests/Persister/ObjectPersisterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use FOS\ElasticaBundle\Persister\ObjectPersister;
use FOS\ElasticaBundle\Transformer\ModelToElasticaAutoTransformer;
use Symfony\Component\PropertyAccess\PropertyAccess;

class POPO
{
Expand Down Expand Up @@ -39,7 +40,7 @@ public function setUp()

public function testThatCanReplaceObject()
{
$modelTransformer = new ModelToElasticaAutoTransformer();
$transformer = $this->getTransformer();

/** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\Elastica_Type */
$typeMock = $this->getMockBuilder('Elastica_Type')
Expand All @@ -53,7 +54,7 @@ public function testThatCanReplaceObject()

$fields = array('name' => array());

$objectPersister = new ObjectPersister($typeMock, $modelTransformer, 'SomeClass', $fields);
$objectPersister = new ObjectPersister($typeMock, $transformer, 'SomeClass', $fields);
$objectPersister->replaceOne(new POPO());
}

Expand All @@ -62,7 +63,7 @@ public function testThatCanReplaceObject()
*/
public function testThatErrorIsHandledWhenCannotReplaceObject()
{
$modelTransformer = new ModelToElasticaAutoTransformer();
$transformer = $this->getTransformer();

/** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\Elastica_Type */
$typeMock = $this->getMockBuilder('Elastica_Type')
Expand All @@ -75,13 +76,13 @@ public function testThatErrorIsHandledWhenCannotReplaceObject()

$fields = array('name' => array());

$objectPersister = new InvalidObjectPersister($typeMock, $modelTransformer, 'SomeClass', $fields);
$objectPersister = new InvalidObjectPersister($typeMock, $transformer, 'SomeClass', $fields);
$objectPersister->replaceOne(new POPO());
}

public function testThatCanInsertObject()
{
$modelTransformer = new ModelToElasticaAutoTransformer();
$transformer = $this->getTransformer();

/** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\Elastica_Type */
$typeMock = $this->getMockBuilder('Elastica_Type')
Expand All @@ -94,7 +95,7 @@ public function testThatCanInsertObject()

$fields = array('name' => array());

$objectPersister = new ObjectPersister($typeMock, $modelTransformer, 'SomeClass', $fields);
$objectPersister = new ObjectPersister($typeMock, $transformer, 'SomeClass', $fields);
$objectPersister->insertOne(new POPO());
}

Expand All @@ -103,7 +104,7 @@ public function testThatCanInsertObject()
*/
public function testThatErrorIsHandledWhenCannotInsertObject()
{
$modelTransformer = new ModelToElasticaAutoTransformer();
$transformer = $this->getTransformer();

/** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\Elastica_Type */
$typeMock = $this->getMockBuilder('Elastica_Type')
Expand All @@ -116,13 +117,13 @@ public function testThatErrorIsHandledWhenCannotInsertObject()

$fields = array('name' => array());

$objectPersister = new InvalidObjectPersister($typeMock, $modelTransformer, 'SomeClass', $fields);
$objectPersister = new InvalidObjectPersister($typeMock, $transformer, 'SomeClass', $fields);
$objectPersister->insertOne(new POPO());
}

public function testThatCanDeleteObject()
{
$modelTransformer = new ModelToElasticaAutoTransformer();
$transformer = $this->getTransformer();

/** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\Elastica_Type */
$typeMock = $this->getMockBuilder('Elastica_Type')
Expand All @@ -135,7 +136,7 @@ public function testThatCanDeleteObject()

$fields = array('name' => array());

$objectPersister = new ObjectPersister($typeMock, $modelTransformer, 'SomeClass', $fields);
$objectPersister = new ObjectPersister($typeMock, $transformer, 'SomeClass', $fields);
$objectPersister->deleteOne(new POPO());
}

Expand All @@ -144,7 +145,7 @@ public function testThatCanDeleteObject()
*/
public function testThatErrorIsHandledWhenCannotDeleteObject()
{
$modelTransformer = new ModelToElasticaAutoTransformer();
$transformer = $this->getTransformer();

/** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\Elastica_Type */
$typeMock = $this->getMockBuilder('Elastica_Type')
Expand All @@ -157,13 +158,13 @@ public function testThatErrorIsHandledWhenCannotDeleteObject()

$fields = array('name' => array());

$objectPersister = new InvalidObjectPersister($typeMock, $modelTransformer, 'SomeClass', $fields);
$objectPersister = new InvalidObjectPersister($typeMock, $transformer, 'SomeClass', $fields);
$objectPersister->deleteOne(new POPO());
}

public function testThatCanInsertManyObjects()
{
$modelTransformer = new ModelToElasticaAutoTransformer();
$transformer = $this->getTransformer();

/** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\Elastica_Type */
$typeMock = $this->getMockBuilder('Elastica_Type')
Expand All @@ -178,7 +179,7 @@ public function testThatCanInsertManyObjects()

$fields = array('name' => array());

$objectPersister = new ObjectPersister($typeMock, $modelTransformer, 'SomeClass', $fields);
$objectPersister = new ObjectPersister($typeMock, $transformer, 'SomeClass', $fields);
$objectPersister->insertMany(array(new POPO(), new POPO()));
}

Expand All @@ -187,7 +188,7 @@ public function testThatCanInsertManyObjects()
*/
public function testThatErrorIsHandledWhenCannotInsertManyObject()
{
$modelTransformer = new ModelToElasticaAutoTransformer();
$transformer = $this->getTransformer();

/** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\Elastica_Type */
$typeMock = $this->getMockBuilder('Elastica_Type')
Expand All @@ -202,7 +203,21 @@ public function testThatErrorIsHandledWhenCannotInsertManyObject()

$fields = array('name' => array());

$objectPersister = new InvalidObjectPersister($typeMock, $modelTransformer, 'SomeClass', $fields);
$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;
}
}
51 changes: 35 additions & 16 deletions Tests/Transformer/ModelToElasticaAutoTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace FOS\ElasticaBundle\Tests\Transformer\ModelToElasticaAutoTransformer;

use FOS\ElasticaBundle\Transformer\ModelToElasticaAutoTransformer;
use Symfony\Component\PropertyAccess\PropertyAccess;

class POPO
{
Expand Down Expand Up @@ -119,7 +120,7 @@ public function setUp()

public function testThatCanTransformObject()
{
$transformer = new ModelToElasticaAutoTransformer();
$transformer = $this->getTransformer();
$document = $transformer->transform(new POPO(), array('name' => array()));
$data = $document->getData();

Expand All @@ -130,7 +131,7 @@ public function testThatCanTransformObject()

public function testThatCanTransformObjectWithCorrectTypes()
{
$transformer = new ModelToElasticaAutoTransformer();
$transformer = $this->getTransformer();
$document = $transformer->transform(
new POPO(), array(
'name' => array(),
Expand All @@ -154,7 +155,7 @@ public function testThatCanTransformObjectWithCorrectTypes()

public function testThatCanTransformObjectWithIteratorValue()
{
$transformer = new ModelToElasticaAutoTransformer();
$transformer = $this->getTransformer();
$document = $transformer->transform(new POPO(), array('iterator' => array()));
$data = $document->getData();

Expand All @@ -163,7 +164,7 @@ public function testThatCanTransformObjectWithIteratorValue()

public function testThatCanTransformObjectWithArrayValue()
{
$transformer = new ModelToElasticaAutoTransformer();
$transformer = $this->getTransformer();
$document = $transformer->transform(new POPO(), array('array' => array()));
$data = $document->getData();

Expand All @@ -177,7 +178,7 @@ public function testThatCanTransformObjectWithArrayValue()

public function testThatCanTransformObjectWithMultiDimensionalArrayValue()
{
$transformer = new ModelToElasticaAutoTransformer();
$transformer = $this->getTransformer();
$document = $transformer->transform(new POPO(), array('multiArray' => array()));
$data = $document->getData();

Expand All @@ -193,25 +194,29 @@ public function testThatCanTransformObjectWithMultiDimensionalArrayValue()

public function testThatNullValuesAreNotFilteredOut()
{
$transformer = new ModelToElasticaAutoTransformer();
$transformer = $this->getTransformer();
$document = $transformer->transform(new POPO(), array('nullValue' => array()));
$data = $document->getData();

$this->assertTrue(array_key_exists('nullValue', $data));
}

/**
* @expectedException \Symfony\Component\Form\Exception\PropertyAccessDeniedException
*/
public function testThatCannotTransformObjectWhenGetterDoesNotExistForPrivateMethod()
{
$transformer = new ModelToElasticaAutoTransformer();
// Support both Symfony 2.1 (Form component) and 2.2 (PropertyAccess component)
$expectedException = class_exists('Symfony\Component\PropertyAccess\PropertyAccess')
? 'Symfony\Component\PropertyAccess\Exception\PropertyAccessDeniedException'
: 'Symfony\Component\Form\Exception\PropertyAccessDeniedException';

$this->setExpectedException($expectedException);

$transformer = $this->getTransformer();
$transformer->transform(new POPO(), array('desc' => array()));
}

public function testFileAddedForAttachmentMapping()
{
$transformer = new ModelToElasticaAutoTransformer();
$transformer = $this->getTransformer();
$document = $transformer->transform(new POPO(), array('file' => array('type' => 'attachment')));
$data = $document->getData();

Expand All @@ -220,7 +225,7 @@ public function testFileAddedForAttachmentMapping()

public function testFileContentsAddedForAttachmentMapping()
{
$transformer = new ModelToElasticaAutoTransformer();
$transformer = $this->getTransformer();
$document = $transformer->transform(new POPO(), array('fileContents' => array('type' => 'attachment')));
$data = $document->getData();

Expand All @@ -231,7 +236,7 @@ public function testFileContentsAddedForAttachmentMapping()

public function testNestedMapping()
{
$transformer = new ModelToElasticaAutoTransformer();
$transformer = $this->getTransformer();
$document = $transformer->transform(new POPO(), array(
'sub' => array(
'type' => 'nested',
Expand All @@ -250,7 +255,7 @@ public function testNestedMapping()

public function tesObjectMapping()
{
$transformer = new ModelToElasticaAutoTransformer();
$transformer = $this->getTransformer();
$document = $transformer->transform(new POPO(), array(
'sub' => array(
'type' => 'object',
Expand All @@ -269,7 +274,7 @@ public function tesObjectMapping()

public function testParentMapping()
{
$transformer = new ModelToElasticaAutoTransformer();
$transformer = $this->getTransformer();
$document = $transformer->transform(new POPO(), array(
'upper' => array(
'_parent' => array('type' => 'upper', 'identifier' => 'id'),
Expand All @@ -281,7 +286,7 @@ public function testParentMapping()

public function testParentMappingWithCustomIdentifier()
{
$transformer = new ModelToElasticaAutoTransformer();
$transformer = $this->getTransformer();
$document = $transformer->transform(new POPO(), array(
'upper' => array(
'_parent' => array('type' => 'upper', 'identifier' => 'name'),
Expand All @@ -290,4 +295,18 @@ public function testParentMappingWithCustomIdentifier()

$this->assertEquals("a random name", $document->getParent());
}

/**
* @return ModelToElasticaAutoTransformer
*/
private function getTransformer()
{
$transformer = new ModelToElasticaAutoTransformer();

if (class_exists('Symfony\Component\PropertyAccess\PropertyAccess')) {
$transformer->setPropertyAccessor(PropertyAccess::getPropertyAccessor());
}

return $transformer;
}
}
Loading

0 comments on commit 8146f8e

Please sign in to comment.