From fab42fa0ceef2c5c5966f8cbf06480aac13cbfbe Mon Sep 17 00:00:00 2001 From: Patrick McAndrew Date: Fri, 24 May 2013 12:27:37 +0100 Subject: [PATCH 01/36] Properities should not be required for object as elastica can automap --- README.md | 8 +++++++ .../ModelToElasticaAutoTransformerTest.php | 24 +++++++++++++++++++ .../ModelToElasticaAutoTransformer.php | 2 +- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 555f5b1..fac11d1 100644 --- a/README.md +++ b/README.md @@ -184,6 +184,8 @@ per type. ### Declaring `nested` or `object` +Note that object can autodetect properties + fos_elastica: clients: default: { host: localhost, port: 9200 } @@ -204,6 +206,12 @@ per type. properties: date: { boost: 5 } content: ~ + user: + type: "object" + approver: + type: "object" + properties: + date: { boost: 5 } ### Populate the types diff --git a/Tests/Transformer/ModelToElasticaAutoTransformerTest.php b/Tests/Transformer/ModelToElasticaAutoTransformerTest.php index cbcb6bd..080cbfe 100644 --- a/Tests/Transformer/ModelToElasticaAutoTransformerTest.php +++ b/Tests/Transformer/ModelToElasticaAutoTransformerTest.php @@ -102,6 +102,11 @@ class POPO ); } + public function getObj() + { + return array('foo' => 'foo', 'bar' => 'foo', 'id' => 1); + } + public function getUpper() { return (object) array('id' => 'parent', 'name' => 'a random name'); @@ -268,6 +273,25 @@ class ModelToElasticaAutoTransformerTest extends \PHPUnit_Framework_TestCase ), $data['sub']); } + public function testObjectDoesNotRequireProperties() + { + $transformer = $this->getTransformer(); + $document = $transformer->transform(new POPO(), array( + 'obj' => array( + 'type' => 'object' + ) + )); + $data = $document->getData(); + + $this->assertTrue(array_key_exists('obj', $data)); + $this->assertInternalType('array', $data['obj']); + $this->assertEquals(array( + 'foo' => 'foo', + 'bar' => 'foo', + 'id' => 1 + ), $data['obj']); + } + public function testParentMapping() { $transformer = $this->getTransformer(); diff --git a/Transformer/ModelToElasticaAutoTransformer.php b/Transformer/ModelToElasticaAutoTransformer.php index 38bd065..ebeacfb 100644 --- a/Transformer/ModelToElasticaAutoTransformer.php +++ b/Transformer/ModelToElasticaAutoTransformer.php @@ -72,7 +72,7 @@ class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterf continue; } - if (isset($mapping['type']) && in_array($mapping['type'], array('nested', 'object'))) { + if (isset($mapping['type']) && in_array($mapping['type'], array('nested', 'object')) && isset($mapping['properties'])) { /* $value is a nested document or object. Transform $value into * an array of documents, respective the mapped properties. */ From 5b6a1f7bd656f6c8490035aa309334cdce415457 Mon Sep 17 00:00:00 2001 From: Lea Haensenberger Date: Wed, 17 Jul 2013 08:58:15 +0200 Subject: [PATCH 02/36] Adding new persister and new transformer to make use of the serializer support of elastica when persisting doctrine objects --- DependencyInjection/FOSElasticaExtension.php | 47 ++++++--- Persister/ObjectSerializerPersister.php | 97 +++++++++++++++++++ Resources/config/config.xml | 13 +++ .../ModelToElasticaIdentifierTransformer.php | 26 +++++ 4 files changed, 169 insertions(+), 14 deletions(-) create mode 100644 Persister/ObjectSerializerPersister.php create mode 100644 Transformer/ModelToElasticaIdentifierTransformer.php diff --git a/DependencyInjection/FOSElasticaExtension.php b/DependencyInjection/FOSElasticaExtension.php index c28fb99..9a15a83 100644 --- a/DependencyInjection/FOSElasticaExtension.php +++ b/DependencyInjection/FOSElasticaExtension.php @@ -16,6 +16,7 @@ class FOSElasticaExtension extends Extension protected $indexConfigs = array(); protected $typeFields = array(); protected $loadedDrivers = array(); + protected $serializerConfig = array(); public function load(array $configs, ContainerBuilder $container) { @@ -40,8 +41,8 @@ class FOSElasticaExtension extends Extension } $clientIdsByName = $this->loadClients($config['clients'], $container); - $serializerConfig = isset($config['serializer']) ? $config['serializer'] : null; - $indexIdsByName = $this->loadIndexes($config['indexes'], $container, $clientIdsByName, $config['default_client'], $serializerConfig); + $this->serializerConfig = isset($config['serializer']) ? $config['serializer'] : null; + $indexIdsByName = $this->loadIndexes($config['indexes'], $container, $clientIdsByName, $config['default_client']); $indexRefsByName = array_map(function($id) { return new Reference($id); }, $indexIdsByName); @@ -94,7 +95,7 @@ class FOSElasticaExtension extends Extension * @throws \InvalidArgumentException * @return array */ - protected function loadIndexes(array $indexes, ContainerBuilder $container, array $clientIdsByName, $defaultClientName, $serializerConfig) + protected function loadIndexes(array $indexes, ContainerBuilder $container, array $clientIdsByName, $defaultClientName) { $indexIds = array(); foreach ($indexes as $name => $index) { @@ -129,7 +130,7 @@ class FOSElasticaExtension extends Extension if (!empty($index['settings'])) { $this->indexConfigs[$name]['config']['settings'] = $index['settings']; } - $this->loadTypes(isset($index['types']) ? $index['types'] : array(), $container, $name, $indexId, $typePrototypeConfig, $serializerConfig); + $this->loadTypes(isset($index['types']) ? $index['types'] : array(), $container, $name, $indexId, $typePrototypeConfig); } return $indexIds; @@ -170,7 +171,7 @@ class FOSElasticaExtension extends Extension * @param $indexId * @param array $typePrototypeConfig */ - protected function loadTypes(array $types, ContainerBuilder $container, $indexName, $indexId, array $typePrototypeConfig, $serializerConfig) + protected function loadTypes(array $types, ContainerBuilder $container, $indexName, $indexId, array $typePrototypeConfig) { foreach ($types as $name => $type) { $type = self::deepArrayUnion($typePrototypeConfig, $type); @@ -179,12 +180,12 @@ class FOSElasticaExtension extends Extension $typeDef = new Definition('%fos_elastica.type.class%', $typeDefArgs); $typeDef->setFactoryService($indexId); $typeDef->setFactoryMethod('getType'); - if ($serializerConfig) { - $callbackDef = new Definition($serializerConfig['callback_class']); + if ($this->serializerConfig) { + $callbackDef = new Definition($this->serializerConfig['callback_class']); $callbackId = sprintf('%s.%s.serializer.callback', $indexId, $name); $typeDef->addMethodCall('setSerializer', array(array(new Reference($callbackId), 'serialize'))); - $callbackDef->addMethodCall('setSerializer', array(new Reference($serializerConfig['serializer']))); + $callbackDef->addMethodCall('setSerializer', array(new Reference($this->serializerConfig['serializer']))); if (isset($type['serializer']['groups'])) { $callbackDef->addMethodCall('setGroups', array($type['serializer']['groups'])); } @@ -307,7 +308,11 @@ class FOSElasticaExtension extends Extension if (isset($typeConfig['model_to_elastica_transformer']['service'])) { return $typeConfig['model_to_elastica_transformer']['service']; } - $abstractId = sprintf('fos_elastica.model_to_elastica_transformer.prototype.auto'); + if ($this->serializerConfig) { + $abstractId = sprintf('fos_elastica.model_to_elastica_transformer.prototype.identifier'); + } else { + $abstractId = sprintf('fos_elastica.model_to_elastica_transformer.prototype.auto'); + } $serviceId = sprintf('fos_elastica.model_to_elastica_transformer.%s.%s', $indexName, $typeName); $serviceDef = new DefinitionDecorator($abstractId); $serviceDef->replaceArgument(0, array( @@ -320,13 +325,27 @@ class FOSElasticaExtension extends Extension protected function loadObjectPersister(array $typeConfig, Definition $typeDef, ContainerBuilder $container, $indexName, $typeName, $transformerId) { - $abstractId = sprintf('fos_elastica.object_persister.prototype'); + if ($this->serializerConfig) { + $abstractId = sprintf('fos_elastica.object_serializer_persister.prototype'); + $arguments = array( + $typeDef, + new Reference($transformerId), + $typeConfig['model'], + ); + } else { + $abstractId = sprintf('fos_elastica.object_persister.prototype'); + $arguments = array( + $typeDef, + new Reference($transformerId), + $typeConfig['model'], + $this->typeFields[sprintf('%s/%s', $indexName, $typeName)], + ); + } $serviceId = sprintf('fos_elastica.object_persister.%s.%s', $indexName, $typeName); $serviceDef = new DefinitionDecorator($abstractId); - $serviceDef->replaceArgument(0, $typeDef); - $serviceDef->replaceArgument(1, new Reference($transformerId)); - $serviceDef->replaceArgument(2, $typeConfig['model']); - $serviceDef->replaceArgument(3, $this->typeFields[sprintf('%s/%s', $indexName, $typeName)]); + foreach ($arguments as $i => $argument) { + $serviceDef->replaceArgument($i, $argument); + } $container->setDefinition($serviceId, $serviceDef); return $serviceId; diff --git a/Persister/ObjectSerializerPersister.php b/Persister/ObjectSerializerPersister.php new file mode 100644 index 0000000..db8122a --- /dev/null +++ b/Persister/ObjectSerializerPersister.php @@ -0,0 +1,97 @@ + + */ +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()); + } +} diff --git a/Resources/config/config.xml b/Resources/config/config.xml index 4097289..3f5f7cf 100644 --- a/Resources/config/config.xml +++ b/Resources/config/config.xml @@ -52,6 +52,12 @@ + + + + + + @@ -64,6 +70,13 @@ + + + + + + + diff --git a/Transformer/ModelToElasticaIdentifierTransformer.php b/Transformer/ModelToElasticaIdentifierTransformer.php new file mode 100644 index 0000000..416eaf7 --- /dev/null +++ b/Transformer/ModelToElasticaIdentifierTransformer.php @@ -0,0 +1,26 @@ +propertyAccessor->getValue($object, $this->options['identifier']); + return new Document($identifier); + } +} From d10e8f56c8b7e1374dae924e1a0efd2067986208 Mon Sep 17 00:00:00 2001 From: Lea Haensenberger Date: Wed, 17 Jul 2013 10:10:07 +0200 Subject: [PATCH 03/36] Tests for new provider and new transformer --- .../ObjectSerializerPersisterTest.php | 117 ++++++++++++++++++ ...delToElasticaIdentifierTransformerTest.php | 66 ++++++++++ 2 files changed, 183 insertions(+) create mode 100644 Tests/Persister/ObjectSerializerPersisterTest.php create mode 100644 Tests/Transformer/ModelToElasticaIdentifierTransformerTest.php diff --git a/Tests/Persister/ObjectSerializerPersisterTest.php b/Tests/Persister/ObjectSerializerPersisterTest.php new file mode 100644 index 0000000..10c63ab --- /dev/null +++ b/Tests/Persister/ObjectSerializerPersisterTest.php @@ -0,0 +1,117 @@ +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; + } +} diff --git a/Tests/Transformer/ModelToElasticaIdentifierTransformerTest.php b/Tests/Transformer/ModelToElasticaIdentifierTransformerTest.php new file mode 100644 index 0000000..59a747a --- /dev/null +++ b/Tests/Transformer/ModelToElasticaIdentifierTransformerTest.php @@ -0,0 +1,66 @@ +id; + } + + public function getName() + { + return $this->name; + } +} + +class ModelToElasticaIdentifierTransformerTest extends \PHPUnit_Framework_TestCase +{ + public function setUp() + { + if (!class_exists('Elastica\Document')) { + ; + $this->markTestSkipped('The Elastica library classes are not available'); + } + } + + public function testGetDocumentWithIdentifierOnly() + { + $transformer = $this->getTransformer(); + $document = $transformer->transform(new POPO(), array()); + $data = $document->getData(); + + $this->assertInstanceOf('Elastica\Document', $document); + $this->assertEquals(123, $document->getId()); + $this->assertCount(0, $data); + } + + public function testGetDocumentWithIdentifierOnlyWithFields() + { + $transformer = $this->getTransformer(); + $document = $transformer->transform(new POPO(), array('name' => array())); + $data = $document->getData(); + + $this->assertInstanceOf('Elastica\Document', $document); + $this->assertEquals(123, $document->getId()); + $this->assertCount(0, $data); + } + + /** + * @return ModelToElasticaIdentifierTransformer + */ + private function getTransformer() + { + $transformer = new ModelToElasticaIdentifierTransformer(); + $transformer->setPropertyAccessor(PropertyAccess::getPropertyAccessor()); + + return $transformer; + } +} From 300d189a9d62559c2fc7c65d1cef03f060cc7d9f Mon Sep 17 00:00:00 2001 From: Lea Haensenberger Date: Thu, 18 Jul 2013 10:51:51 +0200 Subject: [PATCH 04/36] renaming services to avoid potential conflicts --- DependencyInjection/FOSElasticaExtension.php | 4 ++-- Resources/config/config.xml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/DependencyInjection/FOSElasticaExtension.php b/DependencyInjection/FOSElasticaExtension.php index 9a15a83..e0f7a2d 100644 --- a/DependencyInjection/FOSElasticaExtension.php +++ b/DependencyInjection/FOSElasticaExtension.php @@ -309,7 +309,7 @@ class FOSElasticaExtension extends Extension return $typeConfig['model_to_elastica_transformer']['service']; } if ($this->serializerConfig) { - $abstractId = sprintf('fos_elastica.model_to_elastica_transformer.prototype.identifier'); + $abstractId = sprintf('fos_elastica.model_to_elastica_identifier_transformer'); } else { $abstractId = sprintf('fos_elastica.model_to_elastica_transformer.prototype.auto'); } @@ -326,7 +326,7 @@ class FOSElasticaExtension extends Extension protected function loadObjectPersister(array $typeConfig, Definition $typeDef, ContainerBuilder $container, $indexName, $typeName, $transformerId) { if ($this->serializerConfig) { - $abstractId = sprintf('fos_elastica.object_serializer_persister.prototype'); + $abstractId = sprintf('fos_elastica.object_serializer_persister'); $arguments = array( $typeDef, new Reference($transformerId), diff --git a/Resources/config/config.xml b/Resources/config/config.xml index 3f5f7cf..4d85d9f 100644 --- a/Resources/config/config.xml +++ b/Resources/config/config.xml @@ -52,7 +52,7 @@ - + @@ -70,7 +70,7 @@ - + From 77156b35aa0f70420b555151d9700591de1f6fc8 Mon Sep 17 00:00:00 2001 From: Lea Haensenberger Date: Thu, 18 Jul 2013 10:54:11 +0200 Subject: [PATCH 05/36] some formatting and cleaning up --- Persister/ObjectSerializerPersister.php | 8 ++++---- Tests/Transformer/ModelToElasticaAutoTransformerTest.php | 1 - .../ModelToElasticaIdentifierTransformerTest.php | 1 - Transformer/ModelToElasticaIdentifierTransformer.php | 4 ++-- 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/Persister/ObjectSerializerPersister.php b/Persister/ObjectSerializerPersister.php index db8122a..269515b 100644 --- a/Persister/ObjectSerializerPersister.php +++ b/Persister/ObjectSerializerPersister.php @@ -16,9 +16,9 @@ class ObjectSerializerPersister extends ObjectPersister { public function __construct(Type $type, ModelToElasticaTransformerInterface $transformer, $objectClass) { - $this->type = $type; - $this->transformer = $transformer; - $this->objectClass = $objectClass; + $this->type = $type; + $this->transformer = $transformer; + $this->objectClass = $objectClass; } /** @@ -38,7 +38,7 @@ class ObjectSerializerPersister extends ObjectPersister * * @param object $object * @return null - **/ + */ public function replaceOne($object) { $document = $this->transformToElasticaDocument($object); diff --git a/Tests/Transformer/ModelToElasticaAutoTransformerTest.php b/Tests/Transformer/ModelToElasticaAutoTransformerTest.php index cbcb6bd..16bb5cd 100644 --- a/Tests/Transformer/ModelToElasticaAutoTransformerTest.php +++ b/Tests/Transformer/ModelToElasticaAutoTransformerTest.php @@ -113,7 +113,6 @@ class ModelToElasticaAutoTransformerTest extends \PHPUnit_Framework_TestCase public function setUp() { if (!class_exists('Elastica\Document')) { - ; $this->markTestSkipped('The Elastica library classes are not available'); } } diff --git a/Tests/Transformer/ModelToElasticaIdentifierTransformerTest.php b/Tests/Transformer/ModelToElasticaIdentifierTransformerTest.php index 59a747a..f1a77d4 100644 --- a/Tests/Transformer/ModelToElasticaIdentifierTransformerTest.php +++ b/Tests/Transformer/ModelToElasticaIdentifierTransformerTest.php @@ -26,7 +26,6 @@ class ModelToElasticaIdentifierTransformerTest extends \PHPUnit_Framework_TestCa public function setUp() { if (!class_exists('Elastica\Document')) { - ; $this->markTestSkipped('The Elastica library classes are not available'); } } diff --git a/Transformer/ModelToElasticaIdentifierTransformer.php b/Transformer/ModelToElasticaIdentifierTransformer.php index 416eaf7..654850f 100644 --- a/Transformer/ModelToElasticaIdentifierTransformer.php +++ b/Transformer/ModelToElasticaIdentifierTransformer.php @@ -5,8 +5,8 @@ namespace FOS\ElasticaBundle\Transformer; use Elastica\Document; /** - * Creates an elastica document with the id of - * the doctrine object as elastica document id + * Creates an Elastica document with the ID of + * the Doctrine object as Elastica document ID */ class ModelToElasticaIdentifierTransformer extends ModelToElasticaAutoTransformer { From 37cfdb0df79298ece68280984a8313e71a58fde0 Mon Sep 17 00:00:00 2001 From: Lea Haensenberger Date: Thu, 18 Jul 2013 10:56:25 +0200 Subject: [PATCH 06/36] refactoring some code --- DependencyInjection/FOSElasticaExtension.php | 21 ++++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/DependencyInjection/FOSElasticaExtension.php b/DependencyInjection/FOSElasticaExtension.php index e0f7a2d..68e21dd 100644 --- a/DependencyInjection/FOSElasticaExtension.php +++ b/DependencyInjection/FOSElasticaExtension.php @@ -325,21 +325,16 @@ class FOSElasticaExtension extends Extension protected function loadObjectPersister(array $typeConfig, Definition $typeDef, ContainerBuilder $container, $indexName, $typeName, $transformerId) { + $arguments = array( + $typeDef, + new Reference($transformerId), + $typeConfig['model'], + ); if ($this->serializerConfig) { - $abstractId = sprintf('fos_elastica.object_serializer_persister'); - $arguments = array( - $typeDef, - new Reference($transformerId), - $typeConfig['model'], - ); + $abstractId = 'fos_elastica.object_serializer_persister'; } else { - $abstractId = sprintf('fos_elastica.object_persister.prototype'); - $arguments = array( - $typeDef, - new Reference($transformerId), - $typeConfig['model'], - $this->typeFields[sprintf('%s/%s', $indexName, $typeName)], - ); + $abstractId = 'fos_elastica.object_persister.prototype'; + $arguments[] = $this->typeFields[sprintf('%s/%s', $indexName, $typeName)]; } $serviceId = sprintf('fos_elastica.object_persister.%s.%s', $indexName, $typeName); $serviceDef = new DefinitionDecorator($abstractId); From c82006e8aceaf030bef9afa16629fbacca7964b6 Mon Sep 17 00:00:00 2001 From: Luis Cordova Date: Fri, 15 Nov 2013 15:41:10 -0500 Subject: [PATCH 07/36] fix typos --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9792139..c777a91 100644 --- a/README.md +++ b/README.md @@ -182,11 +182,11 @@ per type. content: ~ _parent: { type: "post", property: "post", identifier: "id" } -The parent filed declaration has the following values: +The parent field declaration has the following values: * `type`: The parent type. * `property`: The property in the child entity where to look for the parent entity. It may be ignored if is equal to the parent type. - * `identifier`: The property in the parent entity which have the parent identifier. Defaults to `id`. + * `identifier`: The property in the parent entity which has the parent identifier. Defaults to `id`. Note that to create a document with a parent, you need to call `setParent` on the document rather than setting a _parent field. If you do this wrong, you will see a `RoutingMissingException` as elasticsearch does not know where to store a document that should have a parent but does not specify it. @@ -230,7 +230,7 @@ It applies the configured mappings to the types. This command needs providers to insert new documents in the elasticsearch types. There are 2 ways to create providers. If your elasticsearch type matches a Doctrine repository or a Propel query, go for the persistence automatic provider. -Or, for complete flexibility, go for manual provider. +Or, for complete flexibility, go for a manual provider. #### Persistence automatic provider @@ -495,7 +495,7 @@ If you use multiple drivers then you can choose which one is aliased to `fos_ela using the `default_manager` parameter: fos_elastica: - default_manager: mongodb #defauults to orm + default_manager: mongodb #defaults to orm clients: default: { host: localhost, port: 9200 } #-- From cacb40286c6dd1da6d53d141f453ade237344fa5 Mon Sep 17 00:00:00 2001 From: Luis Cordova Date: Thu, 14 Nov 2013 10:04:08 -0500 Subject: [PATCH 08/36] clean ups --- Command/PopulateCommand.php | 2 -- Command/ResetCommand.php | 2 -- Command/SearchCommand.php | 1 - Configuration/Search.php | 2 -- DependencyInjection/FOSElasticaExtension.php | 2 ++ Doctrine/RepositoryManager.php | 1 - Finder/TransformedFinder.php | 1 + Manager/RepositoryManager.php | 6 +++--- Manager/RepositoryManagerInterface.php | 1 - Paginator/FantaPaginatorAdapter.php | 5 ++--- Paginator/PaginatorAdapterInterface.php | 2 -- Paginator/RawPaginatorAdapter.php | 4 +--- Paginator/RawPartialResults.php | 1 - Paginator/TransformedPaginatorAdapter.php | 1 - Paginator/TransformedPartialResults.php | 1 - Provider/AbstractProvider.php | 1 - Provider/ProviderRegistry.php | 2 -- Resources/views/Collector/elastica.html.twig | 12 ++++++------ Serializer/Callback.php | 2 -- Tests/DataCollector/ElasticaDataCollectorTest.php | 2 -- Tests/Doctrine/RepositoryManagerTest.php | 1 - Tests/Logger/ElasticaLoggerTest.php | 1 - Tests/Manager/RepositoryManagerTest.php | 1 - Tests/RepositoryTest.php | 2 -- .../ElasticaToModelTransformerCollectionTest.php | 1 - Transformer/ModelToElasticaAutoTransformer.php | 1 - 26 files changed, 15 insertions(+), 43 deletions(-) diff --git a/Command/PopulateCommand.php b/Command/PopulateCommand.php index 58e5e9f..7297523 100755 --- a/Command/PopulateCommand.php +++ b/Command/PopulateCommand.php @@ -4,11 +4,9 @@ namespace FOS\ElasticaBundle\Command; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Console\Helper\DialogHelper; -use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Console\Output\Output; use FOS\ElasticaBundle\IndexManager; use FOS\ElasticaBundle\Provider\ProviderRegistry; use FOS\ElasticaBundle\Resetter; diff --git a/Command/ResetCommand.php b/Command/ResetCommand.php index b318827..280f9aa 100755 --- a/Command/ResetCommand.php +++ b/Command/ResetCommand.php @@ -3,11 +3,9 @@ namespace FOS\ElasticaBundle\Command; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; -use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Console\Output\Output; use FOS\ElasticaBundle\IndexManager; use FOS\ElasticaBundle\Resetter; diff --git a/Command/SearchCommand.php b/Command/SearchCommand.php index 62e3340..ec7cfb7 100644 --- a/Command/SearchCommand.php +++ b/Command/SearchCommand.php @@ -7,7 +7,6 @@ use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Console\Output\Output; use Elastica\Query; use Elastica\Result; diff --git a/Configuration/Search.php b/Configuration/Search.php index ded65a5..cee10ab 100644 --- a/Configuration/Search.php +++ b/Configuration/Search.php @@ -11,8 +11,6 @@ namespace FOS\ElasticaBundle\Configuration; */ class Search { - /** @var string */ public $repositoryClass; - } diff --git a/DependencyInjection/FOSElasticaExtension.php b/DependencyInjection/FOSElasticaExtension.php index 9bf732e..50621a5 100644 --- a/DependencyInjection/FOSElasticaExtension.php +++ b/DependencyInjection/FOSElasticaExtension.php @@ -91,6 +91,7 @@ class FOSElasticaExtension extends Extension * @param ContainerBuilder $container A ContainerBuilder instance * @param array $clientIdsByName * @param $defaultClientName + * @param $serializerConfig * @throws \InvalidArgumentException * @return array */ @@ -170,6 +171,7 @@ class FOSElasticaExtension extends Extension * @param $indexName * @param $indexId * @param array $typePrototypeConfig + * @param $serializerConfig */ protected function loadTypes(array $types, ContainerBuilder $container, $indexName, $indexId, array $typePrototypeConfig, $serializerConfig) { diff --git a/Doctrine/RepositoryManager.php b/Doctrine/RepositoryManager.php index 6ba6bf5..f8867eb 100644 --- a/Doctrine/RepositoryManager.php +++ b/Doctrine/RepositoryManager.php @@ -40,5 +40,4 @@ class RepositoryManager extends BaseManager return parent::getRepository($realEntityName); } - } diff --git a/Finder/TransformedFinder.php b/Finder/TransformedFinder.php index 24f680f..4c8aa98 100644 --- a/Finder/TransformedFinder.php +++ b/Finder/TransformedFinder.php @@ -97,6 +97,7 @@ class TransformedFinder implements PaginatedFinderInterface public function createPaginatorAdapter($query) { $query = Query::create($query); + return new TransformedPaginatorAdapter($this->searchable, $query, $this->transformer); } } diff --git a/Manager/RepositoryManager.php b/Manager/RepositoryManager.php index 6459c19..3cf8e96 100644 --- a/Manager/RepositoryManager.php +++ b/Manager/RepositoryManager.php @@ -5,6 +5,7 @@ namespace FOS\ElasticaBundle\Manager; use Doctrine\Common\Annotations\Reader; use FOS\ElasticaBundle\Finder\FinderInterface; use RuntimeException; + /** * @author Richard Miller * @@ -70,11 +71,10 @@ class RepositoryManager implements RepositoryManagerInterface private function createRepository($entityName) { - $repositoryName = $this->getRepositoryName($entityName); - if (!class_exists($repositoryName)) { + if (!class_exists($repositoryName = $this->getRepositoryName($entityName))) { throw new RuntimeException(sprintf('%s repository for %s does not exist', $repositoryName, $entityName)); } + return new $repositoryName($this->entities[$entityName]['finder']); } - } diff --git a/Manager/RepositoryManagerInterface.php b/Manager/RepositoryManagerInterface.php index c831d35..1008371 100644 --- a/Manager/RepositoryManagerInterface.php +++ b/Manager/RepositoryManagerInterface.php @@ -32,5 +32,4 @@ interface RepositoryManagerInterface * @param string $entityName */ public function getRepository($entityName); - } diff --git a/Paginator/FantaPaginatorAdapter.php b/Paginator/FantaPaginatorAdapter.php index a2f8c0e..2ad6983 100644 --- a/Paginator/FantaPaginatorAdapter.php +++ b/Paginator/FantaPaginatorAdapter.php @@ -3,14 +3,13 @@ namespace FOS\ElasticaBundle\Paginator; use Pagerfanta\Adapter\AdapterInterface; -use FOS\ElasticaBundle\Paginator\PaginatorAdapterInterface; class FantaPaginatorAdapter implements AdapterInterface { private $adapter; /** - * @param PaginatorAdapterInterface $adapter + * @param \FOS\ElasticaBundle\Paginator\PaginatorAdapterInterface $adapter */ public function __construct(PaginatorAdapterInterface $adapter) { @@ -42,7 +41,7 @@ class FantaPaginatorAdapter implements AdapterInterface } /** - * Returns an slice of the results. + * Returns a slice of the results. * * @param integer $offset The offset. * @param integer $length The length. diff --git a/Paginator/PaginatorAdapterInterface.php b/Paginator/PaginatorAdapterInterface.php index 9182973..25786a0 100644 --- a/Paginator/PaginatorAdapterInterface.php +++ b/Paginator/PaginatorAdapterInterface.php @@ -2,8 +2,6 @@ namespace FOS\ElasticaBundle\Paginator; -use FOS\ElasticaBundle\Paginator\PartialResultsInterface; - interface PaginatorAdapterInterface { /** diff --git a/Paginator/RawPaginatorAdapter.php b/Paginator/RawPaginatorAdapter.php index 125cd35..d4b5357 100644 --- a/Paginator/RawPaginatorAdapter.php +++ b/Paginator/RawPaginatorAdapter.php @@ -5,9 +5,6 @@ namespace FOS\ElasticaBundle\Paginator; use Elastica\SearchableInterface; use Elastica\Query; use Elastica\ResultSet; -use FOS\ElasticaBundle\Paginator\PaginatorAdapterInterface; -use FOS\ElasticaBundle\Paginator\RawPartialResults; -use FOS\ElasticaBundle\Paginator\PartialResultsInterface; use InvalidArgumentException; /** @@ -52,6 +49,7 @@ class RawPaginatorAdapter implements PaginatorAdapterInterface * * @param $offset * @param $itemCountPerPage + * @throws \InvalidArgumentException * @return ResultSet */ protected function getElasticaResults($offset, $itemCountPerPage) diff --git a/Paginator/RawPartialResults.php b/Paginator/RawPartialResults.php index 270e4cd..a4afb00 100644 --- a/Paginator/RawPartialResults.php +++ b/Paginator/RawPartialResults.php @@ -2,7 +2,6 @@ namespace FOS\ElasticaBundle\Paginator; -use FOS\ElasticaBundle\Paginator\PartialResultsInterface; use Elastica\ResultSet; use Elastica\Result; diff --git a/Paginator/TransformedPaginatorAdapter.php b/Paginator/TransformedPaginatorAdapter.php index a668636..7bc038a 100644 --- a/Paginator/TransformedPaginatorAdapter.php +++ b/Paginator/TransformedPaginatorAdapter.php @@ -3,7 +3,6 @@ namespace FOS\ElasticaBundle\Paginator; use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface; -use FOS\ElasticaBundle\Paginator\TransformedPartialResults; use Elastica\SearchableInterface; use Elastica\Query; diff --git a/Paginator/TransformedPartialResults.php b/Paginator/TransformedPartialResults.php index f7f125a..13d716c 100644 --- a/Paginator/TransformedPartialResults.php +++ b/Paginator/TransformedPartialResults.php @@ -3,7 +3,6 @@ namespace FOS\ElasticaBundle\Paginator; use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface; -use FOS\ElasticaBundle\Paginator\RawPartialResults; use Elastica\ResultSet; /** diff --git a/Provider/AbstractProvider.php b/Provider/AbstractProvider.php index daa57ae..06883a3 100644 --- a/Provider/AbstractProvider.php +++ b/Provider/AbstractProvider.php @@ -3,7 +3,6 @@ namespace FOS\ElasticaBundle\Provider; use FOS\ElasticaBundle\Persister\ObjectPersisterInterface; -use FOS\ElasticaBundle\Provider\ProviderInterface; abstract class AbstractProvider implements ProviderInterface { diff --git a/Provider/ProviderRegistry.php b/Provider/ProviderRegistry.php index ed5b499..2142223 100644 --- a/Provider/ProviderRegistry.php +++ b/Provider/ProviderRegistry.php @@ -2,8 +2,6 @@ namespace FOS\ElasticaBundle\Provider; -use Symfony\Component\DependencyInjection\ContainerBuilder; - use Symfony\Component\DependencyInjection\ContainerAwareInterface; use Symfony\Component\DependencyInjection\ContainerInterface; diff --git a/Resources/views/Collector/elastica.html.twig b/Resources/views/Collector/elastica.html.twig index a665e2d..4a4c3e8 100644 --- a/Resources/views/Collector/elastica.html.twig +++ b/Resources/views/Collector/elastica.html.twig @@ -19,13 +19,13 @@ {% endblock %} {% block menu %} - - - Elastica - - {{ collector.querycount }} + + + Elastica + + {{ collector.querycount }} + - {% endblock %} {% block panel %} diff --git a/Serializer/Callback.php b/Serializer/Callback.php index 50307e0..9fe7064 100644 --- a/Serializer/Callback.php +++ b/Serializer/Callback.php @@ -8,9 +8,7 @@ use JMS\Serializer\SerializerInterface; class Callback { protected $serializer; - protected $groups; - protected $version; public function setSerializer($serializer) diff --git a/Tests/DataCollector/ElasticaDataCollectorTest.php b/Tests/DataCollector/ElasticaDataCollectorTest.php index 758e1c2..aac1fb4 100644 --- a/Tests/DataCollector/ElasticaDataCollectorTest.php +++ b/Tests/DataCollector/ElasticaDataCollectorTest.php @@ -9,7 +9,6 @@ use FOS\ElasticaBundle\DataCollector\ElasticaDataCollector; */ class ElasticaDataCollectorTest extends \PHPUnit_Framework_TestCase { - public function testCorrectAmountOfQueries() { /** @var $requestMock \PHPUnit_Framework_MockObject_MockObject|\Symfony\Component\HttpFoundation\Request */ @@ -93,5 +92,4 @@ class ElasticaDataCollectorTest extends \PHPUnit_Framework_TestCase $elasticaDataCollector->collect($requestMock, $responseMock); $this->assertEquals(30, $elasticaDataCollector->getTime()); } - } diff --git a/Tests/Doctrine/RepositoryManagerTest.php b/Tests/Doctrine/RepositoryManagerTest.php index 2863127..ce7b14b 100644 --- a/Tests/Doctrine/RepositoryManagerTest.php +++ b/Tests/Doctrine/RepositoryManagerTest.php @@ -156,5 +156,4 @@ class RepositoryManagerTest extends \PHPUnit_Framework_TestCase $repository = $manager->getRepository($shortEntityName); $this->assertInstanceOf('FOS\ElasticaBundle\Repository', $repository); } - } diff --git a/Tests/Logger/ElasticaLoggerTest.php b/Tests/Logger/ElasticaLoggerTest.php index 3cf6d2d..9f68baf 100644 --- a/Tests/Logger/ElasticaLoggerTest.php +++ b/Tests/Logger/ElasticaLoggerTest.php @@ -86,5 +86,4 @@ class ElasticaLoggerTest extends \PHPUnit_Framework_TestCase $elasticaLogger->logQuery($path, $method, $data, $time); } - } diff --git a/Tests/Manager/RepositoryManagerTest.php b/Tests/Manager/RepositoryManagerTest.php index 8cdf1b4..8849035 100644 --- a/Tests/Manager/RepositoryManagerTest.php +++ b/Tests/Manager/RepositoryManagerTest.php @@ -97,5 +97,4 @@ class RepositoryManagerTest extends \PHPUnit_Framework_TestCase $manager->addEntity($entityName, $finderMock, 'FOS\ElasticaBundle\Tests\MissingRepository'); $manager->getRepository('Missing Entity'); } - } diff --git a/Tests/RepositoryTest.php b/Tests/RepositoryTest.php index ded8009..6eabe7b 100644 --- a/Tests/RepositoryTest.php +++ b/Tests/RepositoryTest.php @@ -9,7 +9,6 @@ use FOS\ElasticaBundle\Repository; */ class RepositoryTest extends \PHPUnit_Framework_TestCase { - public function testThatFindCallsFindOnFinder() { $testQuery = 'Test Query'; @@ -75,5 +74,4 @@ class RepositoryTest extends \PHPUnit_Framework_TestCase $repository = new Repository($finderMock); $repository->findHybrid($testQuery, $testLimit); } - } diff --git a/Tests/Transformer/ElasticaToModelTransformerCollectionTest.php b/Tests/Transformer/ElasticaToModelTransformerCollectionTest.php index 9b1d782..d33708e 100644 --- a/Tests/Transformer/ElasticaToModelTransformerCollectionTest.php +++ b/Tests/Transformer/ElasticaToModelTransformerCollectionTest.php @@ -119,5 +119,4 @@ class POPO class POPO2 extends POPO { - } diff --git a/Transformer/ModelToElasticaAutoTransformer.php b/Transformer/ModelToElasticaAutoTransformer.php index 6e01907..0ebcc47 100644 --- a/Transformer/ModelToElasticaAutoTransformer.php +++ b/Transformer/ModelToElasticaAutoTransformer.php @@ -149,5 +149,4 @@ class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterf return $value; } - } From b149ac235b6bd8b61aa2c7db9e3e0a8585107067 Mon Sep 17 00:00:00 2001 From: Damien Alexandre Date: Fri, 29 Nov 2013 13:53:25 +0100 Subject: [PATCH 09/36] Replace deprecated calls `Document::add` to `Document::set` --- Transformer/ModelToElasticaAutoTransformer.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Transformer/ModelToElasticaAutoTransformer.php b/Transformer/ModelToElasticaAutoTransformer.php index e960dc5..ecddacf 100644 --- a/Transformer/ModelToElasticaAutoTransformer.php +++ b/Transformer/ModelToElasticaAutoTransformer.php @@ -75,7 +75,7 @@ class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterf /* $value is a nested document or object. Transform $value into * an array of documents, respective the mapped properties. */ - $document->add($key, $this->transformNested($value, $mapping['properties'])); + $document->set($key, $this->transformNested($value, $mapping['properties'])); continue; } @@ -89,7 +89,7 @@ class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterf continue; } - $document->add($key, $this->normalizeValue($value)); + $document->set($key, $this->normalizeValue($value)); } return $document; From 2862259d8e810e852e4872f6e1d57835ab2708c3 Mon Sep 17 00:00:00 2001 From: Damien Alexandre Date: Fri, 29 Nov 2013 14:59:56 +0100 Subject: [PATCH 10/36] Allow empty or null or no `mappings:` key under type configuration refs #300. This commit allow to define types without having to set any mapping as Elasticsearch build his own. The minimal config become: indexes: toto: client: default types: Article: mappings: ~ ... --- DependencyInjection/Configuration.php | 4 ++++ DependencyInjection/FOSElasticaExtension.php | 7 ++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index fe7e9a4..e9e2dbb 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -432,6 +432,10 @@ class Configuration implements ConfigurationInterface } foreach ($index['types'] as $type) { + if (empty($type['mappings'])) { + continue; + } + $nestings = array_merge_recursive($nestings, $this->getNestingsForType($type['mappings'], $nestings)); } } diff --git a/DependencyInjection/FOSElasticaExtension.php b/DependencyInjection/FOSElasticaExtension.php index 7a72d96..12d81ee 100644 --- a/DependencyInjection/FOSElasticaExtension.php +++ b/DependencyInjection/FOSElasticaExtension.php @@ -199,6 +199,11 @@ class FOSElasticaExtension extends Extension $typeDef->addMethodCall('setSerializer', array(array(new Reference($callbackId), 'serialize'))); } $container->setDefinition($typeId, $typeDef); + + $this->indexConfigs[$indexName]['config']['mappings'][$name] = array( + "_source" => array("enabled" => true), // Add a default setting for empty mapping settings + ); + if (isset($type['_id'])) { $this->indexConfigs[$indexName]['config']['mappings'][$name]['_id'] = $type['_id']; } @@ -211,7 +216,7 @@ class FOSElasticaExtension extends Extension if (isset($type['_routing'])) { $this->indexConfigs[$indexName]['config']['mappings'][$name]['_routing'] = $type['_routing']; } - if (isset($type['mappings'])) { + if (isset($type['mappings']) && !empty($type['mappings'])) { $this->indexConfigs[$indexName]['config']['mappings'][$name]['properties'] = $type['mappings']; $typeName = sprintf('%s/%s', $indexName, $name); $this->typeFields[$typeName] = $type['mappings']; From 3027c687e227e6c1d7bacf6793adf03839fc7355 Mon Sep 17 00:00:00 2001 From: Damien Alexandre Date: Fri, 29 Nov 2013 15:35:59 +0100 Subject: [PATCH 11/36] Ref #341, fix the insertMany method to use Bulk indexing To make it works, I inject the serializer defined for the Type into the fos_elastica.object_serializer_persister service. This is the SAME service injected in the setSerializer of Type. We deport the handling of serialization outside Elastica, this is not so good but we need to build our own Documents to get the ID's correctly. --- DependencyInjection/FOSElasticaExtension.php | 2 ++ Persister/ObjectSerializerPersister.php | 19 ++++++++---- Resources/config/config.xml | 1 + .../ObjectSerializerPersisterTest.php | 30 ++++++++++++++----- 4 files changed, 39 insertions(+), 13 deletions(-) diff --git a/DependencyInjection/FOSElasticaExtension.php b/DependencyInjection/FOSElasticaExtension.php index 12d81ee..5b533aa 100644 --- a/DependencyInjection/FOSElasticaExtension.php +++ b/DependencyInjection/FOSElasticaExtension.php @@ -356,6 +356,8 @@ class FOSElasticaExtension extends Extension if ($this->serializerConfig) { $abstractId = 'fos_elastica.object_serializer_persister'; + $callbackId = sprintf('%s.%s.serializer.callback', $this->indexConfigs[$indexName]['index'], $typeName); + $arguments[] = array(new Reference($callbackId), 'serialize'); } else { $abstractId = 'fos_elastica.object_persister'; $arguments[] = $this->typeFields[sprintf('%s/%s', $indexName, $typeName)]; diff --git a/Persister/ObjectSerializerPersister.php b/Persister/ObjectSerializerPersister.php index 269515b..40305c4 100644 --- a/Persister/ObjectSerializerPersister.php +++ b/Persister/ObjectSerializerPersister.php @@ -2,6 +2,7 @@ namespace FOS\ElasticaBundle\Persister; +use Elastica\Document; use Elastica\Type; use FOS\ElasticaBundle\Transformer\ModelToElasticaTransformerInterface; @@ -14,11 +15,12 @@ use FOS\ElasticaBundle\Transformer\ModelToElasticaTransformerInterface; */ class ObjectSerializerPersister extends ObjectPersister { - public function __construct(Type $type, ModelToElasticaTransformerInterface $transformer, $objectClass) + public function __construct(Type $type, ModelToElasticaTransformerInterface $transformer, $objectClass, $serializer) { $this->type = $type; $this->transformer = $transformer; $this->objectClass = $objectClass; + $this->serializer = $serializer; } /** @@ -30,7 +32,7 @@ class ObjectSerializerPersister extends ObjectPersister public function insertOne($object) { $document = $this->transformToElasticaDocument($object); - $this->type->addObject($object, $document); + $this->type->addDocument($document); } /** @@ -43,7 +45,7 @@ class ObjectSerializerPersister extends ObjectPersister { $document = $this->transformToElasticaDocument($object); $this->type->deleteById($document->getId()); - $this->type->addObject($object, $document); + $this->type->addDocument($document); } /** @@ -78,9 +80,11 @@ class ObjectSerializerPersister extends ObjectPersister **/ public function insertMany(array $objects) { + $docs = array(); foreach ($objects as $object) { - $this->insertOne($object); + $docs[] = $this->transformToElasticaDocument($object); } + $this->type->addDocuments($docs); } /** @@ -92,6 +96,11 @@ class ObjectSerializerPersister extends ObjectPersister */ public function transformToElasticaDocument($object) { - return $this->transformer->transform($object, array()); + $document = $this->transformer->transform($object, array()); + + $data = call_user_func($this->serializer, $object); + $document->setData($data); + + return $document; } } diff --git a/Resources/config/config.xml b/Resources/config/config.xml index 77c280d..1fb2c1d 100644 --- a/Resources/config/config.xml +++ b/Resources/config/config.xml @@ -53,6 +53,7 @@ + diff --git a/Tests/Persister/ObjectSerializerPersisterTest.php b/Tests/Persister/ObjectSerializerPersisterTest.php index 10c63ab..aae3a64 100644 --- a/Tests/Persister/ObjectSerializerPersisterTest.php +++ b/Tests/Persister/ObjectSerializerPersisterTest.php @@ -45,9 +45,12 @@ class ObjectSerializerPersisterTest extends \PHPUnit_Framework_TestCase ->method('deleteById') ->with($this->equalTo(123)); $typeMock->expects($this->once()) - ->method('addObject'); + ->method('addDocument'); - $objectPersister = new ObjectSerializerPersister($typeMock, $transformer, 'SomeClass'); + $serializerMock = $this->getMockBuilder('FOS\ElasticaBundle\Serializer\Callback')->getMock(); + $serializerMock->expects($this->once())->method('serialize'); + + $objectPersister = new ObjectSerializerPersister($typeMock, $transformer, 'SomeClass', array($serializerMock, 'serialize')); $objectPersister->replaceOne(new POPO()); } @@ -62,9 +65,12 @@ class ObjectSerializerPersisterTest extends \PHPUnit_Framework_TestCase $typeMock->expects($this->never()) ->method('deleteById'); $typeMock->expects($this->once()) - ->method('addObject'); + ->method('addDocument'); - $objectPersister = new ObjectSerializerPersister($typeMock, $transformer, 'SomeClass'); + $serializerMock = $this->getMockBuilder('FOS\ElasticaBundle\Serializer\Callback')->getMock(); + $serializerMock->expects($this->once())->method('serialize'); + + $objectPersister = new ObjectSerializerPersister($typeMock, $transformer, 'SomeClass', array($serializerMock, 'serialize')); $objectPersister->insertOne(new POPO()); } @@ -79,9 +85,12 @@ class ObjectSerializerPersisterTest extends \PHPUnit_Framework_TestCase $typeMock->expects($this->once()) ->method('deleteById'); $typeMock->expects($this->never()) - ->method('addObject'); + ->method('addDocument'); - $objectPersister = new ObjectSerializerPersister($typeMock, $transformer, 'SomeClass'); + $serializerMock = $this->getMockBuilder('FOS\ElasticaBundle\Serializer\Callback')->getMock(); + $serializerMock->expects($this->once())->method('serialize'); + + $objectPersister = new ObjectSerializerPersister($typeMock, $transformer, 'SomeClass', array($serializerMock, 'serialize')); $objectPersister->deleteOne(new POPO()); } @@ -95,12 +104,17 @@ class ObjectSerializerPersisterTest extends \PHPUnit_Framework_TestCase ->getMock(); $typeMock->expects($this->never()) ->method('deleteById'); - $typeMock->expects($this->exactly(2)) + $typeMock->expects($this->never()) ->method('addObject'); $typeMock->expects($this->never()) ->method('addObjects'); + $typeMock->expects($this->once()) + ->method('addDocuments'); - $objectPersister = new ObjectSerializerPersister($typeMock, $transformer, 'SomeClass'); + $serializerMock = $this->getMockBuilder('FOS\ElasticaBundle\Serializer\Callback')->getMock(); + $serializerMock->expects($this->exactly(2))->method('serialize'); + + $objectPersister = new ObjectSerializerPersister($typeMock, $transformer, 'SomeClass', array($serializerMock, 'serialize')); $objectPersister->insertMany(array(new POPO(), new POPO())); } From cf3e35e892f8dcc40727b8d048819f00119ee87b Mon Sep 17 00:00:00 2001 From: Damien Alexandre Date: Mon, 2 Dec 2013 22:15:11 +0100 Subject: [PATCH 12/36] Remove extra line-break --- Persister/ObjectSerializerPersister.php | 1 - 1 file changed, 1 deletion(-) diff --git a/Persister/ObjectSerializerPersister.php b/Persister/ObjectSerializerPersister.php index 40305c4..c0d2d28 100644 --- a/Persister/ObjectSerializerPersister.php +++ b/Persister/ObjectSerializerPersister.php @@ -72,7 +72,6 @@ class ObjectSerializerPersister extends ObjectPersister $this->type->deleteById($id); } - /** * Inserts an array of objects in the type * From 25e59a311badec60ceccbabbe4b53e40d18acb38 Mon Sep 17 00:00:00 2001 From: Damien Alexandre Date: Mon, 2 Dec 2013 22:26:30 +0100 Subject: [PATCH 13/36] Fix Persister constructor and undefined property --- Persister/ObjectSerializerPersister.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Persister/ObjectSerializerPersister.php b/Persister/ObjectSerializerPersister.php index c0d2d28..8938a2f 100644 --- a/Persister/ObjectSerializerPersister.php +++ b/Persister/ObjectSerializerPersister.php @@ -15,11 +15,11 @@ use FOS\ElasticaBundle\Transformer\ModelToElasticaTransformerInterface; */ class ObjectSerializerPersister extends ObjectPersister { + protected $serializer; + public function __construct(Type $type, ModelToElasticaTransformerInterface $transformer, $objectClass, $serializer) { - $this->type = $type; - $this->transformer = $transformer; - $this->objectClass = $objectClass; + parent::__construct($type, $transformer, $objectClass, array()); $this->serializer = $serializer; } From 8a4848a16dc916afda6ebd3632e4d3894a571755 Mon Sep 17 00:00:00 2001 From: Damien Alexandre Date: Mon, 2 Dec 2013 22:35:50 +0100 Subject: [PATCH 14/36] Add UPGRADE-3.0.md file with descriptions of new features / changes --- UPGRADE-3.0.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 UPGRADE-3.0.md diff --git a/UPGRADE-3.0.md b/UPGRADE-3.0.md new file mode 100644 index 0000000..a9a99a4 --- /dev/null +++ b/UPGRADE-3.0.md @@ -0,0 +1,32 @@ +UPGRADE FROM 2.1 to 3.0 +======================= + +### Serialization + + * you can now define a Serializer service and callback for indexing. All providers and listeners will use it. + + ```yml + serializer: + callback_class: FOS\ElasticaBundle\Serializer\Callback + serializer: serializer + ``` + +### Mapping + + * you do not have to setup any mapping anymore if you use a Serializer, properties are no more indexed only if + they are mapped. So this kind of configuration became valid: + + ```yml + serializer: + callback_class: FOS\ElasticaBundle\Serializer\Callback + serializer: serializer + indexes: + acme: + client: default + types: + Article: + persistence: + driver: orm + model: Acme\Bundle\CoreBundle\Entity\Article + provider: ~ + ``` From 8b6dffbc141d07bdbd446680e1f854a20617686f Mon Sep 17 00:00:00 2001 From: Ben Longden Date: Tue, 3 Dec 2013 15:45:43 +0000 Subject: [PATCH 15/36] Adds enabled mapping option (default true) --- DependencyInjection/Configuration.php | 1 + 1 file changed, 1 insertion(+) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 4482a4c..af27f09 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -311,6 +311,7 @@ class Configuration implements ConfigurationInterface ->scalarNode('term_vector')->end() ->scalarNode('null_value')->end() ->booleanNode('include_in_all')->defaultValue(true)->end() + ->booleanNode('enabled')->defaultValue(true)->end() ->scalarNode('lat_lon')->end() ->scalarNode('index_name')->end() ->booleanNode('omit_norms')->end() From 759950aff66092489a5d40f79d2c5c2aea1a9194 Mon Sep 17 00:00:00 2001 From: Pierre du Plessis Date: Mon, 4 Nov 2013 17:24:35 +0200 Subject: [PATCH 16/36] Add option to disable logger or specify different logger service --- DependencyInjection/Configuration.php | 13 ++++++++++--- DependencyInjection/FOSElasticaExtension.php | 5 ++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 8a067b5..e05dd2e 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -76,8 +76,9 @@ class Configuration implements ConfigurationInterface return array( 'servers' => array( array( - 'host' => $v['host'], - 'port' => $v['port'], + 'host' => $v['host'], + 'port' => $v['port'], + 'logger' => $v['logger'] ) ) ); @@ -89,7 +90,8 @@ class Configuration implements ConfigurationInterface return array( 'servers' => array( array( - 'url' => $v['url'], + 'url' => $v['url'], + 'logger' => $v['logger'] ) ) ); @@ -102,6 +104,11 @@ class Configuration implements ConfigurationInterface ->scalarNode('url')->end() ->scalarNode('host')->end() ->scalarNode('port')->end() + ->scalarNode('logger') + ->defaultValue('fos_elastica.logger') + ->treatNullLike('fos_elastica.logger') + ->treatTrueLike('fos_elastica.logger') + ->end() ->end() ->end() ->end() diff --git a/DependencyInjection/FOSElasticaExtension.php b/DependencyInjection/FOSElasticaExtension.php index eb6dd4d..bcf8e84 100644 --- a/DependencyInjection/FOSElasticaExtension.php +++ b/DependencyInjection/FOSElasticaExtension.php @@ -73,7 +73,10 @@ class FOSElasticaExtension extends Extension foreach ($clients as $name => $clientConfig) { $clientId = sprintf('fos_elastica.client.%s', $name); $clientDef = new Definition('%fos_elastica.client.class%', array($clientConfig)); - $clientDef->addMethodCall('setLogger', array(new Reference('fos_elastica.logger'))); + $logger = $clientConfig['servers'][0]['logger']; + if (false !== $logger) { + $clientDef->addMethodCall('setLogger', array(new Reference($logger))); + } $clientDef->addTag('fos_elastica.client'); $container->setDefinition($clientId, $clientDef); From 08193ec2fd9b0e47961cbe9c64289bff91500729 Mon Sep 17 00:00:00 2001 From: Lea Haensenberger Date: Thu, 5 Dec 2013 16:35:30 +0100 Subject: [PATCH 17/36] Add support to disable the _all field for a type --- DependencyInjection/Configuration.php | 18 ++++++++++++++++++ DependencyInjection/FOSElasticaExtension.php | 3 +++ 2 files changed, 21 insertions(+) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index f9f633f..3c3ea9e 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -288,6 +288,7 @@ class Configuration implements ConfigurationInterface ->append($this->getBoostNode()) ->append($this->getRoutingNode()) ->append($this->getParentNode()) + ->append($this->getAllNode()) ->end() ; @@ -530,4 +531,21 @@ class Configuration implements ConfigurationInterface return $node; } + + /** + * Returns the array node used for "_all" + */ + protected function getAllNode() + { + $builder = new TreeBuilder(); + $node = $builder->root('_all'); + + $node + ->children() + ->scalarNode('enabled')->defaultValue(true)->end() + ->end() + ; + + return $node; + } } diff --git a/DependencyInjection/FOSElasticaExtension.php b/DependencyInjection/FOSElasticaExtension.php index d83b604..cba715c 100644 --- a/DependencyInjection/FOSElasticaExtension.php +++ b/DependencyInjection/FOSElasticaExtension.php @@ -231,6 +231,9 @@ class FOSElasticaExtension extends Extension if (isset($type['index'])) { $this->indexConfigs[$indexName]['config']['mappings'][$name]['index'] = $type['index']; } + if (isset($type['_all'])) { + $this->indexConfigs[$indexName]['config']['mappings'][$name]['_all'] = $type['_all']; + } } } From c97a4abceb3fe0b42c660b30c63e721ee888c6d7 Mon Sep 17 00:00:00 2001 From: Damien Alexandre Date: Mon, 9 Dec 2013 10:09:36 +0100 Subject: [PATCH 18/36] Add display cURL on the web debug queries shower --- Resources/views/Collector/elastica.html.twig | 35 +++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/Resources/views/Collector/elastica.html.twig b/Resources/views/Collector/elastica.html.twig index a665e2d..bfbd31a 100644 --- a/Resources/views/Collector/elastica.html.twig +++ b/Resources/views/Collector/elastica.html.twig @@ -41,7 +41,7 @@

