begin conversion of ShipDriver to use exceptions, in code and in test

This commit is contained in:
Colin Kuskie 2008-02-28 00:24:31 +00:00
parent a8598bc108
commit c9661496fe
2 changed files with 47 additions and 6 deletions

View file

@ -7,6 +7,7 @@ use Carp qw(croak);
use Tie::IxHash;
use WebGUI::International;
use WebGUI::HTMLForm;
use WebGUI::Exception::Shop;
use JSON;
=head1 NAME
@ -102,8 +103,10 @@ A list of properties to assign to this ShipperDriver. See C<definition> for det
sub create {
my $class = shift;
my $session = shift;
WebGUI::Error::InvalidParam->throw(error => q{Must provide a session variable})
unless ref $session eq 'WebGUI::Session';
my $options = shift;
croak "You must pass a hashref of options to create a new ShipDriver object"
WebGUI::Error::InvalidParam->throw(error => 'Must pass in a hashref of params to create a new ShipDriver object')
unless defined($options) and ref $options eq 'HASH' and scalar keys %{ $options };
my $shipperId = $session->id->generate;
my $self = WebGUI::Shop::ShipDriver->_buildObj($session, $class, $shipperId, $options);
@ -127,7 +130,7 @@ the user.
sub definition {
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';
my $definition = shift || [];
my $i18n = WebGUI::International->new($session, 'ShipDriver');

View file

@ -31,12 +31,14 @@ my $session = WebGUI::Test->session;
#----------------------------------------------------------------------------
# Tests
my $tests = 29;
my $tests = 34;
plan tests => 1 + $tests;
#----------------------------------------------------------------------------
# put your tests here
my $e;
my $loaded = use_ok('WebGUI::Shop::ShipDriver');
my $storage;
@ -54,7 +56,15 @@ skip 'Unable to load module WebGUI::Shop::ShipDriver', $tests unless $loaded;
my $definition;
eval { $definition = WebGUI::Shop::ShipDriver->definition(); };
like ($@, qr/^Definition requires a session object/, 'definition croaks without a session object');
$e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::InvalidParam', 'definition takes an exception to not giving it a session variable');
cmp_deeply(
$e,
methods(
error => 'Must provide a session variable',
),
'definition: requires a session variable',
);
$definition = WebGUI::Shop::ShipDriver->definition($session);
@ -106,11 +116,39 @@ cmp_deeply(
my $driver;
eval { $driver = WebGUI::Shop::ShipDriver->create(); };
$e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::InvalidParam', 'create takes exception to not giving it a session object');
cmp_deeply(
$e,
methods(
error => 'Must provide a session variable',
),
'create takes exception to not giving it a session object',
);
eval { $driver = WebGUI::Shop::ShipDriver->create($session); };
like ($@, qr/You must pass a hashref of options to create a new ShipDriver object/, 'create croaks without a hashref of options');
$e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::InvalidParam', 'create takes exception to not giving it a hashref of options');
cmp_deeply(
$e,
methods(
error => 'Must pass in a hashref of params to create a new ShipDriver object',
),
'create takes exception to not giving it a hashref of options',
);
eval { $driver = WebGUI::Shop::ShipDriver->create($session, {}); };
like ($@, qr/You must pass a hashref of options to create a new ShipDriver object/, 'create croaks with an empty hashref of options');
$e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::InvalidParam', 'create takes exception to not giving it an empty hashref of options');
cmp_deeply(
$e,
methods(
error => 'Must pass in a hashref of params to create a new ShipDriver object',
),
'create takes exception to not giving it an empty hashref of options',
);
my $options = {
label => 'Slow and dangerous',