Skip to content

Commit 43af360

Browse files
committed
Corrected Tests
1 parent 32ccba7 commit 43af360

File tree

2 files changed

+88
-19
lines changed

2 files changed

+88
-19
lines changed

src/Darryldecode/Cart/Cart.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,23 +181,28 @@ public function add($id, $name = null, $price = null, $quantity = null, $attribu
181181
$id['quantity'],
182182
Helpers::issetAndHasValueOrAssignDefault($id['attributes'], array()),
183183
Helpers::issetAndHasValueOrAssignDefault($id['conditions'], array()),
184-
Helpers::issetAndHasValueOrAssignDefault($item['associatedModel'], null)
184+
Helpers::issetAndHasValueOrAssignDefault($id['associatedModel'], null)
185185
);
186186
}
187187

188188
return $this;
189189
}
190190

191-
// validate data
192-
$item = $this->validate(array(
191+
$data = array(
193192
'id' => $id,
194193
'name' => $name,
195194
'price' => Helpers::normalizePrice($price),
196195
'quantity' => $quantity,
197196
'attributes' => new ItemAttributeCollection($attributes),
198-
'conditions' => $conditions,
199-
'associatedModel' => $associatedModel,
200-
));
197+
'conditions' => $conditions
198+
);
199+
200+
if (isset($associatedModel) && $associatedModel != '') {
201+
$data['associatedModel'] = $associatedModel;
202+
}
203+
204+
// validate data
205+
$item = $this->validate($data);
201206

202207
// get the cart
203208
$cart = $this->getContent();

tests/CartTest.php

Lines changed: 77 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88

99
use Darryldecode\Cart\Cart;
1010
use Mockery as m;
11+
use Darryldecode\Tests\helpers\MockProduct;
1112

12-
require_once __DIR__.'/helpers/SessionMock.php';
13+
require_once __DIR__ . '/helpers/SessionMock.php';
1314