{% else %}
    - {% for query in collector.queries %} + {% for key, query in collector.queries %}
  • Path: {{ query.path }}
    Method: {{ query.method }} @@ -51,8 +51,41 @@ Time: {{ '%0.2f'|format(query.executionMS * 1000) }} ms + + + + + - + Display cURL query + + +
  • {% endfor %}
+ + {% endif %} {% endblock %} From ca507a5e347d376729f6496030c40ff1eac35b7f Mon Sep 17 00:00:00 2001 From: Damien Alexandre Date: Mon, 9 Dec 2013 10:40:47 +0100 Subject: [PATCH 19/36] Add full_host in logger for HTTP(s) queries --- Client.php | 13 ++++++++++++- Logger/ElasticaLogger.php | 6 ++++-- Resources/views/Collector/elastica.html.twig | 19 ++++++++++--------- Tests/Logger/ElasticaLoggerTest.php | 7 ++++--- 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/Client.php b/Client.php index 8430b36..8c9be1a 100644 --- a/Client.php +++ b/Client.php @@ -4,6 +4,8 @@ namespace FOS\ElasticaBundle; use Elastica\Client as ElasticaClient; use Elastica\Request; +use Elastica\Transport\Http; +use Elastica\Transport\Https; /** * @author Gordon Franke @@ -17,7 +19,16 @@ class Client extends ElasticaClient if (null !== $this->_logger) { $time = microtime(true) - $start; - $this->_logger->logQuery($path, $method, $data, $time); + + $connection = $this->getLastRequest()->getConnection(); + $transport = $connection->getTransportObject(); + $full_host = null; + + if ($transport instanceof Http || $transport instanceof Https) { + $full_host = $connection->getTransport().'://'.$connection->getHost().':'.$connection->getPort(); + } + + $this->_logger->logQuery($path, $method, $data, $time, $full_host); } return $response; diff --git a/Logger/ElasticaLogger.php b/Logger/ElasticaLogger.php index 1705d06..2d8d0ae 100644 --- a/Logger/ElasticaLogger.php +++ b/Logger/ElasticaLogger.php @@ -38,15 +38,17 @@ class ElasticaLogger implements LoggerInterface * @param string $method Rest method to use (GET, POST, DELETE, PUT) * @param array $data arguments * @param float $time execution time + * @param string $full_host host and port of the query */ - public function logQuery($path, $method, $data, $time) + public function logQuery($path, $method, $data, $time, $full_host = null) { if ($this->debug) { $this->queries[] = array( 'path' => $path, 'method' => $method, 'data' => $data, - 'executionMS' => $time + 'executionMS' => $time, + 'full_host' => $full_host ); } diff --git a/Resources/views/Collector/elastica.html.twig b/Resources/views/Collector/elastica.html.twig index bfbd31a..8b27376 100644 --- a/Resources/views/Collector/elastica.html.twig +++ b/Resources/views/Collector/elastica.html.twig @@ -52,15 +52,17 @@ Time: {{ '%0.2f'|format(query.executionMS * 1000) }} ms - - + - - - Display cURL query - + {% if query.full_host %} + + + + - + Display cURL query + - + + {% endif %} {% endfor %} @@ -79,7 +81,6 @@ } else { sections[0].style.display = 'inline'; sections[1].style.display = 'none'; - //sections[3].style.display = 'none'; document.getElementById(link.hash.replace("#", "")).style.display = 'none'; } diff --git a/Tests/Logger/ElasticaLoggerTest.php b/Tests/Logger/ElasticaLoggerTest.php index 3cf6d2d..73ba4ac 100644 --- a/Tests/Logger/ElasticaLoggerTest.php +++ b/Tests/Logger/ElasticaLoggerTest.php @@ -9,7 +9,6 @@ use FOS\ElasticaBundle\Logger\ElasticaLogger; */ class ElasticaLoggerTest extends \PHPUnit_Framework_TestCase { - public function testGetZeroIfNoQueriesAdded() { $elasticaLogger = new ElasticaLogger; @@ -36,15 +35,17 @@ class ElasticaLoggerTest extends \PHPUnit_Framework_TestCase $method = 'testMethod'; $data = array('data'); $time = 12; + $full_host = 'http://example.com:9200'; $expected = array( 'path' => $path, 'method' => $method, 'data' => $data, - 'executionMS' => $time + 'executionMS' => $time, + 'full_host' => $full_host, ); - $elasticaLogger->logQuery($path, $method, $data, $time); + $elasticaLogger->logQuery($path, $method, $data, $time, $full_host); $returnedQueries = $elasticaLogger->getQueries(); $this->assertEquals($expected, $returnedQueries[0]); } From 1ddd7c0e0cb054e7438a3c07fbd6e633267dc503 Mon Sep 17 00:00:00 2001 From: Damien Alexandre Date: Mon, 9 Dec 2013 14:46:06 +0100 Subject: [PATCH 20/36] Store connection infos for all transports, display infos in debug --- Client.php | 14 ++++++-------- Logger/ElasticaLogger.php | 6 +++--- Resources/views/Collector/elastica.html.twig | 6 +++--- Tests/Logger/ElasticaLoggerTest.php | 6 +++--- 4 files changed, 15 insertions(+), 17 deletions(-) diff --git a/Client.php b/Client.php index 8c9be1a..119f420 100644 --- a/Client.php +++ b/Client.php @@ -4,8 +4,6 @@ namespace FOS\ElasticaBundle; use Elastica\Client as ElasticaClient; use Elastica\Request; -use Elastica\Transport\Http; -use Elastica\Transport\Https; /** * @author Gordon Franke @@ -21,14 +19,14 @@ class Client extends ElasticaClient $time = microtime(true) - $start; $connection = $this->getLastRequest()->getConnection(); - $transport = $connection->getTransportObject(); - $full_host = null; - if ($transport instanceof Http || $transport instanceof Https) { - $full_host = $connection->getTransport().'://'.$connection->getHost().':'.$connection->getPort(); - } + $connection_array = array( + 'host' => $connection->getHost(), + 'port' => $connection->getPort(), + 'transport' => $connection->getTransport(), + ); - $this->_logger->logQuery($path, $method, $data, $time, $full_host); + $this->_logger->logQuery($path, $method, $data, $time, $connection_array); } return $response; diff --git a/Logger/ElasticaLogger.php b/Logger/ElasticaLogger.php index 2d8d0ae..bf7694e 100644 --- a/Logger/ElasticaLogger.php +++ b/Logger/ElasticaLogger.php @@ -38,9 +38,9 @@ class ElasticaLogger implements LoggerInterface * @param string $method Rest method to use (GET, POST, DELETE, PUT) * @param array $data arguments * @param float $time execution time - * @param string $full_host host and port of the query + * @param array $connection host, port and transport of the query */ - public function logQuery($path, $method, $data, $time, $full_host = null) + public function logQuery($path, $method, $data, $time, $connection = array()) { if ($this->debug) { $this->queries[] = array( @@ -48,7 +48,7 @@ class ElasticaLogger implements LoggerInterface 'method' => $method, 'data' => $data, 'executionMS' => $time, - 'full_host' => $full_host + 'connection' => $connection ); } diff --git a/Resources/views/Collector/elastica.html.twig b/Resources/views/Collector/elastica.html.twig index 8b27376..5da6048 100644 --- a/Resources/views/Collector/elastica.html.twig +++ b/Resources/views/Collector/elastica.html.twig @@ -44,7 +44,7 @@ {% for key, query in collector.queries %}
  • Path: {{ query.path }}
    - Method: {{ query.method }} + Method: {{ query.method }} ({{ query.connection.transport }} on {{ query.connection.host }}:{{ query.connection.port }})
    {{ query.data|json_encode }}
    @@ -52,7 +52,7 @@ Time: {{ '%0.2f'|format(query.executionMS * 1000) }} ms - {% if query.full_host %} + {% if query.connection.transport in ['Http', 'Https'] %}{# cURL support only HTTP #} + - @@ -60,7 +60,7 @@ {% endif %}
  • diff --git a/Tests/Logger/ElasticaLoggerTest.php b/Tests/Logger/ElasticaLoggerTest.php index 73ba4ac..18eb44b 100644 --- a/Tests/Logger/ElasticaLoggerTest.php +++ b/Tests/Logger/ElasticaLoggerTest.php @@ -35,17 +35,17 @@ class ElasticaLoggerTest extends \PHPUnit_Framework_TestCase $method = 'testMethod'; $data = array('data'); $time = 12; - $full_host = 'http://example.com:9200'; + $connection = array('host' => 'localhost', 'port' => '8999', 'transport' => 'https'); $expected = array( 'path' => $path, 'method' => $method, 'data' => $data, 'executionMS' => $time, - 'full_host' => $full_host, + 'connection' => $connection, ); - $elasticaLogger->logQuery($path, $method, $data, $time, $full_host); + $elasticaLogger->logQuery($path, $method, $data, $time, $connection); $returnedQueries = $elasticaLogger->getQueries(); $this->assertEquals($expected, $returnedQueries[0]); } From 70e6cb2e7ea3459ab2f8040f87d1a41e17fa13b3 Mon Sep 17 00:00:00 2001 From: Tim Nagel Date: Tue, 10 Dec 2013 21:25:38 +1100 Subject: [PATCH 21/36] Fix issue with logger not having logQuery method --- Client.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Client.php b/Client.php index 119f420..ea0c572 100644 --- a/Client.php +++ b/Client.php @@ -4,6 +4,7 @@ namespace FOS\ElasticaBundle; use Elastica\Client as ElasticaClient; use Elastica\Request; +use FOS\ElasticaBundle\Logger\ElasticaLogger; /** * @author Gordon Franke @@ -15,7 +16,7 @@ class Client extends ElasticaClient $start = microtime(true); $response = parent::request($path, $method, $data, $query); - if (null !== $this->_logger) { + if (null !== $this->_logger and $this->_logger instanceof ElasticaLogger) { $time = microtime(true) - $start; $connection = $this->getLastRequest()->getConnection(); From e16ece821d662429ceaf3cf7724874bbfe54335a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cs=C3=A1sz=C3=A1r=20D=C3=A1niel?= Date: Tue, 10 Dec 2013 13:54:50 +0100 Subject: [PATCH 22/36] Fix client configuration missing logger --- DependencyInjection/Configuration.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index a9c9898..8d913f0 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -78,7 +78,7 @@ class Configuration implements ConfigurationInterface array( 'host' => $v['host'], 'port' => $v['port'], - 'logger' => $v['logger'] + 'logger' => isset($v['logger']) ? $v['logger'] : null ) ) ); @@ -91,7 +91,7 @@ class Configuration implements ConfigurationInterface 'servers' => array( array( 'url' => $v['url'], - 'logger' => $v['logger'] + 'logger' => isset($v['logger']) ? $v['logger'] : null ) ) ); From eaf52fa9072c5905b035e69d9c2585218b53838e Mon Sep 17 00:00:00 2001 From: Lea Haensenberger Date: Tue, 10 Dec 2013 08:42:56 +0100 Subject: [PATCH 23/36] Allow a more dynamic mapping for dynamic templates --- DependencyInjection/Configuration.php | 61 ++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 10 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 8d913f0..70718ef 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -341,16 +341,7 @@ class Configuration implements ConfigurationInterface ->children() ->scalarNode('match')->isRequired()->end() ->scalarNode('match_mapping_type')->end() - ->arrayNode('mapping') - ->isRequired() - ->children() - ->scalarNode('type')->end() - ->scalarNode('index')->end() - ->arrayNode('fields') - ->children() - ->end() - ->end() - ->end() + ->append($this->getDynamicTemplateMapping()) ->end() ->end() ; @@ -358,6 +349,21 @@ class Configuration implements ConfigurationInterface return $node; } + /** + * @return the array node used for mapping in dynamic templates + */ + protected function getDynamicTemplateMapping() + { + $builder = new TreeBuilder(); + $node = $builder->root('mapping'); + + $nestings = $this->getNestingsForDynamicTemplates(); + + $this->addFieldConfig($node->children(), $nestings); + + return $node; + } + /** * @param \Symfony\Component\Config\Definition\Builder\NodeBuilder $node The node to which to attach the field config to * @param array $nestings the nested mappings for the current field level @@ -451,6 +457,41 @@ class Configuration implements ConfigurationInterface return $nestings; } + /** + * @return array The unique nested mappings for all dynamic templates + */ + protected function getNestingsForDynamicTemplates() + { + if (!isset($this->configArray[0]['indexes'])) { + return array(); + } + + $nestings = array(); + foreach ($this->configArray[0]['indexes'] as $index) { + if (empty($index['types'])) { + continue; + } + + foreach ($index['types'] as $type) { + if (empty($type['dynamic_templates'])) { + continue; + } + + foreach ($type['dynamic_templates'] as $definition) { + $field = $definition['mapping']; + + if (isset($field['fields'])) { + $this->addPropertyNesting($field, $nestings, 'fields'); + } else if (isset($field['properties'])) { + $this->addPropertyNesting($field, $nestings, 'properties'); + } + } + + } + } + return $nestings; + } + /** * @param array $mappings The mappings for the current type * @return array The nested mappings defined for this type From bad1769c844207f9d64247eb30811f1fcdca1eaf Mon Sep 17 00:00:00 2001 From: Lea Haensenberger Date: Tue, 10 Dec 2013 08:49:20 +0100 Subject: [PATCH 24/36] Fixing tests --- Tests/DependencyInjection/ConfigurationTest.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Tests/DependencyInjection/ConfigurationTest.php b/Tests/DependencyInjection/ConfigurationTest.php index ead9977..84285fb 100644 --- a/Tests/DependencyInjection/ConfigurationTest.php +++ b/Tests/DependencyInjection/ConfigurationTest.php @@ -63,13 +63,10 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase $this->assertArrayHasKey('type', $mapping); $this->assertInstanceOf('Symfony\Component\Config\Definition\ScalarNode', $mapping['type']); - $this->assertNull($mapping['type']->getDefaultValue()); + $this->assertSame('string', $mapping['type']->getDefaultValue()); $this->assertArrayHasKey('index', $mapping); $this->assertInstanceOf('Symfony\Component\Config\Definition\ScalarNode', $mapping['index']); $this->assertNull($mapping['index']->getDefaultValue()); - - $this->assertArrayHasKey('fields', $mapping); - $this->assertInstanceOf('Symfony\Component\Config\Definition\ArrayNode', $mapping['fields']); } } From 09031457cd5159044127e4f5c2a404062ab5912d Mon Sep 17 00:00:00 2001 From: Lea Haensenberger Date: Tue, 10 Dec 2013 09:02:17 +0100 Subject: [PATCH 25/36] Allow more matching methods --- DependencyInjection/Configuration.php | 6 +++++- Tests/DependencyInjection/ConfigurationTest.php | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 70718ef..e9088e8 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -339,8 +339,12 @@ class Configuration implements ConfigurationInterface ->useAttributeAsKey('name') ->prototype('array') ->children() - ->scalarNode('match')->isRequired()->end() + ->scalarNode('match')->end() + ->scalarNode('unmatch')->end() ->scalarNode('match_mapping_type')->end() + ->scalarNode('path_match')->end() + ->scalarNode('path_unmatch')->end() + ->scalarNode('match_pattern')->end() ->append($this->getDynamicTemplateMapping()) ->end() ->end() diff --git a/Tests/DependencyInjection/ConfigurationTest.php b/Tests/DependencyInjection/ConfigurationTest.php index 84285fb..53d060d 100644 --- a/Tests/DependencyInjection/ConfigurationTest.php +++ b/Tests/DependencyInjection/ConfigurationTest.php @@ -48,6 +48,22 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('Symfony\Component\Config\Definition\ScalarNode', $dynamicTemplates['match_mapping_type']); $this->assertNull($dynamicTemplates['match_mapping_type']->getDefaultValue()); + $this->assertArrayHasKey('unmatch', $dynamicTemplates); + $this->assertInstanceOf('Symfony\Component\Config\Definition\ScalarNode', $dynamicTemplates['unmatch']); + $this->assertNull($dynamicTemplates['unmatch']->getDefaultValue()); + + $this->assertArrayHasKey('path_match', $dynamicTemplates); + $this->assertInstanceOf('Symfony\Component\Config\Definition\ScalarNode', $dynamicTemplates['path_match']); + $this->assertNull($dynamicTemplates['path_match']->getDefaultValue()); + + $this->assertArrayHasKey('path_unmatch', $dynamicTemplates); + $this->assertInstanceOf('Symfony\Component\Config\Definition\ScalarNode', $dynamicTemplates['v']); + $this->assertNull($dynamicTemplates['path_unmatch']->getDefaultValue()); + + $this->assertArrayHasKey('match_pattern', $dynamicTemplates); + $this->assertInstanceOf('Symfony\Component\Config\Definition\ScalarNode', $dynamicTemplates['match_pattern']); + $this->assertNull($dynamicTemplates['match_pattern']->getDefaultValue()); + $this->assertArrayHasKey('mapping', $dynamicTemplates); $this->assertInstanceOf('Symfony\Component\Config\Definition\ArrayNode', $dynamicTemplates['mapping']); } From e55c7e8632556bce7d58fbf9cfa3f0936a32b1d2 Mon Sep 17 00:00:00 2001 From: Lea Haensenberger Date: Tue, 10 Dec 2013 09:03:27 +0100 Subject: [PATCH 26/36] Set match to '*' by default --- DependencyInjection/Configuration.php | 2 +- Tests/DependencyInjection/ConfigurationTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index e9088e8..c790f66 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -339,7 +339,7 @@ class Configuration implements ConfigurationInterface ->useAttributeAsKey('name') ->prototype('array') ->children() - ->scalarNode('match')->end() + ->scalarNode('match')->defaultValue('*')->end() ->scalarNode('unmatch')->end() ->scalarNode('match_mapping_type')->end() ->scalarNode('path_match')->end() diff --git a/Tests/DependencyInjection/ConfigurationTest.php b/Tests/DependencyInjection/ConfigurationTest.php index 53d060d..f3ff4a6 100644 --- a/Tests/DependencyInjection/ConfigurationTest.php +++ b/Tests/DependencyInjection/ConfigurationTest.php @@ -42,7 +42,7 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase $this->assertArrayHasKey('match', $dynamicTemplates); $this->assertInstanceOf('Symfony\Component\Config\Definition\ScalarNode', $dynamicTemplates['match']); - $this->assertNull($dynamicTemplates['match']->getDefaultValue()); + $this->assertSame('*', $dynamicTemplates['match']->getDefaultValue()); $this->assertArrayHasKey('match_mapping_type', $dynamicTemplates); $this->assertInstanceOf('Symfony\Component\Config\Definition\ScalarNode', $dynamicTemplates['match_mapping_type']); From 0aa98d2295d536a4b4f0373e1efee33e0f1ca201 Mon Sep 17 00:00:00 2001 From: Lea Haensenberger Date: Tue, 10 Dec 2013 12:58:34 +0100 Subject: [PATCH 27/36] Fixing copy&paste error --- Tests/DependencyInjection/ConfigurationTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/DependencyInjection/ConfigurationTest.php b/Tests/DependencyInjection/ConfigurationTest.php index f3ff4a6..4ea5657 100644 --- a/Tests/DependencyInjection/ConfigurationTest.php +++ b/Tests/DependencyInjection/ConfigurationTest.php @@ -57,7 +57,7 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase $this->assertNull($dynamicTemplates['path_match']->getDefaultValue()); $this->assertArrayHasKey('path_unmatch', $dynamicTemplates); - $this->assertInstanceOf('Symfony\Component\Config\Definition\ScalarNode', $dynamicTemplates['v']); + $this->assertInstanceOf('Symfony\Component\Config\Definition\ScalarNode', $dynamicTemplates['path_unmatch']); $this->assertNull($dynamicTemplates['path_unmatch']->getDefaultValue()); $this->assertArrayHasKey('match_pattern', $dynamicTemplates); From a59385af7b08439c02271904ea3b96c6000f36fd Mon Sep 17 00:00:00 2001 From: Lea Haensenberger Date: Tue, 10 Dec 2013 15:37:41 +0100 Subject: [PATCH 28/36] Do not set default value to *, might conflict when using e.g. path_match and match_pattern regex together --- DependencyInjection/Configuration.php | 2 +- Tests/DependencyInjection/ConfigurationTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index c790f66..e9088e8 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -339,7 +339,7 @@ class Configuration implements ConfigurationInterface ->useAttributeAsKey('name') ->prototype('array') ->children() - ->scalarNode('match')->defaultValue('*')->end() + ->scalarNode('match')->end() ->scalarNode('unmatch')->end() ->scalarNode('match_mapping_type')->end() ->scalarNode('path_match')->end() diff --git a/Tests/DependencyInjection/ConfigurationTest.php b/Tests/DependencyInjection/ConfigurationTest.php index 4ea5657..35b2af3 100644 --- a/Tests/DependencyInjection/ConfigurationTest.php +++ b/Tests/DependencyInjection/ConfigurationTest.php @@ -42,7 +42,7 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase $this->assertArrayHasKey('match', $dynamicTemplates); $this->assertInstanceOf('Symfony\Component\Config\Definition\ScalarNode', $dynamicTemplates['match']); - $this->assertSame('*', $dynamicTemplates['match']->getDefaultValue()); + $this->assertNull($dynamicTemplates['match']->getDefaultValue()); $this->assertArrayHasKey('match_mapping_type', $dynamicTemplates); $this->assertInstanceOf('Symfony\Component\Config\Definition\ScalarNode', $dynamicTemplates['match_mapping_type']); From 090f02b05d827b0f242d70434f17b46138564f6b Mon Sep 17 00:00:00 2001 From: Craig Marvelley Date: Wed, 11 Dec 2013 22:52:25 +0000 Subject: [PATCH 29/36] Improve test coverage --- Logger/ElasticaLogger.php | 2 +- Tests/ClientTest.php | 42 ++++++++++++++ Tests/FOSElasticaBundleTest.php | 39 +++++++++++++ Tests/HybridResultTest.php | 19 ++++++ Tests/Logger/ElasticaLoggerTest.php | 90 +++++++++++++++++++++++++++-- Tests/RepositoryTest.php | 16 +++++ 6 files changed, 203 insertions(+), 5 deletions(-) create mode 100644 Tests/ClientTest.php create mode 100644 Tests/FOSElasticaBundleTest.php create mode 100644 Tests/HybridResultTest.php diff --git a/Logger/ElasticaLogger.php b/Logger/ElasticaLogger.php index bf7694e..7aacac5 100644 --- a/Logger/ElasticaLogger.php +++ b/Logger/ElasticaLogger.php @@ -147,6 +147,6 @@ class ElasticaLogger implements LoggerInterface */ public function log($level, $message, array $context = array()) { - return $this->logger->log($message, $context); + return $this->logger->log($level, $message, $context); } } diff --git a/Tests/ClientTest.php b/Tests/ClientTest.php new file mode 100644 index 0000000..c8509cf --- /dev/null +++ b/Tests/ClientTest.php @@ -0,0 +1,42 @@ +getMock('Elastica\Connection'); + $connection->expects($this->any())->method('getTransportObject')->will($this->returnValue($transport)); + $connection->expects($this->any())->method('toArray')->will($this->returnValue(array())); + + $logger = $this->getMock('FOS\ElasticaBundle\Logger\ElasticaLogger'); + $logger + ->expects($this->once()) + ->method('logQuery') + ->with( + 'foo', + Request::GET, + $this->isType('array'), + $this->isType('float'), + $this->isType('array') + ); + + $client = $this->getMockBuilder('FOS\ElasticaBundle\Client') + ->setMethods(array('getConnection')) + ->getMock(); + + $client->expects($this->any())->method('getConnection')->will($this->returnValue($connection)); + + $client->setLogger($logger); + + $response = $client->request('foo'); + + $this->assertInstanceOf('Elastica\Response', $response); + } +} diff --git a/Tests/FOSElasticaBundleTest.php b/Tests/FOSElasticaBundleTest.php new file mode 100644 index 0000000..2bfc7f9 --- /dev/null +++ b/Tests/FOSElasticaBundleTest.php @@ -0,0 +1,39 @@ +getMock('Symfony\Component\DependencyInjection\ContainerBuilder'); + + $container + ->expects($this->at(0)) + ->method('addCompilerPass') + ->with($this->isInstanceOf($passes[0][0]), $passes[0][1]); + + $container + ->expects($this->at(1)) + ->method('addCompilerPass') + ->with($this->isInstanceOf($passes[1][0])); + + $bundle = new FOSElasticaBundle(); + + $bundle->build($container); + } +} diff --git a/Tests/HybridResultTest.php b/Tests/HybridResultTest.php new file mode 100644 index 0000000..cb382d1 --- /dev/null +++ b/Tests/HybridResultTest.php @@ -0,0 +1,19 @@ +assertSame($result, $hybridResult->getResult()); + $this->assertNull($hybridResult->getTransformed()); + } +} diff --git a/Tests/Logger/ElasticaLoggerTest.php b/Tests/Logger/ElasticaLoggerTest.php index 05624cc..30ce30c 100644 --- a/Tests/Logger/ElasticaLoggerTest.php +++ b/Tests/Logger/ElasticaLoggerTest.php @@ -9,6 +9,40 @@ use FOS\ElasticaBundle\Logger\ElasticaLogger; */ class ElasticaLoggerTest extends \PHPUnit_Framework_TestCase { + /** + * @return \PHPUnit_Framework_MockObject_MockObject|\Symfony\Component\HttpKernel\Log\LoggerInterface + */ + private function getMockLogger() + { + return $this->getMockBuilder('Symfony\Component\HttpKernel\Log\LoggerInterface') + ->disableOriginalConstructor() + ->getMock(); + } + + /** + * @param string $level + * @param string $message + * @param array $context + * @return ElasticaLogger + */ + private function getMockLoggerForLevelMessageAndContext($level, $message, $context) + { + $loggerMock = $this->getMockBuilder('Symfony\Component\HttpKernel\Log\LoggerInterface') + ->disableOriginalConstructor() + ->getMock(); + + $loggerMock->expects($this->once()) + ->method($level) + ->with( + $this->equalTo($message), + $this->equalTo($context) + ); + + $elasticaLogger = new ElasticaLogger($loggerMock); + + return $elasticaLogger; + } + public function testGetZeroIfNoQueriesAdded() { $elasticaLogger = new ElasticaLogger; @@ -64,10 +98,7 @@ class ElasticaLoggerTest extends \PHPUnit_Framework_TestCase public function testQueryIsLogged() { - /** @var $loggerMock \PHPUnit_Framework_MockObject_MockObject|\Symfony\Component\HttpKernel\Log\LoggerInterface */ - $loggerMock = $this->getMockBuilder('Symfony\Component\HttpKernel\Log\LoggerInterface') - ->disableOriginalConstructor() - ->getMock(); + $loggerMock = $this->getMockLogger(); $elasticaLogger = new ElasticaLogger($loggerMock); @@ -87,4 +118,55 @@ class ElasticaLoggerTest extends \PHPUnit_Framework_TestCase $elasticaLogger->logQuery($path, $method, $data, $time); } + + /** + * @return array + */ + public function logLevels() + { + return array( + array('emergency'), + array('alert'), + array('critical'), + array('error'), + array('warning'), + array('notice'), + array('info'), + array('debug'), + ); + } + + /** + * @dataProvider logLevels + */ + public function testMessagesCanBeLoggedAtSpecificLogLevels($level) + { + $message = 'foo'; + $context = array('data'); + + $loggerMock = $this->getMockLoggerForLevelMessageAndContext($level, $message, $context); + + call_user_func(array($loggerMock, $level), $message, $context); + } + + public function testMessagesCanBeLoggedToArbitraryLevels() + { + $loggerMock = $this->getMockLogger(); + + $level = 'info'; + $message = 'foo'; + $context = array('data'); + + $loggerMock->expects($this->once()) + ->method('log') + ->with( + $level, + $message, + $context + ); + + $elasticaLogger = new ElasticaLogger($loggerMock); + + $elasticaLogger->log($level, $message, $context); + } } diff --git a/Tests/RepositoryTest.php b/Tests/RepositoryTest.php index 6eabe7b..c4d4efc 100644 --- a/Tests/RepositoryTest.php +++ b/Tests/RepositoryTest.php @@ -58,6 +58,22 @@ class RepositoryTest extends \PHPUnit_Framework_TestCase $repository->findPaginated($testQuery); } + public function testThatCreatePaginatorCreatesAPaginatorViaFinder() + { + $testQuery = 'Test Query'; + + /** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\FOS\ElasticaBundle\Finder\TransformedFinder */ + $finderMock = $this->getMockBuilder('FOS\ElasticaBundle\Finder\TransformedFinder') + ->disableOriginalConstructor() + ->getMock(); + $finderMock->expects($this->once()) + ->method('createPaginatorAdapter') + ->with($this->equalTo($testQuery)); + + $repository = new Repository($finderMock); + $repository->createPaginatorAdapter($testQuery); + } + public function testThatFindHybridCallsFindHybridOnFinder() { $testQuery = 'Test Query'; From 11a87c5ce3fbe1b7a4e240b494d2be12f4973000 Mon Sep 17 00:00:00 2001 From: Craig Marvelley Date: Thu, 12 Dec 2013 20:48:15 +0000 Subject: [PATCH 30/36] Add test for ElasticaToModelTransformerCollection::hybridTransform --- ...asticaToModelTransformerCollectionTest.php | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/Tests/Transformer/ElasticaToModelTransformerCollectionTest.php b/Tests/Transformer/ElasticaToModelTransformerCollectionTest.php index d33708e..bb5f6a3 100644 --- a/Tests/Transformer/ElasticaToModelTransformerCollectionTest.php +++ b/Tests/Transformer/ElasticaToModelTransformerCollectionTest.php @@ -3,6 +3,7 @@ namespace FOS\ElasticaBundle\Tests\Transformer; use Elastica\Document; +use Elastica\Result; use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerCollection; class ElasticaToModelTransformerCollectionTest extends \PHPUnit_Framework_TestCase @@ -98,6 +99,57 @@ class ElasticaToModelTransformerCollectionTest extends \PHPUnit_Framework_TestCa $result2, ), $results); } + + public function testGetIdentifierFieldReturnsAMapOfIdentifiers() + { + $collection = new ElasticaToModelTransformerCollection(array()); + $identifiers = $collection->getIdentifierField(); + $this->assertInternalType('array', $identifiers); + $this->assertEmpty($identifiers); + + $this->collectionSetup(); + $identifiers = $this->collection->getIdentifierField(); + $this->assertInternalType('array', $identifiers); + $this->assertEquals(array('type1' => 'id', 'type2' => 'id'), $identifiers); + } + + public function elasticaResults() + { + $document = new Result(array('_id' => 123, '_type' => 'type1')); + $result = new POPO(123, array()); + + return array( + array( + $document, $result + ) + ); + } + + /** + * @dataProvider elasticaResults + */ + public function testHybridTransformDecoratesResultsWithHybridResultObjects($document, $result) + { + $transformer = $this->getMock('FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface'); + $transformer->expects($this->any())->method('getIdentifierField')->will($this->returnValue('id')); + + $transformer + ->expects($this->any()) + ->method('transform') + ->will($this->returnValue(array($result))); + + $collection = new ElasticaToModelTransformerCollection(array('type1' => $transformer)); + + $hybridResults = $collection->hybridTransform(array($document)); + + $this->assertInternalType('array', $hybridResults); + $this->assertNotEmpty($hybridResults); + $this->assertContainsOnlyInstancesOf('FOS\ElasticaBundle\HybridResult', $hybridResults); + + $hybridResult = array_pop($hybridResults); + $this->assertEquals($document, $hybridResult->getResult()); + $this->assertEquals($result, $hybridResult->getTransformed()); + } } class POPO From 5d16ffb1bf31c01d0748e2f6b6950af3989d2b2c Mon Sep 17 00:00:00 2001 From: Craig Marvelley Date: Thu, 12 Dec 2013 20:50:15 +0000 Subject: [PATCH 31/36] Remove unused method --- Transformer/ElasticaToModelTransformerCollection.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/Transformer/ElasticaToModelTransformerCollection.php b/Transformer/ElasticaToModelTransformerCollection.php index 6264959..a261e81 100644 --- a/Transformer/ElasticaToModelTransformerCollection.php +++ b/Transformer/ElasticaToModelTransformerCollection.php @@ -84,11 +84,4 @@ class ElasticaToModelTransformerCollection implements ElasticaToModelTransformer return $result; } - - protected function getTypeToClassMap() - { - return array_map(function (ElasticaToModelTransformerInterface $transformer) { - return $transformer->getObjectClass(); - }, $this->transformers); - } } From e0ef8dff23dd7d100905bb8a8a28ee07ef89a660 Mon Sep 17 00:00:00 2001 From: Burkhard Reffeling Date: Thu, 12 Dec 2013 22:41:20 +0000 Subject: [PATCH 32/36] removed duplicate methods --- Persister/ObjectSerializerPersister.php | 63 ------------------------- 1 file changed, 63 deletions(-) diff --git a/Persister/ObjectSerializerPersister.php b/Persister/ObjectSerializerPersister.php index 8938a2f..1a15656 100644 --- a/Persister/ObjectSerializerPersister.php +++ b/Persister/ObjectSerializerPersister.php @@ -23,69 +23,6 @@ class ObjectSerializerPersister extends ObjectPersister $this->serializer = $serializer; } - /** - * 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->addDocument($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->addDocument($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) - { - $docs = array(); - foreach ($objects as $object) { - $docs[] = $this->transformToElasticaDocument($object); - } - $this->type->addDocuments($docs); - } - /** * Transforms an object to an elastica document * with just the identifier set From 292af9f039e2c52b4d02fb9c0f2f916b068f67f9 Mon Sep 17 00:00:00 2001 From: Craig Marvelley Date: Thu, 12 Dec 2013 22:55:37 +0000 Subject: [PATCH 33/36] Better variable names --- .../ElasticaToModelTransformerCollectionTest.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Tests/Transformer/ElasticaToModelTransformerCollectionTest.php b/Tests/Transformer/ElasticaToModelTransformerCollectionTest.php index bb5f6a3..eb4d8e4 100644 --- a/Tests/Transformer/ElasticaToModelTransformerCollectionTest.php +++ b/Tests/Transformer/ElasticaToModelTransformerCollectionTest.php @@ -115,12 +115,12 @@ class ElasticaToModelTransformerCollectionTest extends \PHPUnit_Framework_TestCa public function elasticaResults() { - $document = new Result(array('_id' => 123, '_type' => 'type1')); - $result = new POPO(123, array()); + $result = new Result(array('_id' => 123, '_type' => 'type1')); + $transformedObject = new POPO(123, array()); return array( array( - $document, $result + $result, $transformedObject ) ); } @@ -128,7 +128,7 @@ class ElasticaToModelTransformerCollectionTest extends \PHPUnit_Framework_TestCa /** * @dataProvider elasticaResults */ - public function testHybridTransformDecoratesResultsWithHybridResultObjects($document, $result) + public function testHybridTransformDecoratesResultsWithHybridResultObjects($result, $transformedObject) { $transformer = $this->getMock('FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface'); $transformer->expects($this->any())->method('getIdentifierField')->will($this->returnValue('id')); @@ -136,19 +136,19 @@ class ElasticaToModelTransformerCollectionTest extends \PHPUnit_Framework_TestCa $transformer ->expects($this->any()) ->method('transform') - ->will($this->returnValue(array($result))); + ->will($this->returnValue(array($transformedObject))); $collection = new ElasticaToModelTransformerCollection(array('type1' => $transformer)); - $hybridResults = $collection->hybridTransform(array($document)); + $hybridResults = $collection->hybridTransform(array($result)); $this->assertInternalType('array', $hybridResults); $this->assertNotEmpty($hybridResults); $this->assertContainsOnlyInstancesOf('FOS\ElasticaBundle\HybridResult', $hybridResults); $hybridResult = array_pop($hybridResults); - $this->assertEquals($document, $hybridResult->getResult()); - $this->assertEquals($result, $hybridResult->getTransformed()); + $this->assertEquals($result, $hybridResult->getResult()); + $this->assertEquals($transformedObject, $hybridResult->getTransformed()); } } From 05ee300ddb2c1d13ec44c8f65b79808e06fbfe2a Mon Sep 17 00:00:00 2001 From: Lea Haensenberger Date: Mon, 16 Dec 2013 11:58:58 +0100 Subject: [PATCH 34/36] Fixing reset command to allow resetting of just one type, starting to write test --- Command/ResetCommand.php | 2 +- Tests/Command/ResetCommandTest.php | 53 ++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 Tests/Command/ResetCommandTest.php diff --git a/Command/ResetCommand.php b/Command/ResetCommand.php index 280f9aa..06cfe48 100755 --- a/Command/ResetCommand.php +++ b/Command/ResetCommand.php @@ -60,7 +60,7 @@ class ResetCommand extends ContainerAwareCommand if (null !== $type) { $output->writeln(sprintf('Resetting %s/%s', $index, $type)); - $this->resetter->resetIndex($index, $type); + $this->resetter->resetIndexType($index, $type); } else { $indexes = null === $index ? array_keys($this->indexManager->getAllIndexes()) diff --git a/Tests/Command/ResetCommandTest.php b/Tests/Command/ResetCommandTest.php new file mode 100644 index 0000000..44d493e --- /dev/null +++ b/Tests/Command/ResetCommandTest.php @@ -0,0 +1,53 @@ +resetter = $this->getMockBuilder('\FOS\ElasticaBundle\Resetter') + ->disableOriginalConstructor() + ->setMethods(array('resetIndex', 'resetIndexType')) + ->getMock(); + + $container->set('fos_elastica.resetter', $this->resetter); + + $this->indexManager = $this->getMockBuilder('\FOS\ElasticaBundle\IndexManager') + ->disableOriginalConstructor() + ->setMethods(array('getAllIndexes')) + ->getMock(); + + $this->command = new ResetCommand(); + $this->command->setContainer($container); + } + + public function testResetAllIndexes() + { + $this->indexManager->expects($this->any()) + ->method('getAllIndexes') + ->will($this->returnValue(array('index1', 'index2'))); + + $this->resetter->expects($this->at(1)) + ->method('resetIndex') + ->with($this->equalTo('index1')); + + $this->resetter->expects($this->at(2)) + ->method('resetIndex') + ->with($this->equalTo('index2')); + + $this->command->run( + new ArrayInput(array()), + new NullOutput() + ); + } +} \ No newline at end of file From e906d780ad019d4196b4e0817337f5a5a31eba17 Mon Sep 17 00:00:00 2001 From: Lea Haensenberger Date: Mon, 16 Dec 2013 12:06:53 +0100 Subject: [PATCH 35/36] Adding tests --- Tests/Command/ResetCommandTest.php | 44 ++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/Tests/Command/ResetCommandTest.php b/Tests/Command/ResetCommandTest.php index 44d493e..b6548aa 100644 --- a/Tests/Command/ResetCommandTest.php +++ b/Tests/Command/ResetCommandTest.php @@ -4,6 +4,9 @@ namespace FOS\ElasticaBundle\Tests\Command; use FOS\ElasticaBundle\Command\ResetCommand; +use Symfony\Component\Console\Input\ArrayInput; +use Symfony\Component\Console\Output\NullOutput; +use Symfony\Component\DependencyInjection\Container; class ResetCommandTest extends \PHPUnit_Framework_TestCase { @@ -27,6 +30,8 @@ class ResetCommandTest extends \PHPUnit_Framework_TestCase ->setMethods(array('getAllIndexes')) ->getMock(); + $container->set('fos_elastica.index_manager', $this->indexManager); + $this->command = new ResetCommand(); $this->command->setContainer($container); } @@ -35,13 +40,13 @@ class ResetCommandTest extends \PHPUnit_Framework_TestCase { $this->indexManager->expects($this->any()) ->method('getAllIndexes') - ->will($this->returnValue(array('index1', 'index2'))); + ->will($this->returnValue(array('index1' => true, 'index2' => true))); - $this->resetter->expects($this->at(1)) + $this->resetter->expects($this->at(0)) ->method('resetIndex') ->with($this->equalTo('index1')); - $this->resetter->expects($this->at(2)) + $this->resetter->expects($this->at(1)) ->method('resetIndex') ->with($this->equalTo('index2')); @@ -50,4 +55,37 @@ class ResetCommandTest extends \PHPUnit_Framework_TestCase new NullOutput() ); } + + public function testResetIndex() + { + $this->indexManager->expects($this->never()) + ->method('getAllIndexes'); + + $this->resetter->expects($this->at(0)) + ->method('resetIndex') + ->with($this->equalTo('index1')); + + $this->command->run( + new ArrayInput(array('--index' => 'index1')), + new NullOutput() + ); + } + + public function testResetIndexType() + { + $this->indexManager->expects($this->never()) + ->method('getAllIndexes'); + + $this->resetter->expects($this->never()) + ->method('resetIndex'); + + $this->resetter->expects($this->at(0)) + ->method('resetIndexType') + ->with($this->equalTo('index1'), $this->equalTo('type1')); + + $this->command->run( + new ArrayInput(array('--index' => 'index1', '--type' => 'type1')), + new NullOutput() + ); + } } \ No newline at end of file From d33c5d0ece19f5758f4bc6679f26e4fdd17d7f49 Mon Sep 17 00:00:00 2001 From: Tim Nagel Date: Tue, 17 Dec 2013 21:38:08 +1100 Subject: [PATCH 36/36] Fix tests --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index ad1f366..8f6a9d8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,4 +6,4 @@ php: before_script: - echo "extension = mongo.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini - - composer install --dev + - composer install --dev --prefer-source