forked from pmishev/ElasticsearchBundle
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
221 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace Sineflow\ElasticsearchBundle\Tests\Functional\Command; | ||
|
||
use Sineflow\ElasticsearchBundle\Manager\IndexManager; | ||
use Symfony\Component\Console\Application; | ||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
|
||
/** | ||
* Helper test case for testing commands. | ||
*/ | ||
abstract class AbstractCommandTestCase extends WebTestCase | ||
{ | ||
/** | ||
* @return \Symfony\Component\DependencyInjection\ContainerInterface | ||
*/ | ||
protected function getContainer() | ||
{ | ||
return self::createClient()->getContainer(); | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* | ||
* @return IndexManager | ||
*/ | ||
protected function getIndexManager($name) | ||
{ | ||
return $this->getContainer()->get(sprintf('sfes.index.%s', $name)); | ||
} | ||
|
||
/** | ||
* Returns command | ||
* | ||
* @return Command | ||
*/ | ||
abstract protected function getCommand(); | ||
|
||
/** | ||
* Returns command tester. | ||
* | ||
* @param string commandName | ||
* @return CommandTester | ||
*/ | ||
protected function getCommandTester($commandName) | ||
{ | ||
$app = new Application(); | ||
$app->add($this->getCommand()); | ||
|
||
$command = $app->find($commandName); | ||
$commandTester = new CommandTester($command); | ||
|
||
return $commandTester; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace Sineflow\ElasticsearchBundle\Tests\Functional\Command; | ||
|
||
use Sineflow\ElasticsearchBundle\Command\IndexBuildCommand; | ||
use Symfony\Component\Console\Application; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
|
||
class IndexBuildCommandTest extends AbstractCommandTestCase | ||
{ | ||
/** | ||
* Tests building index | ||
*/ | ||
public function testExecute() | ||
{ | ||
$manager = $this->getIndexManager('foo'); | ||
|
||
// Initialize command | ||
$commandName = 'sineflow:es:index:build'; | ||
$commandTester = $this->getCommandTester($commandName); | ||
$options = []; | ||
$arguments['command'] = $commandName; | ||
$arguments['index'] = $manager->getManagerName(); | ||
$arguments['--delete-old'] = true; | ||
|
||
// Test if the command returns 0 or not | ||
$this->assertSame( | ||
0, | ||
$commandTester->execute($arguments, $options) | ||
); | ||
|
||
$expectedOutput = sprintf( | ||
'Built index for "foo"' | ||
); | ||
|
||
// Test if the command output matches the expected output or not | ||
$this->assertStringMatchesFormat($expectedOutput . '%a', $commandTester->getDisplay()); | ||
|
||
$manager->dropIndex(); | ||
} | ||
|
||
/** | ||
* Returns build index command with assigned container. | ||
* | ||
* @return IndexBuildCommand | ||
*/ | ||
protected function getCommand() | ||
{ | ||
$command = new IndexBuildCommand(); | ||
$command->setContainer($this->getContainer()); | ||
|
||
return $command; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
namespace Sineflow\ElasticsearchBundle\Tests\Functional\Command; | ||
|
||
use Sineflow\ElasticsearchBundle\Command\IndexCreateCommand; | ||
use Symfony\Component\Console\Application; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
|
||
class IndexCreateCommandTest extends AbstractCommandTestCase | ||
{ | ||
/** | ||
* Tests creating index | ||
*/ | ||
public function testExecute() | ||
{ | ||
$manager = $this->getIndexManager('foo'); | ||
|
||
// Make sure we don't have pre-existing index | ||
$manager->dropIndex(); | ||
|
||
// Initialize command | ||
$commandName = 'sineflow:es:index:create'; | ||
$commandTester = $this->getCommandTester($commandName); | ||
$options = []; | ||
$arguments['command'] = $commandName; | ||
$arguments['index'] = $manager->getManagerName(); | ||
|
||
// Test if the command returns 0 or not | ||
$this->assertSame( | ||
0, | ||
$commandTester->execute($arguments, $options) | ||
); | ||
|
||
$expectedOutput = sprintf( | ||
'Created index for "foo"' | ||
); | ||
|
||
// Test if the command output matches the expected output or not | ||
$this->assertStringMatchesFormat($expectedOutput . '%a', $commandTester->getDisplay()); | ||
|
||
$manager->dropIndex(); | ||
} | ||
|
||
/** | ||
* Tests creating index in case of existing this index. | ||
*/ | ||
public function testExecuteWithExistingIndex() | ||
{ | ||
$manager = $this->getIndexManager('foo'); | ||
|
||
// Make sure we don't have pre-existing index | ||
$manager->dropIndex(); | ||
$manager->createIndex(); | ||
|
||
// Initialize command | ||
$commandName = 'sineflow:es:index:create'; | ||
$commandTester = $this->getCommandTester($commandName); | ||
$options = []; | ||
$arguments['command'] = $commandName; | ||
$arguments['index'] = $manager->getManagerName(); | ||
|
||
// Test if the command returns 0 or not | ||
$this->assertSame( | ||
0, | ||
$commandTester->execute($arguments, $options) | ||
); | ||
|
||
$expectedOutput = sprintf( | ||
'Index creation failed' | ||
); | ||
|
||
// Test if the command output matches the expected output or not | ||
$this->assertStringMatchesFormat($expectedOutput . '%a', $commandTester->getDisplay()); | ||
|
||
$manager->dropIndex(); | ||
} | ||
|
||
/** | ||
* Returns create index command with assigned container. | ||
* | ||
* @return IndexCreateCommand | ||
*/ | ||
protected function getCommand() | ||
{ | ||
$command = new IndexCreateCommand(); | ||
$command->setContainer($this->getContainer()); | ||
|
||
return $command; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters