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

Wp Cli Tests Laravel Package

wp-cli/wp-cli-tests

WP-CLI testing framework for WP-CLI packages. Adds Composer scripts and tooling to run PHPUnit, Behat, PHPCS, and linting with optional cross-platform Behat config and custom PHPCS rulesets for consistent CI-ready testing.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup for Laravel Integration

To leverage wp-cli/wp-cli-tests in a Laravel-based WordPress plugin/theme project:

  1. Install the package (add to composer.json under require-dev):

    composer require --dev wp-cli/wp-cli-tests
    
  2. Configure Composer scripts (add to composer.json):

    "scripts": {
        "test": [
            "@phpunit",
            "@behat"
        ],
        "phpunit": "vendor/bin/phpunit",
        "behat": "vendor/bin/behat --config vendor/wp-cli/wp-cli-tests/behat.yml"
    }
    
  3. Set up a Laravel-specific Behat config (behat.yml):

    default:
      suites:
        default:
          contexts:
            - WP_CLI\Tests\Context\FeatureContext
            - App\Tests\Behat\WordPressContext  # Custom Laravel context
          paths:
            - tests/behat/features
    
  4. Run tests:

    composer test
    

First Use Case: Testing a Laravel-WP CLI Command

Create a Behat feature file (tests/behat/features/cli_command.feature):

Feature: Test Laravel-WP CLI command
  Scenario: Execute a custom WP-CLI command
    Given I run the WP-CLI command "wp my-laravel-command"
    Then the output should contain "Success"

Implementation Patterns

1. Hybrid Laravel-WP Testing Workflow

graph TD
    A[Laravel Test] --> B[WP-CLI Test Harness]
    B --> C[Behat Feature Files]
    C --> D[PHPUnit Unit Tests]
    D --> E[Laravel Service Container]
    E --> F[WordPress Core]

Key Integration Points:

  • Service Provider: Extend WP_CLI\Tests\Bootstrap in a Laravel service provider:

    public function boot()
    {
        $this->app->singleton('wp-cli.tests', function () {
            return new WP_CLI\Tests\Bootstrap(
                base_path('vendor/wp-cli/wp-cli-tests')
            );
        });
    }
    
  • Custom Contexts: Create Laravel-specific Behat contexts:

    namespace App\Tests\Behat;
    
    use Behat\Behat\Context\Context;
    use WP_CLI\Tests\Context\FeatureContext;
    
    class WordPressContext extends FeatureContext implements Context
    {
        public function __construct()
        {
            parent::__construct();
            $this->useWordPressCore();
        }
    
        public function useWordPressCore()
        {
            // Laravel-specific WP core integration
        }
    }
    

2. Database Testing Pattern

// In a Behat test
public function setupDatabase()
{
    $this->getDatabase()->createDatabase('wp_cli_test');
    $this->getDatabase()->createUser('wp_cli_test', 'password1');
    $this->getDatabase()->grantAllPrivileges('wp_cli_test', 'wp_cli_test');

    // Laravel-specific DB setup
    config(['database.connections.mysql.database' => 'wp_cli_test']);
}

3. Command Testing Pattern

// tests/Unit/Commands/MyCommandTest.php
public function testCommandExecution()
{
    $command = new MyCommand();
    $command->setLaravel($this->app);

    $output = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')
        ->getMock();

    $command->handle($output, ['option' => 'value']);

    $this->assertStringContainsString('Expected output', $output->getContent());
}

Gotchas and Tips

Laravel-Specific Pitfalls

  1. Namespace Conflicts:

    • Laravel's Artisan vs WP-CLI's wp command naming
    • Fix: Use fully qualified namespaces in Behat steps:
      Given I run the WP-CLI command "wp my-plugin:command"
      
  2. Service Container Initialization:

    • WP-CLI tests may not automatically load Laravel services
    • Solution: Extend the test bootstrap:
      $bootstrap->setLaravelApp($this->app);
      
  3. Database Connection Issues:

    • Laravel's .env may override test DB settings
    • Workaround: Set environment variables before tests:
      WP_CLI_TEST_DBNAME=laravel_test DB_CONNECTION=mysql composer behat
      

Debugging Tips

  1. Behat Verbose Mode:

    composer behat -- -vvv
    
  2. Laravel Log Integration:

    // In Behat context
    public function __construct()
    {
        $this->logger = $this->getLaravelApp()->make('log');
    }
    
    public function logMessage($message)
    {
        $this->logger->info($message);
    }
    
  3. CI-Specific Issues:

    • Windows Paths: Use forward slashes consistently
    • Memory Limits: Increase PHP memory in CI config:
      env:
          MEMORY_LIMIT: -1
      

Extension Points

  1. Custom Behat Steps:

    // app/Tests/Behat/StepDefinition/LaravelSteps.php
    use Behat\Behat\Tester\Exception\PendingException;
    
    /**
     * @Given /^I am authenticated as user "([^"]+)"$/
     */
    public function iAmAuthenticatedAsUser($username)
    {
        $this->getLaravelApp()->make('auth')->loginUsingId(1);
    }
    
  2. PHPUnit Extensions:

    // phpunit.xml
    <extensions>
        <extension class="WP_CLI\Tests\PHPUnit\WP_CLI_TestCase"/>
    </extensions>
    
  3. Custom Test Helpers:

    // tests/TestHelper.php
    if (!function_exists('createTestUser')) {
        function createTestUser($role = 'administrator')
        {
            $user = wp_create_user('testuser', 'password123');
            $role = get_role($role);
            $role->add_user($user);
            return $user;
        }
    }
    

Performance Optimization

  1. Parallel Test Execution:

    composer test -- --parallel
    
  2. Database Caching:

    # behat.yml
    default:
      extensions:
        Behat\MinkExtension:
          base_url: 'http://localhost'
          goutte: ~
        WP_CLI\Tests\Behat\Extension:
          cache: true
    
  3. Selective Test Running:

    composer behat -- features/cli-command.feature
    
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.
besmartand-pro/php-quality-config
sentix/ai-chatbot
codifyo/ts-generator-bundle
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor