PHP code example of koine / db-test-case

1. Go to this page and download the library: Download koine/db-test-case 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/ */

    

koine / db-test-case example snippets


// 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));
    }
}

$tableName = 'blog_post';

$tableHelper = new \Koine\DbTestCase\TableHelper\TableHelper(
  $pdo,
  $tableName,
  'id'
);

$posts = $tableHelper->findAllBy(array(
  'categoryId' => $categoryId,
));

$post = $tableHelper->find(10);

$tableHelper->insert(array(
  'title' => 'First blog',
  'body'  => 'Post body',
));

$tableHelper->update(10, array(
  'title' => 'new title',
));

$tableHelper->delete(10);

$tableHelper->getNumberOfRows();