trying to add more exceptions

This commit is contained in:
JT Smith 2008-03-19 23:23:51 +00:00
parent ec3d2f1eb0
commit 309b16ca09
2 changed files with 31 additions and 3 deletions

View file

@ -51,6 +51,9 @@ A reference to a subclass of WebGUI::Asset::Sku.
sub addItem { sub addItem {
my ($self, $sku) = @_; my ($self, $sku) = @_;
unless (defined $sku && $sku->isa("WebGUI::Asset::Sku")) {
WebGUI::Error::InvalidObject->throw(expected=>"WebGUI::Asset::Sku", got=>(ref $sku), error=>"Need a sku.");
}
my $item = WebGUI::Shop::CartItem->create( $self, $sku); my $item = WebGUI::Shop::CartItem->create( $self, $sku);
return $item; return $item;
} }
@ -142,6 +145,9 @@ The number to format.
sub formatCurrency { sub formatCurrency {
my ($self, $amount) = @_; my ($self, $amount) = @_;
unless (defined $amount) {
WebGUI::Error::InvalidParam->throw(error=>"Need an amount.");
}
return sprintf("%.2f", $amount); return sprintf("%.2f", $amount);
} }
@ -181,10 +187,14 @@ sub getAddressBook {
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 getCartBySession () =head2 getCartBySession ( session )
Class method that figures out if the user has a cart in their session. If they do it returns it. If they don't it creates it and returns it. Class method that figures out if the user has a cart in their session. If they do it returns it. If they don't it creates it and returns it.
=head3 session
A reference to the current session.
=cut =cut
sub getCartBySession { sub getCartBySession {
@ -224,6 +234,9 @@ The id of the item to retrieve.
sub getItem { sub getItem {
my ($self, $itemId) = @_; my ($self, $itemId) = @_;
unless (defined $itemId && $itemId =~ m/^[A-Za-z0-9_-]{22}$/) {
WebGUI::Error::InvalidParam->throw(error=>"Need an itemId.");
}
return WebGUI::Shop::CartItem->new($self, $itemId); return WebGUI::Shop::CartItem->new($self, $itemId);
} }
@ -306,7 +319,7 @@ sub new {
unless (defined $session && $session->isa("WebGUI::Session")) { unless (defined $session && $session->isa("WebGUI::Session")) {
WebGUI::Error::InvalidObject->throw(expected=>"WebGUI::Session", got=>(ref $session), error=>"Need a session."); WebGUI::Error::InvalidObject->throw(expected=>"WebGUI::Session", got=>(ref $session), error=>"Need a session.");
} }
unless (defined $cartId) { unless (defined $cartId && $cartId =~ m/^[A-Za-z0-9_-]{22}$/) {
WebGUI::Error::InvalidParam->throw(error=>"Need a cartId."); WebGUI::Error::InvalidParam->throw(error=>"Need a cartId.");
} }
my $cart = $session->db->quickHashRef('select * from cart where cartId=?', [$cartId]); my $cart = $session->db->quickHashRef('select * from cart where cartId=?', [$cartId]);
@ -363,6 +376,9 @@ The unique id of the configured shipping driver that will be used to ship these
sub update { sub update {
my ($self, $newProperties) = @_; my ($self, $newProperties) = @_;
unless (defined $newProperties && ref $newProperties eq 'HASH') {
WebGUI::Error::InvalidParam->throw(error=>"Need a properties hash ref.");
}
my $id = id $self; my $id = id $self;
foreach my $field (qw(couponId shippingAddressId shipperId)) { foreach my $field (qw(couponId shippingAddressId shipperId)) {
$properties{$id}{$field} = (exists $newProperties->{$field}) ? $newProperties->{$field} : $properties{$id}{$field}; $properties{$id}{$field} = (exists $newProperties->{$field}) ? $newProperties->{$field} : $properties{$id}{$field};

View file

@ -21,6 +21,8 @@ use WebGUI::Test; # Must use this before any other WebGUI modules
use WebGUI::Session; use WebGUI::Session;
use WebGUI::Asset; use WebGUI::Asset;
use WebGUI::Shop::Cart; use WebGUI::Shop::Cart;
use WebGUI::TestException;
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
# Init # Init
@ -30,11 +32,21 @@ my $session = WebGUI::Test->session;
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
# Tests # Tests
plan tests => 19; # Increment this number for each test you create plan tests => 20; # Increment this number for each test you create
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
# put your tests here # put your tests here
throws_deeply ( sub { my $cart = WebGUI::Shop::Cart->newBySession(); },
'WebGUI::Error::InvalidObject',
{
error => 'Needs a session.',
got => '',
expected => 'WebGUI::Session',
},
'newBySession takes an exception to not giving it a session variable'
);
my $cart = WebGUI::Shop::Cart->getCartBySession($session); my $cart = WebGUI::Shop::Cart->getCartBySession($session);
isa_ok($cart, "WebGUI::Shop::Cart"); isa_ok($cart, "WebGUI::Shop::Cart");