Files
barryvdh_laravel-ide-helper/tests/TestCase.php
T
Markus Podar ac6fbffac3 tests: ensure output mocking is disabled so we can actually catch the output (#1018)
Discovered via https://github.com/barryvdh/laravel-ide-helper/pull/1017#issuecomment-679318764

This only happens if you run an artisan command _before_ `runCommand`,
but since every of these tests performs migrations, using artisan,
the mocked output instance is left behind and also affected us.

The call added in `runCommand` explicitly unregisters
`\Illuminate\Console\OutputStyle` and thus also throwing away possibly
mocked versions.
2020-08-25 06:29:44 +02:00

56 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
namespace Barryvdh\LaravelIdeHelper\Tests;
use Illuminate\Console\Command;
use Illuminate\Console\OutputStyle;
use Orchestra\Testbench\TestCase as BaseTestCase;
use Spatie\Snapshots\MatchesSnapshots;
use Symfony\Component\Console\Tester\CommandTester;
abstract class TestCase extends BaseTestCase
{
use MatchesSnapshots;
/**
* The `CommandTester` is directly returned, use methods like
* `->getDisplay()` or `->getStatusCode()` on it.
*
* @param Command $command
* @param array $arguments The command line arguments, array of key=>value
* Examples:
* - named arguments: ['model' => 'Post']
* - boolean flags: ['--all' => true]
* - arguments with values: ['--arg' => 'value']
* @param array $interactiveInput Interactive responses to the command
* I.e. anything the command `->ask()` or `->confirm()`, etc.
* @return CommandTester
*/
protected function runCommand(Command $command, array $arguments = [], array $interactiveInput = []): CommandTester
{
// TODO: once Laravel 5.5 is dropped, call `$this->withoutMockingConsoleOutput()` instead
$this->app->offsetUnset(OutputStyle::class);
$command->setLaravel($this->app);
$tester = new CommandTester($command);
$tester->setInputs($interactiveInput);
$tester->execute($arguments);
return $tester;
}
protected function assertMatchesPhpSnapshot(?string $actualContent)
{
$this->assertMatchesSnapshot($actualContent, new SnapshotPhpDriver());
}
protected function assertMatchesTxtSnapshot(?string $actualContent)
{
$this->assertMatchesSnapshot($actualContent, new SnapshotTxtDriver());
}
}