Base class for testing database with PDO. Tested only against MySql.
Work in progress
Code information:
Package information:
In your bootstrap file set up the connection
// tests/bootstrap.php
// [...]
\Koine\PHPUnit\DbTestCase::setConnection($pdoConnection);
namespace MyAppTest;
use Koine\PHPUnit\DbTestCase;
use MyApp\BlogService;
/**
* @author Marcelo Jacobus <[email protected]>
*/
class DbTestCaseTest extends DbTestCase
{
public function setUp()
{
parent::setUp(); // enclose everything in a transaction
}
public function tearDown()
{
parent::tearDown(); // rollback the transaction
}
/**
* @test
*/
public function canCreatePost()
{
$service = new BlogService($this->getConnection());
$service->create(array(
'title' => 'some title',
'body' => 'some body',
));
$this->assertTableCount(1, 'blog_post');
}
/**
* @test
*/
public function canFindByCategory()
{
$helper = $this->createTableHelper('blog_post');
$helper->insert(array(
'title' => 'foo',
'body' => 'bar',
'categoryId' => 1,
));
$helper->insert(array(
'title' => 'foo',
'body' => 'bar',
'categoryId' => 2,
));
$service = new BlogService($this->getConnection());
$records = $service->findByCategoryId(1);
$this->assertEquals(1, count($records));
}
}
Table helper is a very simple ORM for creating records for test, updating and querying a single table.
Setting up
$tableName = 'blog_post';
$tableHelper = new \Koine\DbTestCase\TableHelper\TableHelper(
$pdo,
$tableName,
'id'
);
Finding records by conditions
$posts = $tableHelper->findAllBy(array(
'categoryId' => $categoryId,
));
Finding record by id
$post = $tableHelper->find(10);
Crating records
$tableHelper->insert(array(
'title' => 'First blog',
'body' => 'Post body',
));
Updating
$tableHelper->update(10, array(
'title' => 'new title',
));
Deleting
$tableHelper->delete(10);
Counting
$tableHelper->getNumberOfRows();
Append the lib to your requirements key in your composer.json.
{
// composer.json
// [..]
require: {
// append this line to your requirements
"koine/db-test-case": "*"
}
}
- Learn composer. You should not be looking for an alternative install. It is worth the time. Trust me ;-)
- Follow this set of instructions
Here is the issue tracker.
Please refer to the contribuiting guide.