1. Go to this page and download the library: Download ddinchev/yii-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/ */
ddinchev / yii-factory-girl example snippets
return array(
// ...
'components' => array(
// ...
'factorygirl' => array(
'class' => 'YiiFactoryGirl\Factory',
// the following properties have the corresponding default values,
// if they don't work out for you, feel free to change them
// 'basePath' => 'protected/tests/factories',
// 'factoryFileSuffix' => Factory, // so it would expect $basePath/UsersFactory.php for Users class factory
)
)
);
// The following is a factory config of Users model.
// protected/tests/factories/UsersFactory.php
// FileName UsersFactory.php
return array(
'attributes' => array(
'username' => 'Average Joe', // $user->username = 'test user'
'type' => Users::TYPE_NORMAL_USER, // $user->type = Users::TYPE_NORMAL_USER
),
'admin' => array(
'name' => 'The Boss',
'type' => Users::TYPE_ADMIN_USER
)
);
class FactoryGirlIntegrationTest extends CTestCase
{
public function testIntegration() {
// this will create a model without actually inserting it in the db, so no primary key / id
$unsaved = Yii::app()->factorygirl->build('Users');
$this->assertTrue($unsaved->id == null);
// this on the other hand will create the user!
$user = Yii::app()->factorygirl->create('Users');
$this->assertFalse($user->id == null);
$this->assertInstanceOf('Users', $user);
$this->assertEquals('Average Joe', $user->username);
$admin = Yii::app()->factorygirl->create('Users', array(), 'admin');
$this->assertEquals(Users::TYPE_ADMIN_USER, $user->type);
$demonstrateOverwrites = Yii::app()->factorygirl->create('Users', array('type' => Users::TYPE_ADMIN_USER));
$this->assertEquals(Users::TYPE_ADMIN_USER, $user->type);
$this->assertEquals('Average Joe', $demonstrateOverwrites->username);
// you can even also do this, even we don't have VehiclesFactory.php
$vehicle = Yii::app()->factorygirl->create('Vehicles', array(
'make' => 'Honda',
'model' => 'Accord',
'user_id' => Yii::app()->factorygirl->create('Users')->id
));
$this->assertInstanceOf('Vehicles', $vehicle);
}
}