Plugin integrates FactoryMuffin and Faker for seeding and testing.
- CakePHP ORM integration for FactoryMuffin.
- Easy to inject into your project.
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": "*"
}
}
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(),
];
}
}
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')
.
FactoryLoader::load('Users');
$user = FactoryLoader::create('Users');
$users = FactoryLoader::seed(10, 'Users');
Here created 11 users records in database.
FactoryLoader::getInstance()->getFactoryMuffin()->deleteSaved();