Skip to content

[12.x] add prompts based expectations to PendingCommand #56260

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

Open
wants to merge 2 commits into
base: 12.x
Choose a base branch
from

Conversation

BinaryKitten
Copy link

Details

This PR augments the PendingCommand with methods targeting the direct output methods of Laravel Prompts

This follows up from the closed PR on Laravel Prompts - laravel/prompts#196

Whats New

this adds the following methods to the PendingCommand class

  • expectsPromptsInfo
  • expectsPromptsWarning
  • expectsPromptsError
  • expectsPromptsAlert
  • expectsPromptsIntro
  • expectsPromptsOutro
  • expectsPromptsTable

along with a protected shared method that all of these use to reduce repetition of code.

Example usage

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use function Laravel\Prompts\info;
use function Laravel\Prompts\alert;
use function Laravel\Prompts\error;
use function Laravel\Prompts\outro;
use function Laravel\Prompts\table;
use function Laravel\Prompts\warning;

class PromptTest extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @inheritdoc
     */
    protected $signature = 'app:prompt-test';

    /**
     * The console command description.
     *
     * @inheritdoc
     */
    protected $description = 'test prompt';

    /**
     * Execute the console command.
     */
    public function handle(): void
    {
        outro('Test Prompt - outro');

        info('Test Prompt - information');

        alert('Test Prompt - information ALERT!');

        table(
            headers: ['ABC', 'DEF', 'GHI'],
            rows: [
                ['123', '456', '789'],
                ['234', '567', '890'],
            ]
        );

        warning('Test Prompt - warning');

        error('Test Prompt - error');

        outro('Test Prompt - outro');
    }
}

for PHPUnit:

<?php

namespace Tests\Unit;

use Tests\TestCase;
use Laravel\Prompts\Testing\InteractsWithPrompts;

class MyCommandTest extends TestCase
{
    public function testCommand(): void
    {
        $this->artisan('app:prompt-test')
            ->expectsPromptIntro('Test Prompt - intro')
            ->expectsPromptTable(
                headers: ['ABC', 'DEF', 'GHI'],
                rows: [
                    ['123', '456', '789'],
                    ['234', '567', '890'],
                ]
            )
            ->assertExitCode(0);
    }
}

for Pest (MyCommandTest.php)

<?php

use function Pest\Laravel\artisan;

test('command prompts', function () {
    artisan('app:prompt-test')
        ->expectsPromptOutro('Test Prompt - outro')
        ->expectsPromptInfo('Test Prompt - information')
        ->expectsPromptTable(
            headers: ['ABC', 'DEF', 'GHI'],
            rows: [
                ['123', '456', '789'],
                ['234', '567', '890'],
            ]
        )
        ->assertExitCode(0);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant