From: Michael Wallner Date: Tue, 5 Mar 2013 22:51:14 +0000 (+0100) Subject: tests X-Git-Tag: v1.0.0~16 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=4879955d1b86d606dc24401f26ebde9be7612fbf;p=m6w6%2Fpq-gateway tests --- diff --git a/lib/pq/Gateway/Row.php b/lib/pq/Gateway/Row.php index 5c0efa4..a8b8793 100644 --- a/lib/pq/Gateway/Row.php +++ b/lib/pq/Gateway/Row.php @@ -79,6 +79,12 @@ class Row implements \JsonSerializable return false; } + function refresh() { + $this->data = $this->table->find($this->criteria(), null, 1, 0)->current()->data; + $this->cell = array(); + return $this; + } + /** * Fill modified cells * @return \pq\Gateway\Row diff --git a/lib/pq/Gateway/Rowset.php b/lib/pq/Gateway/Rowset.php index 2b03993..d6134ca 100644 --- a/lib/pq/Gateway/Rowset.php +++ b/lib/pq/Gateway/Rowset.php @@ -89,24 +89,75 @@ class Rowset implements \SeekableIterator, \Countable, \JsonSerializable return $this->table; } - function create() { - array_map(function ($row) { - $row->create(); - }, $this->rows); + /** + * Create all rows of this rowset + * @param bool $txn + * @return \pq\Gateway\Rowset + * @throws Exception + */ + function create($txn = true) { + $txn = $txn ? $this->table->getConnection()->startTransaction() : false; + try { + foreach ($this->rows as $row) { + $row->create(); + } + } catch (\Exception $e) { + if ($txn) { + $txn->rollback(); + } + throw $e; + } + if ($txn) { + $txn->commit(); + } return $this; } - function update() { - array_map(function ($row) { - $row->update(); - }, $this->rows); + /** + * Update all rows of this rowset + * @param bool $txn + * @return \pq\Gateway\Rowset + * @throws \Exception + */ + function update($txn = true) { + $txn = $txn ? $this->table->getConnection()->startTransaction() : false; + try { + foreach ($this->rows as $row) { + $row->update(); + } + } catch (\Exception $e) { + if ($txn) { + $txn->rollback(); + } + throw $e; + } + if ($txn) { + $txn->commit(); + } return $this; } - function delete() { - array_map(function ($row) { - $row->delete(); - }, $this->rows); + /** + * Delete all rows of this rowset + * @param type $txn + * @return \pq\Gateway\Rowset + * @throws \Exception + */ + function delete($txn = true) { + $txn = $txn ? $this->table->getConnection()->startTransaction() : false; + try { + foreach ($this->rows as $row) { + $row->delete(); + } + } catch (\Exception $e) { + if ($txn) { + $txn->rollback(); + } + throw $e; + } + if ($txn) { + $txn->commit(); + } return $this; } @@ -164,6 +215,8 @@ class Rowset implements \SeekableIterator, \Countable, \JsonSerializable if (!$this->valid()) { throw new \OutOfBoundsException("Invalid seek position ($pos)"); } + + return $this; } /** @@ -182,6 +235,16 @@ class Rowset implements \SeekableIterator, \Countable, \JsonSerializable return $this->rows; } + /** + * Apply a callback on each row of this rowset + * @param callable $cb + * @return \pq\Gateway\Rowset + */ + function apply(callable $cb) { + array_walk($this->rows, $cb, $this); + return $this; + } + /** * Filter by callback * @param callable $cb @@ -192,4 +255,13 @@ class Rowset implements \SeekableIterator, \Countable, \JsonSerializable $rowset->rows = array_filter($this->rows, $cb); return $rowset; } + + /** + * Append a row to the rowset + * @param \pq\Gateway\Row $row + */ + function append(Row $row) { + $this->rows[] = $row; + return $this; + } } diff --git a/tests/lib/pq/Gateway/RowTest.php b/tests/lib/pq/Gateway/RowTest.php new file mode 100644 index 0000000..28d2728 --- /dev/null +++ b/tests/lib/pq/Gateway/RowTest.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); + } + + function testBasic() { + $row = new Row($this->table, array("id" => 3), true); + $this->assertTrue($row->isDirty()); + $row->refresh(); + $this->assertSame( + array( + "id" => "3", + "created" => date("Y-m-d H:i:s", strtotime("tomorrow")), + "counter" => "1", + "number" => "1.1", + "data" => "tomorrow" + ), + $row->getData() + ); + $this->assertFalse($row->isDirty()); + } + + function testGetTable() { + $row = new Row($this->table); + $this->assertSame($this->table, $row->getTable()); + } +} diff --git a/tests/lib/pq/Gateway/RowsetTest.php b/tests/lib/pq/Gateway/RowsetTest.php new file mode 100644 index 0000000..c0ff003 --- /dev/null +++ b/tests/lib/pq/Gateway/RowsetTest.php @@ -0,0 +1,136 @@ +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); + } + + public function test__invoke() { + $rowset = $this->table->find(); + $this->table->setRowsetPrototype(null); + $result = $this->table->find(); + $rowset2 = $rowset($result); + $this->assertEquals($rowset, $rowset2); + } + + public function testSetRowPrototype() { + $prop = new \ReflectionProperty("\\pq\\Gateway\\Rowset", "row"); + $prop->setAccessible(true); + $prototype = new Rowset($this->table); + $this->assertEquals("\\pq\\Gateway\\Row", $prop->getValue($prototype)); + $prototype->setRowPrototype(null); + $this->assertNull($prop->getValue($prototype)); + $this->table->setRowsetPrototype($prototype); + $rowset = $this->table->find(); + foreach ($rowset as $row) { + $this->assertInstanceOf("stdClass", $row); + $this->assertObjectHasAttribute("id", $row); + } + $prototype->setRowPrototype(new Row($this->table)); + $rowset = $this->table->find(); + foreach ($rowset as $index => $row) { + $this->assertInstanceOf("\\pq\\Gateway\\Row", $row); + $this->assertEquals($index+1, $row->id->get()); + } + } + + public function testGetTable() { + $rowset = new Rowset($this->table); + $this->assertSame($this->table, $rowset->getTable()); + } + + public function testCreate() { + $rowset = new Rowset($this->table); + $rowset->append(new Row($this->table)); + $rowset->create(); + $this->assertCount(1, $rowset); + $this->assertCount(4, $this->table->find()); + } + + public function testUpdate() { + $rowset = $this->table->find(); + $rowset->apply(function($row) { + $row->data = "updated"; + }); + $rowset->update(); + $rowset = $this->table->find(); + $rowset->apply(function($row) { + $this->assertSame("updated", $row->data->get()); + }); + } + + public function testDelete() { + $this->table->find()->delete(); + $this->assertCount(0, $this->table->find()); + } + + 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"}' + .',{"id":"3","created":"%s","counter":"1","number":"1.1","data":"tomorrow"}]', + date("Y-m-d H:i:s", strtotime("yesterday")), + date("Y-m-d H:i:s", strtotime("today")), + date("Y-m-d H:i:s", strtotime("tomorrow")) + ); + $this->assertJsonStringEqualsJsonString($json, json_encode($this->table->find())); + } + + public function testIterator() { + $counter = 0; + foreach ($this->table->find() as $index => $row) { + $this->assertSame($counter++, $index); + $this->assertInstanceOf("\\pq\\Gateway\\Row", $row); + } + } + + public function testSeekEx() { + $this->setExpectedException("\\OutOfBoundsException", "Invalid seek position (3)"); + $this->table->find()->seek(3); + } + + public function testSeek() { + $rowset = $this->table->find(); + for ($i = count($rowset); $i > 0; --$i) { + $this->assertEquals($i, $rowset->seek($i-1)->current()->id->get()); + } + } + + public function testCount() { + $this->assertCount(3, $this->table->find()); + } + + public function testGetRows() { + $rowset = $this->table->find(); + $rows = $rowset->getRows(); + $rowset2 = $rowset->filter(function($row) { return true; }); + $this->assertEquals($rows, $rowset2->getRows()); + $rowset3 = $rowset->filter(function($row) { return false; }); + $this->assertCount(0, $rowset3); + $this->assertSame(array(), $rowset3->getRows()); + $this->assertCount(1, $rowset->filter(function($row) { return $row->id->get() == 1; })); + } +} diff --git a/tests/lib/pq/Gateway/TableTest.php b/tests/lib/pq/Gateway/TableTest.php index bee7bdd..65c93f7 100644 --- a/tests/lib/pq/Gateway/TableTest.php +++ b/tests/lib/pq/Gateway/TableTest.php @@ -2,11 +2,8 @@ namespace pq\Gateway; -include __DIR__."/../../../setup.inc"; +include_once __DIR__."/../../../setup.inc"; -/** - * Generated by PHPUnit_SkeletonGenerator 1.2.0 on 2013-03-05 at 16:08:03. - */ class TableTest extends \PHPUnit_Framework_TestCase { /** @@ -17,108 +14,75 @@ class TableTest extends \PHPUnit_Framework_TestCase { /** * @var Table */ - protected $object; + protected $table; - /** - * Sets up the fixture, for example, opens a network connection. - * This method is called before a test is executed. - */ protected function setUp() { $this->conn = new \pq\Connection(PQ_DSN); $this->conn->exec(PQ_TEST_DROP_TABLE); $this->conn->exec(PQ_TEST_CREATE_TABLE); Table::$defaultConnection = $this->conn; - $this->object = new Table(PQ_TEST_TABLE_NAME); + $this->table = new Table(PQ_TEST_TABLE_NAME); } - /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. - */ protected function tearDown() { $this->conn->exec(PQ_TEST_DROP_TABLE); } - /** - * Creates test data in the test table - */ protected function createTestData() { $this->conn->exec(PQ_TEST_CREATE_DATA); } - /** - * @covers pq\Gateway\Table::setRowsetPrototype - */ public function testSetRowsetPrototype() { $prop = new \ReflectionProperty("\\pq\\Gateway\\Table", "rowset"); $prop->setAccessible(true); - $this->assertEquals("\\pq\\Gateway\\Rowset", $prop->getValue($this->object)); - $this->object->setRowsetPrototype(null); - $this->assertNull($prop->getValue($this->object)); - $rowset = new \pq\Gateway\Rowset($this->object); - $this->object->setRowsetPrototype($rowset); - $this->assertSame($rowset, $prop->getValue($this->object)); + $this->assertEquals("\\pq\\Gateway\\Rowset", $prop->getValue($this->table)); + $this->table->setRowsetPrototype(null); + $this->assertNull($prop->getValue($this->table)); + $rowset = new \pq\Gateway\Rowset($this->table); + $this->table->setRowsetPrototype($rowset); + $this->assertSame($rowset, $prop->getValue($this->table)); } - /** - * @covers pq\Gateway\Table::getConnection - */ public function testGetConnection() { - $this->assertSame($this->conn, $this->object->getConnection()); + $this->assertSame($this->conn, $this->table->getConnection()); } - /** - * @covers pq\Gateway\Table::getName - */ public function testGetName() { - $this->assertSame(PQ_TEST_TABLE_NAME, $this->object->getName()); + $this->assertSame(PQ_TEST_TABLE_NAME, $this->table->getName()); } - /** - * @covers pq\Gateway\Table::find - */ public function testFind() { - $rowset = $this->object->find(); + $rowset = $this->table->find(); $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset); - $rowset = $this->object->find(array("id = " => 1)); + $rowset = $this->table->find(array("id = " => 1)); $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset); - $rowset = $this->object->find(array("id = " => 0)); + $rowset = $this->table->find(array("id = " => 0)); $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset); - $rowset = $this->object->find(array(array("id<" => 2), array("id>" => 2))); + $rowset = $this->table->find(array(array("id<" => 2), array("id>" => 2))); $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset); } - /** - * @covers pq\Gateway\Table::create - */ public function testCreate() { - $rowset = $this->object->create(array("id" => new \pq\Query\Expr("DEFAULT"))); + $rowset = $this->table->create(array("id" => new \pq\Query\Expr("DEFAULT"))); $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset); $this->assertCount(1, $rowset); } - /** - * @covers pq\Gateway\Table::update - */ public function testUpdate() { - $row = $this->object->create(array())->current(); + $row = $this->table->create(array())->current(); $data = array( "created" => "2013-03-03 03:03:03", "counter" => 2, "number" => 2.2, "data" => "this is a test", ); - $row = $this->object->update(array("id = " => $row->id), $data)->current(); + $row = $this->table->update(array("id = " => $row->id), $data)->current(); $data = array("id" => $row->id->get()) + $data; $this->assertSame(array_map(function($v){return strval($v);}, $data), $row->getData()); } - /** - * @covers pq\Gateway\Table::delete - */ public function testDelete() { - $this->object->delete(array("id!=" => 0)); - $this->assertCount(0, $this->object->find()); + $this->table->delete(array("id!=" => 0)); + $this->assertCount(0, $this->table->find()); } - }