From 416f62f36af1beae19a508266e07ad4ab9d83ad7 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Wed, 7 Jul 2010 12:31:37 -0700 Subject: [PATCH] Fix problems with Moose style object creation. --- lib/WebGUI/Shop/Vendor.pm | 4 ++-- t/Shop/Vendor.t | 22 +++++++++++++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/WebGUI/Shop/Vendor.pm b/lib/WebGUI/Shop/Vendor.pm index 6dd682a24..4584aa601 100644 --- a/lib/WebGUI/Shop/Vendor.pm +++ b/lib/WebGUI/Shop/Vendor.pm @@ -50,12 +50,12 @@ around BUILDARGS => sub { ##Need same db code as below here. ##Session check goes here? ##Build a new one - my $session = $_[0]->{session}; + my $properties = $_[0]; + my $session = $properties->{session}; if (! (blessed $session && $session->isa('WebGUI::Session')) ) { WebGUI::Error::InvalidObject->throw(expected=>"WebGUI::Session", got=>(ref $session), error=>"Need a session."); } my ($vendorId, $dateCreated) = $class->_init($session); - my $properties = {}; $properties->{vendorId} = $vendorId; $properties->{dateCreated} = $dateCreated; return $class->$orig($properties); diff --git a/t/Shop/Vendor.t b/t/Shop/Vendor.t index eca807bc1..708e1b78c 100644 --- a/t/Shop/Vendor.t +++ b/t/Shop/Vendor.t @@ -31,7 +31,7 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -plan tests => 51; +plan tests => 55; #---------------------------------------------------------------------------- # put your tests here @@ -67,6 +67,19 @@ cmp_deeply( 'new: requires a session variable', ); +eval { $vendor = WebGUI::Shop::Vendor->new({ userId => 3, }); }; +$e = Exception::Class->caught(); +isa_ok($e, 'WebGUI::Error::InvalidObject', 'new via property hash takes an exception to not giving it a session variable'); +cmp_deeply( + $e, + methods( + error => 'Need a session.', + got => '', + expected => 'WebGUI::Session', + ), + '... requires a session variable', +); + eval { $vendor = WebGUI::Shop::Vendor->new($session); }; $e = Exception::Class->caught(); isa_ok($e, 'WebGUI::Error::InvalidParam', 'new takes an exception to not giving it a vendor id to instanciate'); @@ -90,6 +103,13 @@ cmp_deeply( 'new: requires a valid vendorId', ); +my $test_vendor = eval { WebGUI::Shop::Vendor->new({ session => $session, }); }; +$e = Exception::Class->caught(); +ok(!$e, 'new via property hash with session'); +isa_ok($test_vendor, 'WebGUI::Shop::Vendor', '... returns correct type of object'); +WebGUI::Test->addToCleanup($test_vendor); +$test_vendor->delete; + eval { $vendor = WebGUI::Shop::Vendor->new($session, 'defaultvendor000000000'); }; $e = Exception::Class->caught(); ok(!$e, 'No exception thrown');