Convert Shop::Pay to use Moose instead of Class::InsideOut. Update tests, and core usages to Moose syntax. Provide a shim so the old, base ->new($session) syntax.

This commit is contained in:
Colin Kuskie 2010-06-24 13:31:32 -07:00
parent 3fbc109429
commit 9678c3d8a7
5 changed files with 39 additions and 62 deletions

View file

@ -77,7 +77,7 @@ property duration => (
); );
sub _duration_options { sub _duration_options {
my $session = shift->session; my $session = shift->session;
return WebGUI::Shop::Pay->new( $session )->getRecurringPeriodValues, return WebGUI::Shop::Pay->new( session => $session )->getRecurringPeriodValues,
} }
property executeOnSubscription => ( property executeOnSubscription => (
fieldType => 'text', fieldType => 'text',

View file

@ -177,7 +177,7 @@ sub www_pay {
my $session = shift; my $session = shift;
my $output = undef; my $output = undef;
my $method = "www_".$session->form->get("method"); my $method = "www_".$session->form->get("method");
my $pay = WebGUI::Shop::Pay->new($session); my $pay = WebGUI::Shop::Pay->new(session => $session);
if ($method ne "www_" && $pay->can($method)) { if ($method ne "www_" && $pay->can($method)) {
$output = $pay->$method(); $output = $pay->$method();
} }

View file

@ -16,7 +16,8 @@ package WebGUI::Shop::Pay;
use strict; use strict;
use Class::InsideOut qw{ :std }; #use Class::InsideOut qw{ :std };
use Moose;
use WebGUI::Exception; use WebGUI::Exception;
use WebGUI::International; use WebGUI::International;
use WebGUI::Pluggable; use WebGUI::Pluggable;
@ -24,6 +25,7 @@ use WebGUI::Shop::Admin;
#use WebGUI::Shop::PayDriver; #use WebGUI::Shop::PayDriver;
use WebGUI::Utility; use WebGUI::Utility;
use Tie::IxHash; use Tie::IxHash;
use Scalar::Util;
=head1 NAME =head1 NAME
@ -43,8 +45,34 @@ These subroutines are available from this package:
=cut =cut
readonly session => my %session; #-------------------------------------------------------------------
=head2 session ()
Returns a reference to the current session.
=cut
has session => (
is => 'ro',
required => 1,
);
around BUILDARGS => sub {
my $orig = shift;
my $className = shift;
##Original arguments start here.
if (ref $_[0] eq 'HASH') {
return $className->$orig(@_);
}
my $protoSession = $_[0];
if (blessed $protoSession && $protoSession->isa('WebGUI::Session')) {
return $className->$orig(session => $protoSession);
}
return $className->$orig(@_);
};
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -215,36 +243,6 @@ sub getRecurringPeriodValues {
} }
#-------------------------------------------------------------------
=head2 new ( $session )
Constructor.
=head3 $session
A WebGUI::Session object.
=cut
sub new {
my $class = shift;
my $session = shift;
WebGUI::Error::InvalidObject->throw(expected=>"WebGUI::Session", got=>(ref $session), error => q{Must provide a session variable}) unless ref $session eq 'WebGUI::Session';
my $self = register $class;
my $id = id $self;
$session{ $id } = $session;
return $self;
}
#-------------------------------------------------------------------
=head2 session ()
Returns a reference to the current session.
=cut
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 www_addPaymentGateway ( $session ) =head2 www_addPaymentGateway ( $session )

View file

@ -367,7 +367,7 @@ Returns a reference to the payment gateway attached to this transaction.
sub getPaymentGateway { sub getPaymentGateway {
my ($self) = @_; my ($self) = @_;
my $pay = WebGUI::Shop::Pay->new($self->session); my $pay = WebGUI::Shop::Pay->new(session => $self->session);
return $pay->getPaymentGateway($self->get('paymentDriverId')); return $pay->getPaymentGateway($self->get('paymentDriverId'));
} }

View file

@ -18,7 +18,7 @@ use strict;
use lib "$FindBin::Bin/../lib"; use lib "$FindBin::Bin/../lib";
use Test::More; use Test::More;
use Test::Deep; use Test::Deep;
#use Test::Exception; use Test::Exception;
use JSON; use JSON;
use HTML::Form; use HTML::Form;
@ -30,12 +30,6 @@ use WebGUI::TestException;
# Init # Init
my $session = WebGUI::Test->session; my $session = WebGUI::Test->session;
#----------------------------------------------------------------------------
# Tests
my $tests = 18;
plan tests => 1 + $tests;
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
# put your tests here # put your tests here
@ -45,31 +39,19 @@ my $storage;
my $newDriver; my $newDriver;
my $anotherDriver; my $anotherDriver;
SKIP: {
skip 'Unable to load module WebGUI::Shop::Pay', $tests unless $loaded;
####################################################################### #######################################################################
# #
# new # new
# #
####################################################################### #######################################################################
my $e;
my $pay; my $pay;
dies_ok { $pay = WebGUI::Shop::Pay->new(); }
'new takes an exception to not giving it a session variable';
throws_deeply ( sub { $pay = WebGUI::Shop::Pay->new(); }, lives_ok { $pay = WebGUI::Shop::Pay->new(session => $session); } 'new called with hash arguments';
'WebGUI::Error::InvalidObject', lives_ok { $pay = WebGUI::Shop::Pay->new($session); } 'new called only with session';
{
error => 'Must provide a session variable',
got => '',
expected => 'WebGUI::Session',
},
'new takes an exception to not giving it a session variable'
);
$pay = WebGUI::Shop::Pay->new($session);
isa_ok($pay, 'WebGUI::Shop::Pay', 'new returned the right kind of object'); isa_ok($pay, 'WebGUI::Shop::Pay', 'new returned the right kind of object');
####################################################################### #######################################################################
@ -232,14 +214,11 @@ cmp_bag(
# #
####################################################################### #######################################################################
done_testing();
}
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
# Cleanup # Cleanup
END { END {
defined $newDriver and $newDriver->delete; defined $newDriver and $newDriver->delete;
defined $newDriver and $anotherDriver->delete; defined $newDriver and $anotherDriver->delete;
my $count = $session->db->quickScalar('select count(*) from paymentGateway');
is($count, 2, 'WebGUI ships with two drivers by default');
} }