FOSElasticaBundle/Tests/Index/ResetterTest.php

275 lines
9.5 KiB
PHP
Raw Normal View History

2011-07-07 22:42:03 +02:00
<?php
2014-04-21 01:21:50 +02:00
namespace FOS\ElasticaBundle\Tests\Index;
2011-07-07 22:42:03 +02:00
use Elastica\Exception\ResponseException;
use Elastica\Request;
use Elastica\Response;
2015-03-14 12:14:24 +01:00
use Elastica\Type;
use Elastica\Type\Mapping;
2014-06-18 09:13:29 +02:00
use FOS\ElasticaBundle\Configuration\IndexConfig;
2015-03-14 12:14:24 +01:00
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;
2014-04-21 01:21:50 +02:00
use FOS\ElasticaBundle\Index\Resetter;
2011-07-07 22:42:03 +02:00
class ResetterTest extends \PHPUnit_Framework_TestCase
2011-07-07 22:42:03 +02:00
{
2014-06-18 09:13:29 +02:00
/**
* @var Resetter
*/
private $resetter;
2015-03-14 12:14:24 +01:00
private $aliasProcessor;
2014-06-18 09:13:29 +02:00
private $configManager;
2015-03-14 12:14:24 +01:00
private $dispatcher;
private $elasticaClient;
2014-06-18 09:13:29 +02:00
private $indexManager;
private $mappingBuilder;
2011-07-07 22:42:03 +02:00
public function testResetAllIndexes()
2011-07-07 22:42:03 +02:00
{
2015-03-14 12:14:24 +01:00
$indexName = 'index1';
$indexConfig = new IndexConfig($indexName, array(), array());
$this->mockIndex($indexName, $indexConfig);
2014-06-18 09:13:29 +02:00
$this->configManager->expects($this->once())
2015-03-14 12:14:24 +01:00
->method('getIndexNames')
->will($this->returnValue(array($indexName)));
2014-06-18 09:13:29 +02:00
2015-03-14 12:14:24 +01:00
$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'))
));
2011-07-07 22:42:03 +02:00
2015-03-14 12:14:24 +01:00
$this->elasticaClient->expects($this->exactly(2))
->method('request')
->withConsecutive(
array('index1/', 'DELETE'),
array('index1/', 'PUT', array(), array())
);
2014-06-18 09:13:29 +02:00
$this->resetter->resetAllIndexes();
}
2011-07-07 22:42:03 +02:00
public function testResetIndex()
2011-07-07 22:42:03 +02:00
{
2015-03-14 12:14:24 +01:00
$indexConfig = new IndexConfig('index1', array(), array());
$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');
}
2015-03-14 12:14:24 +01:00
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');
}
2011-07-07 22:42:03 +02:00
2015-03-14 12:14:24 +01:00
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');
2011-07-07 22:42:03 +02:00
}
/**
* @expectedException \InvalidArgumentException
*/
2015-03-14 12:14:24 +01:00
public function testFailureWhenMissingIndexDoesntDispatch()
2011-07-07 22:42:03 +02:00
{
2015-03-14 12:14:24 +01:00
$this->configManager->expects($this->once())
->method('getIndexConfiguration')
->with('nonExistant')
->will($this->throwException(new \InvalidArgumentException));
2015-03-14 12:14:24 +01:00
$this->indexManager->expects($this->never())
->method('getIndex');
2015-03-14 12:14:24 +01:00
$this->resetter->resetIndex('nonExistant');
2011-07-07 22:42:03 +02:00
}
2015-03-14 12:14:24 +01:00
public function testResetType()
2011-07-07 22:42:03 +02:00
{
2015-03-14 12:14:24 +01:00
$typeConfig = new TypeConfig('type', array(), array());
$this->mockType('type', 'index', $typeConfig);
$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'))
));
$this->elasticaClient->expects($this->exactly(2))
->method('request')
->withConsecutive(
array('index/type/', 'DELETE'),
array('index/type/_mapping', 'PUT', array('type' => array()), array())
);
$this->resetter->resetIndexType('index', 'type');
}
2011-07-07 22:42:03 +02:00
/**
* @expectedException \InvalidArgumentException
*/
2015-03-14 12:14:24 +01:00
public function testNonExistantResetType()
{
2015-03-14 12:14:24 +01:00
$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');
}
2011-07-07 22:58:43 +02:00
2015-03-14 12:14:24 +01:00
public function testPostPopulateWithoutAlias()
{
2015-03-14 12:14:24 +01:00
$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');
}
2015-03-14 12:14:24 +01:00
public function testPostPopulate()
{
2015-03-14 12:14:24 +01:00
$indexConfig = new IndexConfig('index', array(), array( 'useAlias' => true));
$index = $this->mockIndex('index', $indexConfig);
2015-03-14 12:14:24 +01:00
$this->aliasProcessor->expects($this->once())
->method('switchIndexAlias')
->with($indexConfig, $index);
2015-03-14 12:14:24 +01:00
$this->resetter->postPopulate('index');
}
2015-03-14 12:14:24 +01:00
private function dispatcherExpects(array $events)
{
$expectation = $this->dispatcher->expects($this->exactly(count($events)))
->method('dispatch');
2015-03-14 12:14:24 +01:00
call_user_func_array(array($expectation, 'withConsecutive'), $events);
}
2015-03-14 12:14:24 +01:00
private function mockIndex($indexName, IndexConfig $config, $mapping = array())
{
2015-03-14 12:14:24 +01:00
$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;
}
2011-07-07 22:58:43 +02:00
2015-03-14 12:14:24 +01:00
private function mockType($typeName, $indexName, TypeConfig $config, $mapping = array())
{
2015-03-14 12:14:24 +01:00
$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();
$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();
2015-03-14 12:14:24 +01:00
$this->mappingBuilder = $this->getMockBuilder('FOS\\ElasticaBundle\\Index\\MappingBuilder')
->disableOriginalConstructor()
->getMock();
$this->resetter = new Resetter(
$this->configManager,
$this->indexManager,
$this->aliasProcessor,
$this->mappingBuilder,
$this->dispatcher
);
2011-07-07 22:42:03 +02:00
}
}