Skip to content

Commit

Permalink
Test models
Browse files Browse the repository at this point in the history
  • Loading branch information
trasher committed Feb 11, 2024
1 parent 7fe0000 commit 95c3efa
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/GaletteAuto/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -228,7 +229,7 @@ public function check(array $post): bool
/**
* Get errors
*
* @return array
* @return string[]
*/
public function getErrors(): array
{
Expand Down
143 changes: 143 additions & 0 deletions tests/GaletteAuto/tests/units/Model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php

/**
* Copyright © 2003-2024 The Galette Team
*
* This file is part of Galette (https://galette.eu).
*
* Galette is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Galette is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Galette. If not, see <http://www.gnu.org/licenses/>.
*/

namespace GaletteAuto\tests\units;

use Galette\GaletteTestCase;

/**
* Model tests
*
* @author Johan Cwiklinski <[email protected]>
*/
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));
}
}

0 comments on commit 95c3efa

Please sign in to comment.