Resetter tests

This commit is contained in:
Tim Nagel 2015-03-14 22:14:24 +11:00
parent ac98549eb5
commit 5181b02933
2 changed files with 206 additions and 177 deletions

View file

@ -86,12 +86,12 @@ class Resetter
*/
public function resetIndex($indexName, $populating = false, $force = false)
{
$event = new IndexResetEvent($indexName, $populating, $force);
$this->dispatcher->dispatch(IndexResetEvent::PRE_INDEX_RESET, $event);
$indexConfig = $this->configManager->getIndexConfiguration($indexName);
$index = $this->indexManager->getIndex($indexName);
$event = new IndexResetEvent($indexName, $populating, $force);
$this->dispatcher->dispatch(IndexResetEvent::PRE_INDEX_RESET, $event);
if ($indexConfig->isUseAlias()) {
$this->aliasProcessor->setRootName($indexConfig, $index);
}
@ -117,12 +117,12 @@ class Resetter
*/
public function resetIndexType($indexName, $typeName)
{
$event = new TypeResetEvent($indexName, $typeName);
$this->dispatcher->dispatch(TypeResetEvent::PRE_TYPE_RESET, $event);
$typeConfig = $this->configManager->getTypeConfiguration($indexName, $typeName);
$type = $this->indexManager->getIndex($indexName)->getType($typeName);
$event = new TypeResetEvent($indexName, $typeName);
$this->dispatcher->dispatch(TypeResetEvent::PRE_TYPE_RESET, $event);
try {
$type->delete();
} catch (ResponseException $e) {

View file

@ -5,8 +5,14 @@ namespace FOS\ElasticaBundle\Tests\Index;
use Elastica\Exception\ResponseException;
use Elastica\Request;
use Elastica\Response;
use Elastica\Type;
use Elastica\Type\Mapping;
use FOS\ElasticaBundle\Configuration\IndexConfig;
use FOS\ElasticaBundle\Configuration\TypeConfig;
use FOS\ElasticaBundle\Elastica\Index;
use FOS\ElasticaBundle\Event\IndexResetEvent;
use FOS\ElasticaBundle\Event\TypeResetEvent;
use FOS\ElasticaBundle\Index\AliasProcessor;
use FOS\ElasticaBundle\Index\Resetter;
class ResetterTest extends \PHPUnit_Framework_TestCase
@ -16,230 +22,253 @@ class ResetterTest extends \PHPUnit_Framework_TestCase
*/
private $resetter;
private $configManager;
private $indexManager;
private $aliasProcessor;
private $configManager;
private $dispatcher;
private $elasticaClient;
private $indexManager;
private $mappingBuilder;
public function setUp()
{
$this->markTestIncomplete('To be rewritten');
$this->configManager = $this->getMockBuilder('FOS\\ElasticaBundle\\Configuration\\ConfigManager')
->disableOriginalConstructor()
->getMock();
$this->indexManager = $this->getMockBuilder('FOS\\ElasticaBundle\\Index\\IndexManager')
->disableOriginalConstructor()
->getMock();
$this->aliasProcessor = $this->getMockBuilder('FOS\\ElasticaBundle\\Index\\AliasProcessor')
->disableOriginalConstructor()
->getMock();
$this->mappingBuilder = $this->getMockBuilder('FOS\\ElasticaBundle\\Index\\MappingBuilder')
->disableOriginalConstructor()
->getMock();
$this->resetter = $this->getMockBuilder('FOS\ElasticaBundle\Index\Resetter')
->disableOriginalConstructor()
->getMock();
$this->resetter = new Resetter($this->configManager, $this->indexManager, $this->aliasProcessor, $this->mappingBuilder, $this->resetter);
/*$this->indexConfigsByName = array(
'foo' => array(
'index' => $this->getMockElasticaIndex(),
'config' => array(
'properties' => array(
'a' => array(
'dynamic_templates' => array(),
'properties' => array(),
),
'b' => array('properties' => array()),
),
),
),
'bar' => array(
'index' => $this->getMockElasticaIndex(),
'config' => array(
'properties' => array(
'a' => array('properties' => array()),
'b' => array('properties' => array()),
),
),
),
'parent' => array(
'index' => $this->getMockElasticaIndex(),
'config' => array(
'properties' => array(
'a' => array(
'properties' => array(
'field_2' => array()
),
'_parent' => array(
'type' => 'b',
'property' => 'b',
'identifier' => 'id'
),
),
'b' => array('properties' => array()),
),
),
),
);*/
}
public function testResetAllIndexes()
{
$indexName = 'index1';
$indexConfig = new IndexConfig($indexName, array(), array());
$this->mockIndex($indexName, $indexConfig);
$this->configManager->expects($this->once())
->method('getIndexNames')
->will($this->returnValue(array('index1')));
->will($this->returnValue(array($indexName)));
$this->configManager->expects($this->once())
->method('getIndexConfiguration')
->with('index1')
->will($this->returnValue(new IndexConfig('index1', array(), array())));
$this->dispatcherExpects(array(
array(IndexResetEvent::PRE_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')),
array(IndexResetEvent::POST_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent'))
));
$this->indexManager->expects($this->once())
->method('getIndex')
->with('index1')
->will($this->returnValue());
$this->elasticaClient->expects($this->exactly(2))
->method('request')
->withConsecutive(
array('index1/', 'DELETE'),
array('index1/', 'PUT', array(), array())
);
/*$this->indexConfigsByName['foo']['index']->expects($this->once())
->method('create')
->with($this->indexConfigsByName['foo']['config'], true);
$this->indexConfigsByName['bar']['index']->expects($this->once())
->method('create')
->with($this->indexConfigsByName['bar']['config'], true);
$resetter = new Resetter($this->indexConfigsByName);*/
$this->resetter->resetAllIndexes();
}
public function testResetIndex()
{
$this->indexConfigsByName['foo']['index']->expects($this->once())
->method('create')
->with($this->indexConfigsByName['foo']['config'], true);
$indexConfig = new IndexConfig('index1', array(), array());
$this->mockIndex('index1', $indexConfig);
$this->indexConfigsByName['bar']['index']->expects($this->never())
->method('create');
$this->dispatcherExpects(array(
array(IndexResetEvent::PRE_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')),
array(IndexResetEvent::POST_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent'))
));
$resetter = new Resetter($this->indexConfigsByName);
$resetter->resetIndex('foo');
$this->elasticaClient->expects($this->exactly(2))
->method('request')
->withConsecutive(
array('index1/', 'DELETE'),
array('index1/', 'PUT', array(), array())
);
$this->resetter->resetIndex('index1');
}
public function testResetIndexWithDifferentName()
{
$indexConfig = new IndexConfig('index1', array(), array(
'elasticSearchName' => 'notIndex1'
));
$this->mockIndex('index1', $indexConfig);
$this->dispatcherExpects(array(
array(IndexResetEvent::PRE_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')),
array(IndexResetEvent::POST_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent'))
));
$this->elasticaClient->expects($this->exactly(2))
->method('request')
->withConsecutive(
array('index1/', 'DELETE'),
array('index1/', 'PUT', array(), array())
);
$this->resetter->resetIndex('index1');
}
public function testResetIndexWithDifferentNameAndAlias()
{
$indexConfig = new IndexConfig('index1', array(), array(
'elasticSearchName' => 'notIndex1',
'useAlias' => true
));
$index = $this->mockIndex('index1', $indexConfig);
$this->dispatcherExpects(array(
array(IndexResetEvent::PRE_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')),
array(IndexResetEvent::POST_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent'))
));
$this->aliasProcessor->expects($this->once())
->method('switchIndexAlias')
->with($indexConfig, $index, false);
$this->elasticaClient->expects($this->exactly(2))
->method('request')
->withConsecutive(
array('index1/', 'DELETE'),
array('index1/', 'PUT', array(), array())
);
$this->resetter->resetIndex('index1');
}
/**
* @expectedException \InvalidArgumentException
*/
public function testResetIndexShouldThrowExceptionForInvalidIndex()
public function testFailureWhenMissingIndexDoesntDispatch()
{
$resetter = new Resetter($this->indexConfigsByName);
$resetter->resetIndex('baz');
$this->configManager->expects($this->once())
->method('getIndexConfiguration')
->with('nonExistant')
->will($this->throwException(new \InvalidArgumentException));
$this->indexManager->expects($this->never())
->method('getIndex');
$this->resetter->resetIndex('nonExistant');
}
public function testResetIndexType()
public function testResetType()
{
$type = $this->getMockElasticaType();
$typeConfig = new TypeConfig('type', array(), array());
$this->mockType('type', 'index', $typeConfig);
$this->indexConfigsByName['foo']['index']->expects($this->once())
->method('getType')
->with('a')
->will($this->returnValue($type));
$this->dispatcherExpects(array(
array(TypeResetEvent::PRE_TYPE_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\TypeResetEvent')),
array(TypeResetEvent::POST_TYPE_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\TypeResetEvent'))
));
$type->expects($this->once())
->method('delete');
$this->elasticaClient->expects($this->exactly(2))
->method('request')
->withConsecutive(
array('index/type/', 'DELETE'),
array('index/type/_mapping', 'PUT', array('type' => array()), array())
);
$mapping = Mapping::create($this->indexConfigsByName['foo']['config']['properties']['a']['properties']);
$mapping->setParam('dynamic_templates', $this->indexConfigsByName['foo']['config']['properties']['a']['dynamic_templates']);
$type->expects($this->once())
->method('setMapping')
->with($mapping);
$resetter = new Resetter($this->indexConfigsByName);
$resetter->resetIndexType('foo', 'a');
$this->resetter->resetIndexType('index', 'type');
}
/**
* @expectedException \InvalidArgumentException
*/
public function testResetIndexTypeShouldThrowExceptionForInvalidIndex()
public function testNonExistantResetType()
{
$resetter = new Resetter($this->indexConfigsByName);
$resetter->resetIndexType('baz', 'a');
$this->configManager->expects($this->once())
->method('getTypeConfiguration')
->with('index', 'nonExistant')
->will($this->throwException(new \InvalidArgumentException));
$this->indexManager->expects($this->never())
->method('getIndex');
$this->resetter->resetIndexType('index', 'nonExistant');
}
/**
* @expectedException \InvalidArgumentException
*/
public function testResetIndexTypeShouldThrowExceptionForInvalidType()
public function testPostPopulateWithoutAlias()
{
$resetter = new Resetter($this->indexConfigsByName);
$resetter->resetIndexType('foo', 'c');
$this->mockIndex('index', new IndexConfig('index', array(), array()));
$this->indexManager->expects($this->never())
->method('getIndex');
$this->aliasProcessor->expects($this->never())
->method('switchIndexAlias');
$this->resetter->postPopulate('index');
}
public function testResetIndexTypeIgnoreTypeMissingException()
public function testPostPopulate()
{
$type = $this->getMockElasticaType();
$indexConfig = new IndexConfig('index', array(), array( 'useAlias' => true));
$index = $this->mockIndex('index', $indexConfig);
$this->indexConfigsByName['foo']['index']->expects($this->once())
->method('getType')
->with('a')
->will($this->returnValue($type));
$this->aliasProcessor->expects($this->once())
->method('switchIndexAlias')
->with($indexConfig, $index);
$type->expects($this->once())
->method('delete')
->will($this->throwException(new ResponseException(
new Request(''),
new Response(array('error' => 'TypeMissingException[[de_20131022] type[bla] missing]', 'status' => 404)))
));
$mapping = Mapping::create($this->indexConfigsByName['foo']['config']['properties']['a']['properties']);
$mapping->setParam('dynamic_templates', $this->indexConfigsByName['foo']['config']['properties']['a']['dynamic_templates']);
$type->expects($this->once())
->method('setMapping')
->with($mapping);
$resetter = new Resetter($this->indexConfigsByName);
$resetter->resetIndexType('foo', 'a');
$this->resetter->postPopulate('index');
}
public function testIndexMappingForParent()
private function dispatcherExpects(array $events)
{
$type = $this->getMockElasticaType();
$expectation = $this->dispatcher->expects($this->exactly(count($events)))
->method('dispatch');
$this->indexConfigsByName['parent']['index']->expects($this->once())
->method('getType')
->with('a')
->will($this->returnValue($type));
$type->expects($this->once())
->method('delete');
$mapping = Mapping::create($this->indexConfigsByName['parent']['config']['properties']['a']['properties']);
$mapping->setParam('_parent', array('type' => 'b'));
$type->expects($this->once())
->method('setMapping')
->with($mapping);
$resetter = new Resetter($this->indexConfigsByName);
$resetter->resetIndexType('parent', 'a');
call_user_func_array(array($expectation, 'withConsecutive'), $events);
}
/**
* @return \Elastica\Index
*/
private function getMockElasticaIndex()
private function mockIndex($indexName, IndexConfig $config, $mapping = array())
{
return $this->getMockBuilder('Elastica\Index')
$this->configManager->expects($this->atLeast(1))
->method('getIndexConfiguration')
->with($indexName)
->will($this->returnValue($config));
$index = new Index($this->elasticaClient, $indexName);
$this->indexManager->expects($this->any())
->method('getIndex')
->with($indexName)
->willReturn($index);
$this->mappingBuilder->expects($this->any())
->method('buildIndexMapping')
->with($config)
->willReturn($mapping);
return $index;
}
private function mockType($typeName, $indexName, TypeConfig $config, $mapping = array())
{
$this->configManager->expects($this->atLeast(1))
->method('getTypeConfiguration')
->with($indexName, $typeName)
->will($this->returnValue($config));
$index = new Index($this->elasticaClient, $indexName);
$this->indexManager->expects($this->once())
->method('getIndex')
->with($indexName)
->willReturn($index);
$this->mappingBuilder->expects($this->once())
->method('buildTypeMapping')
->with($config)
->willReturn($mapping);
return $index;
}
protected function setUp()
{
$this->aliasProcessor = $this->getMockBuilder('FOS\\ElasticaBundle\\Index\\AliasProcessor')
->disableOriginalConstructor()
->getMock();
}
/**
* @return \Elastica\Type
*/
private function getMockElasticaType()
{
return $this->getMockBuilder('Elastica\Type')
$this->configManager = $this->getMockBuilder('FOS\\ElasticaBundle\\Configuration\\ConfigManager')
->disableOriginalConstructor()
->getMock();
$this->dispatcher = $this->getMockBuilder('Symfony\\Component\\EventDispatcher\\EventDispatcherInterface')
->getMock();
$this->elasticaClient = $this->getMockBuilder('Elastica\\Client')
->disableOriginalConstructor()
->getMock();
$this->indexManager = $this->getMockBuilder('FOS\\ElasticaBundle\\Index\\IndexManager')
->disableOriginalConstructor()
->getMock();
$this->mappingBuilder = $this->getMockBuilder('FOS\\ElasticaBundle\\Index\\MappingBuilder')
->disableOriginalConstructor()
->getMock();
$this->resetter = new Resetter(
$this->configManager,
$this->indexManager,
$this->aliasProcessor,
$this->mappingBuilder,
$this->dispatcher
);
}
}