Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

New tests added for Nitesi::Cart remove method #2

Merged
merged 6 commits into from

2 participants

@rmtemplet
Collaborator

No description provided.

rmtemplet and others added some commits
@rmtemplet rmtemplet Added tests for remove item 219c504
@racke racke Add author and copyright to Nitesi::Cart's POD. 27bae61
@racke racke Add id method to Nitesi::Cart class. 6b5b566
@racke racke Fix Perl warning on removal of first item in the cart. 2bae2c6
@rmtemplet rmtemplet Adjustments to the testing
1) Remove Data::Dumper
2) Increment the amount of tests
3) Ensure to add the item to the cart before specifiying a hook
   for removing the item.
0e5de0c
@rmtemplet rmtemplet Added 2 new tests
1) Test amount of items in cart after removing an item that
   got caught by a hook.  I also expanded the error message
   from the hook to be a bit more specific.
2) Test the amount of items in the cart after removing an item
   without a hook.
cd52d4b
@racke racke referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@racke racke merged commit 38ebd56 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 4, 2011
  1. @rmtemplet

    Added tests for remove item

    rmtemplet authored
Commits on Oct 5, 2011
  1. @racke @rmtemplet

    Add author and copyright to Nitesi::Cart's POD.

    racke authored rmtemplet committed
  2. @racke @rmtemplet

    Add id method to Nitesi::Cart class.

    racke authored rmtemplet committed
  3. @racke @rmtemplet

    Fix Perl warning on removal of first item in the cart.

    racke authored rmtemplet committed
  4. @rmtemplet

    Adjustments to the testing

    rmtemplet authored
    1) Remove Data::Dumper
    2) Increment the amount of tests
    3) Ensure to add the item to the cart before specifiying a hook
       for removing the item.
  5. @rmtemplet

    Added 2 new tests

    rmtemplet authored
    1) Test amount of items in cart after removing an item that
       got caught by a hook.  I also expanded the error message
       from the hook to be a bit more specific.
    2) Test the amount of items in the cart after removing an item
       without a hook.
This page is out of date. Refresh to see the latest.
Showing with 64 additions and 1 deletion.
  1. +35 −0 lib/Nitesi/Cart.pm
  2. +29 −1 t/003_cart.t
View
35 lib/Nitesi/Cart.pm
@@ -182,6 +182,8 @@ sub remove {
my ($self, $arg) = @_;
my ($pos, $found, $item);
+ $pos = 0;
+
for $item (@{$self->{items}}) {
if ($item->{sku} eq $arg) {
$found = 1;
@@ -227,6 +229,23 @@ sub clear {
$self->{items} = [];
}
+=head2 id
+
+Get or set id of the cart. This can be used for subclasses,
+e.g. primary key value for carts in the database.
+
+=cut
+
+sub id {
+ my $self = shift;
+
+ if (@_ > 0) {
+ $self->{id} = $_[0];
+ }
+
+ return $self->{id};
+}
+
=head2 name
Get or set the name of the cart.
@@ -297,4 +316,20 @@ sub _run_hook {
return $ret;
}
+=head1 AUTHOR
+
+Stefan Hornburg (Racke), <racke@linuxia.de>
+
+=head1 LICENSE AND COPYRIGHT
+
+Copyright 2011 Stefan Hornburg (Racke) <racke@linuxia.de>.
+
+This program is free software; you can redistribute it and/or modify it
+under the terms of either: the GNU General Public License as published
+by the Free Software Foundation; or the Artistic License.
+
+See http://dev.perl.org/licenses/ for more information.
+
+=cut
+
1;
View
30 t/003_cart.t
@@ -5,7 +5,7 @@
use strict;
use warnings;
-use Test::More tests => 20;
+use Test::More tests => 24;
use Nitesi::Cart;
@@ -57,6 +57,34 @@ ok(ref($ret) eq 'HASH', $cart->error);
$ret = $cart->items;
ok(@$ret == 2, "Items: $ret");
+# Cart removal
+$cart = Nitesi::Cart->new(run_hooks => sub {
+ my ($hook, $cart, $item) = @_;
+
+ if ($hook eq 'before_cart_remove' && $item->{sku} eq '123') {
+ $item->{error} = 'Item not removed due to hook.';
+ }
+ });
+
+$item = {sku => 'DEF', name => 'Foobar', price => 5};
+$ret = $cart->add($item);
+
+$item = {sku => '123', name => 'Foobar', price => 5};
+$ret = $cart->add($item);
+
+$ret = $cart->remove('123');
+ok($cart->error eq 'Item not removed due to hook.', "Cart Error: " . $cart->error);
+
+$ret = $cart->items;
+ok(@$ret == 2, "Items: $ret");
+
+$ret = $cart->remove('DEF');
+ok(defined($ret), "Item DEF removed from cart.");
+
+$ret = $cart->items;
+ok(@$ret == 1, "Items: $ret");
+
+#
# Calculating total
$cart->clear;
$ret = $cart->total;
Something went wrong with that request. Please try again.