251 lines
6.3 KiB
PHP
251 lines
6.3 KiB
PHP
<?php
|
|
|
|
use Kletellier\PdoWrapper\Collection;
|
|
use Kletellier\PdoWrapper\Connection;
|
|
use Kletellier\PdoWrapper\EventDispatcher;
|
|
use Kletellier\PdoWrapper\Model;
|
|
use Kletellier\PdoWrapper\QueryBuilder;
|
|
|
|
test("create db",function () {
|
|
$config = ["conone" => ["TYPE"=>"sqlite", "DATABASE" => ":memory:" ], "contwo"=>["TYPE"=>"sqlite", "DATABASE" => ":memory:" ]];
|
|
$db = Connection::init($config,["conone","contwo"]);
|
|
$ret = $db->executeRawQuery("CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, col1 VARCHAR, date1 DATETIME);");
|
|
expect($ret)->toBe(true);
|
|
|
|
$db2 = Connection::getInstance("contwo");
|
|
$ret = $db2->executeRawQuery("CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, col1 VARCHAR, date1 DATETIME);");
|
|
expect($ret)->toBe(true);
|
|
});
|
|
|
|
test("insert row",function () {
|
|
|
|
$row = new Model();
|
|
$row->setTable("test");
|
|
$row->col1 = "test";
|
|
$row->date1 = new \DateTime();
|
|
$ret = $row->save();
|
|
|
|
$row2 = new Model();
|
|
$row2->setTable("test");
|
|
$row2->col1 = "test2";
|
|
$row2->date1 = new \DateTime();
|
|
$ret2 = $row2->save();
|
|
|
|
expect($ret)->toBe(true);
|
|
expect($ret2)->toBe(true);
|
|
});
|
|
|
|
test("model find", function() {
|
|
$row = new Model();
|
|
$row->setTable("test");
|
|
|
|
$res = $row->find(1);
|
|
expect($res->id)->toBe(1);
|
|
expect($row->col1)->toBe("test");
|
|
|
|
$res = $row->find(15);
|
|
expect($res)->toBe(null);
|
|
|
|
$row->col1 = "test2";
|
|
$res = $row->save();
|
|
expect($res)->toBe(true);
|
|
|
|
$row->col1 = "test";
|
|
$res = $row->save();
|
|
expect($res)->toBe(true);
|
|
|
|
$json = json_encode($row);
|
|
expect($json)->toBeJson();
|
|
|
|
$obj = json_decode($json);
|
|
expect($obj->col1)->toBe("test");
|
|
});
|
|
|
|
test("querybuilder", function() {
|
|
$qb = new QueryBuilder("test");
|
|
$row = $qb->find(1);
|
|
|
|
expect($row)->toBeObject();
|
|
expect($row->col1)->toBe("test");
|
|
expect($row->id)->toBe(1);
|
|
|
|
$qb->reset();
|
|
$rows = $qb->where("id",1)->columns("*")->get();
|
|
|
|
expect($rows)->toBeInstanceOf(Collection::class);
|
|
expect(count($rows))->toBe(1);
|
|
|
|
$qb->reset();
|
|
$rows = $qb->where("id",">",2)->columns("*")->get();
|
|
|
|
expect($rows)->toBeInstanceOf(Collection::class);
|
|
expect(count($rows))->toBe(0);
|
|
|
|
$qb->reset();
|
|
$rows = $qb->whereRaw("id = 1")->columns("*")->get();
|
|
expect($rows)->toBeInstanceOf(Collection::class);
|
|
expect(count($rows))->toBe(1);
|
|
|
|
$qb->reset();
|
|
$rows = $qb->whereRaw("id = 1")->get();
|
|
expect($rows)->toBeInstanceOf(Collection::class);
|
|
expect(count($rows))->toBe(1);
|
|
|
|
$qb->reset();
|
|
$rows = $qb->distinct()->get();
|
|
expect(count($rows))->toBe(2);
|
|
|
|
$qb->reset();
|
|
$rows = $qb->whereNotNull("col1")->get();
|
|
expect(count($rows))->toBe(2);
|
|
|
|
$qb->reset();
|
|
$rows = $qb->where("id",1)->orWhere("id",2)->get();
|
|
expect(count($rows))->toBe(2);
|
|
});
|
|
|
|
test("extending model",function()
|
|
{
|
|
$class = "Test";
|
|
$code = "class $class extends \Kletellier\PdoWrapper\Model {}";
|
|
eval($code);
|
|
|
|
$qbs = Test::q();
|
|
expect($qbs)->toBeInstanceOf(QueryBuilder::class);
|
|
|
|
$row = $qbs->find(1);
|
|
expect($row->id)->toBe(1);
|
|
expect($row->col1)->toBe("test");
|
|
});
|
|
|
|
test("order by", function(){
|
|
$qb = new QueryBuilder("test");
|
|
$rows = $qb->orderBy("id","DESC")->get();
|
|
|
|
expect($rows)->toBeInstanceOf(Collection::class);
|
|
expect(count($rows))->toBe(2);
|
|
|
|
expect($rows->first()->col1)->toBe("test2");
|
|
});
|
|
|
|
test("group by", function(){
|
|
$qb = new QueryBuilder("test");
|
|
$rows = $qb->columns("COUNT(*) as Nb")->get();
|
|
|
|
expect($rows)->toBeInstanceOf(Collection::class);
|
|
expect(count($rows))->toBe(1);
|
|
expect($rows->first()->Nb)->toBe(2);
|
|
|
|
$qb->reset();
|
|
$rows = $qb->columns("COUNT(*) as Nb")->groupBy("id")->get();
|
|
expect($rows)->toBeInstanceOf(Collection::class);
|
|
expect(count($rows))->toBe(2);
|
|
expect($rows->first()->Nb)->toBe(1);
|
|
});
|
|
|
|
test("isset",function()
|
|
{
|
|
$q = new QueryBuilder("test");
|
|
$row = $q->where("id",1)->get()->first();
|
|
|
|
expect($row->id)->toBe(1);
|
|
expect($row->col1)->toBe("test");
|
|
|
|
expect(isset($row->col1))->toBe(true);
|
|
expect(isset($row->col2))->toBe(false);
|
|
expect(isset($row->find))->toBe(true);
|
|
});
|
|
|
|
test("update row", function() {
|
|
$qb = new QueryBuilder("test","id");
|
|
$row = $qb->find(1);
|
|
$row->col1 = "testupdate";
|
|
$res = $row->save();
|
|
expect($res)->toBe(true);
|
|
|
|
$qb->reset();
|
|
$rows = $qb->where("id",1)->columns("*")->get();
|
|
expect($rows)->toBeInstanceOf(Collection::class);
|
|
expect(count($rows))->toBe(1);
|
|
expect($rows->first()->col1)->toBe("testupdate");
|
|
});
|
|
|
|
test("event", function() {
|
|
$ret = null;
|
|
EventDispatcher::on("on.query",function($event,$query) use (&$ret) {
|
|
$ret = $query;
|
|
});
|
|
|
|
$qb = new QueryBuilder("test");
|
|
$qb->find(1);
|
|
|
|
expect($ret)->toBeObject();
|
|
});
|
|
|
|
test("where array", function() {
|
|
|
|
$row3 = new Model();
|
|
$row3->setTable("test");
|
|
$row3->col1 = "test3";
|
|
$row3->date1 = new \DateTime();
|
|
$ret3 = $row3->save();
|
|
|
|
$qb = new QueryBuilder("test");
|
|
$all = $qb->get();
|
|
|
|
expect(count($all))->toBe(3);
|
|
|
|
$qb->reset();
|
|
|
|
$between = $qb->whereBetween("id",2,3)->get();
|
|
expect(count($between))->toBe(2);
|
|
|
|
expect($between->first()->id)->toBe(2);
|
|
|
|
$qb->reset();
|
|
$in = $qb->whereIn("id",[2,3])->get();
|
|
expect(count($in))->toBe(2);
|
|
expect($in->first()->id)->toBe(2);
|
|
|
|
});
|
|
|
|
test("is null", function(){
|
|
$row4 = new Model();
|
|
$row4->setTable("test");
|
|
$row4->col1 = null;
|
|
$row4->date1 = new \DateTime();
|
|
$ret4 = $row4->save();
|
|
expect($ret4)->toBe(true);
|
|
|
|
$qb = new QueryBuilder("test");
|
|
$rows = $qb->whereNull("col1")->get();
|
|
|
|
expect($rows)->toBeInstanceOf(Collection::class);
|
|
expect(count($rows))->toBe(1);
|
|
expect($rows->first()->col1)->toBe(null);
|
|
});
|
|
|
|
test("second connection", function() {
|
|
$row = new Model();
|
|
$row->setConnection("contwo");
|
|
$row->setTable("test");
|
|
$row->col1 = "test";
|
|
$row->date1 = new \DateTime();
|
|
$ret = $row->save();
|
|
|
|
$row2 = new Model();
|
|
$row2->setConnection("contwo");
|
|
$row2->setTable("test");
|
|
$row2->col1 = "test2";
|
|
$row2->date1 = new \DateTime();
|
|
$ret2 = $row2->save();
|
|
|
|
expect($ret)->toBe(true);
|
|
expect($ret2)->toBe(true);
|
|
|
|
$qb = new QueryBuilder("test","id","contwo");
|
|
$rows = $qb->get();
|
|
|
|
expect($rows)->toBeInstanceOf(Collection::class);
|
|
expect(count($rows))->toBe(2);
|
|
}); |