tests
authorMichael Wallner <mike@php.net>
Wed, 6 Mar 2013 17:30:39 +0000 (18:30 +0100)
committerMichael Wallner <mike@php.net>
Wed, 6 Mar 2013 17:30:39 +0000 (18:30 +0100)
lib/pq/Gateway/Cell.php
lib/pq/Gateway/Table.php
lib/pq/Query/Expr.php
lib/pq/Query/Writer.php
tests/lib/pq/Gateway/CellTest.php [new file with mode: 0644]
tests/lib/pq/Gateway/RowsetTest.php

index f29fd1f5499cdf4f0ed2042378683d2d3eed880a..b284b75a7d98b7205f70bf3c54f1fc3487e7cf76 100644 (file)
@@ -88,7 +88,7 @@ class Cell
                        $this->data->add(new Expr("+ $data"));
                } else {
                        $data = $this->row->getTable()->getConnection()->quote($data);
                        $this->data->add(new Expr("+ $data"));
                } else {
                        $data = $this->row->getTable()->getConnection()->quote($data);
-                       $this->data->add(new Expr("%s %s"), isset($op) ? $op : "||", $data);
+                       $this->data->add(new Expr("%s %s", isset($op) ? $op : "||", $data));
                }
                
                $this->dirty = true;
                }
                
                $this->dirty = true;
