diff --git a/MappingRegistry.php b/MappingRegistry.php index aeaf073..3573e9f 100644 --- a/MappingRegistry.php +++ b/MappingRegistry.php @@ -51,7 +51,6 @@ class MappingRegistry public function getTypeFieldNames(Elastica_Type $type) { $key = sprintf('%s/%s', $type->getIndex()->getName(), $type->getType()); - if (!isset($this->mappings[$key])) { throw new InvalidArgumentException(sprintf('This type is not registered: "%s".', $key)); } diff --git a/Populator.php b/Populator.php old mode 100644 new mode 100755 index 39287b6..751aebf --- a/Populator.php +++ b/Populator.php @@ -11,7 +11,9 @@ class Populator public function __construct(array $providers) { - $this->providers = $providers; + foreach ($providers as $name => $provider) { + $this->addProvider($name, $provider); + } } public function addProvider($name, ProviderInterface $provider) diff --git a/Tests/MappingRegistryTest.php b/Tests/MappingRegistryTest.php new file mode 100644 index 0000000..a5a1bad --- /dev/null +++ b/Tests/MappingRegistryTest.php @@ -0,0 +1,84 @@ +getMockBuilder('Elastica_Type') + ->disableOriginalConstructor() + ->getMock(); + + $typeMock->expects($this->once()) + ->method('setMapping') + ->with($this->equalTo(array('mappingArray'))); + + $mapping = new MappingRegistry(array( + 'index/type' => array($typeMock, array('mappingArray')) + )); + + $mapping->applyMappings(); + } + + /** + * @dataProvider invalidTypesParametersProvider + * @expectedException InvalidArgumentException + */ + public function testThatCannotGetTypeFieldForTypeWhichNotExists($indexName, $typeName) + { + $type = $this->getTypeMock('index', 'type'); + $mapping = new MappingRegistry(array( + 'index/type' => array($type, array('mappingArray')) + )); + + $mapping->getTypeFieldNames($this->getTypeMock($indexName, $typeName)); + } + + public function testThatCanGetTypeField() + { + $type = $this->getTypeMock('index', 'type'); + $mapping = new MappingRegistry(array( + 'index/type' => array($type, array('mappingArray')) + )); + + $mapping->getTypeFieldNames($this->getTypeMock('index', 'type')); + } + + public static function invalidTypesParametersProvider() + { + return array( + array('index1', 'type'), + array('index', 'type2') + ); + } + + private function getTypeMock($indexName, $typeName) + { + $typeMock = $this->getMockBuilder('Elastica_Type') + ->disableOriginalConstructor() + ->getMock(); + + $indexMock = $this->getMockBuilder('Elastica_Index') + ->disableOriginalConstructor() + ->getMock(); + + $indexMock->expects($this->any()) + ->method('getName') + ->will($this->returnValue($indexName)); + + $typeMock->expects($this->any()) + ->method('getIndex') + ->will($this->returnValue($indexMock)); + + $typeMock->expects($this->any()) + ->method('getType') + ->will($this->returnValue($typeName)); + + return $typeMock; + } +} diff --git a/Tests/PopulatorTest.php b/Tests/PopulatorTest.php old mode 100644 new mode 100755 index 1918b2a..fe1f111 --- a/Tests/PopulatorTest.php +++ b/Tests/PopulatorTest.php @@ -29,9 +29,32 @@ class PopulatorTest extends \PHPUnit_Framework_TestCase { $provider = $this->getMock('FOQ\ElasticaBundle\Provider\ProviderInterface', array('populate')); $provider->expects($this->once()) - ->method('populate'); + ->method('populate'); + + $provider2 = $this->getMock('FOQ\ElasticaBundle\Provider\ProviderInterface', array('populate')); + $provider2->expects($this->once()) + ->method('populate'); - $populator = new Populator(array('l3l0Provider' => $provider)); + $populator = new Populator(array('l3l0Provider' => $provider, 'secondProvider' => $provider2)); + $populator->populate(function ($text) { return $text; }); + } + + /** + * @expectedException PHPUnit_Framework_Error + */ + public function testThatAddProviderHaveToImpelementProviderInterface() + { + $populator = new Populator(array()); + $populator->addProvider('provider', new \stdClass()); + $populator->populate(function ($text) { return $text; }); + } + + /** + * @expectedException PHPUnit_Framework_Error + */ + public function testThatProvidersPassToTheContructorHaveToImpelementProviderInterface() + { + $populator = new Populator(array('provider' => new \stdClass())); $populator->populate(function ($text) { return $text; }); } } diff --git a/Tests/Transformer/ModelToElasticaAutoTransformerTest.php b/Tests/Transformer/ModelToElasticaAutoTransformerTest.php new file mode 100644 index 0000000..a1c2bd5 --- /dev/null +++ b/Tests/Transformer/ModelToElasticaAutoTransformerTest.php @@ -0,0 +1,76 @@ +id; + } + + public function getName() + { + return $this->name; + } + + public function getIterator() + { + $iterator = new \ArrayIterator(); + $iterator->append('value1'); + + return $iterator; + } + + public function getArray() + { + return array('key1' => 'value1', 'key2' => 'value2'); + } +} + +class ModelToElasticaAutoTransformerTest extends \PHPUnit_Framework_TestCase +{ + public function testThatCanTransformObject() + { + $transformer = new ModelToElasticaAutoTransformer(); + $document = $transformer->transform(new POPO(), array('name')); + $data = $document->getData(); + + $this->assertInstanceOf('Elastica_Document', $document); + $this->assertEquals(123, $document->getId()); + $this->assertEquals('someName', $data['name']); + } + + public function testThatCanTransformObjectWithIteratorValue() + { + $transformer = new ModelToElasticaAutoTransformer(); + $document = $transformer->transform(new POPO(), array('iterator')); + $data = $document->getData(); + + $this->assertEquals(array('value1'), $data['iterator']); + } + + public function testThatCanTransformObjectWithArrayValue() + { + $transformer = new ModelToElasticaAutoTransformer(); + $document = $transformer->transform(new POPO(), array('array')); + $data = $document->getData(); + + $this->assertEquals(array('key1' => 'value1', 'key2' => 'value2'), $data['array']); + } + + /** + * @expectedException RuntimeException + */ + public function testThatCannotTransformObjectWhenGetterDoesNotExists() + { + $transformer = new ModelToElasticaAutoTransformer(); + $document = $transformer->transform(new POPO(), array('desc')); + } +}