at the end of the day, it was inevitable
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace Command\Context;
|
||||
|
||||
use Behat\Gherkin\Node\TableNode;
|
||||
use Command\Util\CommandTest;
|
||||
use Command\Util\CommandTestFactory;
|
||||
use Common\Context\AbstractContext;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\HttpKernel\KernelInterface;
|
||||
|
||||
/**
|
||||
* Class CommandContext
|
||||
* @package Command\Context
|
||||
*/
|
||||
class CommandContext extends AbstractContext
|
||||
{
|
||||
|
||||
/**
|
||||
* @var CommandTestFactory
|
||||
*/
|
||||
private $factory;
|
||||
|
||||
/**
|
||||
* @var CommandTest
|
||||
*/
|
||||
private $command;
|
||||
|
||||
/**
|
||||
* @param ContainerInterface $container A ContainerInterface instance.
|
||||
* @param string $fixturesDir Path to fixtures directory.
|
||||
*/
|
||||
public function __construct(ContainerInterface $container, $fixturesDir)
|
||||
{
|
||||
parent::__construct($container, $fixturesDir);
|
||||
|
||||
/** @var KernelInterface $kernel */
|
||||
$kernel = $container->get('kernel');
|
||||
$this->factory = new CommandTestFactory($kernel);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Given /^I run command (?P<command>.+)$/
|
||||
*
|
||||
* @param string $name Command name.
|
||||
* @param TableNode $table Command parameters in table format.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function runCommand($name, TableNode $table = null)
|
||||
{
|
||||
$params = [];
|
||||
if ($table !== null) {
|
||||
foreach ($table as $row) {
|
||||
$params[current($row)] = next($row);
|
||||
}
|
||||
}
|
||||
|
||||
$this->command = $this->factory->create($name, $params)->run();
|
||||
}
|
||||
|
||||
/**
|
||||
* @Then /^(?:|[Cc]ommand )[Rr]eturned (?P<code>\d+) exit code$/
|
||||
*
|
||||
* @param integer $code Command exit code.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function checkExitCode($code = 0)
|
||||
{
|
||||
self::assertEquals($code, $this->command->getExitCode());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace Command\Util;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Console\Application;
|
||||
use Symfony\Component\Console\Input\ArrayInput;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\BufferedOutput;
|
||||
|
||||
/**
|
||||
* Class CommandTest
|
||||
* @package Command\Util
|
||||
*/
|
||||
class CommandTest
|
||||
{
|
||||
|
||||
/**
|
||||
* @var Application
|
||||
*/
|
||||
private $application;
|
||||
|
||||
/**
|
||||
* @var InputInterface
|
||||
*/
|
||||
private $input;
|
||||
|
||||
/**
|
||||
* @var BufferedOutput
|
||||
*/
|
||||
private $output;
|
||||
|
||||
/**
|
||||
* @var integer
|
||||
*/
|
||||
private $exitCode;
|
||||
|
||||
/**
|
||||
* Command constructor.
|
||||
*
|
||||
* @param Application $application A Application instance.
|
||||
* @param string $command Command name.
|
||||
* @param array $params Command parameters.
|
||||
*/
|
||||
public function __construct(
|
||||
Application $application,
|
||||
$command,
|
||||
array $params = []
|
||||
) {
|
||||
$this->application = $application;
|
||||
$this->input = new ArrayInput([ 'command' => $command ] + $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run this command.
|
||||
*
|
||||
* @return CommandTest
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
$this->output = new BufferedOutput();
|
||||
$this->exitCode = $this->application->run($this->input, $this->output);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get exit code.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getExitCode()
|
||||
{
|
||||
return $this->exitCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get output.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getOutput()
|
||||
{
|
||||
return $this->output->fetch();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace Command\Util;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Console\Application;
|
||||
use Symfony\Component\HttpKernel\KernelInterface;
|
||||
|
||||
/**
|
||||
* Class CommandTestFactory
|
||||
* @package Command\Util
|
||||
*/
|
||||
class CommandTestFactory
|
||||
{
|
||||
|
||||
/**
|
||||
* @var Application
|
||||
*/
|
||||
private $application;
|
||||
|
||||
/**
|
||||
* CommandRunner constructor.
|
||||
*
|
||||
* @param KernelInterface $kernel A KernelInterface instance.
|
||||
*/
|
||||
public function __construct(KernelInterface $kernel)
|
||||
{
|
||||
$this->application = new Application($kernel);
|
||||
// If don't set to false, application will call 'exit' after command was
|
||||
// executed.
|
||||
$this->application->setAutoExit(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new command instance.
|
||||
*
|
||||
* @param string $command Command name.
|
||||
* @param array $params Command parameters.
|
||||
*
|
||||
* @return CommandTest
|
||||
*/
|
||||
public function create($command, array $params = [])
|
||||
{
|
||||
return new CommandTest($this->application, $command, $params);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
Feature:
|
||||
Application should fetch document for stored queries in background.
|
||||
So we must have command that do it.
|
||||
|
||||
@db-fixtures @external-index-fixtures
|
||||
Scenario:
|
||||
Fetch document for stored queries.
|
||||
|
||||
|
||||
Given I run command socialhose:stored_query:fetch
|
||||
|
||||
Then command returned 0 exit code
|
||||
|
||||
# Check entities in database.
|
||||
And database has entity CacheBundle:Query\StoredQuery
|
||||
| raw | cat |
|
||||
| status | synced |
|
||||
And has entity CacheBundle:Document
|
||||
| title | About cat |
|
||||
And don't has entity AppBundle:FetchJob
|
||||
| id | 1 |
|
||||
|
||||
When I wait 1000 milliseconds until documents was indexed
|
||||
Then internal index has 1 document
|
||||
| query | eq | #getStoredQuery({'raw': 'cat', 'status': 'synced'}).getId()# |
|
||||
|
||||
# After add new document.
|
||||
Given has new document in external index
|
||||
| sequence | 2 |
|
||||
| title | New cat article |
|
||||
| date_found | #date().getTimestamp()# |
|
||||
|
||||
When I run command socialhose:stored_query:update
|
||||
Then command returned 0 exit code
|
||||
|
||||
# Check entities in database.
|
||||
And database has entity CacheBundle:Query\StoredQuery
|
||||
| raw | cat |
|
||||
| status | synced |
|
||||
And has entity CacheBundle:Document
|
||||
| title | About cat |
|
||||
And has entity CacheBundle:Document
|
||||
| title | New cat article |
|
||||
|
||||
When I wait 1000 milliseconds until documents was indexed
|
||||
Then internal index has 2 document
|
||||
| query | eq | #getStoredQuery({'raw': 'cat', 'status': 'synced'}).getId()# |
|
||||
|
||||
# Add third document.
|
||||
Given has new document in external index
|
||||
| sequence | 3 |
|
||||
| title | About dogs |
|
||||
| date_found | #date().getTimestamp()# |
|
||||
|
||||
When I run command socialhose:stored_query:update
|
||||
Then command returned 0 exit code
|
||||
|
||||
# Check entities in database.
|
||||
And has entity CacheBundle:Document
|
||||
| title | About cat |
|
||||
And has entity CacheBundle:Document
|
||||
| title | New cat article |
|
||||
But don't has entity CacheBundle:Document
|
||||
| title | About dogs |
|
||||
|
||||
When I wait 1000 milliseconds until documents was indexed
|
||||
Then internal index has 2 document
|
||||
| query | eq | #getStoredQuery({'raw': 'cat', 'status': 'synced'}).getId()# |
|
||||
Reference in New Issue
Block a user