This repository has been archived by the owner on May 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test scripts for database unit tests
- Loading branch information
Showing
5 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |