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

Yii2 Codeception Laravel Package

yiisoft/yii2-codeception

Yii2 integration for Codeception: run functional, acceptance, and unit tests with Yii2 bootstrapping, fixtures, and helper classes. Provides Codeception modules and configuration support to test Yii2 apps and components effectively.

Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Add the package via Composer (though deprecated, still functional for legacy projects):

    composer require --dev yiisoft/yii2-codeception
    

    Ensure yiisoft/yii2 is installed as a base dependency.

  2. Configuration Update codeception.yml to include Yii2-specific modules:

    actor: Tester
    modules:
      enabled:
        - Yii2:
            configFile: 'config/web.php'
            entryScript: 'index.php'
    
  3. First Test Case Create a functional test in tests/acceptance/ (e.g., LoginCest.php):

    <?php
    use yii\codeception\Module;
    
    class LoginCest {
        public function tryToLogin(Module $I) {
            $I->amOnPage('/site/login');
            $I->fillField('#username', 'admin');
            $I->fillField('#password', 'secret');
            $I->click('#login-button');
            $I->see('Welcome');
        }
    }
    

    Run tests:

    ./vendor/bin/codecept run acceptance
    

Implementation Patterns

Common Workflows

  1. Database Testing Use Db module to seed/test data:

    $I->haveInDatabase('user', ['username' => 'admin', 'auth_key' => 'test123']);
    $I->seeRecord('user', ['username' => 'admin']);
    
  2. Authentication Leverage Yii’s auth manager:

    $I->amLoggedIn(['id' => 1, 'username' => 'admin']);
    $I->see('Logout');
    
  3. URL Generation Use Yii’s Url helper:

    $I->amOnPage(Yii::$app->urlManager->createUrl(['site/about']));
    
  4. Event Testing Mock events in tests:

    Yii::$app->on('user.login', function() {
        // Assertion logic
    });
    

Integration Tips

  • Laravel-Like Patterns Mimic Laravel’s Http tests by combining with Codeception\Module\Yii2:

    $I->sendGET('/api/users', ['X-CSRF-Token' => $token]);
    $I->seeResponseCodeIs(200);
    
  • Fixtures Use Db module for bulk inserts:

    $I->haveInDatabase('user', [
        ['username' => 'user1', 'email' => 'user1@example.com'],
        ['username' => 'user2', 'email' => 'user2@example.com'],
    ]);
    
  • Mocking Services Override Yii components in tests:

    Yii::$container->set('yii\web\User', function() {
        return $this->make(MockUser::class);
    });
    

Gotchas and Tips

Pitfalls

  1. Deprecation Warnings Ignore yiisoft/yii2-codeception deprecation notices; focus on codeception/yii2 (if available) for new projects.

  2. Configuration Overrides Ensure configFile in codeception.yml points to the correct Yii config (e.g., web.php for frontend, console.php for CLI tests).

  3. Session Handling Yii2’s session may conflict with Codeception’s. Reset sessions explicitly:

    $I->resetSession();
    
  4. Asset Publishing If testing assets (e.g., JS/CSS), publish them first:

    Yii::$app->assetManager->publish(Yii::getAlias('@app/assets'));
    

Debugging Tips

  • Enable Debug Mode Set YII_DEBUG to true in config/web.php for verbose errors:

    'components' => [
        'request' => ['enableCsrfValidation' => false], // Temporarily disable for testing
    ],
    
  • Log Output Redirect Yii logs to Codeception’s output:

    Yii::debug('Test message', 'test');
    
  • Browser Testing Use Codeception\Module\WebDriver for Selenium integration:

    modules:
      enabled:
        - WebDriver:
            url: 'http://localhost'
            browser: 'chrome'
        - Yii2
    

Extension Points

  1. Custom Modules Extend yii\codeception\Module for project-specific logic:

    class CustomYii2Module extends \yii\codeception\Module {
        public function customAction() {
            // Custom test logic
        }
    }
    
  2. Hooks Use Yii events to trigger test actions:

    Yii::$app->on('beforeTest', function() {
        // Pre-test setup
    });
    
  3. Legacy Code For pre-Yii2.0 projects, mock Yii::$app:

    Yii::$app = $this->createMock(\yii\base\Application::class);
    
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4