manageitwa/laravel-fixtures
Composer 安装命令:
composer require --dev manageitwa/laravel-fixtures
包简介
Loads fake data sets into Laravel for testing.
README 文档
README
Laravel Fixtures Package
Fixtures are used to load a "fake" set of data into a database that can then be used for testing or to help give you some interesting data while you're developing your application.
A fixture may depend on other fixtures, specified via its CorpSoft\Fixture\Fixture::$depends property. When a fixture is being loaded, the fixtures it depends on will be automatically loaded BEFORE the fixture; and when the fixture is being unloaded, the dependent fixtures will be unloaded AFTER the fixture.
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist manageitwa/laravel-fixtures "*"
or add
"manageitwa/laravel-fixtures": "*"
to the require section of your composer.json file.
Defining a Fixture
To define a fixture, create a new class by extending CorpSoft\Fixture\ActiveFixture.
The following code defines a fixture about the User Illuminate\Database\Eloquent\Model and the corresponding users table.
<?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; }
Tip: Each ActiveFixture is about preparing a DB table for testing purpose. You may specify the table by setting either the CorpSoft\Fixture\ActiveFixture::$table property or the CorpSoft\Fixture\ActiveFixture::$modelClass property. If the latter, the table name will be taken from the Illuminate\Database\Eloquent\Model class specified by modelClass.
The fixture data for an ActiveFixture fixture is usually provided in a file located at public/storage/fixtures/table_name.php.
The data file should return an array of data rows to be inserted into the user table. For example:
<?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'), ], ];
As we described earlier, a fixture may depend on other fixtures. For example, a UserProfileFixture may need to depends on UserFixture because the user profile table contains a foreign key pointing to the user table. The dependency is specified via the CorpSoft\Fixture\Fixture::$depends property, like the following:
<?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]; }
The dependency also ensures, that the fixtures are loaded and unloaded in a well defined order. In the above example UserFixture will always be loaded before UserProfileFixture to ensure all foreign key references exist and will be unloaded after UserProfileFixture has been unloaded for the same reason.
Using Fixtures
- If you are using
phpunitto test your code, then you need to addCorpSoft\Fixture\Traits\FixtureTraitto abstract classTestCasein thetestsfolder as follows:
<?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(); } }
- If you are using
Laravel Duskto test your code, then you need to addCorpSoft\Fixture\Traits\FixtureTraitto abstract classDuskTestCasein thetestsfolder as follows:
<?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 }
After this steps you can define fixtures in your test classes as follows:
<?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); } }
The fixtures listed in the fixtures() method will be automatically loaded before a test is executed.
And as we described before, when a fixture is being loaded, all its dependent fixtures will be automatically loaded first. In the above example, because UserProfileFixture depends on UserFixture, when running any test method in the test class, two fixtures will be loaded sequentially: UserFixture and UserProfileFixture.
manageitwa/laravel-fixtures 适用场景与选型建议
manageitwa/laravel-fixtures 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 643 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 07 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「fixtures」 「testing」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 manageitwa/laravel-fixtures 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 manageitwa/laravel-fixtures 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 manageitwa/laravel-fixtures 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Testing object factory for PHP
Testing Suite For Lumen like Laravel does.
Testing object factory for PHP
Plugin to compare fixtures with live DB tables
The PHP SDK for Checkmango
A model factory package for Laravel 4 with expressive API for creating custom tailored dummy objects
统计信息
- 总下载量: 643
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-07-17