gourmet / faker
Faker 支持 CakePHP 3
v1.0.3
2015-04-05 15:12 UTC
Requires
- fzaninotto/faker: 1.4.*
Requires (Dev)
- cakephp/cakephp: 3.0.*
- phpunit/phpunit: 4.1.*
This package is auto-updated.
Last update: 2024-08-28 01:45:36 UTC
README
构建用于在 Faker 中启用对 CakePHP 3 的支持。
安装
使用 Composer
composer require gourmet/faker:~1.0
然后需要加载插件。在 boostrap.php
中,类似以下内容
\Cake\Core\Plugin::load('Gourmet/Faker');
示例
测试数据
<?php namespace App\Test\Fixture; use Gourmet\Faker\TestSuite\Fixture\TestFixture; class PostsFixture extends TestFixture { public $fields = [ 'id' => ['type' => 'integer'], 'title' => ['type' => 'string', 'length' => 255, 'null' => false], 'body' => 'text', 'published' => ['type' => 'integer', 'default' => '0', 'null' => false], 'created' => 'datetime', 'updated' => 'datetime', '_constraints' => [ 'primary' => ['type' => 'primary', 'columns' => ['id']] ] ]; public $records = [ [ 'id' => 1, 'title' => 'First Article', 'body' => 'First Article Body', 'published' => '1', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31' ], [ 'id' => 2, 'title' => 'Second Article', 'body' => 'Second Article Body', 'published' => '1', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31' ], [ 'id' => 3, 'title' => 'Third Article', 'body' => 'Third Article Body', 'published' => '1', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31' ] ]; public $number = 20; public $guessers = ['\Faker\Guesser\Name']; public function init() { $this->customColumnFormatters = [ 'id' => function () { return $this->faker->numberBetween(count($this->records) + 2); }, 'published' => function () { return rand(0,3); } ]; parent::init(); } }
迁移
<?php use Phinx\Migration\AbstractMigration; class SeedDataMigration extends AbstractMigration { public function up() { $faker = Faker::create(); $populator = new Populator($faker); $populator->addGuesser('\Faker\Guesser\Name'); $created = $modified = function () use ($faker) { static $beacon; $ret = $beacon; if (empty($ret)) { return $beacon = $faker->dateTimeThisDecade(); } $beacon = null; return $ret; } $timestamp = compact('created', 'modified'); $roles = ['admin', 'editor', 'member']; $populator->addEntity('Users', 100, [ 'email' => function () use ($faker) { return $faker->safeEmail(); }, 'first_name' => function () use ($faker) { return $faker->firstName(); }, 'last_name' => function () use ($faker) { return $faker->lastName(); }, 'timezone' => function () use ($faker) { return $faker->timezone(); }, 'role' => function () use ($roles) { return $roles[array_rand($roles)]; } ] + $timestamp); $populator->execute(['validate' => false]); } }
许可证
版权 (c)2015, Jad Bitar,许可协议为 MIT 许可证。