gourmet/faker

Faker support for CakePHP 3

Installs: 8 452

Dependents: 1

Suggesters: 0

Security: 0

Stars: 18

Watchers: 6

Forks: 3

Type:cakephp-plugin

v1.0.3 2015-04-05 15:12 UTC

This package is auto-updated.

Last update: 2024-03-28 00:38:57 UTC


README

Total Downloads License

Built to enable Faker support in CakePHP 3.

Install

Using Composer:

composer require gourmet/faker:~1.0

You then need to load the plugin. In boostrap.php, something like:

\Cake\Core\Plugin::load('Gourmet/Faker');

Examples

Fixture

<?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();
    }
}

Migration

<?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]);
    }
}

License

Copyright (c)2015, Jad Bitar and licensed under The MIT License.