diff --git a/Client/Connection.php b/Client/Connection.php index 77c1f675..6f9913cb 100644 --- a/Client/Connection.php +++ b/Client/Connection.php @@ -405,17 +405,40 @@ public function addWarmer(WarmerInterface $warmer) /** * Loads warmers into elasticseach. + * + * @param array $names Warmers names to put. */ - public function putWarmers() + public function putWarmers(array $names = []) { foreach ($this->warmers->getWarmers() as $name => $body) { - $this->getClient()->indices()->putWarmer( - [ - 'index' => $this->getIndexName(), - 'name' => $name, - 'body' => $body, - ] - ); + if (empty($names) || in_array($name, $names)) { + $this->getClient()->indices()->putWarmer( + [ + 'index' => $this->getIndexName(), + 'name' => $name, + 'body' => $body, + ] + ); + } + } + } + + /** + * Deletes warmers from elasticsearch index. + * + * @param array $names Warmers names to delete. + */ + public function deleteWarmers(array $names = []) + { + foreach ($this->warmers->getWarmers() as $name => $body) { + if (empty($names) || in_array($name, $names)) { + $this->getClient()->indices()->deleteWarmer( + [ + 'index' => $this->getIndexName(), + 'name' => $name, + ] + ); + } } } } diff --git a/Command/AbstractElasticsearchCommand.php b/Command/AbstractElasticsearchCommand.php index a8a68328..05aaaec4 100644 --- a/Command/AbstractElasticsearchCommand.php +++ b/Command/AbstractElasticsearchCommand.php @@ -43,10 +43,41 @@ protected function configure() */ protected function getManager($name) { - /** @var Manager $manager */ - $manager = $this->getContainer()->get($this->getManagerId($name)); + return $this->getContainer()->get($this->getManagerId($name)); + } - return $manager; + /** + * Returns elasticsearch connection by name. + * + * @param string $name + * + * @return \ONGR\ElasticsearchBundle\Client\Connection + */ + protected function getConnection($name) + { + return $this->getManager($this->getManagerNameByConnection($name))->getConnection(); + } + + /** + * Returns manager name which is using passed connection. + * + * @param string $name Connection name. + * + * @return string + * + * @throws \RuntimeException + */ + private function getManagerNameByConnection($name) + { + foreach ($this->getContainer()->getParameter('es.managers') as $managerName => $params) { + if ($params['connection'] === $name) { + return $managerName; + } + } + + throw new \RuntimeException( + sprintf('Connection named %s is not used by any manager. Check your configuration.', $name) + ); } /** diff --git a/Command/CacheClearCommand.php b/Command/CacheClearCommand.php new file mode 100644 index 00000000..4c9e3509 --- /dev/null +++ b/Command/CacheClearCommand.php @@ -0,0 +1,54 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchBundle\Command; + +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +/** + * Symfony command for clearing elasticsearch cache. + */ +class CacheClearCommand extends AbstractElasticsearchCommand +{ + /** + * {@inheritdoc} + */ + protected function configure() + { + $this + ->setName('es:cache:clear') + ->addOption( + 'connection', + 'c', + InputOption::VALUE_REQUIRED, + 'Connection name to which clear cache.', + 'default' + ); + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $this->getConnection($input->getOption('connection'))->clearCache(); + $output->writeln( + sprintf( + 'Elasticsearch cache has been cleared for %s index.', + $input->getOption('connection') + ) + ); + + return 0; + } +} diff --git a/Command/WarmerDeleteCommand.php b/Command/WarmerDeleteCommand.php new file mode 100644 index 00000000..dc5616c9 --- /dev/null +++ b/Command/WarmerDeleteCommand.php @@ -0,0 +1,69 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchBundle\Command; + +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +/** + * This command removes warmers from elasticsearch index. + */ +class WarmerDeleteCommand extends AbstractElasticsearchCommand +{ + /** + * {@inheritdoc} + */ + protected function configure() + { + $this + ->setName('es:warmer:delete') + ->addArgument( + 'names', + InputArgument::IS_ARRAY | InputArgument::OPTIONAL, + 'Warmers names to delete from index.', + [] + ) + ->addOption( + 'connection', + 'c', + InputOption::VALUE_REQUIRED, + 'Connection name to delete warmers from.', + 'default' + ); + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $names = $input->getArgument('names'); + $this->getConnection($input->getOption('connection'))->deleteWarmers($names); + + $message = ''; + if (empty($names)) { + $message = 'All warmers have been deleted from %s index.'; + } else { + $callback = function ($val) { + return '' . $val . ''; + }; + $message = implode(', ', array_map($callback, $names)) + . ' warmer(s) have been deleted from %s index.'; + } + + $output->writeln(sprintf($message, $input->getOption('connection'))); + + return 0; + } +} diff --git a/Command/WarmerPutCommand.php b/Command/WarmerPutCommand.php new file mode 100644 index 00000000..dafd5a6f --- /dev/null +++ b/Command/WarmerPutCommand.php @@ -0,0 +1,69 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchBundle\Command; + +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +/** + * This command puts warmers into elasticsearch index. + */ +class WarmerPutCommand extends AbstractElasticsearchCommand +{ + /** + * {@inheritdoc} + */ + protected function configure() + { + $this + ->setName('es:warmer:put') + ->addArgument( + 'names', + InputArgument::IS_ARRAY | InputArgument::OPTIONAL, + 'Warmers names to put into index.', + [] + ) + ->addOption( + 'connection', + 'c', + InputOption::VALUE_REQUIRED, + 'Connection name to put warmers to.', + 'default' + ); + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $names = $input->getArgument('names'); + $this->getConnection($input->getOption('connection'))->putWarmers($names); + + $message = ''; + if (empty($names)) { + $message = 'All warmers have been put into %s index.'; + } else { + $callback = function ($val) { + return '' . $val . ''; + }; + $message = implode(', ', array_map($callback, $names)) + . ' warmer(s) have been put into %s index.'; + } + + $output->writeln(sprintf($message, $input->getOption('connection'))); + + return 0; + } +} diff --git a/Tests/Functional/Command/CacheClearCommandTest.php b/Tests/Functional/Command/CacheClearCommandTest.php new file mode 100644 index 00000000..acbed36d --- /dev/null +++ b/Tests/Functional/Command/CacheClearCommandTest.php @@ -0,0 +1,71 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchBundle\Tests\Functional\Command; + +use ONGR\ElasticsearchBundle\Command\CacheClearCommand; +use ONGR\ElasticsearchBundle\Test\ElasticsearchTestCase; +use Symfony\Component\Console\Application; +use Symfony\Component\Console\Tester\CommandTester; + +class CacheClearCommandTest extends ElasticsearchTestCase +{ + /** + * Tests if command is being executed. + */ + public function testExecute() + { + $app = new Application(); + $app->add($this->getCommand()); + $command = $app->find('es:cache:clear'); + $tester = new CommandTester($command); + $tester->execute( + [ + 'command' => $command->getName(), + ] + ); + + $this->assertContains('Elasticsearch cache has been cleared for default index.', $tester->getDisplay()); + $this->assertEquals(0, $tester->getStatusCode(), 'Status code should be zero.'); + } + + /** + * Tests if exception is thown when no connection is found. + * + * @expectedException \RuntimeException + */ + public function testExecuteException() + { + $app = new Application(); + $app->add($this->getCommand()); + $command = $app->find('es:cache:clear'); + $tester = new CommandTester($command); + $tester->execute( + [ + 'command' => $command->getName(), + '--connection' => 'foo', + ] + ); + } + + /** + * Returns cache clear command instance. + * + * @return CacheClearCommand + */ + private function getCommand() + { + $command = new CacheClearCommand(); + $command->setContainer($this->getContainer()); + + return $command; + } +} diff --git a/Tests/Functional/Command/WarmerDeleteCommandTest.php b/Tests/Functional/Command/WarmerDeleteCommandTest.php new file mode 100644 index 00000000..b7c6a2ce --- /dev/null +++ b/Tests/Functional/Command/WarmerDeleteCommandTest.php @@ -0,0 +1,85 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchBundle\Tests\Functional\Command; + +use ONGR\ElasticsearchBundle\Command\WarmerDeleteCommand; +use ONGR\ElasticsearchBundle\Test\ElasticsearchTestCase; +use Symfony\Component\Console\Application; +use Symfony\Component\Console\Tester\CommandTester; + +/** + * Tests es warmer delete command. + */ +class WarmerDeleteCommandTest extends ElasticsearchTestCase +{ + /** + * @return array + */ + public function getTestExecuteData() + { + return [ + ["All warmers have been deleted from default index.\n", []], + ["test_foo_warmer warmer(s) have been deleted from default index.\n", ['names' => ['test_foo_warmer']]], + ]; + } + + /** + * Tests if warmers are being deleted from index if command is executed. + * + * @param string $expected + * @param array $arguments + * + * @dataProvider getTestExecuteData + */ + public function testExecute($expected, $arguments = []) + { + $app = new Application(); + $app->add($this->getCommand()); + $command = $app->find('es:warmer:delete'); + $commandTester = new CommandTester($command); + $connection = $this->getManager()->getConnection(); + $connection->putWarmers(); + + $warmers = $connection->getClient()->indices()->getWarmer( + [ + 'index' => $connection->getIndexName(), + 'name' => '*', + ] + ); + + $this->assertNotEmpty($warmers[$connection->getIndexName()], 'Index should have warmers loaded.'); + + $arguments['command'] = $command->getName(); + $commandTester->execute($arguments); + $this->assertEquals($expected, $commandTester->getDisplay()); + + $warmers = $connection->getClient()->indices()->getWarmer( + [ + 'index' => $connection->getIndexName(), + 'name' => '*', + ] + ); + + $this->assertEmpty($warmers, 'Index should not have any warmers loaded.'); + } + + /** + * @return WarmerDeleteCommand + */ + private function getCommand() + { + $command = new WarmerDeleteCommand(); + $command->setContainer($this->getContainer()); + + return $command; + } +} diff --git a/Tests/Functional/Command/WarmerPutCommandTest.php b/Tests/Functional/Command/WarmerPutCommandTest.php new file mode 100644 index 00000000..513b0896 --- /dev/null +++ b/Tests/Functional/Command/WarmerPutCommandTest.php @@ -0,0 +1,82 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchBundle\Tests\Functional\Command; + +use ONGR\ElasticsearchBundle\Command\WarmerPutCommand; +use ONGR\ElasticsearchBundle\Test\ElasticsearchTestCase; +use Symfony\Component\Console\Application; +use Symfony\Component\Console\Tester\CommandTester; + +/** + * Tests es warmer put command. + */ +class WarmerPutCommandTest extends ElasticsearchTestCase +{ + /** + * @return array + */ + public function getTestExecuteData() + { + return [ + ["All warmers have been put into default index.\n", []], + ["test_foo_warmer warmer(s) have been put into default index.\n", ['names' => ['test_foo_warmer']]], + ]; + } + + /** + * Tests if warmers are being put into index if command is executed. + * + * @param string $expected + * @param array $arguments + * + * @dataProvider getTestExecuteData + */ + public function testExecute($expected, $arguments = []) + { + $app = new Application(); + $app->add($this->getCommand()); + $command = $app->find('es:warmer:put'); + $commandTester = new CommandTester($command); + $connection = $this->getManager()->getConnection(); + + $warmers = $connection->getClient()->indices()->getWarmer( + [ + 'index' => $connection->getIndexName(), + 'name' => '*', + ] + ); + $this->assertEmpty($warmers, 'Index should not have any warmers loaded.'); + + $arguments['command'] = $command->getName(); + $commandTester->execute($arguments); + $this->assertEquals($expected, $commandTester->getDisplay()); + + $warmers = $connection->getClient()->indices()->getWarmer( + [ + 'index' => $connection->getIndexName(), + 'name' => '*', + ] + ); + $this->assertNotEmpty($warmers[$connection->getIndexName()], 'Index should have warmers loaded.'); + } + + /** + * @return WarmerPutCommand + */ + private function getCommand() + { + $command = new WarmerPutCommand(); + $command->setContainer($this->getContainer()); + + return $command; + } +} diff --git a/Tests/Unit/Command/UpdateTypeCommandTest.php b/Tests/Unit/Command/UpdateTypeCommandTest.php index 699752fe..0084c0b5 100644 --- a/Tests/Unit/Command/UpdateTypeCommandTest.php +++ b/Tests/Unit/Command/UpdateTypeCommandTest.php @@ -29,13 +29,20 @@ class UpdateTypeCommandTest extends \PHPUnit_Framework_TestCase */ public function testExecuteUnexpectedValue() { + $managerMock = $this + ->getMockBuilder('ONGR\ElasticsearchBundle\ORM\Manager') + ->disableOriginalConstructor() + ->setMethods(['getConnection', 'updateMapping']) + ->getMock(); + + $managerMock->expects($this->once())->method('getConnection')->willReturnSelf(); + $managerMock->expects($this->once())->method('updateMapping')->willReturn(3); + /** @var TypeUpdateCommand|\PHPUnit_Framework_MockObject_MockObject $command */ $command = $this->getMockBuilder('ONGR\ElasticsearchBundle\Command\TypeUpdateCommand') - ->setMethods(['clearMappingCache', 'getConnection', 'updateMapping']) + ->setMethods(['clearMappingCache']) ->getMock(); - $command->expects($this->once())->method('clearMappingCache')->willReturnSelf(); - $command->expects($this->once())->method('getConnection')->willReturnSelf(); - $command->expects($this->once())->method('updateMapping')->willReturn(3); + $command->expects($this->once())->method('clearMappingCache')->willReturn($managerMock); $app = new Application(); $app->add($command);