9235a39ec81487278d4ecb79494384505f33088f
[m6w6/pq-gateway] / tests / lib / pq / Gateway / CellTest.php
1 <?php
2
3 namespace pq\Gateway;
4
5 include __DIR__."/../../../setup.inc";
6
7 class CellTest extends \PHPUnit_Framework_TestCase {
8
9 /**
10 * @var \pq\Connection
11 */
12 protected $conn;
13
14 /**
15 * @var \pq\Gateway\Table
16 */
17 protected $table;
18
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());
25 }
26
27 protected function tearDown() {
28 $this->conn->exec(PQ_TEST_TEARDOWN_SQL);
29 }
30
31 /**
32 * This is a very bad test…
33 */
34 public function testBasic() {
35 $row = $this->table->find(null, "id desc", 1)->current();
36 foreach ($row->getData() as $key => $val) {
37 $this->assertEquals($val, (string) $row->$key);
38 $this->assertFalse($row->$key->isExpr());
39 $this->assertFalse($row->$key->isDirty());
40 $this->assertSame($val, $row->$key->get());
41 $row->$key->mod(123);
42 $this->assertNotEquals($val, (string) $row->$key);
43 $this->assertTrue($row->$key->isExpr());
44 $this->assertTrue($row->$key->isDirty());
45 $this->assertNotSame($val, $row->$key->get());
46 $this->assertEquals("$key + 123", (string) $row->$key->get());
47 $row->$key->mod("foobar");
48 $this->assertEquals("$key + 123 || 'foobar'", (string) $row->$key);
49 $row->$key->mod(new \pq\Query\Expr(" - %s()", "now"));
50 $this->assertEquals("$key + 123 || 'foobar' - now()", (string) $row->$key);
51 }
52 }
53
54 public function testRef() {
55 $rows = $this->table->find(null, "id desc", 2);
56 $reft = new Table("reftest");
57 $refs = new Rowset($reft);
58 $refs->append($rows->seek(0)->current()->reftest()->current());
59 $refs->append($rows->seek(1)->current()->reftest()->current());
60 $refs->seek(0)->current()->test = $rows->seek(1)->current();
61 $refs->seek(1)->current()->test = $rows->seek(0)->current();
62 $refs->update();
63 }
64 }