ajout retour en collection, modification des tests en conséquences

This commit is contained in:
Gregory Letellier
2023-10-31 10:47:11 +01:00
parent 42d3d38a0a
commit fe6d2f27b8
2 changed files with 18 additions and 17 deletions

View File

@@ -124,7 +124,7 @@ class QueryBuilder
$this->columns = ["*"]; $this->columns = ["*"];
$data = $this->getData(); $data = $this->getData();
if (count($data) > 0) { if (count($data) > 0) {
return $this->prepareModels($data)[0]; return $this->prepareModels($data)->first();
} }
return null; return null;
} }
@@ -172,7 +172,7 @@ class QueryBuilder
$model = new Model(); $model = new Model();
$ret[] = $model->setPk($this->pk)->setTable($this->table)->fillData($row); $ret[] = $model->setPk($this->pk)->setTable($this->table)->fillData($row);
} }
return $ret; return new Collection($ret);
} }
private function getData(): mixed private function getData(): mixed

View File

@@ -1,5 +1,6 @@
<?php <?php
use Kletellier\PdoWrapper\Collection;
use Kletellier\PdoWrapper\Db; use Kletellier\PdoWrapper\Db;
use Kletellier\PdoWrapper\EventDispatcher; use Kletellier\PdoWrapper\EventDispatcher;
use Kletellier\PdoWrapper\Model; use Kletellier\PdoWrapper\Model;
@@ -45,23 +46,23 @@ test("querybuilder", function() {
$qb->reset(); $qb->reset();
$rows = $qb->where("id",1)->columns("*")->get(); $rows = $qb->where("id",1)->columns("*")->get();
expect($rows)->toBeArray(); expect($rows)->toBeInstanceOf(Collection::class);
expect(count($rows))->toBe(1); expect(count($rows))->toBe(1);
$qb->reset(); $qb->reset();
$rows = $qb->where("id",">",2)->columns("*")->get(); $rows = $qb->where("id",">",2)->columns("*")->get();
expect($rows)->toBeArray(); expect($rows)->toBeInstanceOf(Collection::class);
expect(count($rows))->toBe(0); expect(count($rows))->toBe(0);
$qb->reset(); $qb->reset();
$rows = $qb->whereRaw("id = 1")->columns("*")->get(); $rows = $qb->whereRaw("id = 1")->columns("*")->get();
expect($rows)->toBeArray(); expect($rows)->toBeInstanceOf(Collection::class);
expect(count($rows))->toBe(1); expect(count($rows))->toBe(1);
$qb->reset(); $qb->reset();
$rows = $qb->whereRaw("id = 1")->get(); $rows = $qb->whereRaw("id = 1")->get();
expect($rows)->toBeArray(); expect($rows)->toBeInstanceOf(Collection::class);
expect(count($rows))->toBe(1); expect(count($rows))->toBe(1);
}); });
@@ -69,25 +70,25 @@ test("order by", function(){
$qb = new QueryBuilder("test"); $qb = new QueryBuilder("test");
$rows = $qb->orderBy("id","DESC")->get(); $rows = $qb->orderBy("id","DESC")->get();
expect($rows)->toBeArray(); expect($rows)->toBeInstanceOf(Collection::class);
expect(count($rows))->toBe(2); expect(count($rows))->toBe(2);
expect($rows[0]->col1)->toBe("test2"); expect($rows->first()->col1)->toBe("test2");
}); });
test("group by", function(){ test("group by", function(){
$qb = new QueryBuilder("test"); $qb = new QueryBuilder("test");
$rows = $qb->columns("COUNT(*) as Nb")->get(); $rows = $qb->columns("COUNT(*) as Nb")->get();
expect($rows)->toBeArray(); expect($rows)->toBeInstanceOf(Collection::class);
expect(count($rows))->toBe(1); expect(count($rows))->toBe(1);
expect($rows[0]->Nb)->toBe(2); expect($rows->first()->Nb)->toBe(2);
$qb->reset(); $qb->reset();
$rows = $qb->columns("COUNT(*) as Nb")->groupBy("id")->get(); $rows = $qb->columns("COUNT(*) as Nb")->groupBy("id")->get();
expect($rows)->toBeArray(); expect($rows)->toBeInstanceOf(Collection::class);
expect(count($rows))->toBe(2); expect(count($rows))->toBe(2);
expect($rows[0]->Nb)->toBe(1); expect($rows->first()->Nb)->toBe(1);
}); });
test("update row", function() { test("update row", function() {
@@ -99,9 +100,9 @@ test("update row", function() {
$qb->reset(); $qb->reset();
$rows = $qb->where("id",1)->columns("*")->get(); $rows = $qb->where("id",1)->columns("*")->get();
expect($rows)->toBeArray(); expect($rows)->toBeInstanceOf(Collection::class);
expect(count($rows))->toBe(1); expect(count($rows))->toBe(1);
expect($rows[0]->col1)->toBe("testupdate"); expect($rows->first()->col1)->toBe("testupdate");
}); });
test("event", function() { test("event", function() {
@@ -134,12 +135,12 @@ test("where array", function() {
$between = $qb->whereBetween("id",2,3)->get(); $between = $qb->whereBetween("id",2,3)->get();
expect(count($between))->toBe(2); expect(count($between))->toBe(2);
expect($between[0]->id)->toBe(2); expect($between->first()->id)->toBe(2);
$qb->reset(); $qb->reset();
$in = $qb->whereIn("id",[2,3])->get(); $in = $qb->whereIn("id",[2,3])->get();
expect(count($in))->toBe(2); expect(count($in))->toBe(2);
expect($in[0]->id)->toBe(2); expect($in->first()->id)->toBe(2);
}); });
@@ -164,6 +165,6 @@ test("second connection", function() {
$qb = new QueryBuilder("test","id","contwo"); $qb = new QueryBuilder("test","id","contwo");
$rows = $qb->get(); $rows = $qb->get();
expect($rows)->toBeArray(); expect($rows)->toBeInstanceOf(Collection::class);
expect(count($rows))->toBe(2); expect(count($rows))->toBe(2);
}); });