Skip to content

Instantly share code, notes, and snippets.

@remoharsono
Last active November 27, 2019 04:12
Show Gist options
  • Save remoharsono/ba75c90da0f2d04eb624578e2769419e to your computer and use it in GitHub Desktop.
Save remoharsono/ba75c90da0f2d04eb624578e2769419e to your computer and use it in GitHub Desktop.
Reusing token in Codeception
<?php
namespace Helper;
class Api extends \Codeception\Module
{
public $credentials = array(
'username' => 'remo',
'password' => '12345678'
);
private $jwt;
public function getToken() {
return $this->jwt;
}
public function _before(\Codeception\TestInterface $test) {
$I = $this->getModule('REST');
$I->haveHttpHeader('Content-Type', 'application/json');
$I->sendPOST("/auth/login", $this->arrCredentials);
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
$response = $I->grabResponse();
$json = json_decode((string)$jesponse);
if ($json->result == 'OK') {
$this->jwt = $json->jwt;
} else {
// ....
}
}
}
<?php
class ApiCest
{
private $token;
function createMeetingTest(ApiTester $I) {
$data = array(
"meeting_title" => "Meeting title",
"meeting_date" => "2019-11-21 10:00:00"
);
$this->token = $I->getToken();
$I->wantToTest('Create a meeting');
$I->amBearerAuthenticated($this->token);
$I->sendPOST('/meetings', $data);
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
}
}
{
"result": "OK",
"jwt": "blablablabla",
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment