2017 © Pedro Peláez
 

library dbfixture

Light fixtures loader

image

nezarfadle/dbfixture

Light fixtures loader

  • Wednesday, August 30, 2017
  • by nezarfadle
  • Repository
  • 1 Watchers
  • 0 Stars
  • 19 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

DbFixture

This is a light class library working as SQL scripts Executer., (*1)

Installation

composer require nezarfadle/dbfixture, (*2)

Usage

  1. Create a Database and name it dbfixture ( This could be any name )., (*3)

  2. Create a folder and name it fixtures and save your fixtures files inside it:, (*4)

fixtures/articles.create.sql
############################

CREATE TABLE IF NOT EXISTS `articles` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
fixtures/articles.drop.sql
##########################

DROP TABLE IF EXISTS articles;
fixtures/articles.insert.sql
############################

INSERT INTO articles values (1, 'PHP')
  1. Create your own Fixture Class by extending the DbFixture class:
// MysqlDbFixture.php 
<?php

use DbFixture\DbFixture;

class MysqlDbFixture extends DbFixture
{

    public function __construct( $dsn, $username, $password )
    {

        $pdo = new \PDO( $dsn, $username, $password, 
            [
                    \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION
            ]
        );

        parent::__construct( $pdo );


    }
}

instantiate a new MysqlDbFixture instance and start using it:, (*5)

$fixture = new MysqlDbFixture( 'mysql:host=localhost;dbname=dbfixture', 'root', 'password' );

$fixture->runScript('fixtures/articles.create.sql');
$fixture->runScript('fixtures/articles.insert.sql');
$fixture->runScript('fixtures/articles.drop.sql');

Use the basePath method to tell the DbFixture where to find the fixtures files:, (*6)

$fixture->basePath(__DIR__ . '/fixtures/'); 

$fixture->runScript('articles.create.sql');
$fixture->runScript('articles.insert.sql');
$fixture->runScript('articles.drop.sql');

To load multiple scripts use runScripts method:, (*7)

$fixture->basePath(__DIR__ . '/fixtures/'); 

$fixture->runScripts( 
    [ 
        'articles.create.sql', 
        'articles.insert.sql',
        'articles.drop.sql'
    ]
);

Use the run method to execute raw SQL statment:, (*8)

$fixture->run( "INSERT INTO articles values (1, 'PHP')" );

How to run the Test

  1. Create a database and name it dbfixture ( This could be any name )
  2. Edit the tests/config.php file and set the database credentials
  3. run phpunit :, (*9)

    phpunit
    

The Versions

30/08 2017

dev-master

9999999-dev https://github.com/nezarfadle/dbfixture

Light fixtures loader

  Sources   Download

MIT

The Development Requires

by Nezar Fadle

02/08 2016

1.0.1

1.0.1.0 https://github.com/nezarfadle/dbfixture

Light fixtures loader

  Sources   Download

MIT

The Development Requires

by Nezar Fadle

10/06 2016

1.0

1.0.0.0 https://github.com/nezarfadle/dbfixture

Light fixtures loader

  Sources   Download

MIT

The Development Requires

by Nezar Fadle