Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gourmet/faker
CakePHP 3 plugin to support Faker's data generation/seeding in fixtures, migrations, etc.
https://github.com/gourmet/faker
Last synced: 3 months ago
JSON representation
CakePHP 3 plugin to support Faker's data generation/seeding in fixtures, migrations, etc.
- Host: GitHub
- URL: https://github.com/gourmet/faker
- Owner: gourmet
- License: mit
- Created: 2014-12-05T14:27:01.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2016-08-20T13:58:17.000Z (about 8 years ago)
- Last Synced: 2024-04-23T15:02:37.033Z (7 months ago)
- Language: PHP
- Homepage:
- Size: 10.7 KB
- Stars: 18
- Watchers: 6
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
- awesome-cakephp - Faker plugin - [Faker](https://github.com/fzaninotto/Faker) support for CakePHP fixtures. (Testing)
README
# Faker
[![Total Downloads](https://poser.pugx.org/gourmet/faker/downloads.svg)](https://packagist.org/packages/gourmet/faker)
[![License](https://poser.pugx.org/gourmet/faker/license.svg)](https://packagist.org/packages/gourmet/faker)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:
```php
\Cake\Core\Plugin::load('Gourmet/Faker');
```## Examples
### Fixture
```php
['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
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][mit].
[CakePHP 3]:http://cakephp.org
[Composer]:http://getcomposer.org
[mit]:http://www.opensource.org/licenses/mit-license.php
[Faker]:https://github.com/fzaninotto/Faker