koine / db-test-case
Base class for testing database
1.0.1
2015-06-08 12:47 UTC
Requires
- php: >=5.3.3
Requires (Dev)
- phpunit/phpunit: *
- satooshi/php-coveralls: dev-master
This package is not auto-updated.
Last update: 2024-11-09 18:22:10 UTC
README
Base class for testing database with PDO. Tested only against MySql.
Work in progress
Code information:
Package information:
Usage
Db Test Case
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 <marcelo.jacobus@gmail.com> */ 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
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();
Installing
Installing Via Composer
Append the lib to your requirements key in your composer.json.
{ // composer.json // [..] require: { // append this line to your requirements "koine/db-test-case": "*" } }
Alternative install
- Learn composer. You should not be looking for an alternative install. It is worth the time. Trust me ;-)
- Follow this set of instructions
Issues/Features proposals
Here is the issue tracker.
Contributing
Please refer to the contribuiting guide.