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_SETUP_SQL
);
22 Table
::$defaultConnection = $this->conn
;
23 $this->table
= new Table("test");
24 $this->table
->getQueryExecutor()->attach(new \
QueryLogger());
27 protected function tearDown() {
28 $this->conn
->exec(PQ_TEST_TEARDOWN_SQL
);
31 public function testSetRowsetPrototype() {
32 $prop = new \
ReflectionProperty("\\pq\\Gateway\\Table", "rowset");
33 $prop->setAccessible(true);
34 $this->assertEquals("\\pq\\Gateway\\Rowset", $prop->getValue($this->table
));
35 $this->table
->setRowsetPrototype(null);
36 $this->assertNull($prop->getValue($this->table
));
37 $rowset = new \pq\Gateway\
Rowset($this->table
);
38 $this->table
->setRowsetPrototype($rowset);
39 $this->assertSame($rowset, $prop->getValue($this->table
));
42 public function testGetConnection() {
43 $this->assertSame($this->conn
, $this->table
->getConnection());
46 public function testGetName() {
47 $this->assertSame("test", $this->table
->getName());
50 public function testFind() {
51 $rowset = $this->table
->find();
52 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
53 $rowset = $this->table
->find(array("id = " => 1));
54 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
55 $rowset = $this->table
->find(array("id = " => 0));
56 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
57 $rowset = $this->table
->find(array(array("id<" => 2), array("id>" => 2)));
58 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
61 public function testCreate() {
62 $rowset = $this->table
->create(array("id" => new \pq\Query\
Expr("DEFAULT")));
63 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
64 $this->assertCount(1, $rowset);
67 public function testUpdate() {
68 $row = $this->table
->create(array())->current();
70 "created" => new \pq\
DateTime("2013-03-03 03:03:03"),
73 "data" => "this is a test",
74 "list" => array(3,2,1),
77 $row = $this->table
->update(array("id = " => $row->id
), $data)->current();
78 $data = array("id" => $row->id
->get()) +
$data;
79 $this->assertEquals($data, $row->getData());
82 public function testDelete() {
83 $this->table
->delete(array("id!=" => 0));
84 $this->assertCount(0, $this->table
->find());
87 public function testWith() {
88 $rowset = $this->table
->with(["reftest"], array("another_test_id=" => 2));
89 $this->assertCount(1, $rowset);
90 $this->assertEquals(array(
92 "created" => new \pq\
DateTime("today"),
96 "list" => array(0,1,2),
98 ), $rowset->current()->getData());