From 4daee5a30b315b0afe3500b4b6f43871c572600e Mon Sep 17 00:00:00 2001 From: divyesh000 Date: Sun, 19 May 2024 09:49:15 +0400 Subject: [PATCH 1/6] tests for Order and OrderProduct models --- tests/OrderProductTest.php | 132 ++++++++++++++++++++++++ tests/OrderTest.php | 206 +++++++++++++++++++++++++++++++++++++ 2 files changed, 338 insertions(+) create mode 100644 tests/OrderProductTest.php create mode 100644 tests/OrderTest.php diff --git a/tests/OrderProductTest.php b/tests/OrderProductTest.php new file mode 100644 index 00000000..cd31dd61 --- /dev/null +++ b/tests/OrderProductTest.php @@ -0,0 +1,132 @@ +dummy_store = new Store( + phone_no: "987654321", + address: new Location( + street: "Augus", + city: "Flacq", + district_id: 2, + latitude: 60, + longitude: 60 + ) + ); + + $success = $this->dummy_store->save(); + if (!$success) { + $errors = $this->dummy_store->validate(); + $error_msg = "Unable to save store to database. "; + if (!empty($errors)) { + $error_msg .= "Errors: " . implode(',', $errors); + } else { + $error_msg .= "Attributes seem to be ok as per validate()."; + } + + throw new Exception($error_msg); + } + + // Create a dummy client + $this->client = new Client("john@example.com", "John", "Doe", "john_doe", "password", new Location("Royal", "Curepipe", 1, 50, 50)); + $success = $this->client->save(); + if (!$success) { + throw new Exception('Unable to save client'); + } + + // Create dummy products + $this->dummy_product = new Product("Latte", 50, "latte.jpeg", "A delicious latte", "Beverage", 5.0, "A cup of latte", new DateTime()); + $success = $this->dummy_product->save(); + if (!$success) { + throw new Exception('Unable to save product'); + } + + // Create a dummy order + $this->dummy_order = new Order($this->dummy_store->getStoreID(), $this->client->getUserID()); + $success = $this->dummy_order->save(); + if (!$success) { + throw new Exception('Unable to save order'); + } + + // Create dummy orderProduct + $this->orderProduct = new OrderProduct( + product_id: $this->dummy_product->getProductID(), + cup_size: "medium", + milk_type: "oat", + quantity: 2, + unit_price: 2.99, + order_id: $this->dummy_order->getOrderID() + ); + $success = $this->orderProduct->save(); + if (!$success) { + throw new Exception('Unable to save order product'); + } + } + + public function tearDown(): void + { + $this->dummy_order = null; + $this->client = null; + $this->dummy_store = null; + $this->dummy_product = null; + $this->orderProduct = null; + + // Clear all data from relevant tables + self::query('DELETE FROM order_product; DELETE FROM `order`; DELETE FROM client; DELETE FROM user; DELETE FROM store_product; DELETE FROM product; DELETE FROM store;'); + } + + public function testValidate(): void + { + $invalidOrderProduct = new OrderProduct( + product_id: $this->dummy_product->getProductID(), + cup_size: "extra large", // Invalid cup size + milk_type: "cow", // Invalid milk type + quantity: -1, // Invalid quantity + unit_price: -2.99, // Invalid unit price + order_id: $this->dummy_order->getOrderID() + ); + + $errors = $invalidOrderProduct->validate(); + + $this->assertArrayHasKey('quantity', $errors); + $this->assertArrayHasKey('cup_size', $errors); + $this->assertArrayHasKey('milk_type', $errors); + $this->assertArrayHasKey('unit_price', $errors); + } + + public function testGetByID(): void + { + $retrievedOrderProduct = OrderProduct::getByID($this->dummy_order->getOrderID(), $this->dummy_product->getProductID()); + + $this->assertNotNull($retrievedOrderProduct); + $this->assertEquals($this->dummy_order->getOrderID(), $retrievedOrderProduct->getOrderID()); + $this->assertEquals($this->dummy_product->getProductID(), $retrievedOrderProduct->getProductID()); + $this->assertEquals("medium", $retrievedOrderProduct->getCupSize()); + $this->assertEquals("oat", $retrievedOrderProduct->getMilkType()); + $this->assertEquals(2, $retrievedOrderProduct->getQuantity()); + $this->assertEquals(2.99, $retrievedOrderProduct->getUnitPrice()); + } +} diff --git a/tests/OrderTest.php b/tests/OrderTest.php new file mode 100644 index 00000000..ddade17c --- /dev/null +++ b/tests/OrderTest.php @@ -0,0 +1,206 @@ +dummy_store = new Store( + phone_no: "987654321", // Phone number + address: new Location( + street: "Augus", + city: "Flacq", + district_id: 2, + latitude: 60, + longitude: 60 + ) + ); + + $success = $this->dummy_store->save(); + if (!$success) { + $errors = $this->dummy_store->validate(); + $error_msg = "Unable to save store to database. "; + if (!empty($errors)) { + $error_msg .= "Errors: " . implode(',', $errors); + } else { + $error_msg .= "Attributes seem to be ok as per validate()."; + } + + throw new Exception($error_msg); + } + + // Create a dummy client + $this->client = new Client("john@example.com", "John", "Doe", "john_doe", "password", new Location( "Royal", "Curepipe", 1, 50, 50)); + $success = $this->client->save(); + if (!$success) { + throw new Exception('Unable to save client'); + } + + // Create dummy products + $product1 = new Product("Latte", 50, "latte.jpeg", "A delicious latte", "Beverage", 5.0, "A cup of latte", new DateTime()); + $success = $product1->save(); + if (!$success) { + throw new Exception('Unable to save product 1'); + } + + $product2 = new Product("Espresso", 30, "espresso.jpeg", "A strong espresso", "Beverage", 3.0, "A cup of espresso", new DateTime()); + $success = $product2->save(); + if (!$success) { + throw new Exception('Unable to save product 2'); + } + + // Create dummy order line items + $this->line_items = [ + new OrderProduct($product1->getProductID(), "medium", "oat", 2, 2.99), + new OrderProduct($product2->getProductID(), "small", "almond", 1, 4.99) + ]; + + // Create a dummy order + $this->dummy_order = new Order( + $this->dummy_store->getStoreID(), + $this->client->getUserID(), + $this->line_items + ); + } + + /** + * Tear down test data + */ + public function tearDown(): void + { + $this->dummy_order = null; + $this->client = null; + $this->dummy_store = null; + $this->line_items = []; + + // Clear all data from relevant tables + self::query('DELETE FROM order_product; DELETE FROM `order`; DELETE FROM client; DELETE FROM user; DELETE FROM store_product; DELETE FROM product; DELETE FROM store;'); + } + + public function testConstructor(): void + { + $new_order = new Order( + $this->dummy_store->getStoreID(), + $this->client->getUserID(), + $this->line_items + ); + + self::assertEquals($this->dummy_store->getStoreID(), $new_order->getStoreID()); + self::assertEquals($this->client->getUserID(), $new_order->getClientID()); + self::assertEquals(OrderStatus::PENDING, $new_order->getStatus()); + self::assertEquals($this->line_items, $new_order->getLineItems()); + } + + public function testToArray(): void + { + $result = $this->dummy_order->toArray(); + + self::assertArrayHasKey('order_id', $result); + self::assertArrayHasKey('status', $result); + self::assertArrayHasKey('created_date', $result); + self::assertArrayHasKey('pickup_date', $result); + self::assertArrayHasKey('client_id', $result); + self::assertArrayHasKey('store_id', $result); + + self::assertEquals($this->dummy_order->getOrderID(), $result['order_id']); + self::assertEquals($this->dummy_order->getStatus()->value, $result['status']); + self::assertEquals($this->dummy_order->getCreatedDate()->format('Y-m-d H:i:s'), $result['created_date']); + self::assertEquals($this->dummy_order->getPickupDate()?->format('Y-m-d H:i:s'), $result['pickup_date']); + self::assertEquals($this->dummy_order->getClientID(), $result['client_id']); + self::assertEquals($this->dummy_order->getStoreID(), $result['store_id']); + } + + public function testSave(): void + { + $success = $this->dummy_order->save(); + self::assertTrue($success); + + $order_id = $this->dummy_order->getOrderID(); + self::assertGreaterThan(0, $order_id); + + // Verify order in database + $saved_order = Order::getByID($order_id); + self::assertNotNull($saved_order); + self::assertEquals($this->dummy_order->getStoreID(), $saved_order->getStoreID()); + self::assertEquals($this->dummy_order->getClientID(), $saved_order->getClientID()); + } + + public function testSaveWithEmptyLineItems(): void + { + $this->expectException(Exception::class); + $this->expectExceptionMessage('Cart cannot be empty'); + + $order = new Order($this->dummy_store->getStoreID(), $this->client->getUserID(), []); + $order->save(); + } + + public function testAddLineItem(): void + { + $order = new Order($this->dummy_store->getStoreID(), $this->client->getUserID()); + $order->addLineItem(new OrderProduct(1, "medium", "whole", 1)); + self::assertCount(1, $order->getLineItems()); + } + + public function testGetByID(): void + { + $this->dummy_order->save(); + $order_id = $this->dummy_order->getOrderID(); + + $fetched_order = Order::getByID($order_id); + self::assertNotNull($fetched_order); + + self::assertEquals($this->dummy_order->getStoreID(), $fetched_order->getStoreID()); + self::assertEquals($this->dummy_order->getClientID(), $fetched_order->getClientID()); + self::assertEquals($this->dummy_order->getStatus(), $fetched_order->getStatus()); + + // Test getByID with invalid ID + self::assertNull(Order::getByID(-1)); + } + + public function testCalculateTotalPrice(): void + { + $this->dummy_order->save(); + $total_price = $this->dummy_order->calculateTotalPrice(); + + $expected_price = array_reduce($this->line_items, function($carry, $item) { + return $carry + $item->getQuantity() * $item->getUnitPrice(); + }, 0); + + self::assertEquals($expected_price, $total_price); + } + + public function testValidate(): void + { + $errors = $this->dummy_order->validate(); + self::assertEmpty($errors); + + // Test invalid status + $invalid_order = new Order($this->dummy_store->getStoreID(), $this->client->getUserID(), $this->line_items, null, null, OrderStatus::from('INVALID')); + $errors = $invalid_order->validate(); + self::assertNotEmpty($errors); + self::assertArrayHasKey('status', $errors); + self::assertEquals('Status must be one of: pending, cancelled, completed', $errors['status']); + } + +} From 809bb3c1adab8e4b2557eccb1ffb51a4d5d0ee7b Mon Sep 17 00:00:00 2001 From: divyesh000 Date: Sun, 19 May 2024 22:56:40 +0400 Subject: [PATCH 2/6] try to solve the errors getting --- src/models/Order.php | 4 ++++ tests/OrderProductTest.php | 45 +++++++++++++++++++------------------- tests/OrderTest.php | 33 ++++++++++------------------ 3 files changed, 38 insertions(+), 44 deletions(-) diff --git a/src/models/Order.php b/src/models/Order.php index 5a88b5d9..9b389a13 100644 --- a/src/models/Order.php +++ b/src/models/Order.php @@ -194,6 +194,10 @@ public function save(): bool */ public function addLineItem(OrderProduct $orderProduct): void { + $errors = $orderProduct->validate(); + if (!empty($errors)) { + throw new Exception("Invalid line item: " . json_encode($errors)); + } $this->line_items[] = $orderProduct; } diff --git a/tests/OrderProductTest.php b/tests/OrderProductTest.php index cd31dd61..aecc0902 100644 --- a/tests/OrderProductTest.php +++ b/tests/OrderProductTest.php @@ -19,15 +19,15 @@ class OrderProductTest extends TestCase private ?Client $client; private ?Store $dummy_store; private ?Product $dummy_product; - private ?OrderProduct $orderProduct; + private array $line_items = []; public function setUp(): void { parent::setUp(); - + // Initialize a dummy store object for testing $this->dummy_store = new Store( - phone_no: "987654321", + phone_no: "987654321", // Phone number address: new Location( street: "Augus", city: "Flacq", @@ -57,32 +57,32 @@ public function setUp(): void throw new Exception('Unable to save client'); } - // Create dummy products + // Create a dummy product $this->dummy_product = new Product("Latte", 50, "latte.jpeg", "A delicious latte", "Beverage", 5.0, "A cup of latte", new DateTime()); $success = $this->dummy_product->save(); if (!$success) { throw new Exception('Unable to save product'); } + // Create dummy order line items + $this->line_items = [ + new OrderProduct($this->dummy_product->getProductID(), "medium", "oat", 2, 5.0) + ]; + // Create a dummy order - $this->dummy_order = new Order($this->dummy_store->getStoreID(), $this->client->getUserID()); - $success = $this->dummy_order->save(); - if (!$success) { - throw new Exception('Unable to save order'); + $this->dummy_order = new Order( + $this->dummy_store->getStoreID(), + $this->client->getUserID() + ); + + // Add line items to the order + foreach ($this->line_items as $line_item) { + $this->dummy_order->addLineItem($line_item); } - // Create dummy orderProduct - $this->orderProduct = new OrderProduct( - product_id: $this->dummy_product->getProductID(), - cup_size: "medium", - milk_type: "oat", - quantity: 2, - unit_price: 2.99, - order_id: $this->dummy_order->getOrderID() - ); - $success = $this->orderProduct->save(); + $success = $this->dummy_order->save(); if (!$success) { - throw new Exception('Unable to save order product'); + throw new Exception('Unable to save order'); } } @@ -92,7 +92,7 @@ public function tearDown(): void $this->client = null; $this->dummy_store = null; $this->dummy_product = null; - $this->orderProduct = null; + $this->line_items = []; // Clear all data from relevant tables self::query('DELETE FROM order_product; DELETE FROM `order`; DELETE FROM client; DELETE FROM user; DELETE FROM store_product; DELETE FROM product; DELETE FROM store;'); @@ -103,7 +103,7 @@ public function testValidate(): void $invalidOrderProduct = new OrderProduct( product_id: $this->dummy_product->getProductID(), cup_size: "extra large", // Invalid cup size - milk_type: "cow", // Invalid milk type + milk_type: "invalid milk", // Invalid milk type quantity: -1, // Invalid quantity unit_price: -2.99, // Invalid unit price order_id: $this->dummy_order->getOrderID() @@ -119,6 +119,7 @@ public function testValidate(): void public function testGetByID(): void { + // Assuming getByID is a method that retrieves an OrderProduct by order ID and product ID $retrievedOrderProduct = OrderProduct::getByID($this->dummy_order->getOrderID(), $this->dummy_product->getProductID()); $this->assertNotNull($retrievedOrderProduct); @@ -127,6 +128,6 @@ public function testGetByID(): void $this->assertEquals("medium", $retrievedOrderProduct->getCupSize()); $this->assertEquals("oat", $retrievedOrderProduct->getMilkType()); $this->assertEquals(2, $retrievedOrderProduct->getQuantity()); - $this->assertEquals(2.99, $retrievedOrderProduct->getUnitPrice()); + $this->assertEquals(5.0, $retrievedOrderProduct->getUnitPrice()); } } diff --git a/tests/OrderTest.php b/tests/OrderTest.php index ddade17c..1f41efc0 100644 --- a/tests/OrderTest.php +++ b/tests/OrderTest.php @@ -8,18 +8,18 @@ use Steamy\Model\OrderStatus; use Steamy\Model\Store; use Steamy\Model\Client; -use Steamy\Model\Product; use Steamy\Core\Database; use Steamy\Model\Location; +use Steamy\Model\Product; + class OrderTest extends TestCase { use Database; - private ?Order $dummy_order; - private ?Client $client; - private ?Store $dummy_store; - private array $line_items; - + private ?Order $dummy_order = null; + private ?Client $client = null; + private ?Store $dummy_store = null; + private array $line_items = []; public function setUp(): void { @@ -51,7 +51,7 @@ public function setUp(): void } // Create a dummy client - $this->client = new Client("john@example.com", "John", "Doe", "john_doe", "password", new Location( "Royal", "Curepipe", 1, 50, 50)); + $this->client = new Client("john@example.com", "John", "Doe", "john_doe", "password", new Location("Royal", "Curepipe", 1, 50, 50)); $success = $this->client->save(); if (!$success) { throw new Exception('Unable to save client'); @@ -72,8 +72,8 @@ public function setUp(): void // Create dummy order line items $this->line_items = [ - new OrderProduct($product1->getProductID(), "medium", "oat", 2, 2.99), - new OrderProduct($product2->getProductID(), "small", "almond", 1, 4.99) + new OrderProduct($product1->getProductID(), "medium", "oat", 2, 5.0), + new OrderProduct($product2->getProductID(), "small", "almond", 1, 3.0) ]; // Create a dummy order @@ -84,9 +84,6 @@ public function setUp(): void ); } - /** - * Tear down test data - */ public function tearDown(): void { $this->dummy_order = null; @@ -158,7 +155,7 @@ public function testSaveWithEmptyLineItems(): void public function testAddLineItem(): void { $order = new Order($this->dummy_store->getStoreID(), $this->client->getUserID()); - $order->addLineItem(new OrderProduct(1, "medium", "whole", 1)); + $order->addLineItem(new OrderProduct(1, "medium", "oat", 1, 5.0)); self::assertCount(1, $order->getLineItems()); } @@ -183,7 +180,7 @@ public function testCalculateTotalPrice(): void $this->dummy_order->save(); $total_price = $this->dummy_order->calculateTotalPrice(); - $expected_price = array_reduce($this->line_items, function($carry, $item) { + $expected_price = array_reduce($this->line_items, function ($carry, $item) { return $carry + $item->getQuantity() * $item->getUnitPrice(); }, 0); @@ -194,13 +191,5 @@ public function testValidate(): void { $errors = $this->dummy_order->validate(); self::assertEmpty($errors); - - // Test invalid status - $invalid_order = new Order($this->dummy_store->getStoreID(), $this->client->getUserID(), $this->line_items, null, null, OrderStatus::from('INVALID')); - $errors = $invalid_order->validate(); - self::assertNotEmpty($errors); - self::assertArrayHasKey('status', $errors); - self::assertEquals('Status must be one of: pending, cancelled, completed', $errors['status']); } - } From 9327d5fc41ac720fa416368c88ab3d4a9cd3dde7 Mon Sep 17 00:00:00 2001 From: creme332 <65414576+creme332@users.noreply.github.com> Date: Mon, 20 May 2024 10:42:03 +0400 Subject: [PATCH 3/6] use empty() to check if there are no errors with line item --- src/models/Order.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/models/Order.php b/src/models/Order.php index 9b389a13..9c922409 100644 --- a/src/models/Order.php +++ b/src/models/Order.php @@ -121,7 +121,7 @@ public function save(): bool $update_stock_stm = $conn->prepare($query); foreach ($this->line_items as $line_item) { - if (!$line_item->validate()) { + if (!empty($line_item->validate())) { // line item contains invalid attributes $conn->rollBack(); $conn = null; @@ -191,6 +191,7 @@ public function save(): bool * * @param OrderProduct $orderProduct * @return void + * @throws Exception */ public function addLineItem(OrderProduct $orderProduct): void { From ca690c334942109909ba72f23af8d10f23788335 Mon Sep 17 00:00:00 2001 From: divyesh000 Date: Mon, 20 May 2024 19:10:42 +0400 Subject: [PATCH 4/6] 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 e230aa8f..18a96b90 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 aecc0902..7104b4b9 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 1f41efc0..d825c104 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), From 94e6f404b864a8ddf167aa63df0f459b09327910 Mon Sep 17 00:00:00 2001 From: creme332 <65414576+creme332@users.noreply.github.com> Date: Tue, 21 May 2024 11:13:56 +0400 Subject: [PATCH 5/6] refactor --- tests/OrderProductTest.php | 32 +++++++++++++++++++++--- tests/OrderTest.php | 50 +++++++++++++++++++++++++++++++++++--- 2 files changed, 74 insertions(+), 8 deletions(-) diff --git a/tests/OrderProductTest.php b/tests/OrderProductTest.php index 7104b4b9..28508968 100644 --- a/tests/OrderProductTest.php +++ b/tests/OrderProductTest.php @@ -21,6 +21,9 @@ class OrderProductTest extends TestCase private ?Product $dummy_product; private array $line_items = []; + /** + * @throws Exception + */ public function setUp(): void { parent::setUp(); @@ -51,14 +54,30 @@ public function setUp(): void } // Create a dummy client - $this->client = new Client("john@example.com", "John", "Doe", "john_doe", "password", new Location("Royal", "Curepipe", 1, 50, 50)); + $this->client = new Client( + "john@example.com", + "John", + "Doe", + "john_doe", + "password", + new Location("Royal", "Curepipe", 1, 50, 50) + ); $success = $this->client->save(); if (!$success) { throw new Exception('Unable to save client'); } // Create a dummy product - $this->dummy_product = new Product("Latte", 50, "latte.jpeg", "A delicious latte", "Beverage", 5.0, "A cup of latte", new DateTime()); + $this->dummy_product = new Product( + "Latte", + 50, + "latte.jpeg", + "A delicious latte", + "Beverage", + 5.0, + "A cup of latte", + new DateTime() + ); $success = $this->dummy_product->save(); if (!$success) { throw new Exception('Unable to save product'); @@ -98,7 +117,9 @@ public function tearDown(): void $this->line_items = []; // Clear all data from relevant tables - self::query('DELETE FROM order_product; DELETE FROM `order`; DELETE FROM client; DELETE FROM user; DELETE FROM store_product; DELETE FROM product; DELETE FROM store;'); + self::query( + 'DELETE FROM order_product; DELETE FROM `order`; DELETE FROM client; DELETE FROM user; DELETE FROM store_product; DELETE FROM product; DELETE FROM store;' + ); } public function testValidate(): void @@ -123,7 +144,10 @@ public function testValidate(): void public function testGetByID(): void { // Assuming getByID is a method that retrieves an OrderProduct by order ID and product ID - $retrievedOrderProduct = OrderProduct::getByID($this->dummy_order->getOrderID(), $this->dummy_product->getProductID()); + $retrievedOrderProduct = OrderProduct::getByID( + $this->dummy_order->getOrderID(), + $this->dummy_product->getProductID() + ); $this->assertNotNull($retrievedOrderProduct); $this->assertEquals($this->dummy_order->getOrderID(), $retrievedOrderProduct->getOrderID()); diff --git a/tests/OrderTest.php b/tests/OrderTest.php index d825c104..0bbda2a0 100644 --- a/tests/OrderTest.php +++ b/tests/OrderTest.php @@ -21,6 +21,9 @@ class OrderTest extends TestCase private ?Store $dummy_store = null; private array $line_items = []; + /** + * @throws Exception + */ public function setUp(): void { parent::setUp(); @@ -51,20 +54,45 @@ public function setUp(): void } // Create a dummy client - $this->client = new Client("john@example.com", "John", "Doe", "john_doe", "password", new Location("Royal", "Curepipe", 1, 50, 50)); + $this->client = new Client( + "john@example.com", + "John", + "Doe", + "john_doe", + "password", + new Location("Royal", "Curepipe", 1, 50, 50) + ); $success = $this->client->save(); if (!$success) { throw new Exception('Unable to save client'); } // Create dummy products - $product1 = new Product("Latte", 50, "latte.jpeg", "A delicious latte", "Beverage", 5.0, "A cup of latte", new DateTime()); + $product1 = new Product( + "Latte", + 50, + "latte.jpeg", + "A delicious latte", + "Beverage", + 5.0, + "A cup of latte", + new DateTime() + ); $success = $product1->save(); if (!$success) { throw new Exception('Unable to save product 1'); } - $product2 = new Product("Espresso", 30, "espresso.jpeg", "A strong espresso", "Beverage", 3.0, "A cup of espresso", new DateTime()); + $product2 = new Product( + "Espresso", + 30, + "espresso.jpeg", + "A strong espresso", + "Beverage", + 3.0, + "A cup of espresso", + new DateTime() + ); $success = $product2->save(); if (!$success) { throw new Exception('Unable to save product 2'); @@ -96,7 +124,9 @@ public function tearDown(): void $this->line_items = []; // Clear all data from relevant tables - self::query('DELETE FROM order_product; DELETE FROM `order`; DELETE FROM client; DELETE FROM user; DELETE FROM store_product; DELETE FROM product; DELETE FROM store;'); + self::query( + 'DELETE FROM order_product; DELETE FROM `order`; DELETE FROM client; DELETE FROM user; DELETE FROM store_product; DELETE FROM product; DELETE FROM store;' + ); } public function testConstructor(): void @@ -132,6 +162,9 @@ public function testToArray(): void self::assertEquals($this->dummy_order->getStoreID(), $result['store_id']); } + /** + * @throws Exception + */ public function testSave(): void { $success = $this->dummy_order->save(); @@ -156,6 +189,9 @@ public function testSaveWithEmptyLineItems(): void $order->save(); } + /** + * @throws Exception + */ public function testAddLineItem(): void { $order = new Order($this->dummy_store->getStoreID(), $this->client->getUserID()); @@ -163,6 +199,9 @@ public function testAddLineItem(): void self::assertCount(1, $order->getLineItems()); } + /** + * @throws Exception + */ public function testGetByID(): void { $this->dummy_order->save(); @@ -179,6 +218,9 @@ public function testGetByID(): void self::assertNull(Order::getByID(-1)); } + /** + * @throws Exception + */ public function testCalculateTotalPrice(): void { $this->dummy_order->save(); From bc11eeaa2ccd512fcd5f48ae6b97807a7d0bd3e7 Mon Sep 17 00:00:00 2001 From: creme332 <65414576+creme332@users.noreply.github.com> Date: Tue, 21 May 2024 11:15:32 +0400 Subject: [PATCH 6/6] add doc to addProductStock and getProductStock, fix return value of addProductStock --- src/models/Store.php | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/models/Store.php b/src/models/Store.php index 18a96b90..7bb82bef 100644 --- a/src/models/Store.php +++ b/src/models/Store.php @@ -146,16 +146,30 @@ public function validate(): array return $errors; } + /** + * Increments stock of a product + * @param int $product_id ID of product whose stock will increase + * @param int $quantity Amount by which stock increases + * @return bool Success or not + */ public function addProductStock(int $product_id, int $quantity): bool { + $conn = self::connect(); $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); + $stm = $conn->prepare($query); + $stm->execute($params); - return $result; + $rows_affected = $stm->rowCount(); + $conn = null; + return $rows_affected === 1; } + /** + * @param int $product_id + * @return int Stock level of product. Defaults to 0. + */ public function getProductStock(int $product_id): int { $query = "SELECT stock_level FROM store_product WHERE store_id = :store_id AND product_id = :product_id;"; @@ -169,6 +183,9 @@ public function getProductStock(int $product_id): int } } + /** + * @return Product[] Array of products which store sells. + */ public function getProducts(): array { $query = "SELECT p.* FROM product p JOIN store_product sp ON p.product_id = sp.product_id WHERE sp.store_id = :store_id;";