Skip to content

Add PendingCommand testing macros trait #196

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

Closed

Conversation

BinaryKitten
Copy link
Contributor

Description

This new trait adds output testing against the PendingCommand from the core framework.

Adding this trait to your userland tests (Pest or PHPUnit) will allow calls through to the test methods on the PendingCommand (see examples/usage below)

Notes

So far this only works for regular output data and the other downside is that it can only check for a full table match

Usage

with this example command:

<?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
{
    use InteractsWithPrompts;

    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 Tests\Traits\InteractsWithPrompts;

use function Pest\Laravel\artisan;

uses()->use(InteractsWithPrompts::class);

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);
});

@BinaryKitten BinaryKitten force-pushed the feature/user-testing-macros branch from deba693 to 8d2408f Compare July 7, 2025 14:21
@taylorotwell
Copy link
Member

I kinda think it would be better to just bake this straight into the framework's pending command class so you get intellisense on it in your IDE vs. this macro approach.

Thanks!

@BinaryKitten
Copy link
Contributor Author

I thought about it in the framework but that then put a hard tie on always being used, which I didn't feel was best approach for a new contribution.

I'd be happy to see these functions in the framework proper, would it be OK to pr that? Or should I leave it for the team?

@macocci7
Copy link
Contributor

macocci7 commented Jul 9, 2025

I'd be happy to see these functions in the framework proper, would it be OK to pr that? Or should I leave it for the team?

@BinaryKitten You'd better open a new pr by yourself. Laravel team members will welcome it, I think.👍

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.

3 participants