Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
Add test scripts for database unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
romaninsh committed Mar 19, 2016
1 parent 3e4628a commit 0790d23
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 0 deletions.
13 changes: 13 additions & 0 deletions phpunit-mysql.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<phpunit bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="dsql">
<directory>tests/db</directory>
</testsuite>
</testsuites>
<php>
<var name="DB_DSN" value="mysql:dbname=dsql_test;host=localhost" />
<var name="DB_USER" value="travis" />
<var name="DB_PASSWD" value="" />
<var name="DB_DBNAME" value="dsql_test" />
</php>
</phpunit>
13 changes: 13 additions & 0 deletions phpunit-sqlite.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<phpunit bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="dsql">
<directory>tests/db</directory>
</testsuite>
</testsuites>
<php>
<var name="DB_DSN" value="sqlite::memory:" />
<var name="DB_USER" value="travis" />
<var name="DB_PASSWD" value="" />
<var name="DB_DBNAME" value="dsql_test" />
</php>
</phpunit>
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<testsuites>
<testsuite name="dsql">
<directory>tests</directory>
<exclude>tests/db</exclude>
</testsuite>
</testsuites>
<logging>
Expand Down
76 changes: 76 additions & 0 deletions tests/db/SelectTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
namespace atk4\dsql\tests;
use atk4\dsql\Query;
use atk4\dsql\Expression;



class dbSelectTest extends \PHPUnit_Extensions_Database_TestCase
{
protected $pdo;
function __construct()
{
$this->pdo = new \PDO( $GLOBALS['DB_DSN'], $GLOBALS['DB_USER'], $GLOBALS['DB_PASSWD'] );
$this->pdo->query('create temporary table employee (id int, name text, surname text, retired bool)');
}
protected function getConnection()
{
return $this->createDefaultDBConnection($this->pdo, $GLOBALS['DB_DBNAME']);
}
protected function getDataSet()
{
return $this->createFlatXMLDataSet(dirname(__FILE__).'/SelectTest.xml');
}
private function q($table = null){
$q = new Query(['connection'=>$this->pdo]);

if ($table !== null) {
$q->table('employee');
}
return $q;
}
public function testBasicQueries()
{
$this->assertEquals(4, $this->getConnection()->getRowCount('employee'));

$this->assertEquals(
['name'=>'Oliver','surname'=>'Smith'],
$this->q('employee')->field('name,surname')->getRow()
);

$this->assertEquals(
['surname'=>'Taylor'],
$this->q('employee')->field('surname')->where('retired','1')->getRow()
);

$this->assertEquals(
4,
$this->q()->field(new Expression('2+2'))->getOne()
);

$this->assertEquals(
4,
$this->q('employee')->field(new Expression('count(*)'))->getOne()
);

$names = [];
foreach($this->q('employee')->where('retired',false) as $row){
$names[] = $row['name'];
}
$this->assertEquals(
['Oliver','Jack','Charlie'],
$names
);

$this->assertEquals(
[['now'=>4]],
$this->q()->field(new Expression('2+2'),'now')->get()
);

$this->assertEquals(
[['now'=>6]],
$this->q()->field(new Expression('[]+[]',[3,3]),'now')->get()
);
}
}

7 changes: 7 additions & 0 deletions tests/db/SelectTest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<employee name="Oliver" surname="Smith" retired="0"/>
<employee name="Jack" surname="Williams" retired="0"/>
<employee name="Harry" surname="Taylor" retired="1"/>
<employee name="Charlie" surname="Lee" retired="0"/>
</dataset>

0 comments on commit 0790d23

Please sign in to comment.