Coder Social home page Coder Social logo

cakephp-factory-muffin's Introduction

CakePHP FactoryMuffin Integration Plugin

Plugin integrates FactoryMuffin and Faker for seeding and testing.

Provided features

  • CakePHP ORM integration for FactoryMuffin.
  • Easy to inject into your project.

Installing

PHP 5.6+ and Composer are required.

In your composer.json, simply add "skie/cakephp-factory-muffin": "*" to your "require" section:

{
    "require": {
        "skie/cakephp-factory-muffin": "*"
    }
}

Factory definition.

Define factory classes in App\Model\Factory namespace for application level, in ${PluginScope}\Model\Factory for plugins.

Each factory class should contain definition method that describe how to create entity.

By convention factory classes should match with Table classes name but with Factory suffix.

namespace App\Model\Factory;

use CakephpFactoryMuffin\Model\Factory\AbstractFactory;
use League\FactoryMuffin\Faker\Facade as Faker;

class UsersFactory extends AbstractFactory {

    public function definition()
    {
        return [
            'first_name' => Faker::firstName(),
            'last_name' => Faker::lastName(),
            'username' => function ($object, $saved) {
                return strtolower($object['last_name'] . '_' . $object['first_name']);
            },
            'password' => Faker::word(),
        ];
    }
}

Usage

In tests or seed files you can use CakephpFactoryMuffin\FactoryLoader objects that manage Factory loading and dispatch creation process to FactoryMuffin.

This class perform cakephp orm integration with FactoryMuffin and serve cakephp tables naming conventions like 'Users', or 'Plugin.Records'.

To load factory definition one can use FactoryLoader::load('Users'). To load all applicaiton level factories use FactoryLoader::loadAll(). And to load factories for plugin Plugin/Name use FactoryLoader::loadAll('Plugin/Name').

Example

FactoryLoader::load('Users');
$user = FactoryLoader::create('Users');
$users = FactoryLoader::seed(10, 'Users');

Here created 11 users records in database.

In tests we need to flush created objects. It could be achieved by call

FactoryLoader::getInstance()->getFactoryMuffin()->deleteSaved();

cakephp-factory-muffin's People

Contributors

borantula avatar skie avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

borantula

cakephp-factory-muffin's Issues

Gets parent type Cake/ORM/Entity for plugin models

Hi,

When I try to create a model from a plugin, create object is type of Cake/ORM/Entity instead of Plugin/Model/Entity/FooBar and it breaks typecasted listeners etc. It works perfectly for App scope entities. Do we need to specify something extra for correct type? I tried to go through source code but couldn't figure out.

Thanks for the nice tool :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.