5 include __DIR__
."/../../../setup.inc";
7 class CellTest
extends \PHPUnit_Framework_TestCase
{
15 * @var \pq\Gateway\Table
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
);
32 * This is a very bad test…
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());
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);
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()->allOf("reftest")->current());
59 $refs->append($rows->seek(1)->current()->allOf("reftest")->current());
60 $refs->seek(0)->current()->test
= $rows->seek(1)->current();
61 $refs->seek(1)->current()->test
= $rows->seek(0)->current();
65 public function testArray() {
66 $row = $this->table
->find(["id="=>1])->current();
67 $this->assertEquals([-1,0,1], $row->list->get());
71 $this->assertEquals([-1,0,null,4], $row->list->get());
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()];
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;
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);
92 $this->assertEquals($data, $row->prop
->get());
93 $row->prop
["a"] = null;
96 $this->assertEquals($data, $row->prop
->get());
97 unset($data["a"], $row->prop
["a"]);
99 $this->assertEquals($data, $row->prop
->get());
103 class Hstore
implements \pq\Converter
106 function __construct(\pq\Types
$types) {
107 $this->types
= $types["hstore"]->oid
;
109 function convertTypes() {
110 return [$this->types
];
112 function convertFromString($string, $type) {
113 return eval("return [$string];");
115 function convertToString($data, $type) {
117 foreach ($data as $k => $v) {
118 $string .= "\"".addslashes($k)."\"=>";
120 $string .= "\"".addslashes($v)."\",";