5 include_once __DIR__
."/../../../setup.inc";
7 class TableTest
extends \PHPUnit_Framework_TestCase
{
19 protected function setUp() {
20 $this->conn
= new \pq\
Connection(PQ_DSN
);
21 $this->conn
->exec(PQ_TEST_DROP_TABLE
);
22 $this->conn
->exec(PQ_TEST_CREATE_TABLE
);
23 Table
::$defaultConnection = $this->conn
;
24 $this->table
= new Table(PQ_TEST_TABLE_NAME
);
27 protected function tearDown() {
28 $this->conn
->exec(PQ_TEST_DROP_TABLE
);
31 protected function createTestData() {
32 $this->conn
->exec(PQ_TEST_CREATE_DATA
);
35 public function testSetRowsetPrototype() {
36 $prop = new \
ReflectionProperty("\\pq\\Gateway\\Table", "rowset");
37 $prop->setAccessible(true);
38 $this->assertEquals("\\pq\\Gateway\\Rowset", $prop->getValue($this->table
));
39 $this->table
->setRowsetPrototype(null);
40 $this->assertNull($prop->getValue($this->table
));
41 $rowset = new \pq\Gateway\
Rowset($this->table
);
42 $this->table
->setRowsetPrototype($rowset);
43 $this->assertSame($rowset, $prop->getValue($this->table
));
46 public function testGetConnection() {
47 $this->assertSame($this->conn
, $this->table
->getConnection());
50 public function testGetName() {
51 $this->assertSame(PQ_TEST_TABLE_NAME
, $this->table
->getName());
54 public function testFind() {
55 $rowset = $this->table
->find();
56 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
57 $rowset = $this->table
->find(array("id = " => 1));
58 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
59 $rowset = $this->table
->find(array("id = " => 0));
60 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
61 $rowset = $this->table
->find(array(array("id<" => 2), array("id>" => 2)));
62 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
65 public function testCreate() {
66 $rowset = $this->table
->create(array("id" => new \pq\Query\
Expr("DEFAULT")));
67 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
68 $this->assertCount(1, $rowset);
71 public function testUpdate() {
72 $row = $this->table
->create(array())->current();
74 "created" => "2013-03-03 03:03:03",
77 "data" => "this is a test",
79 $row = $this->table
->update(array("id = " => $row->id
), $data)->current();
80 $data = array("id" => $row->id
->get()) +
$data;
81 $this->assertSame(array_map(function($v){return strval($v);}, $data), $row->getData());
84 public function testDelete() {
85 $this->table
->delete(array("id!=" => 0));
86 $this->assertCount(0, $this->table
->find());