cp /data/htdocs/cs637/cart.php .). Turn it into a class Cart. Use an associative array $itemQty as a property of Cart to hold the contents of the cart (in skeletal form), with key product_id, value quantity ordered. Create an empty array in the cart constructor, and code the methods. As an example,
addItem($product_id, $quantity)is implemented simply by
$this->itemQty[$product_id] = $quantity. Although you might be tempted to use Product objects instead of product_ids to represent the cart contents, remember that the cart will be a session object and so should not have unnecessary objects: the full Product is easily obtained from the database using the product_id.
php TestCart1.php. This is a case of unit testing because we are testing outside the whole application, concentrating on one class. Note that when all the sources are in one directory, Netbeans can provide auto-completion on object properties and methods. It is possible but difficult to set this up when using code in multiple directories. Just pause a little after typing "->" and see a pop-up list of possible completions to choose from.
Write TestCart2.php and mock_product_db.php to test this. TestCart2.php should set up the basic cart as the first part of TestCart1, and then call at least getSubtotal. mock_product_db.php has class ProductDB, but it is a fake one. See https://en.wikipedia.org/wiki/Mock_object for more info on mocks. The mock ProductDB class has only one method getProduct($product_id) that tests product_id for values 1 and 2 and returns a Product object for one of these values, new Product($category, 'strat', 'Stratocaster', 700) for product id 1 or new Product($category, 'sg', 'SG', 2500) for product id 2. Show the .php files and a run of your test in your homework paper. The subtotal should be 2*700+3*2500 = 6230. You will need requires for 5 php files in TestCart2.php, luckily all in the model directory. Show your new methods for cart.php, your mock ProductDB code, and your run of TestCart2.php in your homework paper.
PHP Objects, System Testing. Convert the two files in directory cart (the ones copied from ch24_guitar_shop/cart) to work with your Cart class. In the controller, regardless of the $action value, you need to check if there is a cart object in the session, and create a new one if necessary. The the various calls for cart action need to be fixed. Additionally, before or after add_item is called in index.php, the code needs to set $_SESSION['last_category_id'] and $_SESSION['last_category_name'] for use by the cart_view.php (for this and later request cycles). This requires calling ProductDB::get_product($product_id) to find details on this product and its category. Similarly fix up cart_view.php. Deliver this project to your directory under /var/www/html/cs637 on topcat, specifically, to /var/www/html/cs637/username/ch14_guitar_shop. In your homework paper, explain any problems you had and say whether you were successful in getting this to work properly. We may examine and test the projects on topcat or just verify they are there.
If you see a response code of 304 for pizzapie.jpg and the lack of Content-Length (or content, look at Response tab), don't be dismayed. The image has a "conditional get" with a last-modified date (See If-Modified-Since request header), and since it hasn't been modified, the content is not returned. The browser has the content already.