PHP code example of codesleeve / fixture

1. Go to this page and download the library: Download codesleeve/fixture 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/ */

    

codesleeve / fixture example snippets


class fooTest extends PHPUnit_Framework_TestCase
{
	/**
     * A PDO connection instance.
     *
     * @var PDO
     */
	protected $db;
	
	/**
	 * Initialize our test state.
	 *
	 * @return void
	 */
	public function setUp() 
	{
		// create a pdo instance
		if (!$this->db) {
			$this->db = new PDO('sqlite::memory:');
		}
		
		// populate the users table
		$sql = 'INSERT INTO users (email, password, status, created_at, updated_at) values (?, ?, ?, ?, ?)';
		$sth = $this->prepare($sql);
		$sth->execute(array('[email protected]', 'abc12345$%^', 1, date('Y-m-d'), date('Y-m-d')));
		
		$sql = 'INSERT INTO users (email, password, status, created_at, updated_at) values (?, ?, ?, ?, ?)';
		$sth = $this->prepare($sql);
		$sth->execute(array('Jane', 'Doe', '[email protected]', 1, 'abc12345$%^', date('Y-m-d'), date('Y-m-d')));
		
		$sql = 'INSERT INTO users (email, password, status, created_at, updated_at) values (?, ?, ?, ?, ?)';
		$sth = $this->prepare($sql);
		$sth->execute(array('Jim', 'Doe', '[email protected]', 0, 'abc12345$%^', date('Y-m-d'), date('Y-m-d')));
		
		// populate the roles table
		$sql = 'INSERT INTO roles (name, created_at, updated_at) values (?, ?, ?)';
		$sth = $this->prepare($sql);
		$sth->execute(array('admin', date('Y-m-d'), date('Y-m-d')));
		
		$sql = 'INSERT INTO roles (name, created_at, updated_at) values (?, ?, ?)';
		$sth = $this->prepare($sql);
		$sth->execute(array('user', date('Y-m-d'), date('Y-m-d')));
		
		// populate the roles_users table
		$sql = 'INSERT INTO roles_users (role_id, user_id) values (?, ?)';
		$sth = $this->prepare($sql);
		$sth->execute(array(1, 1));
		
		$sql = 'INSERT INTO roles_users (role_id, user_id) values (?, ?)';
		$sth = $this->prepare($sql);
		$sth->execute(array(1, 2));
		
		$sql = 'INSERT INTO roles_users (role_id, user_id) values (?, ?)';
		$sth = $this->prepare($sql);
		$sth->execute(array(2, 3));
	}
	
	/**
	 * Reset our test state.
	 *
	 * @return void
	 */
	public function tearDown 
	{
		$this->db->query("TRUNCATE TABLE users");
		$this->db->query("TRUNCATE TABLE roles");
		$this->db->query("TRUNCATE TABLE roles_users");
	}
	
	/**
	 * A database integration test of some sort.
	 *
     * @test
	 * @return void
	 */
	public function it_should_be_able_to_do_some_query()
	{
        // Test that some query is working correctly.
	}
}

class fooTest extends PHPUnit_Framework_TestCase
{
	use Codesleeve\Fixture\Fixture;
	
	/**
     * The fixture instance.
     *
     * @var Fixture
     */
	protected $fixture;
	
	/**
	 * Initialize our test state.
	 *
	 * @return void
	 */
	public function setUp() 
	{
		// set the fixture instance
		$this->fixture = Fixture::getInstance();
		
		// populate our tables
		$this->fixture->up();
	}
	
	/**
	 * Reset our test state.
	 *
	 * @return void
	 */
	public function tearDown 
	{
		$this->fixture->down();
	}
	
	/**
	 * A database integration test of some sort.
	 *
     * @test
	 * @return void
	 */
	public function it_should_be_able_to_do_some_query()
	{
        // Test that some query is working correctly.
	}
}

// Create a pdo instance (this may already exist in your app)
$db = new \PDO('sqlite::memory:');

// Use the pdo instance to create a new driver instance
$driver = new Codesleeve\Fixture\Drivers\Standard($db);

// Create a configuration array.  
$config = array(
	'location' => 'path/to/your/fixtures.php'	// The directory you wish to load fixture files from.
);

// Fixture implements a singleton pattern.
// Get an instance of Fixture and set the config and driver.
$fixture = Fixture::getInstance();
$fixture->setConfig($config);
$fixture->setDriver($driver);

// Alternatively, you could do this in one swoop.
$fixture = Fixture::getInstance($config, $driver);

return array (
	'Ichigo' => array (
		'first_name' => 'Ichigo',
		'last_name'  => 'Kurosaki'		
	),
	'Renji' => array (
		'first_name' => 'Renji',
		'last_name'  => 'Abarai'		
	),
	'Genryusai' => array(
		'first_name' => 'Genryusai',
		'last_name'  => 'Yammamoto'
	)
);

return array (
	'Zangetsu' => array (
		'soul_reaper_id' => 'Ichigo',
		'name' => 'Zangetsu',
	),
	'Zabimaru' => array (
		'soul_reaper_id' => 'Renji',
		'name' => 'Zabimaru',
	),
	'Ryujin Jakka' => array(
		'soul_reaper_id' => 'Genryusai',
		'name' => 'Ryujin Jakka',
	)
);

