Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock unit tests #48

Merged
merged 3 commits into from
Feb 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,16 @@
],
"require": {
"php": "^7.4|^8.0",
"ext-json": "*",
"nesbot/carbon": "^2.16",
"guzzlehttp/guzzle": "^7.3",
"illuminate/collections": "^8.0|^9.0"
},
"require-dev": {
"phpunit/phpunit": "^9.3",
"squizlabs/php_codesniffer": "^3.5",
"phpfastcache/phpfastcache": "^8.0"
"phpfastcache/phpfastcache": "^8.0",
"mockery/mockery": "^1.5"
},
"autoload": {
"psr-4": {
Expand Down
83 changes: 59 additions & 24 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace TruckersMP\APIClient;

use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\ClientInterface;
use TruckersMP\APIClient\Requests\BanRequest;
use TruckersMP\APIClient\Requests\CompanyIndexRequest;
use TruckersMP\APIClient\Requests\CompanyRequest;
Expand All @@ -16,11 +18,25 @@
class Client
{
/**
* The configuration to use for Guzzle.
* The API domain including the scheme.
*
* @var array
* @var string
*/
protected static $config = [];
protected string $domain = 'https://api.truckersmp.com';

/**
* The currently used API version.
*
* @var int
*/
protected int $version = 2;

/**
* The instance of an HTTP client.
*
* @var ClientInterface
*/
protected ClientInterface $httpClient;

/**
* Create a new Client instance.
Expand All @@ -30,7 +46,36 @@ class Client
*/
public function __construct(array $config = [])
{
self::$config = $config;
$config = array_merge_recursive($config, [
'base_uri' => $this->domain . '/v' . $this->version . '/',
'headers' => [
'User-Agent' => 'TruckersMP API Client (https://github.com/TruckersMP/API-Client)',
'Content-Type' => 'application/json',
],
]);

$this->httpClient = new GuzzleClient($config);
}

/**
* Get the configured HTTP client.
*
* @return ClientInterface
*/
public function getHttpClient(): ClientInterface
{
return $this->httpClient;
}

/**
* Set the instance of the HTTP client.
*
* @param ClientInterface $httpClient
* @return void
*/
public function setHttpClient(ClientInterface $httpClient): void
{
$this->httpClient = $httpClient;
}

/**
Expand All @@ -43,7 +88,7 @@ public function __construct(array $config = [])
*/
public function player(int $id): PlayerRequest
{
return new PlayerRequest($id);
return new PlayerRequest($this, $id);
}

/**
Expand All @@ -56,7 +101,7 @@ public function player(int $id): PlayerRequest
*/
public function bans(int $id): BanRequest
{
return new BanRequest($id);
return new BanRequest($this, $id);
}

/**
Expand All @@ -68,7 +113,7 @@ public function bans(int $id): BanRequest
*/
public function servers(): ServerRequest
{
return new ServerRequest();
return new ServerRequest($this);
}

/**
Expand All @@ -80,7 +125,7 @@ public function servers(): ServerRequest
*/
public function gameTime(): GameTimeRequest
{
return new GameTimeRequest();
return new GameTimeRequest($this);
}

/**
Expand All @@ -92,7 +137,7 @@ public function gameTime(): GameTimeRequest
*/
public function companies(): CompanyIndexRequest
{
return new CompanyIndexRequest();
return new CompanyIndexRequest($this);
}

/**
Expand All @@ -105,7 +150,7 @@ public function companies(): CompanyIndexRequest
*/
public function company(string $key): CompanyRequest
{
return new CompanyRequest($key);
return new CompanyRequest($this, $key);
}

/**
Expand All @@ -117,7 +162,7 @@ public function company(string $key): CompanyRequest
*/
public function version(): VersionRequest
{
return new VersionRequest();
return new VersionRequest($this);
}

/**
Expand All @@ -129,7 +174,7 @@ public function version(): VersionRequest
*/
public function rules(): RuleRequest
{
return new RuleRequest();
return new RuleRequest($this);
}

/**
Expand All @@ -141,7 +186,7 @@ public function rules(): RuleRequest
*/
public function events(): EventIndexRequest
{
return new EventIndexRequest();
return new EventIndexRequest($this);
}

/**
Expand All @@ -154,16 +199,6 @@ public function events(): EventIndexRequest
*/
public function event(int $id): EventRequest
{
return new EventRequest($id);
}

/**
* Get the configuration to use for Guzzle.
*
* @return array
*/
public static function config(): array
{
return self::$config;
return new EventRequest($this, $id);
}
}
46 changes: 22 additions & 24 deletions src/Models/Ban.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,80 +3,78 @@
namespace TruckersMP\APIClient\Models;

use Carbon\Carbon;
use TruckersMP\APIClient\Client;

class Ban
class Ban extends Model
{
/**
* The time the ban will expire.
*
* @var Carbon
* @var Carbon|null
*/
protected $expiration;
protected ?Carbon $expiration;

/**
* The time the ban was issued.
*
* @var Carbon
*/
protected $timeAdded;
protected Carbon $timeAdded;

/**
* If the ban is still active.
*
* @var bool
*/
protected $active;
protected bool $active;

/**
* The reason for the ban.
*
* @var string
*/
protected $reason;
protected string $reason;

/**
* The name of the admin that banned the user.
*
* @var string
*/
protected $adminName;
protected string $adminName;

/**
* The TruckersMP ID for the admin that banned the user.
*
* @var int
*/
protected $adminId;
protected int $adminId;

/**
* Create a new Ban instance.
*
* @param Client $client
* @param array $ban
* @return void
*/
public function __construct(array $ban)
public function __construct(Client $client, array $ban)
{
// Expiration
if ($ban['expiration'] !== null) {
$this->expiration = new Carbon($ban['expiration'], 'UTC');
} else {
$this->expiration = null;
}
parent::__construct($client, $ban);

$expiration = $this->getValue('expiration');
$this->expiration = $expiration ? new Carbon($expiration, 'UTC') : null;

// Time Added
$this->timeAdded = new Carbon($ban['timeAdded'], 'UTC');
$this->timeAdded = new Carbon($this->getValue('timeAdded'), 'UTC');
$this->active = $this->getValue('active', false);

// Active
$this->active = boolval($ban['active']);
if (!is_null($this->expiration) && $this->active) {
if (!$this->expiration->greaterThan(Carbon::now('UTC'))) {
if ($this->expiration !== null && $this->active) {
if ($this->expiration->lessThan(Carbon::now('UTC'))) {
$this->active = false;
}
}

$this->reason = $ban['reason'];
$this->adminName = $ban['adminName'];
$this->adminId = intval($ban['adminID']);
$this->reason = $this->getValue('reason');
$this->adminName = $this->getValue('adminName');
$this->adminId = $this->getValue('adminID');
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Models/Checksum.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ class Checksum
*
* @var string
*/
protected $dll;
protected string $dll;

/**
* The checksum ADB.
*
* @var string
*/
protected $adb;
protected string $adb;

/**
* Create a new Checksum instance.
Expand Down
Loading