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

Module Yii2 Laravel Package

codeception/module-yii2

Codeception Yii2 module for acceptance and functional testing. Provides helpers to bootstrap Yii2 apps, handle fixtures, navigate routes, interact with models and components, and integrate Yii2-specific assertions into your Codeception test suite.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Add the module via Composer in your Laravel project (if using Yii2 integration via yii2-laravel or similar):

    composer require --dev codeception/module-yii2
    

    For pure Laravel projects, ensure you’re using a bridge (e.g., yii2-laravel) or mock Yii2 components directly.

  2. Configure codeception.yml Add the module to your modules section under the appropriate suite (e.g., acceptance):

    modules:
      enabled:
        - Yii2:
            part: [app, tests]  # Paths to Yii2 app and test files
            entryScript: index-test.php  # Yii2 entry script (if applicable)
    
  3. First Use Case: Testing Controllers Write a basic acceptance test to verify a Yii2-style controller action:

    <?php
    use Codeception\Test\Acceptance;
    use Codeception\Module\Yii2;
    
    class SiteCept extends Acceptance
    {
        public function _before()
        {
            $I = $this;
            $I->amOnPage('/site/index'); // Yii2 route
        }
    
        public function testHomepageLoads()
        {
            $I = $this;
            $I->see('Welcome to Yii2'); // Assert content
        }
    }
    

    Run with:

    ./vendor/bin/codecept run acceptance
    

Implementation Patterns

Workflows

  1. Dependency Injection Use the module to inject Yii2 services (e.g., Yii::$app) into tests:

    $I->haveInDatabase('users', ['id' => 1, 'name' => 'Test User']);
    $I->seeRecord('users', ['name' => 'Test User']);
    
  2. Database Testing Leverage Yii2’s Db module for migrations/seeding:

    $I->runYiiCommand('migrate/up'); // Run Yii2 migrations
    $I->loadFixtures('users.yml');   // Load test fixtures
    
  3. Authentication Simulate user logins with Yii2’s auth system:

    $I->amLoggedInAs('admin@example.com', 'password123');
    $I->see('Admin Dashboard');
    
  4. Integration with Laravel If using yii2-laravel, bridge Laravel’s Auth facade to Yii2’s User model:

    // In a Laravel service provider:
    Yii::$container->set('auth', function () {
        return new \yii\web\User([
            'identityClass' => \app\models\User::class,
        ]);
    });
    

Integration Tips

  • Mocking Yii2 Components Use PHPUnit’s mocking to replace Yii2 dependencies in unit tests:

    $mockAuth = $this->getMockBuilder(Yii::$app->authManager)
        ->disableOriginalConstructor()
        ->getMock();
    Yii::$app->set('authManager', $mockAuth);
    
  • Custom Commands Extend the module to support custom Yii2 commands:

    # codeception.yml
    modules:
      config:
        Yii2:
          commands:
            - name: custom-command
              script: yii custom/command
    
  • Event Testing Listen for Yii2 events in tests:

    Yii::$app->on('user.login', function () {
        $this->see('Login event triggered');
    });
    

Gotchas and Tips

Pitfalls

  1. Path Configuration

    • Issue: part: [app, tests] paths must be relative to codeception.yml.
    • Fix: Use absolute paths if needed:
      part: [/var/www/project/app, /var/www/project/tests]
      
  2. Yii2 App Initialization

    • Issue: Tests fail if Yii2’s entryScript (e.g., index-test.php) isn’t configured.
    • Fix: Ensure the script initializes Yii2’s container:
      // index-test.php
      require __DIR__ . '/../vendor/autoload.php';
      require __DIR__ . '/../vendor/yiisoft/yii2/Yii.php';
      $config = require __DIR__ . '/../config/test.php';
      new yii\web\Application($config);
      
  3. Laravel-Yii2 Conflicts

    • Issue: Laravel’s service container may override Yii2’s.
    • Fix: Explicitly set Yii2’s container as the primary:
      Yii::$container->set('app', function () {
          return new yii\web\Application(\Yii::createObject(\app\config\test.php));
      });
      
  4. Database Transactions

    • Issue: Yii2’s Db module may not rollback transactions in Codeception.
    • Fix: Enable transactions in codeception.yml:
      modules:
        config:
          Db:
            dsn: 'mysql:host=localhost;dbname=test_db'
            cleanup: true
            populate: true
      

Debugging Tips

  • Enable Debug Mode Set Yii2’s debug mode in tests:

    Yii::$app->set('debug', true);
    Yii::$app->set('logTarget', ['class' => 'yii\log\FileTarget', 'levels' => ['error', 'warning']]);
    
  • Log Output Redirect Yii2 logs to Codeception’s output:

    $I->debug('Yii2 Logs:', Yii::$app->getLogger()->getLogs());
    
  • Isolated Testing Use Yii::$app->set('request', $this->makeMockRequest()) to mock HTTP requests in unit tests.

Extension Points

  1. Custom Assertions Extend the module to add Yii2-specific assertions:

    // In a custom module
    public function seeYiiFlashMessage($message)
    {
        $this->see($message, 'div.alert');
    }
    
  2. Hooks Override Yii2’s lifecycle hooks in tests:

    Yii::$app->on('app.init', function () {
        // Modify app config before tests run
    });
    
  3. Parallel Testing Configure multiple Yii2 environments in codeception.yml for parallel suites:

    suites:
      acceptance:
        modules:
          config:
            Yii2:
              entryScript: index-test.php
              config: config/test.php
      api:
        modules:
          config:
            Yii2:
              entryScript: index-api.php
              config: config/api.php
    
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
milesj/emojibase
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