From 6ac0aa936c15a6d2eb24efcff9fef4a87b346dde Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Thu, 24 Jun 2010 13:41:07 -0700 Subject: [PATCH] Update Shop::Ship to use Moose instead of C::IO. Update tests, and core code. Provide a shim for the old syntax. --- lib/WebGUI/Content/Shop.pm | 2 +- lib/WebGUI/Shop/Cart.pm | 4 ++-- lib/WebGUI/Shop/Ship.pm | 44 +++++++++++++++++--------------------- t/Shop/Ship.t | 31 ++++++--------------------- 4 files changed, 30 insertions(+), 51 deletions(-) diff --git a/lib/WebGUI/Content/Shop.pm b/lib/WebGUI/Content/Shop.pm index 8fd954c87..6510c3058 100644 --- a/lib/WebGUI/Content/Shop.pm +++ b/lib/WebGUI/Content/Shop.pm @@ -199,7 +199,7 @@ sub www_ship { my $session = shift; my $output = undef; my $method = "www_".$session->form->get("method"); - my $ship = WebGUI::Shop::Ship->new($session); + my $ship = WebGUI::Shop::Ship->new(session => $session); if ($method ne "www_" && $ship->can($method)) { $output = $ship->$method($session); } diff --git a/lib/WebGUI/Shop/Cart.pm b/lib/WebGUI/Shop/Cart.pm index 5e59d113e..126e4d226 100644 --- a/lib/WebGUI/Shop/Cart.pm +++ b/lib/WebGUI/Shop/Cart.pm @@ -369,7 +369,7 @@ Returns the WebGUI::Shop::ShipDriver object that is attached to this cart for sh sub getShipper { my $self = shift; - return WebGUI::Shop::Ship->new($self->session)->getShipper($self->get("shipperId")); + return WebGUI::Shop::Ship->new(session => $self->session)->getShipper($self->get("shipperId")); } #------------------------------------------------------------------- @@ -845,7 +845,7 @@ sub www_view { else { $var{hasShippingAddress} = 1; $var{shippingAddress} = $address->getHtmlFormatted; - my $ship = WebGUI::Shop::Ship->new($self->session); + my $ship = WebGUI::Shop::Ship->new(session => $self->session); my $options = $ship->getOptions($self); my $numberOfOptions = scalar keys %{ $options }; if ($numberOfOptions < 1) { diff --git a/lib/WebGUI/Shop/Ship.pm b/lib/WebGUI/Shop/Ship.pm index 5dc2f1aaf..8e682335c 100644 --- a/lib/WebGUI/Shop/Ship.pm +++ b/lib/WebGUI/Shop/Ship.pm @@ -2,7 +2,7 @@ package WebGUI::Shop::Ship; use strict; -use Class::InsideOut qw{ :std }; +use Moose; use WebGUI::Exception; use WebGUI::International; use WebGUI::Pluggable; @@ -28,7 +28,25 @@ These subroutines are available from this package: =cut -readonly session => my %session; +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(@_); +}; #------------------------------------------------------------------- @@ -163,28 +181,6 @@ sub getShippers { #------------------------------------------------------------------- -=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. diff --git a/t/Shop/Ship.t b/t/Shop/Ship.t index 0efead294..7cfb77bd4 100644 --- a/t/Shop/Ship.t +++ b/t/Shop/Ship.t @@ -18,6 +18,7 @@ use strict; use lib "$FindBin::Bin/../lib"; use Test::More; use Test::Deep; +use Test::Exception; use JSON; use HTML::Form; use Data::Dumper; @@ -33,9 +34,6 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -my $tests = 22; -plan tests => 1 + $tests; - #---------------------------------------------------------------------------- # put your tests here @@ -46,32 +44,16 @@ my $driver; my $driver2; my $ship; -SKIP: { - -skip 'Unable to load module WebGUI::Shop::Ship', $tests unless $loaded; - ####################################################################### # # new # ####################################################################### -my $e; +dies_ok { $ship = WebGUI::Shop::Ship->new(); } 'new takes an exception to not giving it a session variable'; -eval { $ship = WebGUI::Shop::Ship->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', -); - -$ship = WebGUI::Shop::Ship->new($session); +lives_ok { $ship = WebGUI::Shop::Ship->new(session => $session); } 'new takes hash arguments'; +lives_ok { $ship = WebGUI::Shop::Ship->new($session); } 'new takes a bare session object'; isa_ok($ship, 'WebGUI::Shop::Ship', 'new returned the right kind of object'); isa_ok($ship->session, 'WebGUI::Session', 'session method returns a session object'); @@ -107,6 +89,8 @@ cmp_bag( my $shipper; +my $e; + eval { $shipper = $ship->addShipper(); }; $e = Exception::Class->caught(); isa_ok($e, 'WebGUI::Error::InvalidParam', 'addShipper croaks without a class'); @@ -216,12 +200,11 @@ cmp_deeply( $cart->delete; -} +done_testing(); #---------------------------------------------------------------------------- # Cleanup END { $driver->delete; $driver2->delete; - is(scalar @{$ship->getShippers()}, 1, 'getShippers: deleted all test shippers'); }