X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fpq-gateway;a=blobdiff_plain;f=tests%2Fsetup.inc;h=753cb370c50b052f31820e7ac3020da48a2be31d;hp=ec2236bf19f9d0131bbf7ecf3dc6511311c72354;hb=refs%2Fheads%2Fmapper;hpb=c05be811151dbbd5a9b35e886db081bee8338581 diff --git a/tests/setup.inc b/tests/setup.inc index ec2236b..753cb37 100644 --- a/tests/setup.inc +++ b/tests/setup.inc @@ -1,9 +1,17 @@ getConnection(); - $exec->setConnection(new pq\Connection(PQ_TEST_DSN)); - $exec->execute(new \pq\Query\Writer($sql, $params), function(){}); + $xact = (new Connection(PQ_TEST_DSN))->startTransaction(); + $exec->setConnection($xact->connection); + $exec->execute(new Writer($sql, $params)); $exec->setConnection($conn); + return $xact; } -class QueryLogger implements \SplObserver +class QueryLogger implements SplObserver { protected $fp; @@ -57,7 +68,7 @@ class QueryLogger implements \SplObserver $logfile = __DIR__."/query.log"; } if (!$this->fp = @fopen($logfile, "a")) { - throw new \RuntimeException(error_get_last()["message"]); + throw new RuntimeException(error_get_last()["message"]); } } @@ -67,17 +78,81 @@ class QueryLogger implements \SplObserver } } - function update(\SplSubject $executor) { + function update(SplSubject $executor) { $result = $executor->getResult(); if (isset($result)) { fprintf($this->fp, "[%s] R %s\n", date_create()->format("Y-m-d H:i:s"), json_encode($result)); } elseif (($query = $executor->getQuery())) { - fprintf($this->fp, "[%s] Q %s %% %s\n", + $executor->getConnection()->exec("SELECT pg_backend_pid()")->fetchCol($pid); + fprintf($this->fp, "[%s] Q %s %% %s @%d\n", date_create()->format("Y-m-d H:i:s"), preg_replace("/\s+/", " ", $query), - json_encode($query->getParams())); + json_encode($query->getParams()), + $pid); } } } + +class TestModel implements JsonSerializable { + private $id, $created, $counter, $number, $data, $list, $prop; + private $ref1, $ref2; + + function jsonSerialize() { + return get_object_vars($this); + } + + function __get($p) { + return $this->$p; + } + + function __set($p, $v) { + $this->$p = $v; + } + + /** + * @param Mapper $mapper + * @return MapInterface + */ + static function mapAs(Mapper $mapper) { + return new Map( + __CLASS__, + new Table("test"), + $mapper->mapField("id"), + $mapper->mapField("created"), + $mapper->mapField("counter"), + $mapper->mapField("number"), + $mapper->mapField("data"), + $mapper->mapField("list"), + $mapper->mapField("prop"), + $mapper->mapAll("ref1")->to(RefTestModel::class)->by("test"), + $mapper->mapAll("ref2")->to(RefTestModel::class)->by("another_test") + ); + } +} + +class RefTestModel +{ + private $pk1, $pk2; + private $one, $two; + + function __get($p) { + return $this->$p; + } + + function __set($p, $v) { + $this->$p = $v; + } + + static function mapAs($mapper) { + return new Map( + __CLASS__, + new Table("reftest"), + $mapper->mapField("pk1", "test_id"), + $mapper->mapField("pk2", "another_test_id"), + $mapper->mapRef("one")->to(TestModel::class)->by("test"), + $mapper->mapRef("two")->to(TestModel::class)->by("another_test") + ); + } +} \ No newline at end of file