Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Console Test Laravel Package

zenstruck/console-test

Test your Symfony Console commands with ease. zenstruck/console-test provides a lightweight harness to run commands, feed input, capture output, and assert exit codes and messages—ideal for fast, reliable unit/integration tests without booting full apps.

View on GitHub
Deep Wiki
Context7

Getting Started

Install via Composer: composer require --dev zenstruck/console-test. No extra service provider setup needed—it auto-registers with Laravel/Symfony. Start by writing a PHPUnit test that extends Zenstruck\Console\Test\CommandTestCase (Laravel) or uses the trait Zenstruck\Console\Test\WithConsoleCommands. Your first test should run a real command (e.g., php artisan your:command) using $this->run('your:command', ['arg' => 'value']), then assert exit code (assertSuccess()) and output (assertOutputContains('Success!')). This is your entry point to safe, reliable CLI testing.

Implementation Patterns

  • Basic Command Testing: Wrap $this->run('cmd:name', $args, $options) with chained assertions for output, exit code, and error output. Use assertExitCode(0) for success, assertOutputContains('foo') for partial matches, or assertOutputMatches('/pattern/') for regex.
  • Interactive Inputs: Simulate user input with withInput(['yes', 'john']) before run(), or use withInputs([...]) for multi-step prompts.
  • Reusability: Create test helper methods like assertMigrateRunsSuccessfully() to reduce duplication across test suites.
  • Isolation: Always use run() inside each test—commands execute in full isolation, with a fresh container each time (critical for state-sensitive commands).
  • Debugging: Use dumpOutput(), dumpErrorOutput(), or getOutput()/getErrorOutput() to inspect raw output when assertions fail.

Gotchas and Tips

  • Output buffering: Output is captured at the command level—don’t expect dump() calls from nested services to appear in CLI output. Use Laravel’s dd()/dump() in tests to debug.
  • Question helper mocking: Interactive questions (e.g., ask(), confirm()) require withInput() before run(). If questions appear skipped, verify interactive mode isn’t enabled—run() defaults to non-interactive; use withInteractive(true) only when testing full interactive flows.
  • Laravel config caching: If config changes aren’t reflected in tests, run php artisan config:clear—cached configs may override environment variables during command execution.
  • Custom command registration: If a command isn’t found, ensure it’s registered in your app’s app/Console/Kernel.php and not conditionally resolved via service location (which may break auto-discovery in tests).
  • Advanced introspection: Access the underlying CommandTester via getCommandTester() after run() for low-level control (e.g., inspecting output formatting, raw input parameters), but prefer built-in assertions for readability.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge