定制 sil-org/yii2-codeception 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sil-org/yii2-codeception

最新稳定版本:1.2.1

Composer 安装命令:

composer require sil-org/yii2-codeception

包简介

Patches to implement nice features of yii2-codeception since it was abandoned

README 文档

README

Patches to implement nice features of yii2-codeception since it was abandoned

What / Why

We've been developing with Yii2 for a couple years now and have several projects using the yiisoft/yii2-codeception library for unit tests.
This library has been deprecated in favor of using a Codeception module.

Unfortunately this module requires quite a bit of refactoring to run tests in a simlar way as yii2-codeception allowed.

After a few hours of tracing and debugging we figured out that two main things had changed that we depended on and so we decided to create a standalone repo to bring in the needed features.

Loading fixtures into database

The first thing we found was that after reconfiguring Codeception to use the Yii2 module, to look like:

# Codeception Test Suite Configuration

# suite for unit (internal) tests.
class_name: UnitTester
modules:
    enabled:
      - Asserts
      - UnitHelper
      - Yii2:
          configFile: 'tests/codeception/config/unit.php'
          cleanup: true

Running tests it was unable to load the fixtures due to foreign key constraints. We created a new base class for our active record classes that beforeLoad and afterLoad it will disable and enable foreign key constraints respectively.

This new base class is Sil\yii\test\ActiveFixture. To use, simply update your fixture files to extend from it instead of the previous yii\test\ActiveFixture class.

Example fixtures class

<?php
namespace tests\unit\fixtures\common\models;

use Sil\yii\test\ActiveFixture;

class UserFixture extends ActiveFixture
{
    public $modelClass = 'common\models\User';
    public $dataFile = 'tests/unit/fixtures/data/common/models/User.php';
}

Accessing fixtures from test case

After getting fixtures to load properly into the database we found we could not access them in the way we were used to. With yii2-codeception, within a test function we could call $this->fixtureType('fixtureName') to access a specific fixture record. After migrating to using Codeception\TestCase\Test as our unit test base class instead of yii\codeception\DbTestCase we got fatal errors about the method fixtureType does not exist on the object. When using the Yii2 Codeception module you access fixtures by calling $this->tester->grabFixture('fixtureType', 'fixtureName'). So to work around this we created a new base class for our tests that implements the __call() magic method to take the method name and argument and pass them to the new $this->tester->grabFixture() method. You can use this by updating your unit tests to extend Sil\Codeception\TestCase\Test instead of Codeception\TestCase\Test.

Example test case file

<?php
namespace tests\unit\common\models;

use Sil\Codeception\TestCase\Test;
use tests\unit\fixtures\common\models\UserFixture;

class UserTest extends Test
{
    public function fixtures()
    {
        return [
            'users' => UserFixture::className(),
        ];
    }
    
    public function testGetUser1()
    {
        $user1 = $this->users('user1');
        $this->assertEquals(1, $user1->id);
    }
}

统计信息

  • 总下载量: 63
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-16