diff --git a/lib/WebGUI/Shop/Tax.pm b/lib/WebGUI/Shop/Tax.pm index ef660a73b..020922305 100644 --- a/lib/WebGUI/Shop/Tax.pm +++ b/lib/WebGUI/Shop/Tax.pm @@ -37,7 +37,8 @@ Add tax information to the table. Returns the taxId of the newly created tax in =head3 $params -A hash ref of the geographic and rate information. All parameters are required. +A hash ref of the geographic and rate information. All parameters are required and +must have defined values. =head4 field @@ -62,6 +63,12 @@ sub add { my $self = shift; my $params = shift; my $id = $self->session->id->generate(); + croak "Must pass in a hashref" + unless ref($params) eq 'HASH'; + foreach my $key (qw/field value taxRate/) { + croak "Hash ref must contain a $key key with a defined value" + unless exists($params->{$key}) and defined $params->{$key}; + } $self->session->db->write('insert into tax (taxId, field, value, taxRate) VALUES (?,?,?,?)', [$id, @{ $params }{qw[ field value taxRate ]}]); return $id; } diff --git a/t/Shop/Tax.t b/t/Shop/Tax.t index d2824be4e..f351b0f81 100644 --- a/t/Shop/Tax.t +++ b/t/Shop/Tax.t @@ -28,7 +28,7 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -my $tests = 17; +my $tests = 24; plan tests => 1 + $tests; #---------------------------------------------------------------------------- @@ -72,6 +72,46 @@ is($taxIterator->rows, 0, 'WebGUI ships with no predefined tax data'); # ####################################################################### +eval{$taxer->add()}; +like($@, qr{Must pass in a hashref}, + 'add: error handling for missing hashref'); + +eval{$taxer->add({})}; +like($@, qr{Hash ref must contain a field key with a defined value}, + 'add: error handling for missing field hashref key'); + +my $taxData = { + field => undef, +}; + +eval{$taxer->add($taxData)}; +like($@, qr{Hash ref must contain a field key with a defined value}, + 'add: error handling for undefined field key'); + +$taxData->{field} = 'state'; + +eval{$taxer->add($taxData)}; +like($@, qr{Hash ref must contain a value key with a defined value}, + 'add: error handling for missing value key'); + +$taxData->{value} = undef; + +eval{$taxer->add($taxData)}; +like($@, qr{Hash ref must contain a value key with a defined value}, + 'add: error handling for undefined value key'); + +$taxData->{value} = 'Oregon'; + +eval{$taxer->add($taxData)}; +like($@, qr{Hash ref must contain a taxRate key with a defined value}, + 'add: error handling for missing taxRate key'); + +$taxData->{taxRate} = undef; + +eval{$taxer->add($taxData)}; +like($@, qr{Hash ref must contain a taxRate key with a defined value}, + 'add: error handling for undefined taxRate key'); + my $taxData = { field => 'state', value => 'Oregon',