> ## Documentation Index
> Fetch the complete documentation index at: https://kawax.biz/llms.txt
> Use this file to discover all available pages before exploring further.

# Consoletests

> Leer hoe je Artisan-commando's test in Laravel (verwachtingen voor invoer en uitvoer, verificatie van exitcodes, verificatie van console-events).

# Consoletests

In Laravel kun je beknopt tests schrijven voor Artisan-commando's, inclusief invoer en uitvoer.

> [!NOTE]
>
> Deze pagina is afgestemd op de nieuwste consoletest-API van Laravel en behandelt ook `expectsSearch`, waarmee je zoekinvoer van Laravel Prompts verifieert.

## Aan de slag

Voer een commando uit met de `artisan`-methode en verifieer verwachtingen door ze te chainen.

> [tab] Pest
>
> ```php theme={null}
> test('question commando', function () {
>     // Verifieer gebruikersinvoer en uitvoer in volgorde
>     $this->artisan('question')
>         ->expectsQuestion('What is your name?', 'Taylor Otwell')
>         ->expectsQuestion('Which language do you prefer?', 'PHP')
>         ->expectsOutput('Your name is Taylor Otwell and you prefer PHP.')
>         ->doesntExpectOutput('Your name is Taylor Otwell and you prefer Ruby.')
>         ->assertExitCode(0);
> });
> ```

> [tab] PHPUnit
>
> ```php theme={null}
> public function test_question_command(): void
> {
>     // Controleer de interactieve flow van het commando
>     $this->artisan('question')
>         ->expectsQuestion('What is your name?', 'Taylor Otwell')
>         ->expectsQuestion('Which language do you prefer?', 'PHP')
>         ->expectsOutput('Your name is Taylor Otwell and you prefer PHP.')
>         ->doesntExpectOutput('Your name is Taylor Otwell and you prefer Ruby.')
>         ->assertExitCode(0);
> }
> ```

```mermaid theme={null}
flowchart TD
    A[Commando uitvoeren met artisan] --> B[Invoerverwachtingen definiëren]
    B --> C[Uitvoer of tabel verifiëren]
    C --> D[Exitstatus verifiëren]
```

## Assertions voor succes / falen

Je kunt de exitstatus verifiëren om te bepalen of een commando is geslaagd of mislukt.

```php theme={null}
$this->artisan('inspire')->assertExitCode(0);
$this->artisan('inspire')->assertSuccessful();
$this->artisan('inspire')->assertFailed();
```

## Verwachtingen voor invoer / uitvoer

### Invoerverwachtingen

Je kunt gebruikersinteracties mocken voor vraaginvoer en zoekinvoer.

```php theme={null}
// Mock zowel vraaginvoer als zoekinvoer
$this->artisan('example')
    ->expectsQuestion('What is your name?', 'Taylor Otwell')
    ->expectsSearch('What is your name?', search: 'Tay', answers: [
        'Taylor Otwell',
        'Taylor Swift',
        'Darian Taylor',
    ], answer: 'Taylor Otwell')
    ->assertExitCode(0);
```

### Uitvoerverwachtingen

Je kunt exacte overeenkomsten, gedeeltelijke overeenkomsten en tabelweergaven van de uitvoer verifiëren.

```php theme={null}
// Controleer dat alleen de verwachte uitvoer wordt getoond
$this->artisan('users:all')
    ->expectsOutput('The expected output')
    ->doesntExpectOutput('Unexpected output')
    ->expectsOutputToContain('expected')
    ->expectsTable([
        'ID',
        'Email',
    ], [
        [1, 'taylor@example.com'],
        [2, 'abigail@example.com'],
    ])
    ->assertExitCode(0);
```

## Bevestigingsverwachtingen

Voor Yes / No-bevestigingsprompts gebruik je `expectsConfirmation`.

```php theme={null}
$this->artisan('module:import')
    ->expectsConfirmation('Do you really wish to run this command?', 'no')
    ->assertExitCode(1);
```

## Console-events

Standaard worden `CommandStarting` / `CommandFinished` niet afgevuurd tijdens het uitvoeren van tests.

Voeg `WithConsoleEvents` toe aan testklassen die eventverificatie nodig hebben.

```php theme={null}
<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\WithConsoleEvents;
use Tests\TestCase;

class ConsoleEventTest extends TestCase
{
    use WithConsoleEvents;
}
```

> [!TIP]
>
> Beperk het gebruik van `WithConsoleEvents` tot de tests die het nodig hebben, zodat je de normale testsnelheid behoudt.


## Related topics

- [Artisan-commands van een package registreren en afhankelijkheden resolven](https://kawax.biz/nl/advanced/package-commands.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.