From: Michael Wallner Date: Wed, 6 Mar 2013 17:30:39 +0000 (+0100) Subject: tests X-Git-Tag: v1.0.0~15 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=e2709f3f6de6d5c5ba272d353db16b015b5258b3;p=m6w6%2Fpq-gateway tests --- diff --git a/lib/pq/Gateway/Cell.php b/lib/pq/Gateway/Cell.php index f29fd1f..b284b75 100644 --- a/lib/pq/Gateway/Cell.php +++ b/lib/pq/Gateway/Cell.php @@ -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("%s %s"), isset($op) ? $op : "||", $data); + $this->data->add(new Expr("%s %s", isset($op) ? $op : "||", $data)); } $this->dirty = true; diff --git a/lib/pq/Gateway/Table.php b/lib/pq/Gateway/Table.php index 805ed79..ba8094a 100644 --- a/lib/pq/Gateway/Table.php +++ b/lib/pq/Gateway/Table.php @@ -143,7 +143,6 @@ class Table 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; diff --git a/lib/pq/Query/Expr.php b/lib/pq/Query/Expr.php index ae87c1f..0f3d86e 100644 --- a/lib/pq/Query/Expr.php +++ b/lib/pq/Query/Expr.php @@ -18,7 +18,7 @@ class Expr * @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()); } @@ -30,7 +30,11 @@ class Expr * @return string */ function __toString() { - return (string) $this->expression . $this->next; + $string = $this->expression; + if ($this->next) { + $string .= " " . $this->next; + } + return (string) $string; } /** diff --git a/lib/pq/Query/Writer.php b/lib/pq/Query/Writer.php index 55cb5a2..f326f1e 100644 --- a/lib/pq/Query/Writer.php +++ b/lib/pq/Query/Writer.php @@ -139,6 +139,6 @@ class Writer * @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 index 0000000..0afd0e3 --- /dev/null +++ b/tests/lib/pq/Gateway/CellTest.php @@ -0,0 +1,53 @@ +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); + } + } +} diff --git a/tests/lib/pq/Gateway/RowsetTest.php b/tests/lib/pq/Gateway/RowsetTest.php index c0ff003..7ef1a0e 100644 --- a/tests/lib/pq/Gateway/RowsetTest.php +++ b/tests/lib/pq/Gateway/RowsetTest.php @@ -70,6 +70,13 @@ class RowsetTest extends \PHPUnit_Framework_TestCase { $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(); @@ -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 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"}'