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 {
my $session = shift->session;
return WebGUI::Shop::Pay->new( $session )->getRecurringPeriodValues,
return WebGUI::Shop::Pay->new( session => $session )->getRecurringPeriodValues,
}
property executeOnSubscription => (
fieldType => 'text',

View file

@ -177,7 +177,7 @@ sub www_pay {
my $session = shift;
my $output = undef;
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)) {
$output = $pay->$method();
}

View file

@ -16,7 +16,8 @@ package WebGUI::Shop::Pay;
use strict;
use Class::InsideOut qw{ :std };
#use Class::InsideOut qw{ :std };
use Moose;
use WebGUI::Exception;
use WebGUI::International;
use WebGUI::Pluggable;
@ -24,6 +25,7 @@ use WebGUI::Shop::Admin;
#use WebGUI::Shop::PayDriver;
use WebGUI::Utility;
use Tie::IxHash;
use Scalar::Util;
=head1 NAME
@ -43,8 +45,34 @@ These subroutines are available from this package:
=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 )

View file

@ -367,7 +367,7 @@ Returns a reference to the payment gateway attached to this transaction.
sub getPaymentGateway {
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'));
}

View file

@ -18,7 +18,7 @@ use strict;
use lib "$FindBin::Bin/../lib";
use Test::More;
use Test::Deep;
#use Test::Exception;
use Test::Exception;
use JSON;
use HTML::Form;
@ -30,12 +30,6 @@ use WebGUI::TestException;
# Init
my $session = WebGUI::Test->session;
#----------------------------------------------------------------------------
# Tests
my $tests = 18;
plan tests => 1 + $tests;
#----------------------------------------------------------------------------
# put your tests here
@ -45,31 +39,19 @@ my $storage;
my $newDriver;
my $anotherDriver;
SKIP: {
skip 'Unable to load module WebGUI::Shop::Pay', $tests unless $loaded;
#######################################################################
#
# new
#
#######################################################################
my $e;
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(); },
'WebGUI::Error::InvalidObject',
{
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);
lives_ok { $pay = WebGUI::Shop::Pay->new(session => $session); } 'new called with hash arguments';
lives_ok { $pay = WebGUI::Shop::Pay->new($session); } 'new called only with session';
isa_ok($pay, 'WebGUI::Shop::Pay', 'new returned the right kind of object');
#######################################################################
@ -232,14 +214,11 @@ cmp_bag(
#
#######################################################################
}
done_testing();
#----------------------------------------------------------------------------
# Cleanup
END {
defined $newDriver and $newDriver->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');
}