From 41a8396bee8dcc8c090565901a3c58a60961d2ad Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Wed, 14 Feb 2024 07:14:15 +0100 Subject: [PATCH] Fixes, test Auto Crud --- lib/GaletteAuto/AbstractObject.php | 1 + lib/GaletteAuto/Auto.php | 7 +- lib/GaletteAuto/Autos.php | 10 +- lib/GaletteAuto/Controllers/Controller.php | 2 +- lib/GaletteAuto/History.php | 48 ++- templates/default/history.html.twig | 4 +- tests/GaletteAuto/tests/units/Auto.php | 322 +++++++++++++++++++++ tests/GaletteAuto/tests/units/History.php | 2 +- 8 files changed, 369 insertions(+), 27 deletions(-) create mode 100644 tests/GaletteAuto/tests/units/Auto.php diff --git a/lib/GaletteAuto/AbstractObject.php b/lib/GaletteAuto/AbstractObject.php index 4015630..5066469 100644 --- a/lib/GaletteAuto/AbstractObject.php +++ b/lib/GaletteAuto/AbstractObject.php @@ -34,6 +34,7 @@ * @author Johan Cwiklinski * * @property int $id + * @property string $value */ abstract class AbstractObject { diff --git a/lib/GaletteAuto/Auto.php b/lib/GaletteAuto/Auto.php index 8996a29..2a77837 100644 --- a/lib/GaletteAuto/Auto.php +++ b/lib/GaletteAuto/Auto.php @@ -124,8 +124,8 @@ class Auto private Transmission $transmission; private Body $body; private History $history; - private Adherent $owner; private State $state; + private Adherent $owner; public const FUEL_PETROL = 1; public const FUEL_DIESEL = 2; @@ -349,7 +349,7 @@ public function store(bool $new = false): bool break; case 'integer': $values[$k] = ( - ($this->$propName != 0 && $this->$propName != '') + (!empty($this->$propName)) ? $this->$propName : new Expression('NULL') ); @@ -380,6 +380,7 @@ public function store(bool $new = false): bool _T("New car added", "auto"), strtoupper($this->name) ); + $this->history->load((int)$this->id); //handle picture for newly added cars $this->picture = new Picture($this->plugins, (int)$this->id); @@ -769,7 +770,7 @@ public function check(array $post): bool } break; case 'owner': - if (isset($post['change_owner'])) { + if (isset($post['change_owner']) || !isset($this->id)) { $value = (int)$value; if ($value > 0) { $this->$prop->load($value); diff --git a/lib/GaletteAuto/Autos.php b/lib/GaletteAuto/Autos.php index ddc987e..94f9d98 100644 --- a/lib/GaletteAuto/Autos.php +++ b/lib/GaletteAuto/Autos.php @@ -94,7 +94,7 @@ public function removeVehicles(int|array $ids): bool $infos = null; foreach ($vehicles as $vehicle) { $str_v = $vehicle->id_car . ' - ' . $vehicle->car_name . - ' (' . $vehicle->brand->brand . ' ' . $vehicle->model->model . ')'; + ' (' . $vehicle->brand . ' ' . $vehicle->model . ')'; $infos .= $str_v . "\n"; $p = new Picture($this->plugins, $vehicle->id_car); @@ -269,12 +269,12 @@ public function getList( /** * Count vehicles from the query * - * @param Select $select Original select - * @param AutosList $filters Filters + * @param Select $select Original select + * @param ?AutosList $filters Filters * * @return void */ - private function proceedCount($select, AutosList $filters): void + private function proceedCount($select, ?AutosList $filters): void { try { $countSelect = clone $select; @@ -296,7 +296,7 @@ private function proceedCount($select, AutosList $filters): void $results = $this->zdb->execute($countSelect); $this->count = $results->current()->count; - if ($this->count > 0) { + if ($this->count > 0 && $filters !== null) { $filters->setCounter($this->count); } } catch (\Exception $e) { diff --git a/lib/GaletteAuto/Controllers/Controller.php b/lib/GaletteAuto/Controllers/Controller.php index a637463..efd5f38 100644 --- a/lib/GaletteAuto/Controllers/Controller.php +++ b/lib/GaletteAuto/Controllers/Controller.php @@ -497,7 +497,7 @@ public function vehicleHistory(Request $request, Response $response, int $id): R $apk = Auto::PK; $params = [ - 'entries' => $history->entries, + 'entries' => $history->getEntries(), 'page_title' => str_replace('%d', $history->$apk, _T("History of car #%d", "auto")), 'mode' => $request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest' ? 'ajax' : '' ]; diff --git a/lib/GaletteAuto/History.php b/lib/GaletteAuto/History.php index b3b2706..3329133 100644 --- a/lib/GaletteAuto/History.php +++ b/lib/GaletteAuto/History.php @@ -51,7 +51,11 @@ class History State::PK => 'integer' ); - //history entries + /** + * history entries + * + * @var array> $entries + */ private array $entries; private int $id_car; @@ -89,8 +93,7 @@ public function load(int $id): bool )->order('history_date ASC'); $results = $this->zdb->execute($select); - $this->entries = $results->toArray(); - $this->formatEntries(); + $this->formatEntries($results->toArray()); return true; } catch (\Exception $e) { Analog::log( @@ -137,23 +140,30 @@ public function getLatest(): ArrayObject|false /** * Format entries dates, also loads Member * + * @param array> $entries list of entries to format + * * @return void */ - private function formatEntries(): void + private function formatEntries(array $entries): void { - for ($i = 0; $i < count($this->entries); $i++) { + $this->entries = []; + foreach ($entries as $entry) { //put a formatted date to show - $date = new \DateTime($this->entries[$i]['history_date']); - $this->entries[$i]['formatted_date'] = $date->format(__('Y-m-d')); + $date = new \DateTime($entry['history_date']); + $entry['formatted_date'] = $date->format(__('Y-m-d')); + //associate member to current history entry - $this->entries[$i]['owner'] - = new Adherent($this->zdb, (int)$this->entries[$i]['id_adh']); + $entry['owner'] = new Adherent($this->zdb, (int)$entry['id_adh']); + //associate color - $this->entries[$i]['color'] - = new Color($this->zdb, (int)$this->entries[$i]['id_color']); + $color = new Color($this->zdb, (int)$entry['id_color']); + $entry['color'] = $color->value; + //associate state - $this->entries[$i]['state'] - = new State($this->zdb, (int)$this->entries[$i]['id_state']); + $state = new State($this->zdb, (int)$entry['id_state']); + $entry['state'] = $state->value; + + $this->entries[] = $entry; } } @@ -220,8 +230,6 @@ public function __get(string $name) return $this->$name; case 'fields': return array_keys($this->fields); - case 'entries': - return $this->entries; default: Analog::log( '[' . get_class($this) . '] Trying to get an unknown property (' . @@ -231,4 +239,14 @@ public function __get(string $name) break; } } + + /** + * Get current car history entries + * + * @return array> + */ + public function getEntries(): array + { + return $this->entries; + } } diff --git a/templates/default/history.html.twig b/templates/default/history.html.twig index 9984e6a..086c833 100644 --- a/templates/default/history.html.twig +++ b/templates/default/history.html.twig @@ -60,8 +60,8 @@ {{ owner.sfullname }} {{ entry.car_registration }} - {{ entry.color.value }} - {{ entry.state.value }} + {{ entry.color }} + {{ entry.state }} {% endfor %} diff --git a/tests/GaletteAuto/tests/units/Auto.php b/tests/GaletteAuto/tests/units/Auto.php new file mode 100644 index 0000000..b9a4570 --- /dev/null +++ b/tests/GaletteAuto/tests/units/Auto.php @@ -0,0 +1,322 @@ +. + */ + +namespace GaletteAuto\tests\units; + +use Galette\GaletteTestCase; + +/** + * Model tests + * + * @author Johan Cwiklinski + */ +class Auto extends GaletteTestCase +{ + protected int $seed = 20240212212207; + + protected \Galette\Core\Plugins $plugins; + + /** + * Set up tests + * + * @return void + */ + public function setUp(): void + { + parent::setUp(); + $this->plugins = $this->container->get('plugins'); + $this->initStatus(); + } + + /** + * Cleanup after each test method + * + * @return void + */ + public function tearDown(): void + { + $tables = [ + \GaletteAuto\History::TABLE, + \GaletteAuto\Auto::TABLE, + \GaletteAuto\Model::TABLE, + \GaletteAuto\Brand::TABLE, + \GaletteAuto\Color::TABLE, + \GaletteAuto\Body::TABLE, + \GaletteAuto\Finition::TABLE, + \GaletteAuto\State::TABLE, + \GaletteAuto\Transmission::TABLE, + ]; + + foreach ($tables as $table) { + $delete = $this->zdb->delete(AUTO_PREFIX . $table); + $this->zdb->execute($delete); + } + + parent::tearDown(); + } + + /** + * Test add and update + * + * @return void + */ + public function testCrud(): void + { + $body = new \GaletteAuto\Body($this->zdb); + $body->value = 'Berline'; + $this->assertTrue($body->store(true)); + $body_id = $body->id; + + $color = new \GaletteAuto\Color($this->zdb); + $color->value = 'Grey'; + $this->assertTrue($color->store(true)); + $color_id = $color->id; + + $finition = new \GaletteAuto\Finition($this->zdb); + $finition->value = 'Standard'; + $this->assertTrue($finition->store(true)); + $finition_id = $finition->id; + + $state = new \GaletteAuto\State($this->zdb); + $state->value = 'Correct'; + $this->assertTrue($state->store(true)); + $state_id = $state->id; + + $transmission = new \GaletteAuto\Transmission($this->zdb); + $transmission->value = 'Manual'; + $this->assertTrue($transmission->store(true)); + $transmission_id = $transmission->id; + + $brand = new \GaletteAuto\Brand($this->zdb); + $brand->value = 'Peugeot'; + $this->assertTrue($brand->store(true)); + $brand_id = $brand->id; + + $model = new \GaletteAuto\Model($this->zdb); + $data = [ + 'model' => '307', + 'brand' => $brand_id, + ]; + $this->assertTrue($model->check($data)); + $this->assertTrue($model->store(true)); + $model_id = $model->id; + + $auto = new \GaletteAuto\Auto($this->plugins, $this->zdb); + + $data = []; + $this->assertFalse($auto->check($data)); + $this->assertSame( + [ + '- Mandatory field registration empty.', + '- Mandatory field name empty.', + '- Mandatory field first registration date empty.', + '- Mandatory field first circulation date empty.', + '- Mandatory field fuel empty.', + '- Mandatory field finition empty.', + '- Mandatory field color empty.', + '- Mandatory field model empty.', + '- Mandatory field transmission empty.', + '- Mandatory field body empty.', + '- Mandatory field state empty.', + '- you must attach an owner to this car', + ], + $auto->getErrors() + ); + + //set some required values + $data = [ + 'color' => $color_id, + 'body' => $body_id, + 'finition' => $finition_id, + 'state' => $state_id, + 'transmission' => $transmission_id, + ]; + $this->assertFalse($auto->check($data)); + $this->assertSame( + [ + '- Mandatory field registration empty.', + '- Mandatory field name empty.', + '- Mandatory field first registration date empty.', + '- Mandatory field first circulation date empty.', + '- Mandatory field fuel empty.', + '- Mandatory field model empty.', + '- you must attach an owner to this car', + ], + $auto->getErrors() + ); + + //create vehicle + $adh = $this->getMemberOne(); + $data = [ + 'registration' => 'GA-123-TE', + 'name' => 'Titine', + 'first_registration_date' => '2001-02-12', + 'first_circulation_date' => '2001-02-13', + 'fuel' => \GaletteAuto\Auto::FUEL_DIESEL, + 'model' => $model_id, + 'color' => $color_id, + 'body' => $body_id, + 'finition' => $finition_id, + 'state' => $state_id, + 'transmission' => $transmission_id, + 'owner' => $adh->id, + ]; + $check = $auto->check($data); + $this->assertSame([], $auto->getErrors()); + $this->assertTrue($check); + + $stored = $auto->store(true); + $this->assertEquals([], $auto->getErrors()); + $this->assertTrue($stored); + $auto_id = $auto->id; + + //check history + $history = new \GaletteAuto\History($this->zdb); + $this->assertTrue($history->load($auto->id)); + $this->assertCount(1, $history->getEntries()); + + $entry = $history->getEntries()[0]; + $this->assertSame( + [ + 'id_car', + 'id_adh', + 'history_date', + 'car_registration', + 'id_color', + 'id_state', + 'formatted_date', + 'owner', + 'color', + 'state' + ], + array_keys($entry) + ); + + $this->assertSame($auto->id, $entry['id_car']); + $this->assertSame($adh->id, $entry['id_adh']); + $this->assertSame('GA-123-TE', $entry['car_registration']); + $this->assertSame('Grey', $entry['color']); + $this->assertSame('Correct', $entry['state']); + + //update car - change owner and color + $auto = new \GaletteAuto\Auto($this->plugins, $this->zdb); + $this->assertTrue($auto->load($auto_id)); + + $adh2 = $this->getMemberTwo(); + $color2 = new \GaletteAuto\Color($this->zdb); + $color2->value = 'Yellow'; + $this->assertTrue($color2->store(true)); + $color2_id = $color2->id; + + $data = [ + 'registration' => 'GA-123-TE', + 'name' => 'Titine', + 'first_registration_date' => '2001-02-12', + 'first_circulation_date' => '2001-02-13', + 'fuel' => \GaletteAuto\Auto::FUEL_DIESEL, + 'model' => $model_id, + 'color' => $color2_id, + 'body' => $body_id, + 'finition' => $finition_id, + 'state' => $state_id, + 'transmission' => $transmission_id, + 'owner' => $adh2->id, + 'change_owner' => true, + ]; + $check = $auto->check($data); + $this->assertSame([], $auto->getErrors()); + $this->assertTrue($check); + + $stored = $auto->store(); + $this->assertEquals([], $auto->getErrors()); + $this->assertTrue($stored); + + //check history + $history = new \GaletteAuto\History($this->zdb); + $this->assertTrue($history->load($auto->id)); + $this->assertCount(2, $history->getEntries()); + + $entry = $history->getEntries()[1]; + $this->assertSame($auto->id, $entry['id_car']); + $this->assertSame($adh2->id, $entry['id_adh']); + $this->assertSame('GA-123-TE', $entry['car_registration']); + $this->assertSame('Yellow', $entry['color']); + $this->assertSame('Correct', $entry['state']); + + //add another car + $auto = new \GaletteAuto\Auto($this->plugins, $this->zdb); + $data = [ + 'registration' => 'AS-123-SO', + 'name' => 'My car', + 'first_registration_date' => '2015-03-14', + 'first_circulation_date' => '2015-03-15', + 'fuel' => \GaletteAuto\Auto::FUEL_DIESEL, + 'model' => $model_id, + 'color' => $color_id, + 'body' => $body_id, + 'finition' => $finition_id, + 'state' => $state_id, + 'transmission' => $transmission_id, + 'owner' => $adh->id, + ]; + $check = $auto->check($data); + $this->assertSame([], $auto->getErrors()); + $this->assertTrue($check); + + $stored = $auto->store(true); + $this->assertEquals([], $auto->getErrors()); + $this->assertTrue($stored); + $auto2_id = $auto->id; + + $this->assertTrue($history->load($auto2_id)); + $this->assertCount(1, $history->getEntries()); + + $this->logSuperAdmin(); + $autos = new \GaletteAuto\Autos($this->plugins, $this->zdb); + $this->assertCount(2, $autos->getList()); + + $this->assertTrue($autos->removeVehicles([$auto_id])); + $this->assertCount(1, $autos->getList()); + $this->assertFalse($auto->load($auto_id)); + } + + /** + * Test fuels + * + * @return void + */ + public function testListFuels(): void + { + $auto = new \GaletteAuto\Auto($this->plugins, $this->zdb); + $this->assertCount(6, $auto->listFuels()); + } + + /** + * Test load error + * + * @return void + */ + public function testLoadError(): void + { + $auto = new \GaletteAuto\Auto($this->plugins, $this->zdb); + $this->assertFalse($auto->load(999)); + } +} diff --git a/tests/GaletteAuto/tests/units/History.php b/tests/GaletteAuto/tests/units/History.php index c9c3256..f0f0c23 100644 --- a/tests/GaletteAuto/tests/units/History.php +++ b/tests/GaletteAuto/tests/units/History.php @@ -49,6 +49,6 @@ public function testLoadError(): void { $history = new \GaletteAuto\History($this->zdb); $this->assertTrue($history->load(999)); - $this->assertCount(0, $history->entries); + $this->assertCount(0, $history->getEntries()); } }