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

Improve DX for testing commands w/o framework #5

Merged
merged 2 commits into from
Oct 13, 2021
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
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.yml]
indent_size = 2
25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use Zenstruck\Console\Test\InteractsWithConsole;
class CreateUserCommandTest extends KernelTestCase
{
use InteractsWithConsole;

public function test_can_create_user(): void
{
$this->executeConsoleCommand('create:user kbond --admin --role=ROLE_EMPLOYEE --role=ROLE_MANAGER')
Expand Down Expand Up @@ -61,7 +61,7 @@ class CreateUserCommandTest extends KernelTestCase
->assertSuccessful()
->assertOutputContains('Creating regular user "kbond"')
;

// advanced testing interactive commands
$this->consoleCommand(CreateUserCommand::class)
->addInput('kbond')
Expand All @@ -70,13 +70,13 @@ class CreateUserCommandTest extends KernelTestCase
->assertSuccessful()
->assertOutputContains('Creating regular user "kbond"')
;

// access result
$result = $this->executeConsoleCommand('create:user');

$result->statusCode();
$result->output();
$result->errorOutput();
$result->output();
$result->errorOutput();
}
}
```
Expand All @@ -94,6 +94,15 @@ class CreateUserCommandTest extends TestCase
{
public function test_can_create_user(): void
{
TestCommand::for(new CreateUserCommand(/** args... */))
->execute('kbond --admin --role=ROLE_EMPLOYEE --role=ROLE_MANAGER')
->assertSuccessful() // command exit code is 0
->assertOutputContains('Creating admin user "kbond"')
->assertOutputContains('with roles: ROLE_EMPLOYEE, ROLE_MANAGER')
->assertOutputNotContains('regular user')
;

// advanced usage
TestCommand::for(new CreateUserCommand(/** args... */))
->splitOutputStreams() // by default stdout/stderr are combined, this options splits them
->addArgument('kbond')
Expand All @@ -111,7 +120,7 @@ class CreateUserCommandTest extends TestCase
->dump() // dump() the status code/outputs and continue
->dd() // dd() the status code/outputs
;

// testing interactive commands
TestCommand::for(new CreateUserCommand(/** args... */))
->addInput('kbond')
Expand All @@ -125,8 +134,8 @@ class CreateUserCommandTest extends TestCase
$result = TestCommand::for(new CreateUserCommand(/** args... */))->execute();

$result->statusCode();
$result->output();
$result->errorOutput();
$result->output();
$result->errorOutput();
}
}
```
Expand Down
6 changes: 4 additions & 2 deletions src/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,11 @@ public function withInput(array $inputs): self
return $this;
}

public function execute(): CommandResult
public function execute(?string $cli = null): CommandResult
{
$tester = new CommandTester($this->command, new StringInput($this->cli));
$cli = $cli ? \sprintf('%s %s', $this->cli, $cli) : $this->cli;

$tester = new CommandTester($this->command, new StringInput($cli));
$tester->setInputs($this->inputs);

return $tester->execute($this->splitOutputStreams);
Expand Down
17 changes: 17 additions & 0 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,23 @@ public function command_with_builder_arguments_and_options(): void
;
}

/**
* @test
*/
public function can_pass_cli_to_execute(): void
{
$this->consoleCommand('fixture:command')
->execute('value --opt1 --opt2=v1 --opt3=v2 --opt3=v3')
->assertSuccessful()
->assertOutputContains('Executing command')
->assertOutputContains('arg1 value: value')
->assertOutputContains('opt1 option set')
->assertOutputContains('opt2 value: v1')
->assertOutputContains('opt3 value: v2')
->assertOutputContains('opt3 value: v3')
;
}

/**
* @test
*/
Expand Down
39 changes: 39 additions & 0 deletions tests/UnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,45 @@ public function command_with_arguments_and_options(): void
;
}

/**
* @test
*/
public function can_pass_raw_cli_to_execute(): void
{
TestCommand::for(new FixtureCommand())
->execute('value --opt1 --opt2=v1 --opt3=v2 --opt3=v3 --opt3=v4')
->assertSuccessful()
->assertOutputContains('Executing command')
->assertOutputContains('arg1 value: value')
->assertOutputContains('opt1 option set')
->assertOutputContains('opt2 value: v1')
->assertOutputContains('opt3 value: v2')
->assertOutputContains('opt3 value: v3')
->assertOutputContains('opt3 value: v4')
;
}

/**
* @test
*/
public function can_mix_explicit_arguments_with_execute_cli(): void
{
TestCommand::for(new FixtureCommand())
->addArgument('value')
->addOption('opt1')
->addOption('--opt2', 'v1')
->execute('--opt3=v2 --opt3=v3 --opt3=v4')
->assertSuccessful()
->assertOutputContains('Executing command')
->assertOutputContains('arg1 value: value')
->assertOutputContains('opt1 option set')
->assertOutputContains('opt2 value: v1')
->assertOutputContains('opt3 value: v2')
->assertOutputContains('opt3 value: v3')
->assertOutputContains('opt3 value: v4')
;
}

/**
* @test
*/
Expand Down