14-
class CartTest extends PHPUnit\Framework\TestCase {
15+
class CartTest extends PHPUnit\Framework\TestCase
16+
{
1517

1618
/**
1719
* @var Darryldecode\Cart\Cart
@@ -28,8 +30,8 @@ public function setUp()
2830
$events,
2931
'shopping',
3032
'SAMPLESESSIONKEY',
31-
require(__DIR__.'/helpers/configMock.php')
32-
);
33+
require(__DIR__ . '/helpers/configMock.php')
34+
);
3335
}
3436

3537
public function tearDown()
@@ -42,7 +44,7 @@ public function test_cart_can_add_item()
4244
$this->cart->add(455, 'Sample Item', 100.99, 2, array());
4345

4446
$this->assertFalse($this->cart->isEmpty(), 'Cart should not be empty');
45-
$this->assertEquals(1, $this->cart->getContent()->count(),'Cart content should be 1');
47+
$this->assertEquals(1, $this->cart->getContent()->count(), 'Cart content should be 1');
4648
$this->assertEquals(455, $this->cart->getContent()->first()['id'], 'Item added has ID of 455 so first content ID should be 455');
4749
$this->assertEquals(100.99, $this->cart->getContent()->first()['price'], 'Item added has price of 100.99 so first content price should be 100.99');
4850
}
@@ -139,7 +141,7 @@ public function test_cart_update_with_attribute_then_attributes_should_be_still_
139141
'color' => 'red'
140142
)
141143
);
142-
$this->cart->update(456,$updatedItem);
144+
$this->cart->update(456, $updatedItem);
143145

144146
$this->assertInstanceOf('Darryldecode\Cart\ItemAttributeCollection', $item->attributes);
145147
}
@@ -231,7 +233,7 @@ public function test_cart_update_existing_item_with_quantity_as_array_and_not_re
231233
// should be incremented or decremented, we should also allow the quantity
232234
// value to be in array format and provide a field if the quantity should not be
233235
// treated as relative to Item quantity current value
234-
$this->cart->update($itemIdToEvaluate,array('quantity' => array('relative' => false, 'value' => 5)));
236+
$this->cart->update($itemIdToEvaluate, array('quantity' => array('relative' => false, 'value' => 5)));
235237

236238
$item = $this->cart->get($itemIdToEvaluate);
237239
$this->assertEquals(5, $item['quantity'], 'Item quantity should be 5');
@@ -242,7 +244,7 @@ public function test_item_price_should_be_normalized_when_added_to_cart()
242244
// add a price in a string format should be converted to float
243245
$this->cart->add(455, 'Sample Item', '100.99', 2, array());
244246

245-
$this->assertInternalType('float',$this->cart->getContent()->first()['price'], 'Cart price should be a float');
247+
$this->assertInternalType('float', $this->cart->getContent()->first()['price'], 'Cart price should be a float');
246248
}
247249

248250
public function test_it_removes_an_item_on_cart_by_item_id()
@@ -458,12 +460,12 @@ public function test_clearing_cart()
458460

459461
$this->cart->add($items);
460462

461-
$this->assertFalse($this->cart->isEmpty(),'prove first cart is not empty');
463+
$this->assertFalse($this->cart->isEmpty(), 'prove first cart is not empty');
462464

463465
// now let's clear cart
464466
$this->cart->clear();
465467

466-
$this->assertTrue($this->cart->isEmpty(),'cart should now be empty');
468+
$this->assertTrue($this->cart->isEmpty(), 'cart should now be empty');
467469
}
468470

469471
public function test_cart_get_total_quantity()
@@ -487,10 +489,72 @@ public function test_cart_get_total_quantity()
487489

488490
$this->cart->add($items);
489491

490-
$this->assertFalse($this->cart->isEmpty(),'prove first cart is not empty');
492+
$this->assertFalse($this->cart->isEmpty(), 'prove first cart is not empty');
491493

492494
// now let's count the cart's quantity
493495
$this->assertInternalType("int", $this->cart->getTotalQuantity(), 'Return type should be INT');
494-
$this->assertEquals(4, $this->cart->getTotalQuantity(),'Cart\'s quantity should be 4.');
496+
$this->assertEquals(4, $this->cart->getTotalQuantity(), 'Cart\'s quantity should be 4.');
497+
}
498+
499+
public function test_cart_can_add_items_as_array_with_associated_model()
500+
{
501+
$item = array(
502+
'id' => 456,
503+
'name' => 'Sample Item',
504+
'price' => 67.99,
505+
'quantity' => 4,
506+
'attributes' => array(),
507+
'associatedModel' => MockProduct::class
508+
);
509+
510+
$this->cart->add($item);
511+
512+
$addedItem = $this->cart->get($item['id']);
513+
514+
$this->assertFalse($this->cart->isEmpty(), 'Cart should not be empty');
515+
$this->assertEquals(1, $this->cart->getContent()->count(), 'Cart should have 1 item on it');
516+
$this->assertEquals(456, $this->cart->getContent()->first()['id'], 'The first content must have ID of 456');
517+
$this->assertEquals('Sample Item', $this->cart->getContent()->first()['name'], 'The first content must have name of "Sample Item"');
518+
$this->assertInstanceOf('Darryldecode\Tests\helpers\MockProduct', $addedItem->model);
519+
}
520+
521+
public function test_cart_can_add_items_with_multidimensional_array_with_associated_model()
522+
{
523+
$items = array(
524+
array(
525+
'id' => 456,
526+
'name' => 'Sample Item 1',
527+
'price' => 67.99,
528+
'quantity' => 4,
529+
'attributes' => array(),
530+
'associatedModel' => MockProduct::class
531+
),
532+
array(
533+
'id' => 568,
534+
'name' => 'Sample Item 2',
535+
'price' => 69.25,
536+
'quantity' => 4,
537+
'attributes' => array(),
538+
'associatedModel' => MockProduct::class
539+
),
540+
array(
541+
'id' => 856,
542+
'name' => 'Sample Item 3',
543+
'price' => 50.25,
544+
'quantity' => 4,
545+
'attributes' => array(),
546+
'associatedModel' => MockProduct::class
547+
),
548+
);
549+
550+
$this->cart->add($items);
551+
552+
$content = $this->cart->getContent();
553+
foreach ($content as $item) {
554+
$this->assertInstanceOf('Darryldecode\Tests\helpers\MockProduct', $item->model);
555+
}
556+
557+
$this->assertFalse($this->cart->isEmpty(), 'Cart should not be empty');
558+
$this->assertCount(3, $this->cart->getContent()->toArray(), 'Cart should have 3 items');
495559
}
496-
}
560+
}

0 commit comments

Comments
 (0)