manageitwa/laravel-fixtures

将假数据集加载到Laravel中进行测试。

v1.3.0 2024-06-17 02:51 UTC

This package is auto-updated.

Last update: 2024-09-17 03:21:40 UTC


README

Laravel Fixtures Package


固定数据用于将“假”数据集加载到数据库中,该数据集可以用于测试或帮助您在开发应用程序时获取一些有趣的数据。

一个固定数据可能依赖于其他固定数据,通过其CorpSoft\Fixture\Fixture::$depends属性指定。当一个固定数据被加载时,它所依赖的固定数据将自动在固定数据之前加载;当固定数据被卸载时,依赖的固定数据将在固定数据之后卸载。

Latest Stable Version Total Downloads License Build Status Scrutinizer Code Quality

安装

安装此扩展的首选方法是通过composer

运行以下命令之一

php composer.phar require --prefer-dist manageitwa/laravel-fixtures "*"

或者将以下内容添加到您的composer.json文件的require部分

"manageitwa/laravel-fixtures": "*"

定义固定数据

要定义固定数据,通过扩展CorpSoft\Fixture\ActiveFixture创建一个新的类。

以下代码定义了一个关于User Illuminate\Database\Eloquent\Model及其对应的用户表的固定数据。

<?php

namespace Fixtures;

use CorpSoft\Fixture\ActiveFixture;
use App\Models\User;

class UserFixture extends ActiveFixture
{
    /**
     * @var string
     */
    public $dataFile = 'fixtures/users.php';

    /**
     * @var string
     */
    public $modelClass = User::class;
}

提示:每个ActiveFixture都是为测试目的准备一个数据库表。您可以设置CorpSoft\Fixture\ActiveFixture::$table属性或CorpSoft\Fixture\ActiveFixture::$modelClass属性来指定表。如果后者,则表名将从指定的modelClass的Illuminate\Database\Eloquent\Model类中获取。

ActiveFixture固定数据的通常是位于public/storage/fixtures/table_name.php的文件中。数据文件应返回一个要插入到用户表中的数据行数组。例如

<?php
// public/storage/fixtures/users.php
return [
    [
        'name' => 'user1',
        'email' => 'user1@example.org',
        'password' => bcrypt('secret'),
    ],
    [
        'name' => 'user2',
        'email' => 'user2@example.org',
        'password' => bcrypt('secret'),
    ],
];

如我们之前所述,一个固定数据可能依赖于其他固定数据。例如,一个UserProfileFixture可能需要依赖于UserFixture,因为用户资料表包含一个指向用户表的外键。依赖通过CorpSoft\Fixture\Fixture::$depends属性指定,如下所示

<?php

namespace Fixtures;

use CorpSoft\Fixture\ActiveFixture;
use App\Models\UserProfile;

class UserProfileFixture extends ActiveFixture
{
    /**
     * @var string
     */
    public $dataFile = 'fixtures/user_profile.php';

    /**
     * @var string
     */
    public $modelClass = UserProfile::class;

    /**
     * @var array
     */
    public $depends = [UserFixture::class];
}

依赖还确保了固定数据按良好的定义顺序加载和卸载。在上面的例子中,UserFixture将始终在UserProfileFixture之前加载,以确保所有外键引用都存在,并在卸载UserProfileFixture后卸载,出于相同的原因。

使用固定数据

  1. 如果您使用phpunit测试您的代码,那么您需要将CorpSoft\Fixture\Traits\FixtureTrait添加到tests文件夹中的抽象类TestCase中,如下所示
<?php

namespace Tests;

use CorpSoft\Fixture\Traits\FixtureTrait;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication, FixtureTrait;

    /**
     * @inheritdoc
     */
    protected function setUp()
    {
        parent::setUp();

        $this->initFixtures();
    }
}
  1. 如果您使用Laravel Dusk测试您的代码,那么您需要将CorpSoft\Fixture\Traits\FixtureTrait添加到tests文件夹中的抽象类DuskTestCase中,如下所示
<?php

namespace Tests;

use CorpSoft\Fixture\Traits\FixtureTrait;
use Laravel\Dusk\TestCase as BaseTestCase;

abstract class DuskTestCase extends BaseTestCase
{
    use CreatesApplication, FixtureTrait;

    /**
     * @inheritdoc
     */
    protected function setUp()
    {
        parent::setUp();

        $this->initFixtures();
    }
    
    // other methods
}

完成这些步骤后,您可以在测试类中如下定义固定数据

<?php

namespace Tests\Unit;

use Tests\TestCase;
use Fixtures\UserProfileFixture;

class ExampleTest extends TestCase
{
    /**
     * Declares the fixtures that are needed by the current test case.
     *
     * @return array the fixtures needed by the current test case
     */
    public function fixtures(): array
    {
        return [
            'profiles' => UserProfileFixture::class,
        ];
    }
        
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testBasicTest()
    {
        $this->assertTrue(true);
    }
}

fixtures()方法中列出的固定数据将在执行测试之前自动加载。正如我们之前所述,当固定数据被加载时,所有其依赖的固定数据将自动先加载。在上面的例子中,因为UserProfileFixture依赖于UserFixture,所以在运行测试类中的任何测试方法时,将按顺序加载两个固定数据:UserFixtureUserProfileFixture