Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
creme332 committed May 21, 2024
1 parent ca690c3 commit 94e6f40
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 8 deletions.
32 changes: 28 additions & 4 deletions tests/OrderProductTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class OrderProductTest extends TestCase
private ?Product $dummy_product;
private array $line_items = [];

/**
* @throws Exception
*/
public function setUp(): void
{
parent::setUp();
Expand Down Expand Up @@ -51,14 +54,30 @@ public function setUp(): void
}

// Create a dummy client
$this->client = new Client("[email protected]", "John", "Doe", "john_doe", "password", new Location("Royal", "Curepipe", 1, 50, 50));
$this->client = new Client(
"[email protected]",
"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');
Expand Down Expand Up @@ -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
Expand All @@ -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());
Expand Down
50 changes: 46 additions & 4 deletions tests/OrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -51,20 +54,45 @@ public function setUp(): void
}

// Create a dummy client
$this->client = new Client("[email protected]", "John", "Doe", "john_doe", "password", new Location("Royal", "Curepipe", 1, 50, 50));
$this->client = new Client(
"[email protected]",
"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');
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -156,13 +189,19 @@ public function testSaveWithEmptyLineItems(): void
$order->save();
}

/**
* @throws Exception
*/
public function testAddLineItem(): void
{
$order = new Order($this->dummy_store->getStoreID(), $this->client->getUserID());
$order->addLineItem(new OrderProduct(1, "medium", "oat", 1, 5.0));
self::assertCount(1, $order->getLineItems());
}

/**
* @throws Exception
*/
public function testGetByID(): void
{
$this->dummy_order->save();
Expand All @@ -179,6 +218,9 @@ public function testGetByID(): void
self::assertNull(Order::getByID(-1));
}

/**
* @throws Exception
*/
public function testCalculateTotalPrice(): void
{
$this->dummy_order->save();
Expand Down

0 comments on commit 94e6f40

Please sign in to comment.