<?php

$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)]);
<?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

use Codeception\Util\Fixtures;
/** @var Faker\Generator $faker */
$faker = Fixtures::get('faker');
$devs = Fixtures::get('devs');
$I = new ApiTester($scenario);
$I->wantTo('get the user price based on the GitHub API');
$I->amGoingTo('Get a non-existent user');
$I->sendGET('dev/' . $faker->lexify('??????????'));
$I->seeResponseCodeIs(404);
$I->amGoingTo('Get an existent user');
$I->sendGET('dev/' . $devs[array_rand($devs)]);
$I->seeResponseCodeIs(200);
$dev = json_decode($I->grabResponse());
$I->assertTrue(is_numeric($dev->rate), 'rate is numeric');
$I->assertTrue(is_string($dev->username) && strlen($dev->username) != 0, 'username is present and is string');
Exemple #4
0
<?php

$I = new ApiTester($scenario);
$I->wantToTest('Users');
$I->amGoingTo('modify user data (not auth)');
$I->sendPUT("/users/0");
$I->seeResponseCodeIs(401);
$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]);
$I->amGoingTo('add a new item to see the price discount');
$item3 = $gen_item();
$I->sendPUT('cart', $item3);
<?php

use Codeception\Util\Fixtures;
/** @var Faker\Generator $faker */
$faker = Fixtures::get('faker');
$orgs = Fixtures::get('orgs');
$sm_orgs = Fixtures::get('small_orgs');
$I = new ApiTester($scenario);
$I->wantTo('list of developers of an organization');
$I->amGoingTo('Get a non-existent organization');
$I->sendGET('dev/organization/' . $faker->lexify('??????????'));
$I->seeResponseCodeIs(404);
$I->amGoingTo('Get an existent organization on different information levels');
$get_org = function ($level, $orgs) use($I) {
    $I->amGoingTo("Request Org {$level} information");
    $query = $level ? '?level=' . $level : '';
    $I->sendGET('dev/organization/' . $orgs[array_rand($orgs)] . $query);
    $I->seeResponseCodeIs(200);
    $org = json_decode($I->grabResponse(), true);
    $I->assertTrue(is_numeric($org['size']), 'has total of members');
    $I->assertTrue(is_array($org['members']), 'has list of members');
    $I->assertGreaterThanOrEqual(1, $org['size'], 'organization has at least one member');
    $I->assertEquals(sizeof($org['members']), $org['size'], 'total is correctly calculated');
    $I->assertTrue(is_string($org['members'][0]['username']), 'first member has "username"');
    return $org;
};
$org = $get_org('basic', $orgs);
$I->assertEmpty($org['members'][0]['name']);
$I->assertEmpty($org['members'][0]['repos']);
$I->assertEmpty($org['members'][0]['rate']);
$org = $get_org(null, $orgs);