diff --git a/lib/WebGUI/Shop/Cart.pm b/lib/WebGUI/Shop/Cart.pm index 784e1a0f7..e0463d92e 100644 --- a/lib/WebGUI/Shop/Cart.pm +++ b/lib/WebGUI/Shop/Cart.pm @@ -43,7 +43,7 @@ A reference to a subclass of WebGUI::Asset::Sku. sub addItem { my ($self, $sku) = @_; - croak "Need a SKU item." unless (defined $item && $item->isa("WebGUI::Asset::Sku")); + croak "Need a SKU item." unless (defined $sku && $sku->isa("WebGUI::Asset::Sku")); my $item = WebGUI::Shop::CartItem->create( $self, $sku); return $item; } @@ -94,7 +94,7 @@ Removes all items from this cart. sub empty { my ($self) = @_; - foreach my $item = (@{$self->getItems}) { + foreach my $item (@{$self->getItems}) { $item->remove; } } @@ -114,7 +114,7 @@ Any field − returns the value of a field rather than the hash reference. sub get { my ($self, $name) = @_; if (defined $name) { - return $self->properties->{$name}; + return $properties{id $self}{$name}; } my %copyOfHashRef = $properties{id $self}; return \%copyOfHashRef; @@ -169,12 +169,11 @@ The unique id of a cart to instanciate. sub new { my ($class, $session, $cartId) = @_; - croak "Need a session" unless (defined $session && $session->isa("WebGUI::Session"); + croak "Need a session" unless (defined $session && $session->isa("WebGUI::Session")); croak "Need a cartId" unless defined $cartId; my $cart = $session->db->quickHashRef('select * from cart where cartId=?', [$cartId]); croak "No cart with id of $cartId" if ($cart->{cartId} eq ""); - bless my $self, $class; - register $self; + my $self = register $class; my $id = id $self; $session{ $id } = $session; $properties{ $id } = $cart; @@ -204,9 +203,9 @@ The unique id for a shipping address attached to this cart. sub update { my ($self, $newProperties) = @_; my $id = id $self; - $properties{$id}{couponId} = $newProperties->{couponId} || $self->properties->{couponId}; - $properties{$id}{shippingAddressId} = $newProperties->{shippingAddressId} || $self->properties->{shippingAddressId}; - $self->session->db->setRow("cart","cartId",$self->properties); + $properties{$id}{couponId} = $newProperties->{couponId} || $properties{$id}{couponId}; + $properties{$id}{shippingAddressId} = $newProperties->{shippingAddressId} || $properties{$id}{shippingAddressId}; + $self->session->db->setRow("cart","cartId",$properties{$id}); } diff --git a/lib/WebGUI/Shop/CartItem.pm b/lib/WebGUI/Shop/CartItem.pm index 0176dbab7..69b128417 100644 --- a/lib/WebGUI/Shop/CartItem.pm +++ b/lib/WebGUI/Shop/CartItem.pm @@ -81,9 +81,9 @@ sub get { my ($self, $name) = @_; if (defined $name) { if ($name eq "options") { - return JSON::from_json($self->properties->{$name}); + return JSON::from_json($properties{id $self}{$name}); } - return $self->properties->{$name}; + return $properties{id $self}{$name}; } my %copyOfHashRef = $properties{id $self}; return \%copyOfHashRef; @@ -125,7 +125,7 @@ sub incrementQuantity { croak "Cannot have that many in cart."; } $properties{$id}{quantity} += $quantity; - $cart->session->db->setRow("cartItems","itemId", $properties{$id}); + $self->session->db->setRow("cartItems","itemId", $properties{$id}); } @@ -147,13 +147,12 @@ The unique id of the item to instanciate. sub new { my ($class, $cart, $itemId) = @_; - croak "Need a cart" unless (defined $cart && $session->isa("WebGUI::Shop::Cart"); + croak "Need a cart" unless (defined $cart && $cart->isa("WebGUI::Shop::Cart")); croak "Need an itemId" unless defined $itemId; my $item = $cart->session->db->quickHashRef('select * from cart where itemId=?', [$itemId]); croak "No item with id of $itemId" if ($item->{itemId} eq ""); croak "Item $itemId is not in this cart." if ($item->{cartId} ne $cart->getId); - bless my $self, $class; - register $self; + my $self = register $class; my $id = id $self; $cart{ $id } = $cart; $properties{ $id } = $item; @@ -195,12 +194,12 @@ sub update { $newProperties->{options} = $newProperties->{asset}->getOptions; $newProperties->{assetId} = $newProperties->{asset}->getId; } - $properties{$id}{assetId} = $newProperties->{assetId} || $self->properties->{assetId}; + $properties{$id}{assetId} = $newProperties->{assetId} || $properties{$id}{assetId}; if (exists $newProperties->{options} && ref($newProperties->{options}) eq "HASH") { $properties{$id}{options} = JSON::to_json($newProperties->{options}); } - $properties{$id}{shippingAddressId} = $newProperties->{shippingAddressId} || $self->properties->{shippingAddressId}; - $self->session->db->setRow("cart","cartId",$self->properties); + $properties{$id}{shippingAddressId} = $newProperties->{shippingAddressId} || $properties{$id}{shippingAddressId}; + $self->session->db->setRow("cart","cartId",$properties{$id}); } diff --git a/t/Shop/Cart.t b/t/Shop/Cart.t index 8a683e7aa..1495ad5ee 100644 --- a/t/Shop/Cart.t +++ b/t/Shop/Cart.t @@ -30,7 +30,7 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -plan tests => 9; # Increment this number for each test you create +plan tests => 14; # Increment this number for each test you create #---------------------------------------------------------------------------- # put your tests here @@ -41,13 +41,21 @@ isa_ok($cart, "WebGUI::Shop::Cart"); isa_ok($cart->session, "WebGUI::Session"); my $root = WebGUI::Asset->getRoot($session); -my $product = $root->addChild($session, { +my $product = $root->addChild({ className=>"WebGUI::Asset::Sku", title=>"Test Product", }); -$cart->addItem($product, 1); -is(scalar(@{$cart->getItems}), 1, "Added an item to the cart."); +my $item = $cart->addItem($product); +isa_ok($item, "WebGUI::Shop::CartItem"); +isa_ok($item->cart, "WebGUI::Cart", "Does the item have a cart?"); +is(ref($item->get), "HASH", "Do we have a hash of properties?"); + +is($item->get("quantity"), 2, "Should have 1 of these in the cart."); +$item->incrementQuantity(2); +is($item->get("quantity"), 3, "Should have 3 of these in the cart."); +is(scalar(@{$cart->getItems}), 1, "Should have 3 of these in the cart."); + like($cart->getId, qr/[A-Za-z0-9\_\-]{22}/, "Id looks like a guid."); is(ref($cart->get), "HASH", "Cart properties are a hash reference."); @@ -61,6 +69,7 @@ is($session->db->quickScalar("select count(*) from cartItems where cartId=?",[$c $cart->delete; is($cart, undef, "Can destroy cart."); + $product->purge; #---------------------------------------------------------------------------- diff --git a/t/Shop/CartItem.t b/t/Shop/CartItem.t deleted file mode 100644 index b26e329e9..000000000 --- a/t/Shop/CartItem.t +++ /dev/null @@ -1,60 +0,0 @@ -# vim:syntax=perl -#------------------------------------------------------------------- -# WebGUI is Copyright 2001-2008 Plain Black Corporation. -#------------------------------------------------------------------- -# Please read the legal notices (docs/legal.txt) and the license -# (docs/license.txt) that came with this distribution before using -# this software. -#------------------------------------------------------------------ -# http://www.plainblack.com info@plainblack.com -#------------------------------------------------------------------ - -# Write a little about what this script tests. -# -# - -use FindBin; -use strict; -use lib "$FindBin::Bin/../lib"; -use Test::More; -use WebGUI::Test; # Must use this before any other WebGUI modules -use WebGUI::Session; -use WebGUI::Asset; -use WebGUI::Shop::CartItem; - -#---------------------------------------------------------------------------- -# Init -my $session = WebGUI::Test->session; - - -#---------------------------------------------------------------------------- -# Tests - -plan tests => 5; # Increment this number for each test you create - -#---------------------------------------------------------------------------- -# put your tests here - -my $root = WebGUI::Asset->getRoot($session); -my $product = $root->addChild($session, { - className=>"WebGUI::Asset::Sku::Product", - title=>"Test Product", - price=>4.99 - }); - -my $item = WebGUI::Shop::CartItem->create($session, "XXX", $product, 2); -isa_ok($item, "WebGUI::Shop::CartItem"); -isa_ok($item->session, "WebGUI::Session", "did we get a session"); - -is(ref($item->get), "HASH", "Do we have a hash of properties?"); -is($item->get("quantity"), 2, "Should have 2 of these in the cart."); -is($item->delete, undef, "actually deletes the item"); - - -$product->purge; - -#---------------------------------------------------------------------------- -# Cleanup -END { - -}