From ca690c334942109909ba72f23af8d10f23788335 Mon Sep 17 00:00:00 2001 From: divyesh000 Date: Mon, 20 May 2024 19:10:42 +0400 Subject: [PATCH] add `addProductStock` method to `Store` model and update `OrderProductTest` and `OrderTest` to use it --- src/models/Store.php | 10 ++++++++++ tests/OrderProductTest.php | 3 +++ tests/OrderTest.php | 4 ++++ 3 files changed, 17 insertions(+) diff --git a/src/models/Store.php b/src/models/Store.php index e230aa8..18a96b9 100644 --- a/src/models/Store.php +++ b/src/models/Store.php @@ -146,6 +146,16 @@ public function validate(): array return $errors; } + public function addProductStock(int $product_id, int $quantity): bool + { + $query = "INSERT INTO store_product (store_id, product_id, stock_level) VALUES (:store_id, :product_id, :quantity) + ON DUPLICATE KEY UPDATE stock_level = stock_level + :quantity"; + $params = ['store_id' => $this->store_id, 'product_id' => $product_id, 'quantity' => $quantity]; + $result = self::query($query, $params); + + return $result; + } + public function getProductStock(int $product_id): int { $query = "SELECT stock_level FROM store_product WHERE store_id = :store_id AND product_id = :product_id;"; diff --git a/tests/OrderProductTest.php b/tests/OrderProductTest.php index aecc090..7104b4b 100644 --- a/tests/OrderProductTest.php +++ b/tests/OrderProductTest.php @@ -64,6 +64,9 @@ public function setUp(): void throw new Exception('Unable to save product'); } + // Update stock level for the product + $this->dummy_store->addProductStock($this->dummy_product->getProductID(), 10); + // Create dummy order line items $this->line_items = [ new OrderProduct($this->dummy_product->getProductID(), "medium", "oat", 2, 5.0) diff --git a/tests/OrderTest.php b/tests/OrderTest.php index 1f41efc..d825c10 100644 --- a/tests/OrderTest.php +++ b/tests/OrderTest.php @@ -70,6 +70,10 @@ public function setUp(): void throw new Exception('Unable to save product 2'); } + // Add stock to the store for the products + $this->dummy_store->addProductStock($product1->getProductID(), 10); + $this->dummy_store->addProductStock($product2->getProductID(), 10); + // Create dummy order line items $this->line_items = [ new OrderProduct($product1->getProductID(), "medium", "oat", 2, 5.0),