Skip to content
This repository has been archived by the owner on Mar 12, 2019. It is now read-only.

rodrigorm/fixture-factory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 

Repository files navigation

Fixture Factory

Fixture Factory is a tool that let’s you easily create data for testing in CakePHP. Instead of maintaining test data placed in many *_fixture.php files or importing it from other database, you can create specific test data while writing your tests.

Installation

If you are using git in you app do this:

	git submodule add git://github.com/rodrigorm/fixture-factory.git plugins/fixture_factory

Ou just download this plugin and place files in app/plugins/fixture_factory and add following line before a test case you want to use FF in.

  App::Import('Vendor', 'FixtureFactory.fixture_factory');

Defining factories

Before you start creating test data you must define factories for your models.

	FFactory::define('User', array(
		'active' => 1,
		'gender' => 'm'
	));

This will create factory for User model with some default values. Now you can create an user with that factory.

	$user = FFactory::create('User', array(
		'login' => 'joe',
		'email' => '[email protected]'
	));

Model attributes passed to create method with be merged with defaults (or will overwrite them) passed during factory definition. Create method saves the record to the database, then reads it back (with $model→read()) and returns whole record. If you want just to build user data without saving it to database use build method instead.

	$user = FFactory::build('User', array(
		'login' => 'joe',
		'email' => '[email protected]'
	));

Sequences

Sometimes there is a need to have unique values for some fields, for example emails or logins. You can generate unique values with sequences.

	FFactory::sequence('logins', 'user {n}');

	FFactory::define('User', array(
		'login' => array('sequence', 'logins');
	));

Now logins will be set to ‘user 1’, ‘user 2’, ‘user 3’ and so on.

Associations

When you test your models you often expect that records for associated models are already present in database. For example when you test Article model you expect that article’s author already exists. You don’t need to create a record for author by hand, Fixture Factory is able to take care of it. All you have to do is to define factory for User before creating/building Article data.

	FFactory::define('Article', array(
		'title' => 'new title',
		'user_id' => array('assoc', 'User', 'id')
	));

Associated user will now be saved to database before article. It will also be saved even if you only build data for Article.

Aliased factories

You can have many factories for one model. It is useful if you want to test your data in different context and keep the code more readable. In such situations you just pass model name as an additional parameter.

	FFactory::define('User', array(
		'active' => 1
	));
  
	FFactory::define('InactiveUser', array(
		'active' => 0
	), 'User');

Copyright

Copyright © 2009 Michał Szajbe (http://codetunes.com), released under the MIT license.

About

Fixture Factory for testing CakePHP.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages