add support for one-dimensional arrays; type input parameters
[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", "Indirect modification of overloaded element of pq\Gateway\Cell has no effect");
81 $row->list[0][0] = -1;
82 }
83
84 public function testHstore() {
85 $this->conn->setConverter(new Hstore(new \pq\Types($this->conn)));
86 $row = $this->table->find(["id="=>3])->current();
87 $this->assertEquals(null, $row->prop->get());
88 $data = array("foo" => "bar", "a" => 1, "b" => 2);
89 $row->prop = $data;
90 $row->update();
91 $this->assertEquals($data, $row->prop->get());
92 $row->prop["a"] = null;
93 $row->update();
94 $data["a"] = null;
95 $this->assertEquals($data, $row->prop->get());
96 unset($data["a"], $row->prop["a"]);
97 $row->update();
98 $this->assertEquals($data, $row->prop->get());
99 }
100 }
101
102 class Hstore implements \pq\ConverterInterface
103 {
104 protected $types;
105 function __construct(\pq\Types $types) {
106 $this->types = $types;
107 }
108 function convertTypes() {
109 return [$this->types["hstore"]->oid];
110 }
111 function convertFromString($string) {
112 return eval("return [$string];");
113 }
114 function convertToString($data) {
115 $string = "";
116 foreach ($data as $k => $v) {
117 $string .= "\"".addslashes($k)."\"=>";
118 if (isset($v)) {
119 $string .= "\"".addslashes($v)."\",";
120 } else {
121 $string .= "NULL,";
122 }
123 }
124 return $string;
125 }
126 }