PHP code example of hi-media / pdo-tools

1. Go to this page and download the library: Download hi-media/pdo-tools 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/ */

    

hi-media / pdo-tools example snippets


    

    

class MyTestCase extends DbTestCase
{
    public function __construct($sName = null, array $aData = array(), $sDataName = '')
    {
        // BUILD_NUMBER environment variable is handled by Jenkins:
        $iSuffix = isset($_SERVER['BUILD_NUMBER']) ? $_SERVER['BUILD_NUMBER'] : floor(microtime(true));
        $sTestDbName = "tests_$iSuffix";
        $aDbBuilderDsn = array(
            'driver'   => 'pgsql',
            'hostname' => 'localhost',
            'port'     => 5432,
            'dbname'   => $sTestDbName,
            'username' => 'user',
            'password' => ''
        );
        
        $sDbBuildFile = '/path/to/buildfile.php';
        parent::__construct($aDbBuilderDsn, array(), $sDbBuildFile);
    }
}

public function testSimple ()
{
    // Load SQL dump file, possibly gzipped (.gz):
    $this->loadSqlDumpFile('/path/to/dump.sql');

    // calls to tested program…

    // Asserts that SQL query result is equal to CSV file content:
    $this->assertQueryResultEqualsCsv('SELECT … FROM A', '/path/to/expected.csv');
    
    // Asserts that SQL query doesn't return any rows:
    $this->assertQueryReturnsNoRows('SELECT * FROM B');
    
    // Optional clean up:
    $this->loadSqlDumpFile('/path/to/clean_up.sql');
}

public function testWithMockDb ()
{
    /* @var $oMockPDO \Himedia\DW\Tests\Mocks\PDO|\PHPUnit_Framework_MockObject_MockObject */
    $oMockPDO = $this->getMock('Himedia\PDOTools\Mocks\PDO', array('query'));
    $that = $this;
    $oMockPDO->expects($this->any())->method('query')->will(
        $this->returnCallback(
            function ($sQuery) use ($that, $sResourcePath) {
                return $that->getPdoStmtMock(
                    $sQuery,
                    array(
                        'SELECT … FROM A' => '/path/to/fixture.csv',
                        'SELECT … FROM B' => function () {
                            static $i = 0;
                            return ++$i > 10 ? false : array('id' => $i, 'name' => md5(rand()));
                        }
                    )
                );
            }
        )
    );
    
    // injection of $oMockPDO, to mock third-party database…
    
    // calls to tested program…
    
    // assertions…
}
bash
    $ curl -sS https://getcomposer.org/installer | php