getName, getId, set, create with db storage.
Added tests for all those methods. Check that definition gets a session object or else.
This commit is contained in:
parent
72177f762c
commit
b174ce51fb
2 changed files with 109 additions and 16 deletions
|
|
@ -6,6 +6,7 @@ use Class::InsideOut qw{ :std };
|
|||
use Carp qw(croak);
|
||||
use Tie::IxHash;
|
||||
use WebGUI::International;
|
||||
use JSON;
|
||||
|
||||
=head1 NAME
|
||||
|
||||
|
|
@ -79,7 +80,7 @@ sub create {
|
|||
$className{ $id } = __PACKAGE__;
|
||||
|
||||
$session->db->write('insert into shipper (shipperId,className) VALUES (?,?)', [$shipperId, $className{$id}]);
|
||||
#$self->set($options);
|
||||
$self->set($options);
|
||||
|
||||
return $self;
|
||||
}
|
||||
|
|
@ -97,6 +98,8 @@ the user.
|
|||
sub definition {
|
||||
my $class = shift;
|
||||
my $session = shift;
|
||||
croak "Definition requires a session object"
|
||||
unless ref $session eq 'WebGUI::Session';
|
||||
my $definition = shift || [];
|
||||
my $i18n = WebGUI::International->new($session, 'ShipDriver');
|
||||
tie my %properties, 'Tie::IxHash';
|
||||
|
|
@ -123,19 +126,53 @@ sub definition {
|
|||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 label ( )
|
||||
=head2 delete ( )
|
||||
|
||||
Accessor for the label property. This is the name assigned to this
|
||||
driver, something like "Slow and dangerous".
|
||||
Removes this ShipDriver object from the db.
|
||||
|
||||
=cut
|
||||
|
||||
sub delete {
|
||||
my $self = shift;
|
||||
$self->session->db->write('delete from shipper');
|
||||
return;
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 getID ( )
|
||||
|
||||
Returns the shipperId. This is an alias for shipperId provided
|
||||
since a lot of WebGUI classes have a getId method.
|
||||
|
||||
=cut
|
||||
|
||||
sub getId {
|
||||
return shift->shipperId;
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 getName ( )
|
||||
|
||||
Return a human readable name for this driver. Never overridden in the
|
||||
subclass, instead specified in definition with the name "name".
|
||||
|
||||
=cut
|
||||
|
||||
sub getName {
|
||||
my $self = shift;
|
||||
my $definition = WebGUI::Shop::ShipDriver->definition($self->session);
|
||||
return $definition->[0]->{name};
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 options ( )
|
||||
|
||||
Accessor for the driver properties. This returns a JSON string of
|
||||
any driver specific properties. Driver properties have a
|
||||
Accessor for the driver properties. This returns a hashref
|
||||
any driver specific properties. To set the properties, use
|
||||
the C<set> method.
|
||||
|
||||
=cut
|
||||
|
||||
|
|
@ -149,6 +186,28 @@ Accessor for the session object. Returns the session object.
|
|||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 set ( $options )
|
||||
|
||||
Setter for user configurable options in the ship objects.
|
||||
|
||||
=head4 $options
|
||||
|
||||
A list of properties to assign to this ShipperDriver. See C<definition> for details.
|
||||
|
||||
=cut
|
||||
|
||||
sub set {
|
||||
my $self = shift;
|
||||
my $options = shift;
|
||||
croak "set was not sent a hashref of options to store in the database"
|
||||
unless ref($options) eq 'HASH' and scalar keys %{ $options };
|
||||
my $jsonOptions = to_json($options);
|
||||
$self->session->db->write('update shipper set options=? where shipperId=?', [$jsonOptions, $self->shipperId]);
|
||||
return;
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 shipperId ( )
|
||||
|
||||
Accessor for the unique identifier for this shipperDriver. The shipperId is
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ use strict;
|
|||
use lib "$FindBin::Bin/../lib";
|
||||
use Test::More;
|
||||
use Test::Deep;
|
||||
use JSON;
|
||||
use WebGUI::Test; # Must use this before any other WebGUI modules
|
||||
use WebGUI::Session;
|
||||
|
||||
|
|
@ -28,7 +29,7 @@ my $session = WebGUI::Test->session;
|
|||
#----------------------------------------------------------------------------
|
||||
# Tests
|
||||
|
||||
my $tests = 9;
|
||||
my $tests = 15;
|
||||
plan tests => 1 + $tests;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
|
|
@ -48,7 +49,12 @@ skip 'Unable to load module WebGUI::Shop::ShipDriver', $tests unless $loaded;
|
|||
#
|
||||
#######################################################################
|
||||
|
||||
my $definition = WebGUI::Shop::ShipDriver->definition($session);
|
||||
my $definition;
|
||||
|
||||
eval { $definition = WebGUI::Shop::ShipDriver->definition(); };
|
||||
like ($@, qr/^Definition requires a session object/, 'definition croaks without a session object');
|
||||
|
||||
$definition = WebGUI::Shop::ShipDriver->definition($session);
|
||||
|
||||
cmp_deeply(
|
||||
$definition,
|
||||
|
|
@ -92,7 +98,7 @@ cmp_deeply(
|
|||
|
||||
#######################################################################
|
||||
#
|
||||
# new
|
||||
# create
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
|
|
@ -104,13 +110,11 @@ like ($@, qr/You must pass a hashref of options to create a new ShipDriver objec
|
|||
eval { $driver = WebGUI::Shop::ShipDriver->create($session, {}); };
|
||||
like ($@, qr/You must pass a hashref of options to create a new ShipDriver object/, 'create croaks with an empty hashref of options');
|
||||
|
||||
$driver = WebGUI::Shop::ShipDriver->create(
|
||||
$session,
|
||||
{
|
||||
label => 'Slow and dangerous',
|
||||
enabled => 1,
|
||||
}
|
||||
);
|
||||
my $options = {
|
||||
label => 'Slow and dangerous',
|
||||
enabled => 1,
|
||||
};
|
||||
$driver = WebGUI::Shop::ShipDriver->create( $session, $options);
|
||||
|
||||
isa_ok($driver, 'WebGUI::Shop::ShipDriver');
|
||||
|
||||
|
|
@ -119,21 +123,50 @@ isa_ok($driver->session, 'WebGUI::Session', 'session method returns a session ob
|
|||
is($session->getId, $driver->session->getId, 'session method returns OUR session object');
|
||||
|
||||
like($driver->shipperId, $session->id->getValidator, 'got a valid GUID for shipperId');
|
||||
is($driver->getId, $driver->shipperId, 'getId returns the same thing as shipperId');
|
||||
|
||||
is($driver->className, ref $driver, 'className property set correctly');
|
||||
|
||||
cmp_deeply($driver->options, $options, 'options accessor works');
|
||||
|
||||
my $dbData = $session->db->quickHashRef('select * from shipper limit 1');
|
||||
cmp_deeply(
|
||||
$dbData,
|
||||
{
|
||||
shipperId => $driver->shipperId,
|
||||
className => ref($driver),
|
||||
options => q|{"label":"Slow and dangerous","enabled":1}|,
|
||||
},
|
||||
'Correct data written to the db',
|
||||
);
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# getName
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
is ($driver->getName, 'Shipper Driver', 'getName returns the human readable name of this driver');
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# getEditForm
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# delete
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
$driver->delete;
|
||||
|
||||
my $count = $session->db->quickScalar('select count(*) from shipper where shipperId=?',[$driver->shipperId]);
|
||||
is($count, 0, 'delete deleted the object');
|
||||
|
||||
undef $driver;
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# calculate
|
||||
|
|
@ -145,4 +178,5 @@ is($driver->className, ref $driver, 'className property set correctly');
|
|||
#----------------------------------------------------------------------------
|
||||
# Cleanup
|
||||
END {
|
||||
$session->db->write('delete from shipper');
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue