PHP code example of kengos / factory_girl

1. Go to this page and download the library: Download kengos/factory_girl library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kengos / factory_girl example snippets



use FactoryGirl\Factory as FactoryGirl;
$factoryPaths = array('foo/bar/factories', 'bar/baz/factories');
FactoryGirl::setup($factoryPaths);

FactoryGirl::build('User')

FactoryGirl::create('User')

FactoryGirl::attributes('User')


// FileName UserFactory.php
return array(
  'class' => 'User', // -> new User
  'attributes' => array(
    'name' => 'xxxx', // $user->name = 'xxxx'
    'permission' => 'default', // $user->permission = 'default'
  ),
  'admin' => array(
    'name' => 'admin',
    'permission' => 'admin' // $user->permission = 'admin'
  )
);




return array(
  'class' => 'Foo',
  'attributes' => array(
    'name' => 'bar_{{sequence}}',
  ),
);

// UserFactory.php
return array(
  'class' => 'User',
  'attributes' => array(),
  'save' => array('generate'),
);

// In your test
FactoryGirl::create('User');
// called `generate`, instead of `save`

// UserFactory.php
return array(
  'class' => 'User',
  'attributes' => array(
    'setName' => 'foo',
    'generatePassword' => array('plain_password', 'seed'), 
  ),
);

// In your test
FactoryGirl::create('User');
// $user = new User;
// $user->setName('foo');
// $user->generatePassword('plain_password', 'seed');