Skip to content

Commit

Permalink
Role::Cost->cost returns undef if cost not found. Closes interchange#19
Browse files Browse the repository at this point in the history
Previous behaviour was to croak if a valid-looking argument was provided
but the cost was not found. This can cause a problem if for example no
shipping has been defined but a cart page tries to show the shipping.
  • Loading branch information
SysPete committed May 23, 2016
1 parent 57bbc98 commit ef7a715
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
8 changes: 7 additions & 1 deletion lib/Interchange6/Role/Costs.pm
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ B<Example:> Return tax value by position
Returns the cost that was first applied to subtotal. By increasing the number you can retrieve other costs applied.
Returns undef if the cost cannot be found.
=cut

sub cost {
Expand All @@ -197,6 +199,9 @@ sub cost {
}
}
}
else {
croak "Bad argument to cost";
}
}
else {
croak "Either position or name required as argument to cost";
Expand All @@ -207,7 +212,8 @@ sub cost {
$self->total;
}
else {
croak "Bad argument to cost: " . $loc;
# not found
return undef;
}

return $cost->current_amount;
Expand Down
10 changes: 6 additions & 4 deletions t/unit/costs_role.t
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,16 @@ cmp_ok( $cost, "==", 2, "amount is good" );
throws_ok { $obj->cost } qr/position or name required/,
"fail call cost with no args";

throws_ok { $obj->cost(2) } qr/Bad argument to cost/,
"fail call cost with index that doesn't exist";
lives_ok { $cost = $obj->cost(2) }
"call cost with index that doesn't exist lives";
ok !defined $cost, "returned undef";

throws_ok { $obj->cost("") } qr/Bad argument to cost/,
"fail call cost with empty name";

throws_ok { $obj->cost("BadName") } qr/Bad argument to cost/,
"fail call cost with name that doesn't exist";
lives_ok { $cost = $obj->cost("BadName") }
"call cost with name that doesn't exist lives";
ok !defined $cost, "returned undef";

lives_ok { $obj->clear_costs } "clear_costs";

Expand Down

0 comments on commit ef7a715

Please sign in to comment.