return array (
	'Commander' => array (
		'title' => 'Commander'
	),
	'Captain' => array (
		'title' => 'Captain',
	),
	'Lieutenant' => array (
		'title' => 'Lieutenant',
	),
	'Substitute' => array (
		'title' => 'Substitute Shinigami',
	),
);

return array (
	'CommanderYammamoto' => array (
		'soul_reaper_id' => 'Yammamoto',
		'rank_id' 		 => 'Commander'
	),
	'CaptainYammamoto' => array (
		'soul_reaper_id' => 'Yammamoto',
		'rank_id' 		 => 'Captain'
	),
	'LieutenantAbari' => array (
		'soul_reaper_id' => 'Renji',
		'rank_id' 		 => 'Lieutenant'
	),
	'SubstituteKurosaki' => array (
		'soul_reaper_id' => 'Ichigo',
		'rank_id' 		 => 'Substitute'
	)
);


	use Codesleeve\Fixture\Fixture;
    use Codesleeve\Fixture\Drivers\Standard as StandardDriver;
	
	/**
     * The fixture instance.
     *
     * @var Fixture
     */
	protected $fixture;
	
	/**
	 * Initialize our test state.
	 *
	 * @return void
	 */
	public function setUp() 
	{
		// set the fixture instance
		$this->fixture = Fixture::getInstance();
		
		// populate our tables
		$this->fixture->up();
	}
	
	/**
	 * Reset our test state.
	 *
	 * @return void
	 */
	public function tearDown 
	{
		$this->fixture->down();
	}

// Returns 'Kurosaki'
echo $this->fixture->soul_reapers('Ichigo')->last_name;

	class SoulReaper extends Eloquent 
	{
		protected $table = 'soul_reapers';
		
		/**
	     * A soul reaper has one zanpakuto.
	     * 
	     * @return hasOne
	     */
	    public function zanpakuto()
	    {
	       return $this->hasOne('Zanpakuto');
	    }
	    
	    /**
	     * A soul reaper belongs to many ranks.
	     * 
	     * @return belongsToMany
	     */
	    public function ranks()
	    {
	       return $this->belongsToMany('ranks');
	    }
	}
	
	class Zanpakuto extends Eloquent 
	{
		protected $table = 'zanpakutos';
		
		/**
	     * A zanpakuto belongs to a Soul Reaper.
	     * 
	     * @return belongsTo
	     */
	    public function soulReaper()
	    {
	       return $this->belongsTo('SoulReaper');
	    }
	}

return array (
	'Ichigo' => array (
		'first_name' => 'Ichigo',
		'last_name'  => 'Kurosaki',
		'ranks' => array('Substitute|active:1')		
	),
	'Renji' => array (
		'first_name' => 'Renji',
		'last_name'  => 'Abarai',
		'ranks' => array('Lieutenant|active:1')		
	),
	'Genryusai' => array(
		'first_name' => 'Genryusai',
		'last_name'  => 'Yammamoto',
		'ranks' => array('Captain|active:1', 'Commander|active:1')
	)
);

return array (
	'Zangetsu' => array (
		'soulReaper' => 'Ichigo',
		'name' => 'Zangetsu',
	),
	'Zabimaru' => array (
		'soulReaper' => 'Renji',
		'name' => 'Zabimaru',
	),
	'Ryujin Jakka' => array(
		'soulReaper' => 'Genryusai',
		'name' => 'Ryujin Jakka',
	)
);

return array (
	'Commander' => array (
		'title' => 'Commander'
	),
	'Captain' => array (
		'title' => 'Captain',
	),
	'Lieutenant' => array (
		'title' => 'Lieutenant',
	),
	'Substitute' => array (
		'title' => 'Substitute Shinigami',
	),
);

 // Creates the 'Zangetsu' record in the zanpakutos table and assigns ownership
 // to 'Ichigo' (It populates the 'soul_reaper_id' foreign key for us automatically).
 'Zangetsu' => array (
	'soulReaper' => 'Ichigo',
	'name' => 'Zangetsu',
),
 

// This assigns Genryusai the ranks of Captain and Commander and 
// sets the 'active' pivot column to 1 for each rank.
'Genryusai' => array(
	'first_name' => 'Genryusai',
	'last_name'  => 'Yammamoto',
	'ranks' => array('Captain|active:1', 'Commander|active:1')
)


	use Codesleeve\Fixture\Fixture;
    use Codesleeve\Fixture\Drivers\Eloquent as EloquentDriver;
	
	/**
     * The fixture instance.
     *
     * @var Fixture
     */
	protected $fixture;
	
	/**
	 * Initialize our test state.
	 *
	 * @return void
	 */
	public function setUp() 
	{
		// set the fixture instance
		$db = new \PDO('sqlite::memory:');
		$driver = new EloquentDriver($db);
		$this->fixture = Fixture::getInstance(array('location' => 'path/to/your/fixtures.php'), $driver);
		
		// populate our tables
		$this->fixture->up();
	}
	
	/**
	 * Reset our test state.
	 *
	 * @return void
	 */
	public function tearDown 
	{
		$this->fixture->down();
	}

// We can call Faker through fixture via the 'Fake' method.
// Here, we'll whip up some fake info for Ichigo:
'Ichigo' => array (
	'first_name' => 'Ichigo',
	'last_name'  => 'Kurosaki',
	'ranks' => array('Substitute|active:1'),
	'bio' => Fixture::fake('text'),
	'age' => Fixture::fake('randomNumber', 15, 18),
	'address' => Fixture::fake('address')	
),