Skip to content

Commit

Permalink
Merge pull request #169 from Divyesh000/teststore
Browse files Browse the repository at this point in the history
write tests for Store model
  • Loading branch information
creme332 authored May 19, 2024
2 parents 95f5f76 + 27c39ad commit bd40a63
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/models/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function save(): bool
// remove created_date (let database handle it)
unset($data['created_date']);

// perform insertion to the review table
// perform insertion to the comment table
$inserted_id = $this->insert($data, $this->table);

if ($inserted_id === null) {
Expand Down
35 changes: 22 additions & 13 deletions src/models/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static function getAll(): array
*/
public function save(): bool
{
if (!$this->validate()) {
if (!empty($this->validate())) {
return false;
}

Expand Down Expand Up @@ -126,14 +126,23 @@ public function save(): bool

public function validate(): array
{
$errors = [];
$errors = $this->address->validate();

// Perform phone number length check
if (strlen($this->phone_no) < 7) {
$errors ['phone_no'] = "Phone number must be at least 7 characters long";
}

// Validate latitude and longitude
$latitude = $this->address->getLatitude();
$longitude = $this->address->getLongitude();

if ($latitude == null || $longitude == null ||
($latitude < -90 || $latitude > 90 ||
$longitude < -180 || $longitude > 180)) {
$errors['coordinates'] = "Invalid latitude or longitude.";
}

return $errors;
}

Expand All @@ -157,17 +166,17 @@ public function getProducts(): array
$results = self::query($query, $params);

$products = [];
foreach ($results as $result) {
$products[] = new Product(
$result->product_id,
$result->name,
$result->calories,
$result->img_url,
$result->img_alt_text,
$result->category,
$result->price,
$result->description
);
foreach ($results as $result) {
$products[] = new Product(
$result->product_id,
$result->name,
$result->calories,
$result->img_url,
$result->img_alt_text,
$result->category,
$result->price,
$result->description
);
}
return $products;
}
Expand Down
126 changes: 126 additions & 0 deletions tests/StoreTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

declare(strict_types=1);

use PHPUnit\Framework\TestCase;
use Steamy\Model\Store;
use Steamy\Model\Location;
use Steamy\Core\Database;

class StoreTest extends TestCase
{
use Database;

private ?Store $dummy_store;

/**
* @throws Exception
*/
public function setUp(): void
{
parent::setUp();

// Initialize a dummy store object for testing
$this->dummy_store = new Store(
phone_no: "12345678", // Phone number
address: new Location(
street: "Royal",
city: "Curepipe",
district_id: 1,
latitude: 50,
longitude: 50
)
);

$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);
}
}

public function tearDown(): void
{
// Clean up the dummy store object after each test
if ($this->dummy_store) {
$this->dummy_store = null;
}

// clear all data from store tables
self::query('DELETE FROM store;');
}

/**
* @dataProvider saveDataProvider
* @param string $phone_no
* @param Location $address
* @param bool $expected_success
* @throws Exception
*/
public function testSave(string $phone_no, Location $address, bool $expected_success)
{
$this->dummy_store->setPhoneNo($phone_no);
$this->dummy_store->setAddress($address);

$success = $this->dummy_store->save();

$this->assertEquals($expected_success, $success);
}

public static function saveDataProvider(): array
{
return [
// Valid phone number, valid address
["1234567890", new Location("Royal", "Curepipe", 1, 50, 50), true],
// Invalid phone number (less than 7 characters)
["123456", new Location("Royal", "Curepipe", 1, 50, 50), false],
// Empty phone number
["", new Location("Royal", "Curepipe", 1, 50, 50), false],
// Invalid characters in phone number
["123abc", new Location("Royal", "Curepipe", 1, 50, 50), false],
// Valid address with valid latitude/longitude
["1234567890", new Location("Royal", "Curepipe", 1, 50, 50), true],
// Invalid latitude value (out of range)
["1234567890", new Location("Royal", "Curepipe", 1, -100, 50), false],
];
}

/**
* @dataProvider validateDataProvider
* @param string $phone_no
* @param Location $address
* @param array $expected_errors
*/
public function testValidate(string $phone_no, Location $address, array $expected_errors)
{
$this->dummy_store->setPhoneNo($phone_no);
$this->dummy_store->setAddress($address);

$errors = $this->dummy_store->validate();

$this->assertEquals($expected_errors, $errors);
}

public static function validateDataProvider(): array
{
return [
// Valid phone number, valid address (no errors)
["1234567890", new Location("Royal", "Curepipe", 1, 50, 50), []],
// Invalid phone number (less than 7 characters)
["123456", new Location("Royal", "Curepipe", 1, 50, 50), ["phone_no" => "Phone number must be at least 7 characters long"]],
// Empty phone number
["", new Location("Royal", "Curepipe", 1, 50, 50), ["phone_no" => "Phone number must be at least 7 characters long"]],
// Invalid characters in phone number
["123abc", new Location("Royal", "Curepipe", 1, 50, 50), ["phone_no" => "Phone number must be at least 7 characters long"]],
// Invalid address with invalid latitude/longitude
["1234567890", new Location("Royal", "Curepipe", 1, -100, 50), ["coordinates" => "Invalid latitude or longitude."]],
];
}
}

0 comments on commit bd40a63

Please sign in to comment.