Skip to content

Commit

Permalink
Merge pull request #460 from magnetik/command-tester
Browse files Browse the repository at this point in the history
Use symfony command tester
  • Loading branch information
alexislefebvre authored Nov 7, 2018
2 parents 95b0d00 + 45ce226 commit 4dd5142
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 86 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ This new major version introduces a number of breaking changes; see the [upgrade
* Compatibility to JackalopeDoctrineDBAL at least 1.3
* Switched from `nelmio/alice` to `theofidry/alice-data-fixtures` (which uses `nelmio/alice` 3)
* The fixtures should be declared as services and tagged with `doctrine.fixture.orm`. It's done automatically if you use [autoconfigure](https://symfony.com/doc/current/service_container.html#service-container-services-load-example)
* The `runCommand` method now returns a Symfony `CommandTester` instance instead of the command output.

### Removed
* Drop support for Symfony 2.x
Expand Down
2 changes: 2 additions & 0 deletions UPGRADE-2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ This is the list of actions that you need to take when upgrading this bundle fro
* MySQL database is created automatically with `doctrine/orm` ≥ 2.6, see [Non-SQLite documentation](README.md#non-sqlite)
* Declare your fixtures as services and tag when with `doctrine.fixture.orm`.
It's done automatically if you use [autoconfigure](https://symfony.com/doc/current/service_container.html#service-container-services-load-example).
* The `runCommand` method returns a `CommandTester`, you need to call `getDisplay()` to keep the old behavior of getting the output.
34 changes: 14 additions & 20 deletions src/Test/WebTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as BaseWebTestCase;
use Symfony\Component\BrowserKit\Cookie;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\StreamOutput;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ResettableContainerInterface;
use Symfony\Component\DomCrawler\Crawler;
Expand Down Expand Up @@ -87,38 +86,33 @@ protected function getServiceMockBuilder(string $id): MockBuilder
*
* @return string
*/
protected function runCommand(string $name, array $params = [], bool $reuseKernel = false): string
protected function runCommand(string $name, array $params = [], bool $reuseKernel = false): CommandTester
{
array_unshift($params, $name);

if (!$reuseKernel) {
if (null !== static::$kernel) {
static::$kernel->shutdown();
}

$kernel = static::$kernel = $this->createKernel(['environment' => $this->environment]);
$kernel = static::$kernel = static::createKernel(['environment' => $this->environment]);
$kernel->boot();
} else {
$kernel = $this->getContainer()->get('kernel');
}

$application = new Application($kernel);
$application->setAutoExit(false);

$input = new ArrayInput($params);
$input->setInteractive(false);

$fp = fopen('php://temp/maxmemory:'.$this->maxMemory, 'r+');
$verbosityLevel = $this->getVerbosityLevel();

$this->setVerbosityLevelEnv($verbosityLevel);
$output = new StreamOutput($fp, $verbosityLevel, $this->getDecorated());

$application->run($input, $output);

rewind($fp);
$command = $application->find($name);
$commandTester = new CommandTester($command);
$commandTester->execute(
array_merge(['command' => $command->getName()], $params),
[
'interactive' => false,
'decorated' => $this->getDecorated(),
'verbosity' => $this->getVerbosityLevel(),
]
);

return stream_get_contents($fp);
return $commandTester;
}

/**
Expand Down
51 changes: 51 additions & 0 deletions tests/App/Command/TestStatusCodeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Liip/FunctionalTestBundle
*
* (c) Lukas Kahwe Smith <smith@pooteeweet.org>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Liip\FunctionalTestBundle\Tests\App\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class TestStatusCodeCommand extends ContainerAwareCommand
{
private $container;

protected function configure(): void
{
parent::configure();

$this->setName('liipfunctionaltestbundle:test-status-code')
->setDescription('Test command');
}

/**
* @param InputInterface $input
* @param OutputInterface $output
*/
protected function initialize(InputInterface $input, OutputInterface $output): void
{
parent::initialize($input, $output);

$this->container = $this->getContainer();
}

/**
* @param InputInterface $input
* @param OutputInterface $output
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
return 10;
}
}
2 changes: 2 additions & 0 deletions tests/App/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,5 @@ services:
tags: ['doctrine.fixture.orm']
Liip\FunctionalTestBundle\Tests\App\Command\TestCommand:
tags: ['console.command']
Liip\FunctionalTestBundle\Tests\App\Command\TestStatusCodeCommand:
tags: ['console.command']
11 changes: 6 additions & 5 deletions tests/Command/CommandConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Liip\FunctionalTestBundle\Test\WebTestCase;
use Liip\FunctionalTestBundle\Tests\AppConfig\AppConfigKernel;
use Symfony\Component\Console\Tester\CommandTester;

/**
* Use Tests/AppConfig/AppConfigKernel.php instead of
Expand All @@ -26,7 +27,7 @@
*/
class CommandConfigTest extends WebTestCase
{
private $display;
private $commandTester;

protected static function getKernelClass(): string
{
Expand All @@ -36,13 +37,13 @@ protected static function getKernelClass(): string
public function testRunCommand(): void
{
// Run command without options
$this->display = $this->runCommand('liipfunctionaltestbundle:test');
$this->commandTester = $this->runCommand('liipfunctionaltestbundle:test');

$this->assertInternalType('string', $this->display);
$this->assertInstanceOf(CommandTester::class, $this->commandTester);

// Test values from configuration
$this->assertContains('Environment: test', $this->display);
$this->assertContains('Verbosity level: VERY_VERBOSE', $this->display);
$this->assertContains('Environment: test', $this->commandTester->getDisplay());
$this->assertContains('Verbosity level: VERY_VERBOSE', $this->commandTester->getDisplay());

$this->assertInternalType('boolean', $this->getDecorated());
$this->assertFalse($this->getDecorated());
Expand Down
Loading

0 comments on commit 4dd5142

Please sign in to comment.