diff --git a/src/models/Comment.php b/src/models/Comment.php index 286f5dad..09c23c0c 100644 --- a/src/models/Comment.php +++ b/src/models/Comment.php @@ -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) { diff --git a/src/models/Store.php b/src/models/Store.php index 9513182e..e230aa8f 100644 --- a/src/models/Store.php +++ b/src/models/Store.php @@ -96,7 +96,7 @@ public static function getAll(): array */ public function save(): bool { - if (!$this->validate()) { + if (!empty($this->validate())) { return false; } @@ -126,7 +126,6 @@ public function save(): bool public function validate(): array { - $errors = []; $errors = $this->address->validate(); // Perform phone number length check @@ -134,6 +133,16 @@ public function validate(): array $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; } @@ -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; } diff --git a/tests/StoreTest.php b/tests/StoreTest.php new file mode 100644 index 00000000..a560c7d9 --- /dev/null +++ b/tests/StoreTest.php @@ -0,0 +1,126 @@ +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."]], + ]; + } +}