summaryrefslogtreecommitdiff
path: root/tests/unit/Data/TableGateway
diff options
context:
space:
mode:
Diffstat (limited to 'tests/unit/Data/TableGateway')
-rw-r--r--tests/unit/Data/TableGateway/BaseGatewayTest.php96
-rw-r--r--tests/unit/Data/TableGateway/CountTest.php18
-rw-r--r--tests/unit/Data/TableGateway/MagicCallTest.php33
-rw-r--r--tests/unit/Data/TableGateway/TableGatewayDeleteByPkTest.php54
-rw-r--r--tests/unit/Data/TableGateway/TableGatewayPgsqlTest.php58
-rw-r--r--tests/unit/Data/TableGateway/TableInfoGatewayTest.php19
-rw-r--r--tests/unit/Data/TableGateway/TestFindByPk.php50
7 files changed, 328 insertions, 0 deletions
diff --git a/tests/unit/Data/TableGateway/BaseGatewayTest.php b/tests/unit/Data/TableGateway/BaseGatewayTest.php
new file mode 100644
index 00000000..b97ef4a0
--- /dev/null
+++ b/tests/unit/Data/TableGateway/BaseGatewayTest.php
@@ -0,0 +1,96 @@
+<?php
+Prado::using('System.Data.*');
+Prado::using('System.Data.DataGateway.TTableGateway');
+
+/**
+ * @package System.Data.TableGateway
+ */
+class BaseGatewayTest extends PHPUnit_Framework_TestCase
+{
+ protected $gateway1;
+ protected $gateway2;
+
+ /**
+ * @return TTableGateway
+ */
+ function getGateway()
+ {
+ if($this->gateway1===null)
+ {
+ $conn = new TDbConnection('pgsql:host=localhost;dbname=test', 'test','test');
+ $this->gateway1 = new TTableGateway('address', $conn);
+ }
+ return $this->gateway1;
+ }
+
+ /**
+ * @return TTableGateway
+ */
+ function getGateway2()
+ {
+ if($this->gateway2===null)
+ {
+ $conn = new TDbConnection('pgsql:host=localhost;dbname=test', 'test','test');
+ $this->gateway2 = new TTableGateway('department_sections', $conn);
+ }
+ return $this->gateway2;
+ }
+
+ function setup()
+ {
+ $this->delete_all();
+ }
+
+ function add_record1()
+ {
+ $result = $this->getGateway()->insert($this->get_record1());
+ $this->assertTrue(intval($result) > 0);
+ }
+ function add_record2()
+ {
+ $result = $this->getGateway()->insert($this->get_record2());
+ $this->assertTrue(intval($result) > 0);
+ }
+ function get_record1()
+ {
+ return array(
+ 'username' => 'Username',
+ 'phone' => 121987,
+ 'field1_boolean' => true,
+ 'field2_date' => '2007-12-25',
+ 'field3_double' => 121.1,
+ 'field4_integer' => 3,
+ 'field5_text' => 'asdasd',
+ 'field6_time' => '12:40:00',
+ 'field7_timestamp' => 'NOW',
+ 'field8_money' => '$121.12',
+ 'field9_numeric' => 98.2232,
+ 'int_fk1'=>1,
+ 'int_fk2'=>1,
+ );
+ }
+
+
+ function get_record2()
+ {
+ return array(
+ 'username' => 'record2',
+ 'phone' => 45233,
+ 'field1_boolean' => false,
+ 'field2_date' => '2004-10-05',
+ 'field3_double' => 1221.1,
+ 'field4_integer' => 2,
+ 'field5_text' => 'hello world',
+ 'field6_time' => '22:40:00',
+ 'field7_timestamp' => 'NOW',
+ 'field8_money' => '$1121.12',
+ 'field9_numeric' => 8.2213,
+ 'int_fk1'=>1,
+ 'int_fk2'=>1,
+ );
+ }
+ function delete_all()
+ {
+ $this->getGateway()->deleteAll('1=1');
+ }
+} \ No newline at end of file
diff --git a/tests/unit/Data/TableGateway/CountTest.php b/tests/unit/Data/TableGateway/CountTest.php
new file mode 100644
index 00000000..074d935b
--- /dev/null
+++ b/tests/unit/Data/TableGateway/CountTest.php
@@ -0,0 +1,18 @@
+<?php
+
+require_once(dirname(__FILE__).'/BaseGatewayTest.php');
+
+/**
+ * @package System.Data.TableGateway
+ */
+class CountTest extends BaseGatewayTest
+{
+ function test_simple_count()
+ {
+ $result = $this->getGateway2()->count();
+ $this->assertEqual(44,$result);
+
+ $result = $this->getGateway2()->count('department_id = ?', 1);
+ $this->assertEqual(4, $result);
+ }
+} \ No newline at end of file
diff --git a/tests/unit/Data/TableGateway/MagicCallTest.php b/tests/unit/Data/TableGateway/MagicCallTest.php
new file mode 100644
index 00000000..56cc85b3
--- /dev/null
+++ b/tests/unit/Data/TableGateway/MagicCallTest.php
@@ -0,0 +1,33 @@
+<?php
+
+require_once(dirname(__FILE__).'/BaseGatewayTest.php');
+
+/**
+ * @package System.Data.TableGateway
+ */
+class MagicCallTest extends BaseGatewayTest
+{
+ function test_magic_call()
+ {
+ $this->add_record1(); $this->add_record2();
+
+ $result = $this->getGateway()->findByUsername("record2");
+ $this->assertEqual($result['username'], 'record2');
+ }
+
+ function test_combined_and_or()
+ {
+ $this->add_record1(); $this->add_record2();
+
+ $result = $this->getGateway()->findAllByUsername_OR_phone('Username', '45233')->readAll();
+ $this->assertEqual(2, count($result));
+ }
+
+ function test_no_result()
+ {
+ $this->add_record1(); $this->add_record2();
+ $result = $this->getGateway()->findAllByUsername_and_phone('Username', '45233')->readAll();
+
+ $this->assertEqual(0, count($result));
+ }
+} \ No newline at end of file
diff --git a/tests/unit/Data/TableGateway/TableGatewayDeleteByPkTest.php b/tests/unit/Data/TableGateway/TableGatewayDeleteByPkTest.php
new file mode 100644
index 00000000..6bcaeb91
--- /dev/null
+++ b/tests/unit/Data/TableGateway/TableGatewayDeleteByPkTest.php
@@ -0,0 +1,54 @@
+<?php
+
+require_once(dirname(__FILE__).'/BaseGatewayTest.php');
+
+/**
+ * @package System.Data.TableGateway
+ */
+class TableGatewayDeleteByPkTest extends BaseGatewayTest
+{
+ function test_delete_by_1_pk()
+ {
+ $this->add_record1();
+ $id = $this->getGateway()->getLastInsertId();
+ $deleted = $this->getGateway()->deleteByPk($id);
+
+ $this->assertEqual(1, $deleted);
+ }
+
+ function test_delete_by_multiple_pk()
+ {
+ $this->add_record1();
+ $id1 = $this->getGateway()->getLastInsertId();
+ $this->add_record2();
+ $id2 = $this->getGateway()->getLastInsertId();
+
+ $deleted = $this->getGateway()->deleteByPk($id1, $id2);
+
+ $this->assertEqual(2, $deleted);
+ }
+
+ function test_delete_by_multiple_pk2()
+ {
+ $this->add_record1();
+ $id1 = $this->getGateway()->getLastInsertId();
+ $this->add_record2();
+ $id2 = $this->getGateway()->getLastInsertId();
+
+ $deleted = $this->getGateway()->deleteByPk(array($id1, $id2));
+
+ $this->assertEqual(2, $deleted);
+ }
+
+ function test_delete_by_multiple_pk3()
+ {
+ $this->add_record1();
+ $id1 = $this->getGateway()->getLastInsertId();
+ $this->add_record2();
+ $id2 = $this->getGateway()->getLastInsertId();
+
+ $deleted = $this->getGateway()->deleteByPk(array(array($id1), array($id2)));
+
+ $this->assertEqual(2, $deleted);
+ }
+} \ No newline at end of file
diff --git a/tests/unit/Data/TableGateway/TableGatewayPgsqlTest.php b/tests/unit/Data/TableGateway/TableGatewayPgsqlTest.php
new file mode 100644
index 00000000..234f70bd
--- /dev/null
+++ b/tests/unit/Data/TableGateway/TableGatewayPgsqlTest.php
@@ -0,0 +1,58 @@
+<?php
+require_once(dirname(__FILE__).'/BaseGatewayTest.php');
+
+/**
+ * @package System.Data.TableGateway
+ */
+class TableGatewayPgsqlTest extends BaseGatewayTest
+{
+
+ function test_update()
+ {
+ $this->add_record1();
+ $address = array('username' => 'tester 1', 'field5_text'=>null);
+ $result = $this->getGateway()->update($address, 'username = ?', 'Username');
+ $this->assertTrue($result);
+
+ $test = $this->getGateway()->find('username = ?', 'tester 1');
+ unset($test['id']);
+ $expect = $this->get_record1();
+ $expect['username'] = 'tester 1';
+ $expect['field5_text'] = null;
+ unset($expect['field7_timestamp']); unset($test['field7_timestamp']);
+ $this->assertEqual($expect, $test);
+
+ $this->assertTrue($this->getGateway()->deleteAll('username = ?', 'tester 1'));
+ }
+
+ function test_update_named()
+ {
+ $this->add_record1();
+ $address = array('username' => 'tester 1', 'field5_text'=>null);
+ $result = $this->getGateway()->update($address, 'username = :name', array(':name'=>'Username'));
+ $this->assertTrue($result);
+
+ $test = $this->getGateway()->find('username = :name', array(':name'=>'tester 1'));
+ unset($test['id']);
+ $expect = $this->get_record1();
+ $expect['username'] = 'tester 1';
+ $expect['field5_text'] = null;
+ unset($expect['field7_timestamp']); unset($test['field7_timestamp']);
+ $this->assertEqual($expect, $test);
+
+ $this->assertTrue($this->getGateway()->deleteAll('username = :name', array(':name'=>'tester 1')));
+ }
+
+ function test_find_all()
+ {
+ $this->add_record1();
+ $this->add_record2();
+
+ $results = $this->getGateway()->findAll('true')->readAll();
+ $this->assertEqual(count($results), 2);
+
+ $result = $this->getGateway()->findAllBySql('SELECT username FROM address WHERE phone = ?', '45233')->read();
+ $this->assertEqual($result['username'], 'record2');
+ }
+
+} \ No newline at end of file
diff --git a/tests/unit/Data/TableGateway/TableInfoGatewayTest.php b/tests/unit/Data/TableGateway/TableInfoGatewayTest.php
new file mode 100644
index 00000000..24b94690
--- /dev/null
+++ b/tests/unit/Data/TableGateway/TableInfoGatewayTest.php
@@ -0,0 +1,19 @@
+<?php
+
+require_once(dirname(__FILE__).'/BaseGatewayTest.php');
+
+/**
+ * @package System.Data.TableGateway
+ */
+class TableInfoGatewayTest extends BaseGatewayTest
+{
+ function test_table_info()
+ {
+ $conn = $this->getGateway()->getDbConnection();
+ $this->add_record1();
+ $this->add_record2();
+ $info = TDbMetaData::getInstance($conn)->getTableInfo('address');
+ $table = new TTableGateway($info, $conn);
+ $this->assertEqual(count($table->findAll()->readAll()), 2);
+ }
+} \ No newline at end of file
diff --git a/tests/unit/Data/TableGateway/TestFindByPk.php b/tests/unit/Data/TableGateway/TestFindByPk.php
new file mode 100644
index 00000000..f23fafa7
--- /dev/null
+++ b/tests/unit/Data/TableGateway/TestFindByPk.php
@@ -0,0 +1,50 @@
+<?php
+
+require_once(dirname(__FILE__).'/BaseGatewayTest.php');
+
+/**
+ * @package System.Data.TableGateway
+ */
+class TestFindByPk extends BaseGatewayTest
+{
+ function test_one_key()
+ {
+ $this->add_record1();
+ $id = $this->getGateway()->getLastInsertId();
+ $result = $this->getGateway()->findByPk($id);
+
+ $record1 = $this->get_record1();
+
+ //clean and ignore some fields
+ unset($result['id']);
+ unset($result['field7_timestamp']);
+ unset($record1['field7_timestamp']);
+ $result['phone'] = intval($result['phone']);
+ $result['field9_numeric'] = floatval($result['field9_numeric']);
+
+ $this->assertEqual($record1, $result);
+ }
+
+ function test_composite_key()
+ {
+ $gateway = $this->getGateway2();
+
+ $result = $gateway->findByPk(1,1);
+ $expect = array("department_id" => 1, "section_id" => 1, "order" => 0);
+ $this->assertEqual($expect, $result);
+ }
+
+ function test_find_all_keys()
+ {
+ $gateway = $this->getGateway2();
+
+ $result = $gateway->findAllByPks(array(1,1), array(3,13))->readAll();
+
+ $expect = array(
+ array("department_id" => 1, "section_id" => 1, "order" => 0),
+ array("department_id" => 3, "section_id" => 13, "order" => 0));
+
+ $this->assertEqual($expect, $result);
+
+ }
+} \ No newline at end of file