2 Commits

Author SHA1 Message Date
Gregory Letellier
fe6d2f27b8 ajout retour en collection, modification des tests en conséquences 2023-10-31 10:47:11 +01:00
Gregory Letellier
42d3d38a0a ajout Countable interface 2023-10-31 10:46:49 +01:00
3 changed files with 20 additions and 18 deletions

View File

@@ -3,10 +3,11 @@
namespace Kletellier\PdoWrapper;
use ArrayIterator;
use Countable;
use IteratorAggregate;
use Traversable;
class Collection implements IteratorAggregate
class Collection implements IteratorAggregate, Countable
{
private array $elements;

View File

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

View File

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