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");
26 $this->table
->getQueryExecutor()->attach(new \
QueryLogger());
29 protected function tearDown() {
30 $this->conn
->exec(PQ_TEST_REFTABLE_DROP
);
31 $this->conn
->exec(PQ_TEST_TABLE_DROP
);
34 public function testSetRowsetPrototype() {
35 $prop = new \
ReflectionProperty("\\pq\\Gateway\\Table", "rowset");
36 $prop->setAccessible(true);
37 $this->assertEquals("\\pq\\Gateway\\Rowset", $prop->getValue($this->table
));
38 $this->table
->setRowsetPrototype(null);
39 $this->assertNull($prop->getValue($this->table
));
40 $rowset = new \pq\Gateway\
Rowset($this->table
);
41 $this->table
->setRowsetPrototype($rowset);
42 $this->assertSame($rowset, $prop->getValue($this->table
));
45 public function testGetConnection() {
46 $this->assertSame($this->conn
, $this->table
->getConnection());
49 public function testGetName() {
50 $this->assertSame("test", $this->table
->getName());
53 public function testFind() {
54 $rowset = $this->table
->find();
55 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
56 $rowset = $this->table
->find(array("id = " => 1));
57 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
58 $rowset = $this->table
->find(array("id = " => 0));
59 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
60 $rowset = $this->table
->find(array(array("id<" => 2), array("id>" => 2)));
61 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
64 public function testCreate() {
65 $rowset = $this->table
->create(array("id" => new \pq\Query\
Expr("DEFAULT")));
66 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
67 $this->assertCount(1, $rowset);
70 public function testUpdate() {
71 $row = $this->table
->create(array())->current();
73 "created" => "2013-03-03 03:03:03",
76 "data" => "this is a test",
78 $row = $this->table
->update(array("id = " => $row->id
), $data)->current();
79 $data = array("id" => $row->id
->get()) +
$data;
80 $this->assertSame(array_map(function($v){return strval($v);}, $data), $row->getData());
83 public function testDelete() {
84 $this->table
->delete(array("id!=" => 0));
85 $this->assertCount(0, $this->table
->find());
88 public function testWith() {
89 $relation = $this->table
->getRelations("test")->reftest
;
90 $rowset = $this->table
->with([$relation], array("another_test_id=" => 2));
91 $this->assertCount(1, $rowset);
92 $this->assertEquals(array(
94 "created" => date_create("today")->format("Y-m-d H:i:s"),
98 ), $rowset->current()->getData());