Require a label to be passed to WebGUI::Shop::Pay->addPaymentGateway

This commit is contained in:
Martin Kamerbeek 2008-03-11 16:54:20 +00:00
parent 60362cb747
commit b8d9d38da6
2 changed files with 30 additions and 11 deletions

View file

@ -33,7 +33,7 @@ readonly session => my %session;
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 addPaymentGateway ( $class, $options ) =head2 addPaymentGateway ( $class, $label, $options )
The interface method for creating new, configured instances of PayDriver. If the PayDriver throws an exception, it is propagated The interface method for creating new, configured instances of PayDriver. If the PayDriver throws an exception, it is propagated
back up to the top. back up to the top.
@ -42,6 +42,10 @@ back up to the top.
The class of the new PayDriver object to create. The class of the new PayDriver object to create.
=head4 $label
The label for this instance.
=head4 $options =head4 $options
A list of properties to assign to this PayDriver. See C<definition> for details. A list of properties to assign to this PayDriver. See C<definition> for details.
@ -49,16 +53,19 @@ A list of properties to assign to this PayDriver. See C<definition> for details
=cut =cut
sub addPaymentGateway { sub addPaymentGateway {
my $self = shift; my $self = shift;
my $requestedClass = shift; my $requestedClass = shift;
my $options = shift; my $label = shift;
my $options = shift;
WebGUI::Error::InvalidParam->throw(error => q{Must provide a class to create an object}) WebGUI::Error::InvalidParam->throw(error => q{Must provide a class to create an object})
unless defined $requestedClass; unless defined $requestedClass;
WebGUI::Error::InvalidParam->throw(error => q{The requested class is not enabled in your WebGUI configuration file}, param => $requestedClass) WebGUI::Error::InvalidParam->throw(error => q{The requested class is not enabled in your WebGUI configuration file}, param => $requestedClass)
unless isIn($requestedClass, (keys %{$self->getDrivers}) ); unless isIn($requestedClass, (keys %{$self->getDrivers}) );
WebGUI::Error::InvalidParam->throw(error => q{Must provide a label to create an object})
unless $label;
WebGUI::Error::InvalidParam->throw(error => q{You must pass a hashref of options to create a new PayDriver object}) WebGUI::Error::InvalidParam->throw(error => q{You must pass a hashref of options to create a new PayDriver object})
unless defined($options) and ref $options eq 'HASH' and scalar keys %{ $options }; unless defined($options) and ref $options eq 'HASH' and scalar keys %{ $options };
my $driver = eval { WebGUI::Pluggable::instanciate($requestedClass, 'create', [ $self->session, 'TEMPORARY_LABEL', $options ]) }; my $driver = eval { WebGUI::Pluggable::instanciate($requestedClass, 'create', [ $self->session, $label, $options ]) };
return $driver; return $driver;
} }

View file

@ -31,7 +31,7 @@ my $session = WebGUI::Test->session;
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
# Tests # Tests
my $tests = 25; my $tests = 27;
plan tests => 1 + $tests; plan tests => 1 + $tests;
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
@ -114,6 +114,18 @@ cmp_deeply(
eval { $gateway = $pay->addPaymentGateway('WebGUI::Shop::PayDriver::Cash'); }; eval { $gateway = $pay->addPaymentGateway('WebGUI::Shop::PayDriver::Cash'); };
$e = Exception::Class->caught(); $e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::InvalidParam', 'addPaymentGateway croaks without a label');
cmp_deeply(
$e,
methods(
error => 'Must provide a label to create an object',
),
'addPaymentGateway requires a label',
);
eval { $gateway = $pay->addPaymentGateway('WebGUI::Shop::PayDriver::Cash', 'JAL'); };
$e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::InvalidParam', 'addPaymentGateway croaks without options to build a object with'); isa_ok($e, 'WebGUI::Error::InvalidParam', 'addPaymentGateway croaks without options to build a object with');
cmp_deeply( cmp_deeply(
$e, $e,
@ -123,7 +135,7 @@ cmp_deeply(
'addPaymentGateway croaks without options to build a object with', 'addPaymentGateway croaks without options to build a object with',
); );
eval { $gateway = $pay->addPaymentGateway('WebGUI::Shop::PayDriver::Cash', {}); }; eval { $gateway = $pay->addPaymentGateway('WebGUI::Shop::PayDriver::Cash', 'JAL', {}); };
$e = Exception::Class->caught(); $e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::InvalidParam', 'addPaymentGateway croaks without options to build a object with'); isa_ok($e, 'WebGUI::Error::InvalidParam', 'addPaymentGateway croaks without options to build a object with');
cmp_deeply( cmp_deeply(
@ -138,14 +150,14 @@ my $options = {
enabled => 1, enabled => 1,
label => 'Cold, stone hard cash', label => 'Cold, stone hard cash',
}; };
my $newDriver = $pay->addPaymentGateway('WebGUI::Shop::PayDriver::Cash', $options); my $newDriver = $pay->addPaymentGateway('WebGUI::Shop::PayDriver::Cash', 'JAL', $options);
isa_ok($newDriver, 'WebGUI::Shop::PayDriver::Cash', 'added a new, configured Cash driver'); isa_ok($newDriver, 'WebGUI::Shop::PayDriver::Cash', 'added a new, configured Cash driver');
is($newDriver->label, 'JAL', 'label passed correctly to paydriver');
diag ('----> THE NEXT TEST IS SUPPOSED TO FAIL! REMOVE WHEN RESOLVED. <----');
isnt ($newDriver->label, 'TEMPORARY_LABEL', 'fail test until the addPaymentGateway interface is resolved.');
#TODO: check if options are stored. #TODO: check if options are stored.
####################################################################### #######################################################################
# #
# getDrivers # getDrivers
@ -225,7 +237,7 @@ my $otherOptions = {
enabled => 1, enabled => 1,
label => 'Even harder cash', label => 'Even harder cash',
}; };
my $anotherDriver = $pay->addPaymentGateway('WebGUI::Shop::PayDriver::Cash', $otherOptions); my $anotherDriver = $pay->addPaymentGateway('WebGUI::Shop::PayDriver::Cash', 'Pomade', $otherOptions);
my $gateways = $pay->getPaymentGateways; my $gateways = $pay->getPaymentGateways;
my @returnedIds = map {$_->getId} @{ $gateways }; my @returnedIds = map {$_->getId} @{ $gateways };