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

View file

@ -5,8 +5,14 @@ namespace FOS\ElasticaBundle\Tests\Index;
use Elastica\Exception\ResponseException; use Elastica\Exception\ResponseException;
use Elastica\Request; use Elastica\Request;
use Elastica\Response; use Elastica\Response;
use Elastica\Type;
use Elastica\Type\Mapping; use Elastica\Type\Mapping;
use FOS\ElasticaBundle\Configuration\IndexConfig; 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; use FOS\ElasticaBundle\Index\Resetter;
class ResetterTest extends \PHPUnit_Framework_TestCase class ResetterTest extends \PHPUnit_Framework_TestCase
@ -16,230 +22,253 @@ class ResetterTest extends \PHPUnit_Framework_TestCase
*/ */
private $resetter; private $resetter;
private $configManager;
private $indexManager;
private $aliasProcessor; private $aliasProcessor;
private $configManager;
private $dispatcher;
private $elasticaClient;
private $indexManager;
private $mappingBuilder; 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() public function testResetAllIndexes()
{ {
$indexName = 'index1';
$indexConfig = new IndexConfig($indexName, array(), array());
$this->mockIndex($indexName, $indexConfig);
$this->configManager->expects($this->once()) $this->configManager->expects($this->once())
->method('getIndexNames') ->method('getIndexNames')
->will($this->returnValue(array('index1'))); ->will($this->returnValue(array($indexName)));
$this->configManager->expects($this->once()) $this->dispatcherExpects(array(
->method('getIndexConfiguration') array(IndexResetEvent::PRE_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')),
->with('index1') array(IndexResetEvent::POST_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent'))
->will($this->returnValue(new IndexConfig('index1', array(), array()))); ));
$this->indexManager->expects($this->once()) $this->elasticaClient->expects($this->exactly(2))
->method('getIndex') ->method('request')
->with('index1') ->withConsecutive(
->will($this->returnValue()); 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(); $this->resetter->resetAllIndexes();
} }
public function testResetIndex() public function testResetIndex()
{ {
$this->indexConfigsByName['foo']['index']->expects($this->once()) $indexConfig = new IndexConfig('index1', array(), array());
->method('create') $this->mockIndex('index1', $indexConfig);
->with($this->indexConfigsByName['foo']['config'], true);
$this->indexConfigsByName['bar']['index']->expects($this->never()) $this->dispatcherExpects(array(
->method('create'); 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); $this->elasticaClient->expects($this->exactly(2))
$resetter->resetIndex('foo'); ->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 * @expectedException \InvalidArgumentException
*/ */
public function testResetIndexShouldThrowExceptionForInvalidIndex() public function testFailureWhenMissingIndexDoesntDispatch()
{ {
$resetter = new Resetter($this->indexConfigsByName); $this->configManager->expects($this->once())
$resetter->resetIndex('baz'); ->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()) $this->dispatcherExpects(array(
->method('getType') array(TypeResetEvent::PRE_TYPE_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\TypeResetEvent')),
->with('a') array(TypeResetEvent::POST_TYPE_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\TypeResetEvent'))
->will($this->returnValue($type)); ));
$type->expects($this->once()) $this->elasticaClient->expects($this->exactly(2))
->method('delete'); ->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']); $this->resetter->resetIndexType('index', 'type');
$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');
} }
/** /**
* @expectedException \InvalidArgumentException * @expectedException \InvalidArgumentException
*/ */
public function testResetIndexTypeShouldThrowExceptionForInvalidIndex() public function testNonExistantResetType()
{ {
$resetter = new Resetter($this->indexConfigsByName); $this->configManager->expects($this->once())
$resetter->resetIndexType('baz', 'a'); ->method('getTypeConfiguration')
->with('index', 'nonExistant')
->will($this->throwException(new \InvalidArgumentException));
$this->indexManager->expects($this->never())
->method('getIndex');
$this->resetter->resetIndexType('index', 'nonExistant');
} }
/** public function testPostPopulateWithoutAlias()
* @expectedException \InvalidArgumentException
*/
public function testResetIndexTypeShouldThrowExceptionForInvalidType()
{ {
$resetter = new Resetter($this->indexConfigsByName); $this->mockIndex('index', new IndexConfig('index', array(), array()));
$resetter->resetIndexType('foo', 'c');
$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()) $this->aliasProcessor->expects($this->once())
->method('getType') ->method('switchIndexAlias')
->with('a') ->with($indexConfig, $index);
->will($this->returnValue($type));
$type->expects($this->once()) $this->resetter->postPopulate('index');
->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');
} }
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()) call_user_func_array(array($expectation, 'withConsecutive'), $events);
->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');
} }
/** private function mockIndex($indexName, IndexConfig $config, $mapping = array())
* @return \Elastica\Index
*/
private function getMockElasticaIndex()
{ {
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() ->disableOriginalConstructor()
->getMock(); ->getMock();
} $this->configManager = $this->getMockBuilder('FOS\\ElasticaBundle\\Configuration\\ConfigManager')
/**
* @return \Elastica\Type
*/
private function getMockElasticaType()
{
return $this->getMockBuilder('Elastica\Type')
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->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
);
} }
} }