Skip to content

Commit

Permalink
Merge pull request #214 from Divyesh000/addtestproduct
Browse files Browse the repository at this point in the history
update tests for Product
  • Loading branch information
creme332 authored Jun 2, 2024
2 parents 6ee328a + defab0a commit 0ed4435
Show file tree
Hide file tree
Showing 5 changed files with 481 additions and 114 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"vlucas/phpdotenv": "^5.6",
"phpmailer/phpmailer": "^6.9",
"opis/json-schema": "^2.3",
"nesbot/carbon": "^3.3"
"nesbot/carbon": "^3.3",
"fakerphp/faker": "^1.23"
},
"scripts": {
"test": "phpunit tests"
Expand Down
118 changes: 117 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/models/Review.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ public function validate(): array
$errors['text'] = "Review text must have at least 2 characters";
}

if (strlen($this->text) > 2000) {
$errors['text'] = "Review text must have at most 2000 characters";
}

if (!filter_var($this->rating, FILTER_VALIDATE_INT, [
"options" => [
"min_range" => Review::MIN_RATING,
Expand Down
158 changes: 147 additions & 11 deletions tests/ProductTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,38 @@
use Steamy\Model\Product;
use Steamy\Model\Review;
use Steamy\Core\Database;
use Steamy\Model\Location;
use Steamy\Model\Client;


final class ProductTest extends TestCase
{
use Database;

private ?Product $dummy_product;
private ?Client $dummy_client;
private ?Review $dummy_review;

/**
* @throws Exception
*/
public function setUp(): void
{
$address = new Location("Royal Road", "Curepipe", 1);
$this->dummy_client = new Client(
"[email protected]",
"john",
"johnny",
"abcd",
"13213431",
$address
);

$success = $this->dummy_client->save();
if (!$success) {
throw new Exception('Unable to save client');
}

// Create a dummy product for testing
$this->dummy_product = new Product(
"Velvet Bean",
Expand All @@ -26,19 +49,35 @@ public function setUp(): void
"Each bottle contains 90% Pure Coffee powder and 10% Velvet bean Powder",
new DateTime()
);

$success = $this->dummy_product->save();
if (!$success) {
throw new Exception('Unable to save product');
}

// Create a review object and save to the database
$this->dummy_review = new Review(
product_id: $this->dummy_product->getProductID(),
client_id: $this->dummy_client->getUserID(),
text: "This is a test review.",
rating: 5,
created_date: new DateTime()
);
$success = $this->dummy_review->save();

if (!$success) {
throw new Exception('Unable to save review');
}
}

public function tearDown(): void
{
$this->dummy_product = null;
$this->dummy_client = null;
$this->dummy_review = null;

// Clear all data from product tables
self::query('DELETE FROM store_product; DELETE FROM product;');
// Clear all data from product, review, and client tables
self::query('DELETE FROM review; DELETE FROM product; DELETE FROM client; DELETE FROM user;');
}

public function testConstructor(): void
Expand All @@ -50,14 +89,20 @@ public function testConstructor(): void
self::assertEquals("Velvet Bean Image", $this->dummy_product->getImgAltText());
self::assertEquals("Velvet", $this->dummy_product->getCategory());
self::assertEquals(6.50, $this->dummy_product->getPrice());
self::assertEquals("Each bottle contains 90% Pure Coffee powder and 10% Velvet bean Powder", $this->dummy_product->getDescription());
self::assertInstanceOf(DateTime::class, $this->dummy_product->getCreatedDate()); // Check if created_date is an instance of DateTime
self::assertEquals(
"Each bottle contains 90% Pure Coffee powder and 10% Velvet bean Powder",
$this->dummy_product->getDescription()
);
self::assertInstanceOf(
DateTime::class,
$this->dummy_product->getCreatedDate()
); // Check if created_date is an instance of DateTime
}

public function testToArray(): void
{
$result = $this->dummy_product->toArray();

// Check if all required keys are present
$this->assertArrayHasKey('product_id', $result);
$this->assertArrayHasKey('name', $result);
Expand All @@ -68,35 +113,126 @@ public function testToArray(): void
$this->assertArrayHasKey('price', $result);
$this->assertArrayHasKey('description', $result);
$this->assertArrayHasKey('created_date', $result); // Ensure created_date is included in toArray result

// Check if the actual values are correct
self::assertEquals("Velvet Bean", $result['name']);
self::assertEquals(70, $result['calories']);
self::assertEquals("Velvet.jpeg", $result['img_url']);
self::assertEquals("Velvet Bean Image", $result['img_alt_text']);
self::assertEquals("Velvet", $result['category']);
self::assertEquals(6.50, $result['price']);
self::assertEquals("Each bottle contains 90% Pure Coffee powder and 10% Velvet bean Powder", $result['description']);
self::assertInstanceOf(DateTime::class, $result['created_date']); // Check if created_date is an instance of DateTime
self::assertEquals(
"Each bottle contains 90% Pure Coffee powder and 10% Velvet bean Powder",
$result['description']
);
self::assertInstanceOf(
DateTime::class,
$result['created_date']
); // Check if created_date is an instance of DateTime
}

public function testSave(): void
{
// Save the dummy product
$result = $this->dummy_product->save();

// Check if the product was saved successfully
self::assertTrue($result); // Assert that save() returns true upon successful save
self::assertNotNull($this->dummy_product->getProductID());
}

public function testValidate(): void
{
// Validate the dummy product
$errors = $this->dummy_product->validate();

// Check if there are no validation errors
$this->assertEmpty($errors);

$this->markTestIncomplete(
'This test lacks test cases, ...',
);
}

public function testGetRatingDistribution(): void
{
$distribution = $this->dummy_product->getRatingDistribution();

// Check if the distribution contains the expected keys and values
$this->assertArrayHasKey(5, $distribution);
$this->assertEquals(100.0, $distribution[5]); // 1 out of 1 reviews is 5 stars

$this->markTestIncomplete(
'This test lacks test cases, ...',
);
}

public function testDeleteProduct(): void
{
$product_id = $this->dummy_product->getProductID();
$result = $this->dummy_product->deleteProduct();

// Check if the product was deleted successfully
$this->assertTrue($result);

// Check if the product no longer exists in the database
$product = Product::getByID($product_id);
$this->assertNull($product);

$this->markTestIncomplete(
'This test lacks test cases, ...',
);
}

public function testUpdateProduct(): void
{
$newData = [
'name' => 'Updated Velvet Bean',
'calories' => 75,
'img_url' => 'UpdatedVelvet.jpeg',
'img_alt_text' => 'Updated Velvet Bean Image',
'category' => 'Updated Velvet',
'price' => 7.00,
'description' => 'Updated description'
];

$result = $this->dummy_product->updateProduct($newData);

// Check if the product was updated successfully
$this->assertTrue($result);

// Reload the product from the database and check the updated values
$updatedProduct = Product::getByID($this->dummy_product->getProductID());
$this->assertEquals('Updated Velvet Bean', $updatedProduct->getName());
$this->assertEquals(75, $updatedProduct->getCalories());
$this->assertEquals('UpdatedVelvet.jpeg', $updatedProduct->getImgRelativePath());
$this->assertEquals('Updated Velvet Bean Image', $updatedProduct->getImgAltText());
$this->assertEquals('Updated Velvet', $updatedProduct->getCategory());
$this->assertEquals(7.00, $updatedProduct->getPrice());
$this->assertEquals('Updated description', $updatedProduct->getDescription());
}

public function testGetAverageRating(): void
{
$averageRating = $this->dummy_product->getAverageRating();

$this->assertNotEquals(999.0, $averageRating);

$this->markTestIncomplete(
'This test lacks test cases, ...',
);
}

public function testGetReviews(): void
{
$reviews = $this->dummy_product->getReviews();

// Check if reviews are returned
$this->assertNotEmpty($reviews);

// Check if the reviews contain the expected values
$this->assertCount(1, $reviews);
$this->assertEquals('This is a test review.', $reviews[0]->getText());
$this->assertEquals(5, $reviews[0]->getRating());
}
}
Loading

0 comments on commit 0ed4435

Please sign in to comment.