2017 © Pedro Peláez
 

cakephp-plugin cake_fabricate

CakeFabricate plugin for CakePHP

image

sizuhiko/cake_fabricate

CakeFabricate plugin for CakePHP

  • Monday, May 28, 2018
  • by sizuhiko
  • Repository
  • 2 Watchers
  • 14 Stars
  • 37,446 Installations
  • PHP
  • 1 Dependents
  • 1 Suggesters
  • 4 Forks
  • 0 Open issues
  • 7 Versions
  • 8 % Grown

The README.md

Build Status Coverage Status Total Downloads Latest Stable Version, (*1)

CakeFabricate plugin for CakePHP

CakeFabricate is adaptor for Fabricate, and integrate Fabricate(version 2) to CakePHP3., (*2)

Installation

You can install this plugin into your CakePHP application using composer., (*3)

The recommended way to install composer packages is:, (*4)

composer require sizuhiko/cake_fabricate

Usage

At first, Fabricate require to config for using. In app/tests/bootstrap.php, add followings :, (*5)

use Fabricate\Fabricate;
use CakeFabricate\Adaptor\CakeFabricateAdaptor;

Fabricate::config(function($config) {
    $config->adaptor = new CakeFabricateAdaptor();
});

APIs

CakeFabricateAdaptor has options. The options set with constructor., (*6)

Configuration

Fabricate::config(function($config) {
    $config->adaptor = new CakeFabricateAdaptor([
        CakeFabricateAdaptor::OPTION_FILTER_KEY => true
        CakeFabricateAdaptor::OPTION_VALIDATE   => true
    ]);
});

Supported Options

CakeFabricateAdaptor::OPTION_FILTER_KEY

OPTION_FILTER_KEY If true, not generate any primary key for auto incrementation id., (*7)

Default: false, (*8)

OPTION_VALIDATE

Indicates whether or not to validate when create new entity. see: CakePHP's Entity::newEntity(), (*9)

Default: false, (*10)

Generate model attributes as array (not saved)

Fabricate::attributes_for(:name, :number_of_generation, :array_or_callback) generate only attributes., (*11)

  • name: Table class alias. For get table instance of CakePHP3, called TableRegistry::get(:name);.
  • number_of_generation: Generated number of records
  • array_or_callback: it can override each generated attributes

Example App

Entities:, (*12)

  • Model\Entity\Post
  • Model\Entity\User

Tables:, (*13)

  • Model\Entity\PostsTable
  • Model\Entity\UsersTable

Associations:, (*14)

PostsTable has many UsersTable as Author, (*15)

Example

$results = Fabricate::attributes_for('Posts', 10, function($data){
    return ["created" => "2013-10-09 12:40:28", "updated" => "2013-10-09 12:40:28"];
});

// $results is array followings :
array (
  0 => 
  array (
    'title' => 'Lorem ipsum dolor sit amet',
    'body' => 'Lorem ipsum dolor sit amet, aliquet feugiat. Convallis morbi fringilla gravida, phasellus feugiat dapibus velit nunc, pulvinar eget sollicitudin venenatis cum nullam, vivamus ut a sed, mollitia lectus. Nulla vestibulum massa neque ut et, id hendrerit sit, feugiat in taciti enim proin nibh, tempor dignissim, rhoncus duis vestibulum nunc mattis convallis.',
    'created' => '2013-10-09 12:40:28',
    'updated' => '2013-10-09 12:40:28',
  ),
  1 => 
  array (
  ....

Generate a entity instance (not saved)

Fabricate::build(:name, :array_or_callback) generate a entity instance (using Table::newInstance)., (*16)

  • name: Table class alias. For get table instance of CakePHP3, called TableRegistry::get(:name);.
  • array_or_callback: it can override each generated attributes

Example

$result = Fabricate::build('Posts', function($data){
    return ["created" => "2013-10-09 12:40:28", "updated" => "2013-10-09 12:40:28"];
});

// $result a Model\Entity\Post object.
 ......

Generate records to database

Fabricate::create(:name, :number_of_generation, :array_or_callback) generate and save records to database., (*17)

  • name: Table class alias. For get table instance of CakePHP3, called TableRegistry::get(:name);.
  • number_of_generation: Generated number of records
  • array_or_callback: it can override each generated attributes

Example

Fabricate::create('Posts', 10, function($data){
    return ["created" => "2013-10-09 12:40:28", "updated" => "2013-10-09 12:40:28"];
});

Associations

It's possible to set up associations(hasOne/hasMany/belongsTo) within Fabricate::create(). You can also specify a FabricateContext::association(). It will generate the attributes, and set(merge) it in the current array., (*18)

Usage

Fabricate::create('Users', function($data, $world) {
    return [
        'user' => 'taro',
        'posts' => $world->association('Posts', 3, ['author_id'=>false]),
    ];
});
// can use defined onbject.
Fabricate::define(['PublishedPost', 'class'=>'Posts'], ['published'=>'1']);
Fabricate::create('Users', function($data, $world) {
    return [
        'user' => 'taro',
        'posts' => $world->association(['PublishedPost', 'association'=>'Posts'], 3, ['author_id'=>false]),
    ];
});
// can use association alias (Post belongs to Author of User class)
Fabricate::define(['PublishedPost', 'class'=>'Posts'], ['published'=>'1']);
Fabricate::create('PublishedPost', 3, function($data, $world) {
    return [
        'author' => $world->association(['Users', 'association'=>'Author'], ['id'=>1,'user'=>'taro']),
    ];
});

Any more features

Please see documentation of Fabricate., (*19)

Contributing to this Library

Please feel free to contribute to the library with new issues, requests, unit tests and code fixes or new features. If you want to contribute some code, create a feature branch from develop, and send us your pull request., (*20)

The Versions

28/05 2018

dev-develop

dev-develop

CakeFabricate plugin for CakePHP

  Sources   Download

The Requires

 

The Development Requires

16/12 2017

dev-master

9999999-dev

CakeFabricate plugin for CakePHP

  Sources   Download

The Requires

 

The Development Requires

16/12 2017

v0.2.2

0.2.2.0

CakeFabricate plugin for CakePHP

  Sources   Download

The Requires

 

The Development Requires

14/08 2015

v0.2.1

0.2.1.0

CakeFabricate plugin for CakePHP

  Sources   Download

The Requires

 

The Development Requires

18/04 2015

v0.2.0

0.2.0.0

CakeFabricate plugin for CakePHP

  Sources   Download

The Requires

 

The Development Requires

23/03 2015

v0.1.1

0.1.1.0

CakeFabricate plugin for CakePHP

  Sources   Download

The Requires

 

The Development Requires

07/03 2015

v0.1

0.1.0.0

CakeFabricate plugin for CakePHP

  Sources   Download

The Requires

 

The Development Requires