File: //home/aliazzsr/api.crm.vqode.com/tests/_support/ApiTester.php
<?php
/**
* Inherited Methods
* @method void wantToTest($text)
* @method void wantTo($text)
* @method void execute($callable)
* @method void expectTo($prediction)
* @method void expect($prediction)
* @method void amGoingTo($argumentation)
* @method void am($role)
* @method void lookForwardTo($achieveValue)
* @method void comment($description)
* @method \Codeception\Lib\Friend haveFriend($name, $actorClass = NULL)
*
* @SuppressWarnings(PHPMD)
*/
class ApiTester extends \Codeception\Actor
{
use _generated\ApiTesterActions;
public $authToken;
protected $_faker;
const ADMIN_USER = '[email protected]';
const BLOCKED_USER = '[email protected]';
const STAFF_USER = '[email protected]';
const PASSWORD_DEFAULT = 'Cvthnjrhsk2010';
public static $defaultCredentials = [
'username' => self::ADMIN_USER,
'password' => self::PASSWORD_DEFAULT,
];
public function __construct(\Codeception\Scenario $scenario)
{
parent::__construct($scenario);
$this->_faker = \Faker\Factory::create();
}
/**
* @return \Faker\Generator
*/
public function fake()
{
return $this->_faker;
}
/**
* Logs user in and returns authToken object
* @param $params
* @return mixed
*/
public function login($params = null)
{
$I = $this;
$I->updateInDatabase('user', ['eula_accepted_at' => '2000-01-01 01:00:00']);
$params = $params ?: self::$defaultCredentials;
$I->sendPOST('auth/login', $params);
$I->seeResponseCodeIs(\Codeception\Util\HttpCode::OK);
$this->authToken = json_decode($I->grabResponse());
$I->haveHttpHeader('Authorization', 'Bearer ' . $this->authToken->token);
return $this->authToken;
}
public function getRandomProjectJSON()
{
$faker = $this->_faker;
return [
'name' => $faker->company,
'revised' => $faker->date('d/m/Y'),
'latest_revision' => $faker->text(100),
'location' => 'Unknown Location',
'street' => $faker->streetAddress,
'building_no' => $faker->buildingNumber,
'city' => $faker->city,
'country' => $faker->country,
'postcode' => $faker->postcode,
'sector_id' => $this->getRandomId('sector'),
'stage_id' => $this->getRandomId('stage'),
'riba_id' => $this->getRandomId('riba'),
'number' => $faker->buildingNumber,
'responsible_office' => $faker->address,
'office_postcode' => $faker->postcode,
'value' => round($faker->randomFloat(9, 0), 2),
'edv' => round($faker->randomFloat(9, 0), 2),
'order_at' => $faker->date('d/m/Y'),
'next_action_at' => $faker->date('d/m/Y'),
'efv' => round($faker->randomFloat(9, 0), 2),
'notes' => $faker->text,
'owner_id' => $this->getRandomId('user'),
'reference' => $faker->safeColorName,
'longitude' => $faker->longitude,
'latitude' => $faker->latitude,
];
}
public function getRandomId($model, $idName = 'id')
{
$ids = $this->grabColumnFromDatabase($model, $idName);
$ids = array_map('intval', $ids);
return $this->_faker->randomElement($ids);
}
public function seeProjectsStatisticsArray()
{
$I = $this;
$I->seeResponseJsonMatchesJsonPath('$[0].group_id');
$I->seeResponseJsonMatchesJsonPath('$[0].group_name');
$I->seeResponseJsonMatchesJsonPath('$[0].count');
$I->seeResponseJsonMatchesJsonPath('$[0].count_percentage');
$I->seeResponseJsonMatchesJsonPath('$[0].value');
$I->seeResponseJsonMatchesJsonPath('$[0].value_percentage');
$I->seeResponseJsonMatchesJsonPath('$[0].edv');
$I->seeResponseJsonMatchesJsonPath('$[0].edv_percentage');
$I->seeResponseJsonMatchesJsonPath('$[0].efv');
$I->seeResponseJsonMatchesJsonPath('$[0].efv_percentage');
}
public function seeUsersStatisticsArray()
{
$I = $this;
$I->seeResponseJsonMatchesJsonPath('$[0].id');
$I->seeResponseJsonMatchesJsonPath('$[0].name');
$I->seeResponseJsonMatchesJsonPath('$[0].times');
$I->seeResponseJsonMatchesJsonPath('$[0].amount');
}
public function fakeRibaJson()
{
return [
'name' => $this->fake()->text(15),
];
}
public function fakeSectorJson()
{
return [
'name' => $this->fake()->text(15),
];
}
public function fakeStageJson()
{
return [
'name' => $this->fake()->text(15),
'description' => $this->fake()->text(80),
'icon_color' => str_replace('#', '', strtoupper($this->fake()->hexColor)),
'icon_percentage' => $this->fake()->numberBetween(0, 100),
'active' => $this->fake()->boolean,
];
}
}