Skip to content

Commit

Permalink
Improved workaround for GH #28
Browse files Browse the repository at this point in the history
Seems there is an issue with Type::Tiny::XS Int check. If we stringify
Product quantity in the isa check then the assertion works correctly
whether Type::Tiny::XS is used or not. Sucks. Time to create a ticket
against Type::Tiny::XS...
  • Loading branch information
SysPete committed Feb 29, 2016
1 parent 274f5d8 commit a0e5d29
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
3 changes: 1 addition & 2 deletions lib/Interchange6/Cart.pm
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,7 @@ sub update {

die "sku not defined in arg to update" unless defined $sku;

PositiveOrZeroInt->check($qty)
or die "quantity argument to update must be positive integer or zero";
defined($qty) or die "quantity argument to update must be defined";

unless ( $product = $self->find($sku) ) {
die "Product for $sku not found in cart.";
Expand Down
13 changes: 11 additions & 2 deletions lib/Interchange6/Cart/Product.pm
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,17 @@ than zero. Default for quantity is 1.
=cut

has quantity => (
is => 'ro',
isa => PositiveInt,
is => 'ro',
# https://github.com/interchange/Interchange6/issues/28
# Tupe::Tiny::XS sometimes incorrectly passes Int assertion for
# non-integer values such as 2.3 so we can't just do:
# isa => PositiveInt
# but if we stringify the value then things work as expected. Huh?
# Also prevent uninitialized warning in case value is undef.
isa => sub {
no warnings 'uninitialized';
PositiveInt->assert_valid("$_[0]");
},
default => 1,
writer => 'set_quantity',
);
Expand Down
17 changes: 10 additions & 7 deletions t/unit/cart.t
Original file line number Diff line number Diff line change
Expand Up @@ -339,17 +339,20 @@ throws_ok { $cart->update( "badsku" => 1 ) } qr/badsku not found in cart/,
"fail update with bad sku";

throws_ok { $cart->update("SKU01") }
qr/quantity argument to update must be positive integer or zero/,
"fail update no quantity";
qr/quantity argument to update must be defined/, "fail update no quantity";

throws_ok { $cart->update(undef) } qr/sku not defined/, "fail update sku undef";

throws_ok { $cart->update( SKU01 => 2.3 ) }
qr/quantity argument to update must be positive integer or zero/,
# NOTE: this is the test which fails if we simply use 'isa => PositiveInt'
# for Cart::Products's quantity attribute.
# https://github.com/interchange/Interchange6/issues/28
throws_ok { $cart->update( SKU01 => 2.3 ) } qr/Must be a positive integer/,
"fail update with non-integer quantity"
or diag "SKU01 quantity set 2.3 is: "
. $cart->product_get( $cart->product_index( sub { $_->sku eq 'SKU01' } ) )
->quantity;
or diag(
"SKU01 quantity set 2.3 is: ",
$cart->product_get( $cart->product_index( sub { $_->sku eq 'SKU01' } ) )
->quantity
);

lives_ok { @products = $cart->update( SKU01 => 3 ) }
"set SKU01 qty to what it already is in cart";
Expand Down

0 comments on commit a0e5d29

Please sign in to comment.