Skip to content

Commit

Permalink
Commands tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pmishev committed Feb 15, 2016
1 parent 1d34795 commit d3c977a
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Subscriber/KnpPaginateQuerySubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function items(ItemsEvent $event)
break;

default:
throw new Exception(sprintf('Unsupported results type "%s" for KNP paginator', $resultsType));
throw new \InvalidArgumentException(sprintf('Unsupported results type "%s" for KNP paginator', $resultsType));
}

$event->stopPropagation();
Expand Down
58 changes: 58 additions & 0 deletions Tests/Functional/Command/AbstractCommandTestCase.php
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;
}

}
55 changes: 55 additions & 0 deletions Tests/Functional/Command/IndexBuildCommandTest.php
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;
}

}
91 changes: 91 additions & 0 deletions Tests/Functional/Command/IndexCreateCommandTest.php
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;
}

}
15 changes: 15 additions & 0 deletions Tests/Functional/Paginator/KnpPaginatorAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,19 @@ public function testPagination()
$this->assertEquals(3, $pagination->current()['_id']);
$this->assertEquals('3rd Product', $pagination->current()['_source']['title']);
}

/**
* @expectedException \InvalidArgumentException
*/
public function testInvalidResultsType()
{
/** @var Repository $repo */
$repo = $this->getIndexManager('bar')->getRepository('AcmeBarBundle:Product');
$paginator = $this->getContainer()->get('knp_paginator');

$query = ['query' => ['match_all' => []]];

$adapter = $repo->find($query, Finder::ADAPTER_KNP);
$paginator->paginate($adapter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public function testLoad($parameters, $expectedConnections, $expectedManagers)
class_exists('testClass') ? : eval('class testClass {}');
$container->setParameter('kernel.bundles', ['testBundle' => 'testClass']);
$container->setParameter('kernel.cache_dir', '');
$container->setParameter('kernel.logs_dir', '');
$container->setParameter('kernel.debug', true);
$extension = new SineflowElasticsearchExtension();
$extension->load(
Expand Down

0 comments on commit d3c977a

Please sign in to comment.