Added test for MappingRegistry Populator and ModelToElasticaAutoTransformer. Extend Populator

This commit is contained in:
Leszek 2011-07-14 20:49:18 +02:00
parent 3896ffd9ff
commit c430aeaea1
5 changed files with 188 additions and 4 deletions

View file

@ -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));
}

4
Populator.php Normal file → Executable file
View file

@ -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)

View file

@ -0,0 +1,84 @@
<?php
namespace FOQ\ElasticaBundle\Tests\MappingRegistry;
use FOQ\ElasticaBundle\MappingRegistry;
use Elastica_Type;
use Elastica_Index;
class MappingRegistryTest extends \PHPUnit_Framework_TestCase
{
public function testThatCanApplyMappings()
{
$typeMock = $this->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;
}
}

27
Tests/PopulatorTest.php Normal file → Executable file
View file

@ -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; });
}
}

View file

@ -0,0 +1,76 @@
<?php
namespace FOQ\ElasticaBundle\Tests\Transformer\ModelToElasticaAutoTransformer;
use FOQ\ElasticaBundle\Transformer\ModelToElasticaAutoTransformer;
class POPO
{
public $id = 123;
public $name = 'someName';
public $desc = 'desc';
public function getId()
{
return $this->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'));
}
}