From 6e2a4c9e095c9501b5aca325cc1238fa1dac4d31 Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Tue, 21 May 2024 23:36:16 +0200 Subject: [PATCH] Add status test --- .../Controllers/Crud/StatusController.php | 4 +- lib/GaletteObjectsLend/Entity/LendStatus.php | 2 +- .../Entity/tests/units/LendStatus.php | 118 ++++++++++++++++++ 3 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 tests/GaletteObjectsLend/Entity/tests/units/LendStatus.php diff --git a/lib/GaletteObjectsLend/Controllers/Crud/StatusController.php b/lib/GaletteObjectsLend/Controllers/Crud/StatusController.php index 98dfc95..9019c38 100644 --- a/lib/GaletteObjectsLend/Controllers/Crud/StatusController.php +++ b/lib/GaletteObjectsLend/Controllers/Crud/StatusController.php @@ -265,9 +265,9 @@ public function doEdit(Request $request, Response $response, int $id = null, str $status->in_stock = isset($post['in_stock']); $status->is_active = isset($post['is_active']); $days = trim($post['rent_day_number']); - $status->rent_day_number = strlen($days) > 0 ? intval($days) : null; + $status->rent_day_number = strlen($days) > 0 ? (int)$days : null; if (!$status->store()) { - $error_detected[] = _T("An error occured while storing the status.", "objectslend"); + $error_detected[] = _T("An error occurred while storing the status.", "objectslend"); } if (count($error_detected)) { diff --git a/lib/GaletteObjectsLend/Entity/LendStatus.php b/lib/GaletteObjectsLend/Entity/LendStatus.php index 4f85165..cc05a9d 100644 --- a/lib/GaletteObjectsLend/Entity/LendStatus.php +++ b/lib/GaletteObjectsLend/Entity/LendStatus.php @@ -99,7 +99,7 @@ public function __construct(Db $zdb, int|ArrayObject $args = null) */ private function loadFromRS(ArrayObject $r): void { - $this->status_id = $r->status_id; + $this->status_id = (int)$r->status_id; $this->status_text = $r->status_text; $this->in_stock = $r->in_stock == '1'; $this->is_active = $r->is_active == '1'; diff --git a/tests/GaletteObjectsLend/Entity/tests/units/LendStatus.php b/tests/GaletteObjectsLend/Entity/tests/units/LendStatus.php new file mode 100644 index 0000000..3dfa1e8 --- /dev/null +++ b/tests/GaletteObjectsLend/Entity/tests/units/LendStatus.php @@ -0,0 +1,118 @@ +. + */ + +namespace GaletteObjectsLends\tests\units; + +use Galette\GaletteTestCase; + +/** + * Status tests + * + * @author Johan Cwiklinski + */ +class LendStatus extends GaletteTestCase +{ + protected int $seed = 20240521230915; + + /** + * Cleanup after each test method + * + * @return void + */ + public function tearDown(): void + { + $delete = $this->zdb->delete(LEND_PREFIX . \GaletteObjectsLend\Entity\LendStatus::TABLE); + $this->zdb->execute($delete); + parent::tearDown(); + } + + /** + * Test empty + * + * @return void + */ + public function testEmpty(): void + { + $status = new \GaletteObjectsLend\Entity\LendStatus($this->zdb); + $this->assertNull($status->status_id); + $this->assertSame('', $status->status_text); + $this->assertFalse($status->in_stock); + $this->assertTrue($status->is_active); + $this->assertNull($status->rent_day_number); + } + + /** + * Test add and update + * + * @return void + */ + public function testCrud(): void + { + $status = new \GaletteObjectsLend\Entity\LendStatus($this->zdb); + $status->status_text = 'One active status'; + $status->in_stock = true; + $status->is_active = true; + $this->assertTrue($status->store()); + $status_one = $status->status_id; + + $status = new \GaletteObjectsLend\Entity\LendStatus($this->zdb); + $status->status_text = 'Another active status'; + $status->in_stock = false; + $status->is_active = true; + $this->assertTrue($status->store()); + $status_two = $status->status_id; + + $status = new \GaletteObjectsLend\Entity\LendStatus($this->zdb); + $status->status_text = 'One inactive status'; + $status->in_stock = true; + $status->is_active = false; + $this->assertTrue($status->store()); + $status_three = $status->status_id; + + $list = $status::getActiveTakeAwayStatuses($this->zdb); + $this->assertCount(1, $list); + + $active_one = $list[0]; + $this->assertSame($status_two, $active_one->status_id); + $this->assertSame('Another active status', $active_one->status_text); + + $list = $status::getActiveStockStatuses($this->zdb); + $this->assertCount(1, $list); + + $active_one = $list[0]; + $this->assertSame($status_one, $active_one->status_id); + $this->assertSame('One active status', $active_one->status_text); + + $list = $status::getActiveStatuses($this->zdb); + $this->assertCount(2, $list); + + $status = new \GaletteObjectsLend\Entity\LendStatus($this->zdb, $status_one); + $status->status_text = 'One active status (edited)'; + $this->assertTrue($status->store()); + + $status = new \GaletteObjectsLend\Entity\LendStatus($this->zdb, $status_one); + $this->assertSame('One active status (edited)', $status->status_text); + + $this->assertTrue($status->delete()); + $status = new \GaletteObjectsLend\Entity\LendStatus($this->zdb, $status_one); + $this->assertNull($status->status_id); + } +}