Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tester] Restructure CommandTestTrait to allow more flexibility #270

Merged
merged 1 commit into from
May 21, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

Expand All @@ -20,9 +19,9 @@ class CronDumpToFileCommandTest extends TestCase

private CronManager&MockObject $cronManager;

public function createCommand(): Command
protected function setUp(): void
{
return new CronDumpToFileCommand(
$this->command = new CronDumpToFileCommand(
$this->cronManager = $this->createMock(CronManager::class)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Command\Command;

#[CoversClass(UpdateDeployedVersionCommand::class)]
class UpdateDeployedVersionCommandTest extends TestCase
Expand All @@ -18,9 +17,9 @@ class UpdateDeployedVersionCommandTest extends TestCase

private VersionManager&MockObject $versionManager;

public function createCommand(): Command
protected function setUp(): void
{
return new UpdateDeployedVersionCommand(
$this->command = new UpdateDeployedVersionCommand(
$this->versionManager = $this->createMock(VersionManager::class)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

Expand All @@ -23,7 +22,7 @@ class CloudWatchLogsDownloadCommandTest extends TestCase

private CloudWatchLogsClient&MockObject $cloudWatchLogsClient;

public function createCommand(): Command
protected function setUp(): void
{
$this->cloudWatchLogsClient = $this
->getMockBuilder(CloudWatchLogsClient::class)
Expand All @@ -34,7 +33,7 @@ public function createCommand(): Command
->addMethods(['getLogEvents'])
->getMock();

return new CloudWatchLogsDownloadCommand($this->cloudWatchLogsClient);
$this->command = new CloudWatchLogsDownloadCommand($this->cloudWatchLogsClient);
}

public function getCommandName(): string
Expand Down
5 changes: 2 additions & 3 deletions packages/console/Tests/Command/PurgeExecutionCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;

#[CoversClass(PurgeExecutionCommand::class)]
Expand All @@ -24,12 +23,12 @@ class PurgeExecutionCommandTest extends TestCase

private LoggerInterface&MockObject $logger;

public function createCommand(): Command
protected function setUp(): void
{
$this->connection = $this->createMock(Connection::class);
$this->logger = $this->createMock(LoggerInterface::class);

return new PurgeExecutionCommand($this->connection, $this->logger);
$this->command = new PurgeExecutionCommand($this->connection, $this->logger);
}

public function getCommandName(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class QueueCronJobByNameCommandTest extends TestCase

private EntityRepository&MockObject $repository;

public function createCommand(): Command
protected function setUp(): void
{
$this->managerRegistry = $this->createMock(ManagerRegistry::class);
$this->managerRegistry
Expand All @@ -37,7 +37,7 @@ public function createCommand(): Command
->with(CronJob::class)
->willReturn($this->repository = $this->createMock(EntityRepository::class));

return new QueueCronJobByNameCommand(
$this->command = new QueueCronJobByNameCommand(
$this->managerRegistry,
$this->cronJobProcessor = $this->createMock(CronJobProcessor::class)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ class QueueDueCronJobsCommandTest extends TestCase

private CronJobProcessor&MockObject $cronJobProcessor;

public function createCommand(): Command
protected function setUp(): void
{
return new QueueDueCronJobsCommand(
$this->command = new QueueDueCronJobsCommand(
$this->managerRegistry = $this->createMock(ManagerRegistry::class),
$this->cronJobProcessor = $this->createMock(CronJobProcessor::class)
);
Expand Down
5 changes: 2 additions & 3 deletions packages/mailer/Tests/Command/SendTestEmailCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
Expand All @@ -20,9 +19,9 @@ class SendTestEmailCommandTest extends TestCase

private MailerInterface&MockObject $mailer;

public function createCommand(): Command
protected function setUp(): void
{
return new SendTestEmailCommand(
$this->command = new SendTestEmailCommand(
$this->mailer = $this->createMock(MailerInterface::class)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\InvalidOptionException;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\EventDispatcher\EventDispatcher;
Expand All @@ -30,9 +29,9 @@ class StartMessengerBrokerCommandTest extends TestCase

private string $consolePath;

public function createCommand(): Command
protected function setUp(): void
{
return new StartMessengerBrokerCommand(
$this->command = new StartMessengerBrokerCommand(
$this->consolePath = uniqid('console-path-'),
$this->processFactory = $this->createMock(ProcessFactoryInterface::class),
$this->eventDispatcher = new EventDispatcher(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Messenger\Transport\TransportInterface;
Expand All @@ -24,9 +23,9 @@ class PurgeExpiredMessageCommandTest extends TestCase

private TransportRepository&MockObject $transportRepository;

public function createCommand(): Command
protected function setUp(): void
{
return new PurgeExpiredMessageCommand(
$this->command = new PurgeExpiredMessageCommand(
$this->transportRepository = $this->createMock(TransportRepository::class),
);
}
Expand Down
5 changes: 2 additions & 3 deletions packages/open-api/Tests/Command/InstallSandboxCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Draw\Component\Tester\MockTrait;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Filesystem\Filesystem;
Expand All @@ -19,9 +18,9 @@ class InstallSandboxCommandTest extends TestCase
use CommandTestTrait;
use MockTrait;

public function createCommand(): Command
protected function setUp(): void
{
return new InstallSandboxCommand();
$this->command = new InstallSandboxCommand();
}

public function getCommandName(): string
Expand Down
68 changes: 50 additions & 18 deletions packages/tester/Application/CommandTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,37 @@ trait CommandTestTrait
{
private static int $argumentsCount;

protected CommandTester $commandTester;
protected ?CommandTester $commandTester = null;

protected Command $command;
protected ?Command $command = null;

abstract public function createCommand(): Command;
private ?Application $application = null;

protected function createCommand(): Command
{
if (null === $this->command) {
throw new \RuntimeException('You must override createCommand method or set the command property.');
}

return $this->command;
}

final protected function loadCommand(): Command
{
$command = $this->command ??= $this->createCommand();

if (null === $this->application) {
$this->application = new Application();
$this->application->add($command);
}

return $command;
}

final protected function loadCommandTester(): CommandTester
{
return $this->commandTester ??= new CommandTester($this->loadCommand());
}

abstract public function getCommandName(): string;

Expand All @@ -31,14 +57,6 @@ public static function setUpBeforeClass(): void
self::$argumentsCount = 0;
}

public function setUp(): void
{
$application = new Application();
$application->add($this->createCommand());
$this->command = $application->find($this->getCommandName());
$this->commandTester = new CommandTester($this->command);
}

protected function getMinimumCommandDescriptionStringLength(): int
{
return 10;
Expand All @@ -49,22 +67,34 @@ protected function getMinimumInputDescriptionStringLength(): int
return 10;
}

public function testGetCommandName(): void
{
$command = $this->loadCommand();

TestCase::assertSame(
$command,
$this->application->find($this->getCommandName()),
'Cannot find the command by name'
);
}

public function testGetDescription(): void
{
TestCase::assertGreaterThanOrEqual(
$this->getMinimumCommandDescriptionStringLength(),
mb_strlen($this->command->getDescription()),
mb_strlen($this->loadCommand()->getDescription()),
'Command description is too short'
);
}

public function testArguments(): void
{
$definition = $this->command->getDefinition();
$command = $this->loadCommand();
$definition = $command->getDefinition();

$position = 0;
foreach ($this->provideTestArgument() as $argument) {
array_unshift($argument, $this->command, $position);
array_unshift($argument, $command, $position);
\call_user_func_array([$this, 'assertArgument'], $argument);
++$position;
}
Expand Down Expand Up @@ -118,7 +148,8 @@ public function assertArgument(
public function testOptions(): void
{
$count = 0;
$definition = $this->command->getDefinition();
$command = $this->loadCommand();
$definition = $command->getDefinition();
$realCommandOptions = [];
foreach ($definition->getOptions() as $option) {
$realCommandOptions[$option->getName()] = $option;
Expand All @@ -127,7 +158,7 @@ public function testOptions(): void
foreach ($this->provideTestOption() as $optionConfiguration) {
unset($realCommandOptions[$optionConfiguration[0]]);
++$count;
array_unshift($optionConfiguration, $this->command);
array_unshift($optionConfiguration, $command);
\call_user_func_array([$this, 'assertOption'], $optionConfiguration);
}

Expand Down Expand Up @@ -207,12 +238,13 @@ public function assertOption(
*/
public function execute(array $input, array $options = []): DataTester
{
$commandTester = $this->loadCommandTester();
$columns = getenv('COLUMNS');
putenv('COLUMNS=120');
$options += ['capture_stderr_separately' => true];
$this->commandTester->execute($input, $options);
$commandTester->execute($input, $options);
putenv('COLUMNS='.$columns);

return new DataTester($this->commandTester);
return new DataTester($commandTester);
}
}
13 changes: 5 additions & 8 deletions tests/Command/NullCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,19 @@

use App\Command\NullCommand;
use App\Tests\TestCase;
use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowiredInterface;
use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireService;
use Draw\Component\Tester\Application\CommandDataTester;
use Draw\Component\Tester\Application\CommandTestTrait;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;

/**
* @covers \App\Command\NullCommand
*/
class NullCommandTest extends TestCase
class NullCommandTest extends TestCase implements AutowiredInterface
{
use CommandTestTrait;

public function createCommand(): Command
{
return static::getService(NullCommand::class);
}
#[AutowireService(NullCommand::class)]
protected ?Command $command = null;

public function getCommandName(): string
{
Expand Down
10 changes: 5 additions & 5 deletions tests/Console/Command/GenerateDocumentationCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use App\Tests\FilteredCommandTestTrait;
use App\Tests\TestCase;
use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowiredInterface;
use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireService;
use Draw\Component\Console\Command\GenerateDocumentationCommand;
use Draw\Component\Tester\Application\CommandDataTester;
use Symfony\Component\Console\Command\Command;
Expand All @@ -13,16 +15,14 @@
/**
* @covers \Draw\Component\Console\Command\GenerateDocumentationCommand
*/
class GenerateDocumentationCommandTest extends TestCase
class GenerateDocumentationCommandTest extends TestCase implements AutowiredInterface
{
use FilteredCommandTestTrait;

private bool $writeFile = false;

public function createCommand(): Command
{
return static::getContainer()->get(GenerateDocumentationCommand::class);
}
#[AutowireService(GenerateDocumentationCommand::class)]
protected ?Command $command = null;

public function getCommandName(): string
{
Expand Down
Loading