Permalink
Browse files

Test

  • Loading branch information...
vrana committed Aug 12, 2012
1 parent 735c635 commit 07d4f587839c44feb43b21331e0a85deb040e5e0
Showing with 41 additions and 7 deletions.
  1. +1 −0 functions.inc.php
  2. +17 −0 model.inc.php
  3. +2 −7 order.php
  4. +21 −0 test/order.phpt
View
@@ -109,3 +109,4 @@ function getOrdered(array $products_ids) {
}
include "./constants.inc.php";
+include "./model.inc.php";
View
@@ -0,0 +1,17 @@
+<?php
+
+/**
+* @param array ($id => array("amount" => $amount, "visible" => $visible))
+* @param array ($id => $amount)
+* @return string error
+*/
+function validateOrder($products, $basket) {
+ foreach ($basket as $id => $amount) {
+ if (!array_key_exists($id, $products) || ($products[$id]["amount"] !== null && $products[$id]["amount"] < $amount)) {
+ return "Not enough supply.";
+ }
+ if (!$products[$id]["visible"]) {
+ return "Product is not visible.";
+ }
+ }
+}
View
@@ -21,15 +21,10 @@
query("START TRANSACTION");
$products = array();
- foreach (query("SELECT id, name, amount, price FROM products WHERE id IN (%s) AND visible = 1 LOCK IN SHARE MODE", array_keys($basket)) as $row) {
+ foreach (query("SELECT id, name, amount, price, visible FROM products WHERE id IN (%s) AND visible = 1 LOCK IN SHARE MODE", array_keys($basket)) as $row) {
$products[$row["id"]] = $row;
}
- foreach ($basket as $id => $amount) {
- if (!array_key_exists($id, $products) || ($products[$id]["amount"] !== null && $products[$id]["amount"] < $amount)) {
- $error = "<p>Not enough supply.</p>\n";
- break;
- }
- }
+ $error = validateOrder($products, $basket);
if (!$error) {
$ok = query(
View
@@ -0,0 +1,21 @@
+--TEST--
+Validate order
+--FILE--
+<?php
+include dirname(__FILE__) . "/../model.inc.php";
+
+$products = array(
+ 1 => array("amount" => 10, "visible" => 1),
+ 2 => array("amount" => 10, "visible" => 0),
+);
+
+var_dump(!validateOrder($products, array(1 => 2)));
+var_dump(!validateOrder($products, array(2 => 2)));
+var_dump(!validateOrder($products, array(1 => 20)));
+var_dump(!validateOrder($products, array(2 => 20)));
+?>
+--EXPECTF--
+bool(true)
+bool(false)
+bool(false)
+bool(false)

0 comments on commit 07d4f58

Please sign in to comment.