diff --git a/lib/WebGUI/Shop/Ship.pm b/lib/WebGUI/Shop/Ship.pm index 7906bf52e..a06460b45 100644 --- a/lib/WebGUI/Shop/Ship.pm +++ b/lib/WebGUI/Shop/Ship.pm @@ -2,7 +2,6 @@ package WebGUI::Shop::Ship; use strict; -use Carp qw(croak); use WebGUI::International; use WebGUI::Shop::ShipDriver; use WebGUI::Pluggable; @@ -104,8 +103,9 @@ A WebGUI::Session object. A WebGUI::Error::InvalidParam exception will be throw sub getOptions { my $class = shift; my $session = shift; - croak "Definition requires a session object" + WebGUI::Error::InvalidParam->throw(error => q{Must provide a session variable}) unless ref $session eq 'WebGUI::Session'; + return; } #------------------------------------------------------------------- diff --git a/lib/WebGUI/Shop/ShipDriver.pm b/lib/WebGUI/Shop/ShipDriver.pm index aa3e77959..766b25cbd 100644 --- a/lib/WebGUI/Shop/ShipDriver.pm +++ b/lib/WebGUI/Shop/ShipDriver.pm @@ -273,8 +273,11 @@ sub new { my $properties = $session->db->quickHashRef('select * from shipper where shipperId=?',[$shipperId]); WebGUI::Error::ObjectNotFound->throw(error => q{shipperId not found in db}, id => $shipperId) unless scalar keys %{ $properties }; - croak "Somehow, the options property of this object, $shipperId, got broken in the db" - unless exists $properties->{options} and $properties->{options}; + ##This check is just to guardband the from_json call below. + WebGUI::Error::InvalidParam->throw( + error => qq{Options property for $shipperId was broken in the db}, + param => $properties->{options}, + ) unless $properties->{options}; ##Note, existence is controlled by the columns in the db my $options = from_json($properties->{options}); my $self = WebGUI::Shop::ShipDriver->_buildObj($session, $class, $shipperId, $options); return $self; diff --git a/t/Shop/Ship.t b/t/Shop/Ship.t index 229e4f09e..018c995f6 100644 --- a/t/Shop/Ship.t +++ b/t/Shop/Ship.t @@ -31,7 +31,7 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -my $tests = 27; +my $tests = 29; plan tests => 1 + $tests; #---------------------------------------------------------------------------- @@ -220,6 +220,23 @@ cmp_bag( 'Returned shippers have the right data' ); +####################################################################### +# +# getOptions +# +####################################################################### + +eval { $shippers = WebGUI::Shop::Ship->getOptions(); }; +$e = Exception::Class->caught(); +isa_ok($e, 'WebGUI::Error::InvalidParam', 'getOptions takes exception to not giving it a session object'); +cmp_deeply( + $e, + methods( + error => 'Must provide a session variable', + ), + 'getOptions takes exception to not giving it a session object', +); + } #---------------------------------------------------------------------------- diff --git a/t/Shop/ShipDriver.t b/t/Shop/ShipDriver.t index d23ace6b9..8bc7a080c 100644 --- a/t/Shop/ShipDriver.t +++ b/t/Shop/ShipDriver.t @@ -31,7 +31,7 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -my $tests = 39; +my $tests = 42; plan tests => 1 + $tests; #---------------------------------------------------------------------------- @@ -302,10 +302,35 @@ is($driver->getId, $driverCopy->getId, 'same id'); is($driver->className, $driverCopy->className, 'same className'); cmp_deeply($driver->options, $driverCopy->options, 'same options'); -TODO: { - local $TODO = 'tests for new'; - ok(0, 'Test broken options in the db'); -} +my $brokenDriver = WebGUI::Shop::ShipDriver->create($session, {label=>'to be broken', enabled=>'0'}); +$session->db->write('update shipper set options=NULL where shipperId=?',[$brokenDriver->getId]); + +eval { $oldDriver = WebGUI::Shop::ShipDriver->new($session, $brokenDriver->getId); }; +$e = Exception::Class->caught(); +isa_ok($e, 'WebGUI::Error::InvalidParam', 'new croaks if the options column in the db is null'); +cmp_deeply( + $e, + methods( + error => re('Options property for \S{22} was broken in the db'), + param => undef, + ), + 'new croaks if the options column in the db is null', +); + +$session->db->write(q{update shipper set options='' where shipperId=?},[$brokenDriver->getId]); + +eval { $oldDriver = WebGUI::Shop::ShipDriver->new($session, $brokenDriver->getId); }; +$e = Exception::Class->caught(); +isa_ok($e, 'WebGUI::Error::InvalidParam', 'new croaks if the options column in the db is empty string'); +cmp_deeply( + $e, + methods( + error => re('Options property for \S{22} was broken in the db'), + param => '', + ), + 'new croaks if the options column in the db is empty string', +); + ####################################################################### #