diff --git a/lib/GaletteAuto/Model.php b/lib/GaletteAuto/Model.php index 1cacc69..b571a7a 100644 --- a/lib/GaletteAuto/Model.php +++ b/lib/GaletteAuto/Model.php @@ -45,8 +45,9 @@ class Model protected string $model; protected Brand $brand; - private $errors; - private $zdb; + /** @var string[] */ + private array $errors; + private Db $zdb; /** * Default constructor @@ -228,7 +229,7 @@ public function check(array $post): bool /** * Get errors * - * @return array + * @return string[] */ public function getErrors(): array { diff --git a/tests/GaletteAuto/tests/units/Model.php b/tests/GaletteAuto/tests/units/Model.php new file mode 100644 index 0000000..c6e49c9 --- /dev/null +++ b/tests/GaletteAuto/tests/units/Model.php @@ -0,0 +1,143 @@ +. + */ + +namespace GaletteAuto\tests\units; + +use Galette\GaletteTestCase; + +/** + * Model tests + * + * @author Johan Cwiklinski + */ +class Model extends GaletteTestCase +{ + protected int $seed = 20240130141727; + + /** + * Test empty + * + * @return void + */ + public function testEmpty(): void + { + /*$brand = new \GaletteAuto\Brand($this->zdb); + $this->assertSame('Brand', $brand->getFieldLabel()); + + $this->assertCount(0, $brand->getList()); + $this->assertSame('0 brand', $brand->displayCount());*/ + } + + /** + * Test add and update + * + * @return void + */ + public function testCrud(): void + { + $brand = new \GaletteAuto\Brand($this->zdb); + //Add new brand + $brand->value = 'Audi'; + $this->assertTrue($brand->store(true)); + $first_brand_id = $brand->id; + + //add another brand + $brand = new \GaletteAuto\Brand($this->zdb); + $brand->value = 'Mercedes'; + $this->assertTrue($brand->store(true)); + $second_brand_id = $brand->id; + + $this->assertCount(2, $brand->getList()); + + $model = new \GaletteAuto\Model($this->zdb); + $this->assertCount(0, $model->getList()); + + $data = []; + $this->assertFalse($model->check($data)); + $this->assertSame( + [ + "- You must select a brand!", + "- You must provide a value!" + ], + $model->getErrors() + ); + + $data = [ + 'model' => 'A3' + ]; + $this->assertFalse($model->check($data)); + $this->assertSame( + [ + "- You must select a brand!" + ], + $model->getErrors() + ); + + $data = [ + 'brand' => $first_brand_id + ]; + $this->assertFalse($model->check($data)); + $this->assertSame( + [ + "- You must provide a value!" + ], + $model->getErrors() + ); + + $data = [ + 'model' => 'A3', + 'brand' => $first_brand_id, + ]; + $this->assertTrue($model->check($data)); + $this->assertTrue($model->store(true)); + + $this->assertCount(1, $model->getList()); + + /*$this->assertCount(2, $brand->getList()); + $this->assertSame('2 brands', $brand->displayCount()); + + $brand = new \GaletteAuto\Brand($this->zdb); + $this->assertTrue($brand->load($id)); + $brand->value = 'Mercedes'; + $this->assertTrue($brand->store()); + + $this->assertCount(2, $brand->getList()); + $this->assertSame('2 brands', $brand->displayCount()); + + $brand = new \GaletteAuto\Brand($this->zdb); + $this->assertTrue($brand->delete([$first_id])); + $list = $brand->getList(); + $this->assertCount(1, $list); + $last_brand = $list[0]; + $this->assertSame($id, $last_brand->id_brand);*/ + } + + /** + * Test load error + * + * @return void + */ + public function testLoadError(): void + { + $brand = new \GaletteAuto\Model($this->zdb); + $this->assertFalse($brand->load(999)); + } +}