$I = new ApiTester($scenario); $I->wantTo('Test the cart behaviour with items'); $I->amGoingTo('confirm the cart is empty'); $I->sendGET('cart'); $I->seeCodeAndJson(200, ['items' => [], 'total' => floatify(0)]); /** * @var array $item1 * @var array $item2 * @var int $item_id */ require "_AddItems.php"; $I->amGoingTo('Verify order'); $I->sendGET('cart'); $I->expectTo('see items in order'); $I->seeCodeAndJson(200, ['items' => [$item1, $item2]]); $I->expectTo('see correct total'); $I->seeResponseContainsJson(['total' => floatify($item1['final_price'] + $item2['final_price'])]); $I->amGoingTo('delete an item'); $I->sendDELETE('cart/item/' . $item_id); $I->seeResponseCodeIs(204); $I->seeResponseEquals(''); $I->sendGET('cart'); $I->seeCodeAndJson(200, ['items' => [$item2], 'total' => floatify($item2['final_price'])]); $I->amGoingTo('clear the cart'); $I->sendDELETE('cart'); $I->seeResponseCodeIs(204); $I->seeResponseEquals(''); $I->sendGET('cart'); $I->seeCodeAndJson(200, ['items' => [], 'total' => floatify(0)]);
/** @var Faker\Generator $faker */ $faker = Fixtures::get('faker'); $coupons = Fixtures::get('coupons'); $I = new ApiTester($scenario); $I->wantTo('Use a coupon in my cart'); /** * @var array $item1 * @var array $item2 * @var int $order * @var callable $gen_item */ require "_AddItems.php"; $calculate_discount = function ($discount, ...$items) { $mult = 1 - $discount; return array_reduce($items, function ($total, $item) use($mult) { return floatify($total + floatify($item['final_price'] * $mult)); }, 0); }; $I->amGoingTo('use an invalid coupon code'); $I->sendPOST('cart/coupon', ['code' => 'XXX']); $I->seeResponseCodeIs(HTTP_NOT_FOUND); $set_coupon = function ($coupon) use($I, $item1, $item2, $calculate_discount) { $I->sendPOST('cart/coupon', ['code' => $coupon['code']]); $I->seeCodeAndJson(HTTP_OK, $coupon); $I->sendGET('cart'); $I->seeResponseContainsJson(['total' => $calculate_discount($coupon['discount'], $item1, $item2)]); }; $I->amGoingTo('use a valid coupon code'); $set_coupon($coupons[0]); $I->amGoingTo('change the coupon'); $set_coupon($coupons[1]);
<?php $I = new ApiTester($scenario); $I->wantTo('Insert items and checkout'); /** * @var array $item1 * @var array $item2 * @var int $order */ require "_AddItems.php"; $I->amGoingTo('checkout'); $I->sendPATCH('cart'); $I->seeCodeAndJson(200, ['id' => $order, 'total' => floatify($item1['final_price'] + $item2['final_price'])]); $I->amGoingTo('verify the order was really ~closed~'); $I->sendGET('cart'); $I->assertEquals(sizeof(json_decode($I->grabResponse())->items), 0, 'verify the cart is now empty');
<?php $devs = \Codeception\Util\Fixtures::get('devs'); $gen_item = function () use($devs) { return ['item' => $devs[array_rand($devs)], 'price' => floatify(rand(1000, 99999) / 100), 'qty' => floatify(rand(10, 200) / 10)]; }; $add_item = function () use($gen_item, $I) { $item = $gen_item(); $I->sendPUT('cart', $item); $item += ['final_price' => floatify($item['price'] * $item['qty'])]; $I->seeCodeAndJson(201, $item); return $item; }; /* ********************************************************** */ $I->amGoingTo('add items to cart'); $item1 = $add_item(); $data = json_decode($I->grabResponse()); $order = $data->order_id; $item_id = $data->id; $I->amGoingTo('add another item to the same order'); $item2 = $add_item(); $I->assertEquals($order, json_decode($I->grabResponse())->order_id, 'The two items are in the same order');