Skip to content

Commit

Permalink
Refactored console command tests to use Application tester.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed Dec 17, 2024
1 parent 58ce3f0 commit a08ec39
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 94 deletions.
4 changes: 3 additions & 1 deletion init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ remove_php() {
remove_php_command() {
rm -Rf php-command || true
rm -Rf src || true
rm -Rf tests/phpunit/Unit/Command || true
rm -Rf tests/phpunit/Functional/ApplicationFunctionalTestCase.php || true
rm -Rf tests/phpunit/Functional/JokeCommandTest.php || true
rm -Rf tests/phpunit/Functional/SayHelloCommandTest.php || true
rm -f docs/content/php/php-command.mdx || true

remove_tokens_with_content "PHP_COMMAND"
Expand Down
23 changes: 23 additions & 0 deletions tests/phpunit/Functional/ApplicationFunctionalTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace YourNamespace\App\Tests\Functional;

use PHPUnit\Framework\TestCase;
use YourNamespace\App\Tests\Traits\ArrayTrait;
use YourNamespace\App\Tests\Traits\AssertTrait;
use YourNamespace\App\Tests\Traits\MockTrait;

/**
* Class ApplicationFunctionalTestCase.
*
* Base class to unit test scripts.
*/
abstract class ApplicationFunctionalTestCase extends TestCase {

use ArrayTrait;
use AssertTrait;
use MockTrait;

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

declare(strict_types=1);

namespace YourNamespace\App\Tests\Unit\Command;
namespace YourNamespace\App\Tests\Functional;

use PHPUnit\Framework\Attributes\CoversMethod;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use Symfony\Component\Console\Command\Command;
use YourNamespace\App\Command\JokeCommand;
use YourNamespace\App\Tests\Traits\ConsoleTrait;
use YourNamespace\App\Tests\Traits\MockTrait;

/**
* Class JokeCommandTest.
Expand All @@ -19,32 +20,33 @@
#[CoversMethod(JokeCommand::class, 'configure')]
#[CoversMethod(JokeCommand::class, 'getJoke')]
#[Group('command')]
class JokeCommandTest extends CommandTestCase {
class JokeCommandTest extends ApplicationFunctionalTestCase {

use ConsoleTrait;
use MockTrait;

#[DataProvider('dataProviderExecute')]
public function testExecute(string $content, int $expected_code, array|string $expected_output = []): void {
public function testExecute(string $content, array $expected_output, bool $expected_fail = FALSE): void {
/** @var \YourNamespace\App\Command\JokeCommand $mock */
// @phpstan-ignore varTag.nativeType
$mock = $this->prepareMock(JokeCommand::class, [
'getContent' => $content,
]);
$mock->setName('joke');

$output = $this->runExecute($mock);

$this->assertEquals($expected_code, $this->commandTester->getStatusCode());
$expected_output = is_array($expected_output) ? $expected_output : [$expected_output];
$this->consoleInitApplicationTester($mock);
$output = $this->consoleApplicationRun([], [], $expected_fail);
foreach ($expected_output as $expected_output_string) {
$this->assertArrayContainsString($expected_output_string, $output);
$this->assertStringContainsString($expected_output_string, $output);
}
}

public static function dataProviderExecute(): array {
return [
[static::fixturePayload(['setup' => 'Test setup', 'punchline' => 'Test punchline']), Command::SUCCESS, ['Test setup', 'Test punchline']],
['', Command::FAILURE, ['Unable to retrieve a joke.']],
['non-json', Command::FAILURE, ['Unable to retrieve a joke.']],
[static::fixturePayload(['setup' => 'Test setup']), Command::FAILURE, ['Unable to retrieve a joke.']],
[static::fixturePayload(['setup' => 'Test setup', 'punchline' => 'Test punchline']), ['Test setup', 'Test punchline']],
['', ['Unable to retrieve a joke.'], TRUE],
['non-json', ['Unable to retrieve a joke.'], TRUE],
[static::fixturePayload(['setup' => 'Test setup']), ['Unable to retrieve a joke.'], TRUE],
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

declare(strict_types=1);

namespace YourNamespace\App\Tests\Unit\Command;
namespace YourNamespace\App\Tests\Functional;

use PHPUnit\Framework\Attributes\CoversMethod;
use PHPUnit\Framework\Attributes\Group;
use YourNamespace\App\Command\SayHelloCommand;
use YourNamespace\App\Tests\Traits\ConsoleTrait;

/**
* Class SayHelloCommandTest.
Expand All @@ -16,11 +17,15 @@
#[CoversMethod(SayHelloCommand::class, 'execute')]
#[CoversMethod(SayHelloCommand::class, 'configure')]
#[Group('command')]
class SayHelloCommandTest extends CommandTestCase {
class SayHelloCommandTest extends ApplicationFunctionalTestCase {

use ConsoleTrait;

public function testExecute(): void {
$output = $this->runExecute(SayHelloCommand::class);
$this->assertArrayContainsString('Hello, Symfony console!', $output);
$this->consoleInitApplicationTester(SayHelloCommand::class);

$output = $this->consoleApplicationRun();
$this->assertStringContainsString('Hello, Symfony console!', $output);
}

}
8 changes: 5 additions & 3 deletions tests/phpunit/Traits/ConsoleTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
trait ConsoleTrait {

use ReflectionTrait;

/**
* Application tester.
*/
Expand Down Expand Up @@ -54,9 +56,7 @@ protected function consoleInitApplicationTester(string|object $object_or_class,

$application->setAutoExit(FALSE);
$application->setCatchExceptions(FALSE);
if (method_exists($application, 'setCatchErrors')) {
$application->setCatchErrors(FALSE);
}
$application->setCatchErrors(FALSE);

$this->appTester = new ApplicationTester($application);
}
Expand All @@ -75,6 +75,8 @@ protected function consoleInitApplicationTester(string|object $object_or_class,
* Run output (stdout or stderr).
*/
protected function consoleApplicationRun(array $input = [], array $options = [], bool $expect_fail = FALSE): string {
$output = '';

$options += ['capture_stderr_separately' => TRUE];

try {
Expand Down
65 changes: 0 additions & 65 deletions tests/phpunit/Unit/Command/CommandTestCase.php

This file was deleted.

8 changes: 6 additions & 2 deletions tests/scaffold/_assert_init.bash
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ assert_files_present_php_command() {
assert_file_exists force-crystal
assert_dir_exists src

assert_dir_exists tests/phpunit/Unit/Command
assert_file_exists tests/phpunit/Functional/ApplicationFunctionalTestCase.php
assert_file_exists tests/phpunit/Functional/JokeCommandTest.php
assert_file_exists tests/phpunit/Functional/SayHelloCommandTest.php

assert_file_contains "phpcs.xml" "<file>src</file>"
assert_file_contains "phpstan.neon" "- src"
Expand All @@ -156,7 +158,9 @@ assert_files_absent_php_command() {

assert_file_not_exists php-command
assert_dir_not_exists src
assert_dir_not_exists tests/phpunit/Unit/Command
assert_file_not_exists tests/phpunit/Functional/ApplicationFunctionalTestCase.php
assert_file_not_exists tests/phpunit/Functional/JokeCommandTest.php
assert_file_not_exists tests/phpunit/Functional/SayHelloCommandTest.php

assert_file_not_contains "phpcs.xml" "<file>src</file>"
assert_file_not_contains "phpstan.neon" "- src"
Expand Down
12 changes: 6 additions & 6 deletions tests/scaffold/functional.composer.bats
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,28 @@ BATS_FIXTURE_EXPORT_CODEBASE_ENABLED=1

run composer test
assert_success
assert_output_contains "OK (21 tests, 36 assertions)"
assert_output_contains "OK (21 tests, 32 assertions)"
assert_dir_not_exists ".coverage-html"

composer_reset_env

run bash -c "XDEBUG_MODE=coverage composer test"
assert_success
assert_output_contains "OK (21 tests, 36 assertions)"
assert_output_contains "OK (21 tests, 32 assertions)"
assert_dir_not_exists ".coverage-html"

composer_reset_env

run composer test -- --group=command
assert_success
assert_output_contains "OK (5 tests, 10 assertions)"
assert_output_contains "OK (5 tests, 6 assertions)"
assert_dir_not_exists ".coverage-html"

composer_reset_env

run bash -c "XDEBUG_MODE=coverage composer test -- --group=command"
assert_success
assert_output_contains "OK (5 tests, 10 assertions)"
assert_output_contains "OK (5 tests, 6 assertions)"
assert_dir_not_exists ".coverage-html"
}

Expand All @@ -76,7 +76,7 @@ BATS_FIXTURE_EXPORT_CODEBASE_ENABLED=1
run bash -c "XDEBUG_MODE=coverage composer test-coverage"
assert_success
assert_output_not_contains "WARNINGS!"
assert_output_contains "OK (21 tests, 36 assertions)"
assert_output_contains "OK (21 tests, 32 assertions)"
assert_dir_exists ".coverage-html"

composer_reset_env
Expand All @@ -91,7 +91,7 @@ BATS_FIXTURE_EXPORT_CODEBASE_ENABLED=1
run bash -c "XDEBUG_MODE=coverage composer test-coverage -- --group=command"
assert_success
assert_output_not_contains "WARNINGS!"
assert_output_contains "OK (5 tests, 10 assertions)"
assert_output_contains "OK (5 tests, 6 assertions)"
assert_dir_exists ".coverage-html"
}

Expand Down

1 comment on commit a08ec39

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.