2017 © Pedro Peláez
 

library database

Collection of database classes to allow database operations and unit testing

image

martynbiz/database

Collection of database classes to allow database operations and unit testing

  • Sunday, November 30, 2014
  • by martynbiz
  • Repository
  • 1 Watchers
  • 0 Stars
  • 8 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Database

Light weight ORM classes. It's designed to be well suited for unit testing where the database adapter can be mocked easily. Also provides just enough to do common tasks such as find/select/create/update/delete, and associations between tables (e.g. hasMany/ belongsTo). Can be run stand along or within a framework., (*1)

Installation

Install with composer:, (*2)

"martynbiz/database": "dev-master"

ORM

Models/User.php, (*3)

class User extends MartynBiz\Database\Table
{
    protected $table = 'users';
}

Script, (*4)

$adapter = new MartynBiz\Database\Adapter(array(
    'dsn' => '...',
    'user' => '...',
    'password' => '...',
));

$usersTable = Users::getInstance($adapter);

// return Row object
$user = $usersTable->find($id);

// select many rows by query (with options)
$where = 'age > 34';
$options = array(
    'limitStart' => 10,
    'limitMax' => 25,
);
$users = $usersTable->select($where, $options);

// insert new row
$values = array(
    'name' => 'Hugo',
    'age' => 54,
);
$users->create($values);

// update rows
$usersTable->update($values, $where, $options);

// delete rows
$usersTable->delete($where, $options);

Associations

class User extends Table
{
    protected $tableName = 'users';

    protected $hasMany = array(
        'transactions' => array(
            'table' => 'App\Model\Transaction', // class name for the hasMany rows
            'foreign_key' => 'user_id',
        )
    );
}

class Transaction extends Table
{
    protected $tableName = 'transactions';

    protected $belongsTo = array(
        'user' => array(
            'table' => 'App\Model\User', // class name for the belongsTo row
            'foreign_key' => 'user_id',
        )
    );
}

Database adapter

..., (*5)

Unit testing

Table

$adapterMock = $this->getMockBuilder('MartynBiz\Database\Adapter')
    ->disableOriginalConstructor()
    ->getMock();

$usersTable = new Users($adapterMock);

Adapter

If it was neccessary to extend the adapter class, the PDO object which is usually generated internally can be injected instead (upon which the database credentials will be ingored). This allows the extended class to be unit testing with a mock PDO object., (*6)

class MyCustomAdapter extands Adapter {

}

$PDOMock->expects( $this->once() )
    ->method('prepare')
    ->with($sql);
$PDOStatementMock->expects( $this->once() )
    ->method('execute')
    ->with($whereValues);
$PDOStatementMock->expects( $this->once() )
    ->method('fetchAll')
    ->will( $this->returnValue($mockExecuteResult) );

// create instance with mock
$adapter = new MyCustomAdapter(null, $PDOMock);

TODO, (*7)

  • cascade

The Versions

30/11 2014

dev-master

9999999-dev http://github.com/martynbiz/database

Collection of database classes to allow database operations and unit testing

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

database database adapter table gateway row gateway