Created
February 3, 2018 16:04
-
-
Save PeterDKC/e7a5476cf9723f90f9a3584db46d7862 to your computer and use it in GitHub Desktop.
Example Command Tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Tests\Command; | |
use PeterDKC\Sprocket\Tests\TestCase; | |
use Illuminate\Support\Facades\Artisan; | |
use Symfony\Component\Console\Tester\CommandTester; | |
class ExampleTest extends TestCase | |
{ | |
/** @test */ | |
public function testsSimpleExampleCommand() | |
{ | |
Artisan::call('example:simple'); | |
$this->assertEquals( | |
"bob\n", | |
Artisan::output() | |
); | |
} | |
/** @test */ | |
public function testsArgumentExampleCommand() | |
{ | |
Artisan::call('example:argument', [ | |
'--name' => 'bob' | |
]); | |
$this->assertEquals( | |
"bob\n", | |
Artisan::output() | |
); | |
} | |
/** @test */ | |
public function testsInputExampleCommand() | |
{ | |
// setup the application and add the command | |
$application = app() | |
->makeWith(\Illuminate\Console\Application::class, [ | |
'version' => $this->app::VERSION | |
]); | |
$application->add( | |
$application->resolve( | |
'PeterDKC\Sprocket\Console\Commands\ExampleInput' | |
) | |
); | |
// create a CommandTester based on the command | |
$command = $application->find('example:input'); | |
$tester = new CommandTester($command); | |
$tester->setInputs(['bob']); | |
$tester->execute(['command' => $command->getName()]); | |
// set the expected output and assert on it | |
$expected = "\n" . | |
" What's your name boss?:\n" . | |
" > \n" . | |
"bob\n"; | |
$this->assertEquals( | |
$expected, | |
$tester->getDisplay() | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment