Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jessarcher committed Jul 28, 2023
1 parent f97a714 commit f149434
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Console/Concerns/ConfiguresPrompts.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected function configurePrompts(InputInterface $input)
{
Prompt::setOutput($this->output);

Prompt::fallbackWhen(! $input->isInteractive() || windows_os() || app()->runningUnitTests());
Prompt::fallbackWhen(! $input->isInteractive() || windows_os() || $this->laravel->runningUnitTests());

TextPrompt::fallbackUsing(fn (TextPrompt $prompt) => $this->promptUntilValid(
fn () => $this->components->ask($prompt->label, $prompt->default ?: null) ?? '',
Expand Down
1 change: 1 addition & 0 deletions tests/Console/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function handle()

$command->call(Command::class);
});
$application->shouldReceive('runningUnitTests')->andReturn(true);

$command->run($input, $output);
}
Expand Down
21 changes: 19 additions & 2 deletions tests/Console/ConsoleApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,37 @@ public function testCallFullyStringCommandLine()
$this->assertSame($outputOfCallingArrayInput, $outputOfCallingStringInput);
}

public function testCommandInputPromptingWorksCorrectly()
public function testCommandInputPromptsWhenRequiredArgumentIsMissing()
{
$app = new Application(
$app = new \Illuminate\Foundation\Application(__DIR__),
$events = m::mock(Dispatcher::class, ['dispatch' => null, 'fire' => null]),
'testing'
);

$app->addCommands([new FakeCommandWithInputPrompting(false)]);
$app->addCommands([$command = new FakeCommandWithInputPrompting()]);

$statusCode = $app->call('fake-command-for-testing');

$this->assertTrue($command->prompted);
$this->assertSame(0, $statusCode);
}

public function testCommandInputDoesntPromptWhenRequiredArgumentIsPassed()
{
$app = new Application(
$app = new \Illuminate\Foundation\Application(__DIR__),
$events = m::mock(Dispatcher::class, ['dispatch' => null, 'fire' => null]),
'testing'
);

$app->addCommands([$command = new FakeCommandWithInputPrompting()]);

$statusCode = $app->call('fake-command-for-testing', [
'name' => 'foo',
]);

$this->assertFalse($command->prompted);
$this->assertSame(0, $statusCode);
}

Expand Down
31 changes: 15 additions & 16 deletions tests/Console/Fixtures/FakeCommandWithInputPrompting.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,33 @@

namespace Illuminate\Tests\Console\Fixtures;

use BadMethodCallException;
use Illuminate\Console\Command;
use Illuminate\Contracts\Console\PromptsForMissingInput;
use Laravel\Prompts\Prompt;
use Laravel\Prompts\TextPrompt;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class FakeCommandWithInputPrompting extends Command implements PromptsForMissingInput
{
use \Illuminate\Console\Concerns\PromptsForMissingInput {
askPersistently as traitAskPersistently;
}

protected $signature = 'fake-command-for-testing {name : An argument}';

public function __construct(private $expectToRequestInput = true)
public $prompted = false;

protected function interact(InputInterface $input, OutputInterface $output)
{
parent::__construct();
Prompt::fallbackWhen(true);
TextPrompt::fallbackUsing(function () {
$this->prompted = true;

return 'foo';
});

parent::interact($input, $output);
}

public function handle(): int
{
return self::SUCCESS;
}

private function askPersistently($question)
{
if (! $this->expectToRequestInput) {
throw new BadMethodCallException('No prompts for input were expected, but a question was asked.');
}

return $this->traitAskPersistently($question);
}
}
2 changes: 1 addition & 1 deletion tests/Database/DatabaseMigratorIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected function setUp(): void
$output = m::mock(OutputStyle::class);
$output->shouldReceive('write');
$output->shouldReceive('writeln');
$output->shouldReceive('newLineWritten');
$output->shouldReceive('newLinesWritten');

$this->migrator->setOutput($output);

Expand Down
10 changes: 5 additions & 5 deletions tests/Database/PruneCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Illuminate\Tests\Database;

use Closure;
use Illuminate\Container\Container;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Console\PruneCommand;
Expand All @@ -15,6 +14,7 @@
use Illuminate\Database\Events\ModelPruningStarting;
use Illuminate\Database\Events\ModelsPruned;
use Illuminate\Events\Dispatcher;
use Illuminate\Foundation\Application;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArrayInput;
Expand All @@ -26,7 +26,7 @@ protected function setUp(): void
{
parent::setUp();

Container::setInstance($container = new Container);
Application::setInstance($container = new Application);

$container->singleton(DispatcherContract::class, function () {
return new Dispatcher();
Expand Down Expand Up @@ -212,7 +212,7 @@ public function testTheCommandDispatchesEvents()
});
$dispatcher->shouldReceive('forget')->once()->with(ModelsPruned::class);

Container::getInstance()->singleton(DispatcherContract::class, fn () => $dispatcher);
Application::getInstance()->singleton(DispatcherContract::class, fn () => $dispatcher);

$this->artisan(['--model' => PrunableTestModelWithPrunableRecords::class]);
}
Expand All @@ -223,7 +223,7 @@ protected function artisan($arguments)
$output = new BufferedOutput;

tap(new PruneCommand())
->setLaravel(Container::getInstance())
->setLaravel(Application::getInstance())
->run($input, $output);

return $output;
Expand All @@ -233,7 +233,7 @@ protected function tearDown(): void
{
parent::tearDown();

Container::setInstance(null);
Application::setInstance(null);

m::close();
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Database/SeedCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function testHandle()
$container = m::mock(Container::class);
$container->shouldReceive('call');
$container->shouldReceive('environment')->once()->andReturn('testing');
$container->shouldReceive('runningUnitTests')->once()->andReturn('true');
$container->shouldReceive('make')->with('DatabaseSeeder')->andReturn($seeder);
$container->shouldReceive('make')->with(OutputStyle::class, m::any())->andReturn(
$outputStyle
Expand Down Expand Up @@ -79,6 +80,7 @@ public function testWithoutModelEvents()
$container = m::mock(Container::class);
$container->shouldReceive('call');
$container->shouldReceive('environment')->once()->andReturn('testing');
$container->shouldReceive('runningUnitTests')->once()->andReturn('true');
$container->shouldReceive('make')->with(UserWithoutModelEventsSeeder::class)->andReturn($seeder);
$container->shouldReceive('make')->with(OutputStyle::class, m::any())->andReturn(
$outputStyle
Expand Down
4 changes: 2 additions & 2 deletions tests/Foundation/FoundationDocsCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ public function testItCanOpenTheLaravelDocumentation(): void
{
$this->artisan('docs')
->expectsQuestion('Which page would you like to open?', '')
->expectsOutputToContain('Opening the docs to: https://laravel.com/docs/8.x')
->expectsOutputToContain('Opening the docs to: https://laravel.com/docs/8.x/installation')
->assertSuccessful();

$this->assertSame($this->openedUrl, 'https://laravel.com/docs/8.x');
$this->assertSame($this->openedUrl, 'https://laravel.com/docs/8.x/installation');
}

public function testItCanSpecifyAutocompleteInOriginalCasing(): void
Expand Down
5 changes: 3 additions & 2 deletions tests/Queue/PruneBatchesCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Bus\BatchRepository;
use Illuminate\Bus\DatabaseBatchRepository;
use Illuminate\Container\Container;
use Illuminate\Foundation\Application;
use Illuminate\Queue\Console\PruneBatchesCommand;
use Mockery as m;
use PHPUnit\Framework\TestCase;
Expand All @@ -20,7 +21,7 @@ protected function tearDown(): void

public function testAllowPruningAllUnfinishedBatches()
{
$container = new Container;
$container = new Application;
$container->instance(BatchRepository::class, $repo = m::spy(DatabaseBatchRepository::class));

$command = new PruneBatchesCommand;
Expand All @@ -33,7 +34,7 @@ public function testAllowPruningAllUnfinishedBatches()

public function testAllowPruningAllCancelledBatches()
{
$container = new Container;
$container = new Application;
$container->instance(BatchRepository::class, $repo = m::spy(DatabaseBatchRepository::class));

$command = new PruneBatchesCommand;
Expand Down

0 comments on commit f149434

Please sign in to comment.