ddinchev / yii-factory-girl Goto Github PK
View Code? Open in Web Editor NEWYii 1.1 test factories (fixtures replacement) library inspired by `factory_girl`.
License: MIT License
Yii 1.1 test factories (fixtures replacement) library inspired by `factory_girl`.
License: MIT License
The Sequence
static utility was borrowed from kengos' FactoryGirl implementation but instead of static utility, values could be represented by value object that know their state or the Sequence
at least could be instantiated per FactoryData
.
In the classical way of defining fixtures in Yii, data files return array of attribute arrays. While I don't like that approach as return
in root of a PHP file:
So I would suggest the following way of managing data files.
Data files by default go under protected\tests\factories
, path that is configurable. Files have the format of TableNameFactory.php
and contain a class named TableNameFactory
. For example if we have table named Vehicles
, we would create VehiclesFactory.php
that contain Vehicles
class. If we have user_profile
table (singular, lowercase convention), we would have user_profileFactory.php
that contains user_profileFactory
class. Although the suffix is configurable param and can be changed to _factory
to match user convention.
Those classes have simple interface like:
// FileName VehiclesFactory.php
class VehiclesFactory
{
public static function getConfig()
{
return array(
'class' => 'Vehicles', // -> new Vehicles, but if left black it's expected to have a model with TableName
'attributes' => array(
// this is default base configuration added to any newly created Vehicles factory, if no additional config was supplied
'ModelID' => 123, // $vehicle->ModelID = 123
'UserID' => UsersFactory::create()->UserID
),
'motorcylcle' => array(
// this is alias that can be called with VehiclesFactory::create(array(), 'motorcycle') that would overwrite the default config with this config
// of course you can supply attributes to overwrite the motorcycle alias config as well
'ModelID' => 246, // motorcycle model
)
);
}
/**
* @param array $attributes
* @param null $alias
* @return \Vehicles
*/
public static function create(array $attributes = null, $alias = null) {
return Yii::app()->factorygirl->create('Vehicles', $attributes, $alias);
}
}
This is one possible solution where the config
and create
are static methods. But I think I could also define an abstract FactoryData
class which the data classes (like VehiclesFactory
) subclass.
Another one is to just return config directly as in fixtures
and use YiiFactoryGirl
like Yii::app()->factorygirl->create('Vehicles', $attr, $alias)
but it kind'a sucks as you have to type hint anywhere you create a factory in your tests.
Any suggestions and questions are welcome.
Hey!
I admire your effort to put this all together. I would like to contribute to this library to add support for Yii2.
Any suggestion on where to start? Do you want to have backwards compatibility with Yii1? Should I create a new project called yii2-factory-girl
or something?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.