5 include_once __DIR__
."/../../../setup.inc";
7 class TableTest
extends \PHPUnit_Framework_TestCase
{
19 protected function setUp() {
20 $this->conn
= new \pq\
Connection(PQ_TEST_DSN
);
21 $this->conn
->exec(PQ_TEST_TABLE_CREATE
);
22 $this->conn
->exec(PQ_TEST_REFTABLE_CREATE
);
23 $this->conn
->exec(PQ_TEST_DATA
);
24 Table
::$defaultConnection = $this->conn
;
25 $this->table
= new Table("test");
28 protected function tearDown() {
29 $this->conn
->exec(PQ_TEST_REFTABLE_DROP
);
30 $this->conn
->exec(PQ_TEST_TABLE_DROP
);
33 public function testSetRowsetPrototype() {
34 $prop = new \
ReflectionProperty("\\pq\\Gateway\\Table", "rowset");
35 $prop->setAccessible(true);
36 $this->assertEquals("\\pq\\Gateway\\Rowset", $prop->getValue($this->table
));
37 $this->table
->setRowsetPrototype(null);
38 $this->assertNull($prop->getValue($this->table
));
39 $rowset = new \pq\Gateway\
Rowset($this->table
);
40 $this->table
->setRowsetPrototype($rowset);
41 $this->assertSame($rowset, $prop->getValue($this->table
));
44 public function testGetConnection() {
45 $this->assertSame($this->conn
, $this->table
->getConnection());
48 public function testGetName() {
49 $this->assertSame("test", $this->table
->getName());
52 public function testFind() {
53 $rowset = $this->table
->find();
54 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
55 $rowset = $this->table
->find(array("id = " => 1));
56 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
57 $rowset = $this->table
->find(array("id = " => 0));
58 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
59 $rowset = $this->table
->find(array(array("id<" => 2), array("id>" => 2)));
60 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
63 public function testCreate() {
64 $rowset = $this->table
->create(array("id" => new \pq\Query\
Expr("DEFAULT")));
65 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
66 $this->assertCount(1, $rowset);
69 public function testUpdate() {
70 $row = $this->table
->create(array())->current();
72 "created" => "2013-03-03 03:03:03",
75 "data" => "this is a test",
77 $row = $this->table
->update(array("id = " => $row->id
), $data)->current();
78 $data = array("id" => $row->id
->get()) +
$data;
79 $this->assertSame(array_map(function($v){return strval($v);}, $data), $row->getData());
82 public function testDelete() {
83 $this->table
->delete(array("id!=" => 0));
84 $this->assertCount(0, $this->table
->find());