index 805ed79353a0926461cc86ce53030e369676f5e3..ba8094ab0fba87d766b55120df7282f87ac24a46 100644 (file)
@@ -143,7 +143,6 @@ class Table
        function update(array $where, array $data, $returning = "*") {
                $query = new QueryWriter("UPDATE ".$this->conn->quoteName($this->name));
                $first = true;
        function update(array $where, array $data, $returning = "*") {
                $query = new QueryWriter("UPDATE ".$this->conn->quoteName($this->name));
                $first = true;
-               $params = array();
                foreach ($data as $key => $val) {
                        $query->write($first ? "SET" : ",", $key, "=", $query->param($val));
                        $first and $first = false;
                foreach ($data as $key => $val) {
                        $query->write($first ? "SET" : ",", $key, "=", $query->param($val));
                        $first and $first = false;
index ae87c1f92abe4b4bdd4d20f1cc6d5c2d9f9b6ce0..0f3d86e52ba1db3ae3cfb99ca9391e7f3e2396e1 100644 (file)
@@ -18,7 +18,7 @@ class Expr
         * @param string $e the expression or a format string followed by arguments
         * @param string ...
         */
         * @param string $e the expression or a format string followed by arguments
         * @param string ...
         */
-       function __construct($e) {
+       function __construct($e, $arg = null) {
                if (func_num_args() > 1) {
                        $e = call_user_func_array("sprintf", func_get_args());
                }
                if (func_num_args() > 1) {
                        $e = call_user_func_array("sprintf", func_get_args());
                }
@@ -30,7 +30,11 @@ class Expr
         * @return string
         */
        function __toString() {
         * @return string
         */
        function __toString() {
-               return (string) $this->expression . $this->next;
+               $string = $this->expression;
+               if ($this->next) {
+                       $string .= " " . $this->next;
+               }
+               return (string) $string;
        }
        
        /**
        }
        
        /**
index 55cb5a2089cfc1a5102b4465fe2b24205bd01dcd..f326f1ebc86a4e3d4d6808e04f4da3a08dcff66b 100644 (file)
@@ -139,6 +139,6 @@ class Writer
         * @return \pq\Result
         */
        function exec(\pq\Connection $c) {
         * @return \pq\Result
         */
        function exec(\pq\Connection $c) {
-               return $c->execParams($this, $this->params, $this->types);
+               return $c->execParams($this, $this->getParams(), $this->getTypes());
        }
 }
        }
 }
diff --git a/tests/lib/pq/Gateway/CellTest.php b/tests/lib/pq/Gateway/CellTest.php
new file mode 100644 (file)
index 0000000..0afd0e3
--- /dev/null
@@ -0,0 +1,53 @@
+<?php
+
+namespace pq\Gateway;
+
+include __DIR__."/../../../setup.inc";
+
+class CellTest extends \PHPUnit_Framework_TestCase {
+
+       /**
+        * @var \pq\Connection
+        */
+       protected $conn;
+       
+       /**
+        * @var \pq\Gateway\Table
+        */
+       protected $table;
+
+       protected function setUp() {
+               $this->conn = new \pq\Connection(PQ_DSN);
+               $this->conn->exec(PQ_TEST_DROP_TABLE);
+               $this->conn->exec(PQ_TEST_CREATE_TABLE);
+               $this->conn->exec(PQ_TEST_CREATE_DATA);
+               $this->table = new Table(PQ_TEST_TABLE_NAME, $this->conn);
+       }
+
+       protected function tearDown() {
+               $this->conn->exec(PQ_TEST_DROP_TABLE);
+       }
+
+       /**
+        * This is very bad test…
+        */
+       public function testBasic() {
+               $row = $this->table->find(null, "id desc", 1)->current();
+               foreach ($row->getData() as $key => $val) {
+                       $this->assertEquals($val, (string) $row->$key);
+                       $this->assertFalse($row->$key->isExpr());
+                       $this->assertFalse($row->$key->isDirty());
+                       $this->assertSame($val, $row->$key->get());
+                       $row->$key->mod(123);
+                       $this->assertNotEquals($val, (string) $row->$key);
+                       $this->assertTrue($row->$key->isExpr());
+                       $this->assertTrue($row->$key->isDirty());
+                       $this->assertNotSame($val, $row->$key->get());
+                       $this->assertEquals("$key + 123", (string) $row->$key->get());
+                       $row->$key->mod("foobar");
+                       $this->assertEquals("$key + 123 || 'foobar'", (string) $row->$key);
+                       $row->$key->mod(new \pq\Query\Expr(" - %s()", "now"));
+                       $this->assertEquals("$key + 123 || 'foobar' - now()", (string) $row->$key);
+               }
+       }
+}
index c0ff003b204df80f8bd613d3946c077d1fd2b259..7ef1a0eb21721a374ba36b41c59108197b08b387 100644 (file)
@@ -70,6 +70,13 @@ class RowsetTest extends \PHPUnit_Framework_TestCase {
                $this->assertCount(1, $rowset);
                $this->assertCount(4, $this->table->find());
        }
                $this->assertCount(1, $rowset);
                $this->assertCount(4, $this->table->find());
        }
+       
+       public function testCreateFail() {
+               $this->setExpectedException("\\pq\\Exception");
+               $rowset = new Rowset($this->table);
+               $rowset->append(new Row($this->table, array("foo" => "bar"), true));
+               $rowset->create();
+       }
 
        public function testUpdate() {
                $rowset = $this->table->find();
 
        public function testUpdate() {
                $rowset = $this->table->find();
@@ -83,11 +90,27 @@ class RowsetTest extends \PHPUnit_Framework_TestCase {
                });
        }
 
                });
        }
 
+       public function testUpdateFail() {
+               $this->setExpectedException("pq\\Exception");
+               $rowset = $this->table->find();
+               $rowset->apply(function($row) {
+                       $row->data = new \pq\Query\Expr("die");
+               });
+               $rowset->update();
+               
+       }
+
        public function testDelete() {
                $this->table->find()->delete();
                $this->assertCount(0, $this->table->find());
        }
 
        public function testDelete() {
                $this->table->find()->delete();
                $this->assertCount(0, $this->table->find());
        }
 
+       public function testDeleteFail() {
+               $this->setExpectedException("pq\\Exception");
+               $rowset = new Rowset($this->table);
+               $rowset->append(new Row($this->table, array("xx" => 0)))->delete();
+       }
+
        public function testJsonSerialize() {
                $json = sprintf('[{"id":"1","created":"%s","counter":"-1","number":"-1.1","data":"yesterday"}'
                        .',{"id":"2","created":"%s","counter":"0","number":"0","data":"today"}'
        public function testJsonSerialize() {
                $json = sprintf('[{"id":"1","created":"%s","counter":"-1","number":"-1.1","data":"yesterday"}'
                        .',{"id":"2","created":"%s","counter":"0","number":"0","data":"today"}'