Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/Test/LegacyWebTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?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\Test;

use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as SymfonyWebTestCase;
use Symfony\Component\Console\Tester\CommandTester;

abstract class LegacyWebTestCase extends SymfonyWebTestCase
{
/**
* Runs a command and returns a CommandTester.
*/
protected function runCommand(string $name, array $params = [], bool $reuseKernel = false): CommandTester
{
if (!$reuseKernel) {
if (null !== static::$kernel) {
static::ensureKernelShutdown();
}

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

$application = new Application($kernel);

$options = [
'interactive' => false,
'decorated' => $this->getDecorated(),
'verbosity' => $this->getVerbosityLevel(),
];

$command = $application->find($name);
$commandTester = new CommandTester($command);

if (null !== $inputs = $this->getInputs()) {
$commandTester->setInputs($inputs);
$options['interactive'] = true;
$this->inputs = null;
}

$commandTester->execute(
array_merge(['command' => $command->getName()], $params),
$options
);

return $commandTester;
}
}
55 changes: 55 additions & 0 deletions src/Test/ModernWebTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?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\Test;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as SymfonyWebTestCase;
use Symfony\Component\Console\Tester\ExecutionResult;

abstract class ModernWebTestCase extends SymfonyWebTestCase
{
/**
* Runs a console command and returns the execution result.
*/
public static function runCommand(
string $name,
array $input = [],
mixed $interactiveInputs = [],
?bool $interactive = null,
?bool $decorated = null,
?int $verbosity = null,
array $normalizers = []
): ExecutionResult {
if (\is_bool($interactiveInputs)) {
$reuseKernel = $interactiveInputs;
if (!$reuseKernel) {
static::ensureKernelShutdown();
}
$interactiveInputs = [];
}

// Retrieve properties from the active test instance if not explicitly provided
if (null === $decorated && WebTestCase::$activeInstance) {
$decorated = WebTestCase::$activeInstance->getDecorated();
}
if (null === $verbosity && WebTestCase::$activeInstance) {
$verbosity = WebTestCase::$activeInstance->getVerbosityLevel();
}
if (empty($interactiveInputs) && WebTestCase::$activeInstance) {
$interactiveInputs = WebTestCase::$activeInstance->getInputs() ?? [];
WebTestCase::$activeInstance->inputs = null;
}

return parent::runCommand($name, $input, $interactiveInputs, $interactive, $decorated, $verbosity, $normalizers);
}
}
67 changes: 22 additions & 45 deletions src/Test/WebTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@
use Liip\FunctionalTestBundle\Utils\HttpAssertions;
use PHPUnit\Framework\MockObject\MockBuilder;
use Symfony\Bundle\FrameworkBundle\Client;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as BaseWebTestCase;
use Symfony\Component\BrowserKit\Cookie;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ResettableContainerInterface;
use Symfony\Component\DomCrawler\Crawler;
Expand All @@ -40,6 +37,16 @@
class_alias(KernelBrowser::class, Client::class);
}

if (!method_exists(\Symfony\Bundle\FrameworkBundle\Test\WebTestCase::class, 'runCommand')) {
if (!class_exists(BaseWebTestCase::class, false)) {
class_alias(LegacyWebTestCase::class, BaseWebTestCase::class);
}
} else {
if (!class_exists(BaseWebTestCase::class, false)) {
class_alias(ModernWebTestCase::class, BaseWebTestCase::class);
}
}

/**
* @author Lea Haensenberger
* @author Lukas Kahwe Smith <smith@pooteeweet.org>
Expand All @@ -51,6 +58,8 @@ class_alias(KernelBrowser::class, Client::class);
*/
abstract class WebTestCase extends BaseWebTestCase
{
public static ?self $activeInstance = null;

protected static $env = 'test';

protected $containers;
Expand All @@ -66,7 +75,7 @@ abstract class WebTestCase extends BaseWebTestCase
/**
* @var array|null
*/
private $inputs;
protected $inputs;

/**
* @var array
Expand Down Expand Up @@ -103,47 +112,6 @@ protected function setServiceMock(
}
}

/**
* Builds up the environment to run the given command.
*/
protected function runCommand(string $name, array $params = [], bool $reuseKernel = false): CommandTester
{
if (!$reuseKernel) {
if (null !== static::$kernel) {
static::ensureKernelShutdown();
}

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

$application = new Application($kernel);

$options = [
'interactive' => false,
'decorated' => $this->getDecorated(),
'verbosity' => $this->getVerbosityLevel(),
];

$command = $application->find($name);
$commandTester = new CommandTester($command);

if (null !== $inputs = $this->getInputs()) {
$commandTester->setInputs($inputs);
$options['interactive'] = true;
$this->inputs = null;
}

$commandTester->execute(
array_merge(['command' => $command->getName()], $params),
$options
);

return $commandTester;
}

/**
* Retrieves the output verbosity level.
*
Expand Down Expand Up @@ -535,8 +503,17 @@ public static function assertValidationErrors(array $expected, ContainerInterfac
HttpAssertions::assertValidationErrors($expected, $container);
}

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

self::$activeInstance = $this;
}

protected function tearDown(): void
{
self::$activeInstance = null;

if (null !== $this->containers) {
foreach ($this->containers as $container) {
if ($container instanceof ResettableContainerInterface) {
Expand Down
6 changes: 5 additions & 1 deletion tests/Command/CommandConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ public function testRunCommand(): void
// Run command without options
$this->commandTester = $this->runCommand('liipfunctionaltestbundle:test');

$this->assertInstanceOf(CommandTester::class, $this->commandTester);
if ($this->commandTester instanceof CommandTester) {
$this->assertInstanceOf(CommandTester::class, $this->commandTester);
} else {
$this->assertInstanceOf(\Symfony\Component\Console\Tester\ExecutionResult::class, $this->commandTester);
}

// Test values from configuration
$this->assertStringContainsString('Environment: test', $this->commandTester->getDisplay());
Expand Down
Loading