- Added tests for Pay

- Fixed some tests/code for PayDriver
 - Added i18n for PayDriver
 - Added a really, really bare-bones Cash plugin.
This commit is contained in:
Martin Kamerbeek 2008-03-11 16:33:10 +00:00
parent 39a0b6ff1f
commit 60362cb747
7 changed files with 415 additions and 23 deletions

View file

@ -222,7 +222,7 @@ sub insertCommercePayDriverTable {
print "\tInstall the Commerce PayDriver Table.\n" unless ($quiet);
# and here's our code
$session->db->write(<<EOSQL);
CREATE TABLE payment_Gateway (
CREATE TABLE paymentGateway (
paymentGatewayId VARCHAR(22) binary NOT NULL primary key,
label VARCHAR(255),
className VARCHAR(255),

View file

@ -58,7 +58,7 @@ sub addPaymentGateway {
unless isIn($requestedClass, (keys %{$self->getDrivers}) );
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 };
my $driver = eval { WebGUI::Pluggable::instanciate($requestedClass, 'create', [ $self->session, $options ]) };
my $driver = eval { WebGUI::Pluggable::instanciate($requestedClass, 'create', [ $self->session, 'TEMPORARY_LABEL', $options ]) };
return $driver;
}
@ -109,7 +109,7 @@ sub getOptions {
#-------------------------------------------------------------------
=head2 getPaymentGateway ( )
=head2 getPaymentGateway ( $id )
Looks up an existing PayDriver in the db by paymentGatewayId and returns
that object. If the PayDriver throws an exception, it is propagated

View file

@ -111,7 +111,7 @@ sub create {
my $self = WebGUI::Shop::PayDriver->_buildObj($session, $class, $paymentGatewayId, $label, $options);
# and persist this instance in the db
$session->db->write('insert into payment_Gateway (paymentGatewayId, label, className) VALUES (?,?,?)', [
$session->db->write('insert into paymentGateway (paymentGatewayId, label, className) VALUES (?,?,?)', [
$paymentGatewayId,
$label,
$class,
@ -146,26 +146,26 @@ sub definition {
%fields = (
label => {
fieldType => 'text',
label => $i18n->echo('label'),
hoverHelp => $i18n->echo('label help'),
label => $i18n->get('label'),
hoverHelp => $i18n->get('label help'),
defaultValue => "Credit Card",
},
enabled => {
fieldType => 'yesNo',
label => $i18n->echo('enabled'),
hoverHelp => $i18n->echo('enabled help'),
label => $i18n->get('enabled'),
hoverHelp => $i18n->get('enabled help'),
defaultValue => 1,
},
groupToUse => {
fieldType => 'group',
label => $i18n->echo('who can use'),
hoverHelp => $i18n->echo('who can use help'),
label => $i18n->get('who can use'),
hoverHelp => $i18n->get('who can use help'),
defaultValue => 1,
},
receiptMessage => {
fieldType => 'text',
label => $i18n->echo('receipt message'),
hoverHelp => $i18n->echo('receipt message help'),
label => $i18n->get('receipt message'),
hoverHelp => $i18n->get('receipt message help'),
defaultValue => undef,
},
);
@ -190,7 +190,7 @@ Removes this PayDriver object from the db.
sub delete {
my $self = shift;
$self->session->db->write('delete from payment_Gateway where paymentGatewayId=?', [
$self->session->db->write('delete from paymentGateway where paymentGatewayId=?', [
$self->getId,
]);
@ -285,11 +285,17 @@ sub getId {
Return a human readable name for this driver. Never overridden in the
subclass, instead specified in definition with the name "name".
This is a class method.
=cut
sub getName {
my $self = shift;
my $definition = $self->definition($self->session);
my $class = shift;
my $session = shift;
WebGUI::Error::InvalidParam->throw(error => q{Must provide a session variable})
unless ref $session eq 'WebGUI::Session';
my $definition = $class->definition($session);
return $definition->[0]->{name};
}
@ -312,7 +318,7 @@ sub new {
unless defined $paymentGatewayId;
# Fetch the instance data from the db
my $properties = $session->db->quickHashRef('select * from payment_Gateway where paymentGatewayId=?', [
my $properties = $session->db->quickHashRef('select * from paymentGateway where paymentGatewayId=?', [
$paymentGatewayId,
]);
WebGUI::Error::ObjectNotFound->throw(error => q{paymentGatewayId not found in db}, id => $paymentGatewayId)
@ -368,7 +374,7 @@ sub set {
unless ref $properties eq 'HASH' and scalar keys %{ $properties };
my $jsonOptions = to_json($properties);
$self->session->db->write('update payment_Gateway set options=? where paymentGatewayId=?', [
$self->session->db->write('update paymentGateway set options=? where paymentGatewayId=?', [
$jsonOptions,
$self->paymentGatewayId
]);

View file

@ -0,0 +1,56 @@
package WebGUI::Shop::PayDriver::Cash;
use strict;
use WebGUI::Shop::PayDriver;
use WebGUI::Exception;
use base qw/WebGUI::Shop::PayDriver/;
#-------------------------------------------------------------------
sub definition {
my $class = shift;
my $session = shift;
my $definition = shift;
my $i18n = WebGUI::International->new($session, 'PayDriver_Cash');
tie my %fields, 'Tie::IxHash';
%fields = (
sendReceipt => {
fieldType => 'yesNo',
label => $i18n->echo('sendReceipt'),
hoverHelp => $i18n->echo('sendReceipt help'),
defaultValue => 0,
},
);
push @{ $definition }, {
name => $i18n->echo('Cash'),
fields => \%fields,
};
return $class->SUPER::definition($session, $definition);
}
#-------------------------------------------------------------------
sub getButton {
}
#-------------------------------------------------------------------
sub www_collectPaymentInfo {
}
#-------------------------------------------------------------------
sub www_displayStatus {
}
1;

View file

@ -0,0 +1,57 @@
package WebGUI::i18n::English::PayDriver;
use strict;
our $I18N = {
'label' => {
message => q|Label|,
lastUpdated => 0,
context => q|Label for the label option.|
},
'label help' => {
message => q|The name by which this pagyment gateway is displayed.|,
lastUpdated => 0,
context => q|Hover help for the label option.|
},
'enabled' => {
message => q|Enabled|,
lastUpdated => 0,
context => q|Label for the enabled option.|,
},
'enabled help' => {
message => q|Sets whether this payment gateway is enabled|,
lastUpdated => 0,
context => q|Hover help for the enabled option.|,
},
'who can use' => {
message => q|Group to use this gateway|,
lastUpdate => 0,
context => q|Label for the group to use option.|,
},
'who can use help' => {
message => q|Specifies which group is allowed to use this payment gateway.|,
lastUpdated => 0,
context => q|Hover help for the group to use option.|,
},
'receipt message' => {
message => q|Receipt message|,
lastUpdated => 0,
context => q|Label for the receipt message option.|,
},
'receipt message help' => {
message => q|The message that will be attached to the receipt.|,
lastUpdated => 0,
context => q|Hover help the receipt message option.|,
},
};
1;

262
t/Shop/Pay.t Normal file
View file

@ -0,0 +1,262 @@
# 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 Test::Deep;
use JSON;
use HTML::Form;
use WebGUI::Test; # Must use this before any other WebGUI modules
use WebGUI::Session;
#----------------------------------------------------------------------------
# Init
my $session = WebGUI::Test->session;
#----------------------------------------------------------------------------
# Tests
my $tests = 25;
plan tests => 1 + $tests;
#----------------------------------------------------------------------------
# put your tests here
my $loaded = use_ok('WebGUI::Shop::Pay');
my $storage;
SKIP: {
skip 'Unable to load module WebGUI::Shop::Pay', $tests unless $loaded;
#######################################################################
#
# new
#
#######################################################################
my $e;
my $pay;
eval { $pay = WebGUI::Shop::Pay->new(); };
$e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::InvalidParam', 'new takes an exception to not giving it a session variable');
cmp_deeply(
$e,
methods(
error => 'Must provide a session variable',
got => '',
expected => 'WebGUI::Session',
),
'new: requires a session variable',
);
$pay = WebGUI::Shop::Pay->new($session);
isa_ok($pay, 'WebGUI::Shop::Pay', 'new returned the right kind of object');
#######################################################################
#
# session
#
#######################################################################
isa_ok($pay->session, 'WebGUI::Session', 'session method returns a session object');
is($session->getId, $pay->session->getId, 'session method returns OUR session object');
#######################################################################
#
# addPaymentGateway
#
#######################################################################
my $gateway;
eval { $gateway = $pay->addPaymentGateway(); };
$e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::InvalidParam', 'addPaymentGateway croaks without a class');
cmp_deeply(
$e,
methods(
error => 'Must provide a class to create an object',
),
'addPaymentGateway croaks without a class',
);
eval { $gateway = $pay->addPaymentGateway('WebGUI::Shop::PayDriver::NoSuchDriver'); };
$e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::InvalidParam', 'addPaymentGateway croaks without a configured class');
cmp_deeply(
$e,
methods(
error => 'The requested class is not enabled in your WebGUI configuration file',
param => 'WebGUI::Shop::PayDriver::NoSuchDriver',
),
'addPaymentGateway croaks without a configured class',
);
eval { $gateway = $pay->addPaymentGateway('WebGUI::Shop::PayDriver::Cash'); };
$e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::InvalidParam', 'addPaymentGateway croaks without options to build a object with');
cmp_deeply(
$e,
methods(
error => 'You must pass a hashref of options to create a new PayDriver object',
),
'addPaymentGateway croaks without options to build a object with',
);
eval { $gateway = $pay->addPaymentGateway('WebGUI::Shop::PayDriver::Cash', {}); };
$e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::InvalidParam', 'addPaymentGateway croaks without options to build a object with');
cmp_deeply(
$e,
methods(
error => 'You must pass a hashref of options to create a new PayDriver object',
),
'addPaymentGateway croaks without options to build a object with',
);
my $options = {
enabled => 1,
label => 'Cold, stone hard cash',
};
my $newDriver = $pay->addPaymentGateway('WebGUI::Shop::PayDriver::Cash', $options);
isa_ok($newDriver, 'WebGUI::Shop::PayDriver::Cash', 'added a new, configured Cash driver');
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.
#######################################################################
#
# getDrivers
#
#######################################################################
my $drivers = $pay->getDrivers();
my $defaultPayDrivers = {
'WebGUI::Shop::PayDriver::Cash' => 'Cash',
};
cmp_deeply(
$drivers,
$defaultPayDrivers,
'getDrivers returns the default PayDrivers',
);
#######################################################################
#
# getOptions
#
#######################################################################
eval { $drivers = $pay->getOptions(); };
$e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::InvalidParam', 'getOptions takes exception to not giving it a cart');
cmp_deeply(
$e,
methods(
error => 'Need a cart.',
),
'getOptions takes exception to not giving it a cart',
);
#######################################################################
#
# getPaymentGateway
#
#######################################################################
eval { $gateway = $pay->getPaymentGateway(); };
$e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::InvalidParam', 'getPaymentDriver throws an exception when no paymentGatewayId is passed');
cmp_deeply(
$e,
methods(
error => q{Must provide a paymentGatewayId},
),
'getPaymentGateway throws exception without paymentGatewayId',
);
eval { $gateway = $pay->getPaymentGateway('NoSuchThing'); };
$e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::ObjectNotFound', 'getPaymentGateway thows exception when a non-existant paymentGatewayId is passed');
cmp_deeply(
$e,
methods(
error => q{payment gateway not found in db},
id => 'NoSuchThing',
),
'getPaymentGateway throws exception when called with a non-existant paymentGatewayId',
);
$gateway = $pay->getPaymentGateway( $newDriver->getId );
isa_ok($gateway, 'WebGUI::Shop::PayDriver::Cash', 'returned payment gateway has correct class');
is($gateway->getId, $newDriver->getId, 'getPaymentGateway instantiated the requested driver');
#######################################################################
#
# getPaymentGateways
#
#######################################################################
# Create an extra driver for testing purposes
my $otherOptions = {
enabled => 1,
label => 'Even harder cash',
};
my $anotherDriver = $pay->addPaymentGateway('WebGUI::Shop::PayDriver::Cash', $otherOptions);
my $gateways = $pay->getPaymentGateways;
my @returnedIds = map {$_->getId} @{ $gateways };
cmp_bag(
\@returnedIds,
[
$newDriver->getId,
$anotherDriver->getId,
],
'getPaymentGateways returns all create payment drivers',
);
#######################################################################
#
# www_do
#
#######################################################################
#######################################################################
#
# www_manage
#
#######################################################################
}
#----------------------------------------------------------------------------
# Cleanup
END {
$session->db->write('delete from paymentGateway');
}

View file

@ -31,7 +31,7 @@ my $session = WebGUI::Test->session;
#----------------------------------------------------------------------------
# Tests
my $tests = 43;
my $tests = 45;
plan tests => 1 + $tests;
#----------------------------------------------------------------------------
@ -188,7 +188,7 @@ $driver = WebGUI::Shop::PayDriver->create( $session, $label, $options );
isa_ok ($driver, 'WebGUI::Shop::PayDriver', 'create creates WebGUI::Shop::PayDriver object');
my $dbData = $session->db->quickHashRef('select * from payment_Gateway where paymentGatewayId=?', [ $driver->getId ]);
my $dbData = $session->db->quickHashRef('select * from paymentGateway where paymentGatewayId=?', [ $driver->getId ]);
#diag ($driver->getId);
cmp_deeply (
@ -245,7 +245,18 @@ cmp_deeply ($driver->options, $options, 'options accessor works');
#
#######################################################################
is ($driver->getName, 'Payment Driver', 'getName returns the human readable name of this driver');
eval { WebGUI::Shop::PayDriver->getName(); };
$e = Exception::Class->caught();
isa_ok ($e, 'WebGUI::Error::InvalidParam', 'getName requires a session object passed to it');
cmp_deeply (
$e,
methods(
error => 'Must provide a session variable',
),
'getName requires a session object passed to it',
);
is (WebGUI::Shop::PayDriver->getName($session), 'Payment Driver', 'getName returns the human readable name of this driver');
#######################################################################
#
@ -403,7 +414,7 @@ my $newOptions = {
};
$driver->set($newOptions);
my $storedOptions = $session->db->quickScalar('select options from payment_Gateway where paymentGatewayId=?', [
my $storedOptions = $session->db->quickScalar('select options from paymentGateway where paymentGatewayId=?', [
$driver->getId,
]);
cmp_deeply(
@ -422,7 +433,7 @@ cmp_deeply(
$driver->delete;
my $count = $session->db->quickScalar('select count(*) from payment_Gateway where paymentGatewayId=?', [
my $count = $session->db->quickScalar('select count(*) from paymentGateway where paymentGatewayId=?', [
$driver->paymentGatewayId
]);
@ -436,5 +447,5 @@ undef $driver;
#----------------------------------------------------------------------------
# Cleanup
END {
#$session->db->write('delete from payment_Gateway');
$session->db->write('delete from paymentGateway');
}