ddinchev/yii-factory-girl

此包已被弃用且不再维护。未建议替代包。

Yii 1.1 fixture 替代框架。受 factory_girl_rails 和 kengos' FactoryGirl for Yii 的启发。

1.0.1 2015-02-19 23:31 UTC

This package is not auto-updated.

Last update: 2020-09-18 20:59:28 UTC


README

yii-factory-girl 是一个为 Yii 1.1 提供简单定义语法的 fixtures 替代方案,受 though_bot's factory_girl for Rubykengos' FactoryGirl 启发。

它被创建以解决 kengos' FactoryGirlPHP 的以下限制

  • 绕过外键约束,就像 Yii CDbFixtureManager 一样
  • 成为本地的 Yii 扩展
  • 允许不同的策略存储 fixtures - yml, json // 我实际上从未感到有这种需要

安装

通过 Composer 最容易安装 yii-factory-girl。使用以下命令安装最新的 1.x.x 版本。如果您计划在未安装 require-dev composer 包的环境中运行测试,请删除 --dev 标志。

php composer.phar require --dev --prefer-dist ddinchev/yii-factory-girl:~1.0.0

用法

确保您有单独的数据库用于运行单元测试。与 fixtures 一样,您正在为创建工厂的表中的任何记录都将首先被截断!

  1. 将组件添加到您的测试配置(protected\config\test.php)。YiiFactoryGirl\Factory 组件具有比列出的更多的属性。它们在类本身中有很好的文档。
return array(
    // ...
    'components' => array(
        // ...
        'factorygirl' => array(
            'class' => 'YiiFactoryGirl\Factory',
            // the following properties have the corresponding default values,
            // if they don't work out for you, feel free to change them
            // 'basePath' => 'protected/tests/factories',
            // 'factoryFileSuffix' => Factory, // so it would expect $basePath/UsersFactory.php for Users class factory
        )
    )
);
  1. 可选地,在 protected\tests\factories 下创建预定义的工厂数据文件(可配置)。它们使用以下文件格式
// The following is a factory config of Users model.
// protected/tests/factories/UsersFactory.php
// FileName UsersFactory.php
return array(
  'attributes' => array(
    'username' => 'Average Joe', // $user->username = 'test user'
    'type' => Users::TYPE_NORMAL_USER, // $user->type = Users::TYPE_NORMAL_USER
  ),
  'admin' => array(
    'name' => 'The Boss',
    'type' => Users::TYPE_ADMIN_USER
  )
);
  1. 现在,如果您的测试中调用 Yii::app()->factorygirl->create('Users');,这将创建一个 Users 记录。
class FactoryGirlIntegrationTest extends CTestCase
{

    public function testIntegration() {
        // this will create a model without actually inserting it in the db, so no primary key / id
        $unsaved = Yii::app()->factorygirl->build('Users');
        $this->assertTrue($unsaved->id == null);
    
        // this on the other hand will create the user!
        $user = Yii::app()->factorygirl->create('Users');
        $this->assertFalse($user->id == null);
        $this->assertInstanceOf('Users', $user);
        $this->assertEquals('Average Joe', $user->username);
        
        $admin = Yii::app()->factorygirl->create('Users', array(), 'admin');
        $this->assertEquals(Users::TYPE_ADMIN_USER, $user->type);
        
        $demonstrateOverwrites = Yii::app()->factorygirl->create('Users', array('type' => Users::TYPE_ADMIN_USER));
        $this->assertEquals(Users::TYPE_ADMIN_USER, $user->type);
        $this->assertEquals('Average Joe', $demonstrateOverwrites->username);
        
        // you can even also do this, even we don't have VehiclesFactory.php
        $vehicle = Yii::app()->factorygirl->create('Vehicles', array(
            'make' => 'Honda',
            'model' => 'Accord',
            'user_id' => Yii::app()->factorygirl->create('Users')->id
        ));
        $this->assertInstanceOf('Vehicles', $vehicle);
    }

}

在上面的最后一个示例中,如果您没有定义 VehiclesFactory.php 文件,您仍然可以创建工厂。所有 YiiFactoryGirl\Factory 期望的是存在一个名为 Vehicles 的类(或您正在尝试创建的任何内容),并且您不会从预定义的别名/属性中受益,这在许多情况下是完全可以接受的!

但是有一件不愉快的小事,您将无法在用 Yii::app()->factorygirl->create() 创建的模型上使用 IDE 自动完成。您可以做的第一件事是在测试引导中定义一个快捷函数

/**
 * @return YiiFactoryGirl\Factory
 */
function fg() {
    return Yii::app()->factorygirl;
}

现在您可以用 $user = fg()->create('Users'); 创建新模型。现在您的 IDE 应该知道 $userCActiveRecord,即使不是特定的 Users。当然,您可以创建特定模型的快捷方式。您可以在 protected\tests\factories\shortcuts.php 文件中创建一个文件,并在 bootstrap.php 中包含它,如下定义

class UsersFactory {
    /**
     * @return Users
     */
    public static function create(array $attributes = array(), $alias = null) {
        return Yii::app()->factorygirl->create('Users', $attributes, $alias); 
    }
    
    /**
     * @return Users
     */
    public static function build(array $attributes = array(), $alias = null) {
        return Yii::app()->factorygirl->build('Users', $attributes, $alias); 
    }
}

从现在起,您只需在测试中调用 $testuser = UsersFactory::create(['username' => 'testname']); 就可以创建用户工厂,并且您的 IDE 将知道 $testuserUsers 的实例!这当然是一些重复性的工作,因此对于在应用程序中广泛使用的模型来说是有意义的。

FactoryGirl 序列

这直接“借用”自 kengos' FactoryGirl。

<?php

return array(
  'attributes' => array(
    'name' => 'bar_{{sequence}}',
  ),
);
?>
Yii::app()->factorygirl->build('Foo')->name // -> bar_0
Yii::app()->factorygirl->build('Foo')->name // -> bar_1

贡献

为功能请求/错误报告开启问题是一种选择,但提出解决方案更有帮助!

  1. 进行Fork操作
  2. 创建你的功能分支(git checkout -b my-new-feature
  3. 提交你的更改(git commit -am '添加一些功能 / 修复一些错误。’
  4. 推送到分支(git push origin my-new-feature
  5. 创建新的Pull Request