8ec28598ad220ff489ca8e3d1bf7c955125f9ea1
[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, $row->$key->get());
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
65 public function testArray() {
66 $row = $this->table->find(["id="=>1])->current();
67 $this->assertEquals([-1,0,1], $row->list->get());
68 $row->list[] = 4;
69 $row->list[2] = null;
70 $row->update();
71 $this->assertEquals([-1,0,null,4], $row->list->get());
72 }
73
74 public function testMultiArray() {
75 $row = $this->table->find(["id="=>2])->current();
76 $this->assertEquals([0,1,2], $row->list->get());
77 $row->list = [$row->list->get()];
78 $row->update();
79 $this->assertEquals([[0,1,2]], $row->list->get());
80 $this->setExpectedException("PHPUnit_Framework_Error_Notice",
81 "Indirect modification of overloaded element of pq\Gateway\Cell has no effect");
82 $row->list[0][0] = -1;
83 }
84
85 public function testHstore() {
86 $this->conn->setConverter(new Hstore(new \pq\Types($this->conn)));
87 $row = $this->table->find(["id="=>3])->current();
88 $this->assertEquals(null, $row->prop->get());
89 $data = array("foo" => "bar", "a" => 1, "b" => 2);
90 $row->prop = $data;
91 $row->update();
92 $this->assertEquals($data, $row->prop->get());
93 $row->prop["a"] = null;
94 $row->update();
95 $data["a"] = null;
96 $this->assertEquals($data, $row->prop->get());
97 unset($data["a"], $row->prop["a"]);
98 $row->update();
99 $this->assertEquals($data, $row->prop->get());
100 }
101 }
102
103 class Hstore implements \pq\Converter
104 {
105 protected $types;
106 function __construct(\pq\Types $types) {
107 $this->types = $types["hstore"]->oid;
108 }
109 function convertTypes() {
110 return [$this->types];
111 }
112 function convertFromString($string, $type) {
113 return eval("return [$string];");
114 }
115 function convertToString($data, $type) {
116 $string = "";
117 foreach ($data as $k => $v) {
118 $string .= "\"".addslashes($k)."\"=>";
119 if (isset($v)) {
120 $string .= "\"".addslashes($v)."\",";
121 } else {
122 $string .= "NULL,";
123 }
124 }
125 return $string;
126 }
127 }