Skip to content

Commit a98758d

Browse files
authored
Mock unit tests (#48)
Major changes: * Mock API and model tests instead of sending API requests * Create a base class for entity models * Add types to model attributes (supporting PHP 7.4) Other minor changes which were done together with the above: * Move the HTTP client to the API client * Move the API configuration to the API client * Rename the Patreon get method in the Player model * Deprecate the company role get method in the Player model * Fix the start_at type in the Event model * Fix parameter types for the company key in requests * Fix the ProMods name in the Server model * Do not use confirmed and unsure counts in the EventAttendance model
1 parent fb8725e commit a98758d

File tree

130 files changed

+3497
-2985
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

130 files changed

+3497
-2985
lines changed

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,16 @@
4949
],
5050
"require": {
5151
"php": "^7.4|^8.0",
52+
"ext-json": "*",
5253
"nesbot/carbon": "^2.16",
5354
"guzzlehttp/guzzle": "^7.3",
5455
"illuminate/collections": "^8.0|^9.0"
5556
},
5657
"require-dev": {
5758
"phpunit/phpunit": "^9.3",
5859
"squizlabs/php_codesniffer": "^3.5",
59-
"phpfastcache/phpfastcache": "^8.0"
60+
"phpfastcache/phpfastcache": "^8.0",
61+
"mockery/mockery": "^1.5"
6062
},
6163
"autoload": {
6264
"psr-4": {

src/Client.php

Lines changed: 59 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace TruckersMP\APIClient;
44

5+
use GuzzleHttp\Client as GuzzleClient;
6+
use GuzzleHttp\ClientInterface;
57
use TruckersMP\APIClient\Requests\BanRequest;
68
use TruckersMP\APIClient\Requests\CompanyIndexRequest;
79
use TruckersMP\APIClient\Requests\CompanyRequest;
@@ -16,11 +18,25 @@
1618
class Client
1719
{
1820
/**
19-
* The configuration to use for Guzzle.
21+
* The API domain including the scheme.
2022
*
21-
* @var array
23+
* @var string
2224
*/
23-
protected static $config = [];
25+
protected string $domain = 'https://api.truckersmp.com';
26+
27+
/**
28+
* The currently used API version.
29+
*
30+
* @var int
31+
*/
32+
protected int $version = 2;
33+
34+
/**
35+
* The instance of an HTTP client.
36+
*
37+
* @var ClientInterface
38+
*/
39+
protected ClientInterface $httpClient;
2440

2541
/**
2642
* Create a new Client instance.
@@ -30,7 +46,36 @@ class Client
3046
*/
3147
public function __construct(array $config = [])
3248
{
33-
self::$config = $config;
49+
$config = array_merge_recursive($config, [
50+
'base_uri' => $this->domain . '/v' . $this->version . '/',
51+
'headers' => [
52+
'User-Agent' => 'TruckersMP API Client (https://github.com/TruckersMP/API-Client)',
53+
'Content-Type' => 'application/json',
54+
],
55+
]);
56+
57+
$this->httpClient = new GuzzleClient($config);
58+
}
59+
60+
/**
61+
* Get the configured HTTP client.
62+
*
63+
* @return ClientInterface
64+
*/
65+
public function getHttpClient(): ClientInterface
66+
{
67+
return $this->httpClient;
68+
}
69+
70+
/**
71+
* Set the instance of the HTTP client.
72+
*
73+
* @param ClientInterface $httpClient
74+
* @return void
75+
*/
76+
public function setHttpClient(ClientInterface $httpClient): void
77+
{
78+
$this->httpClient = $httpClient;
3479
}
3580

3681
/**
@@ -43,7 +88,7 @@ public function __construct(array $config = [])
4388
*/
4489
public function player(int $id): PlayerRequest
4590
{
46-
return new PlayerRequest($id);
91+
return new PlayerRequest($this, $id);
4792
}
4893

4994
/**
@@ -56,7 +101,7 @@ public function player(int $id): PlayerRequest
56101
*/
57102
public function bans(int $id): BanRequest
58103
{
59-
return new BanRequest($id);
104+
return new BanRequest($this, $id);
60105
}
61106

62107
/**
@@ -68,7 +113,7 @@ public function bans(int $id): BanRequest
68113
*/
69114
public function servers(): ServerRequest
70115
{
71-
return new ServerRequest();
116+
return new ServerRequest($this);
72117
}
73118

74119
/**
@@ -80,7 +125,7 @@ public function servers(): ServerRequest
80125
*/
81126
public function gameTime(): GameTimeRequest
82127
{
83-
return new GameTimeRequest();
128+
return new GameTimeRequest($this);
84129
}
85130

86131
/**
@@ -92,7 +137,7 @@ public function gameTime(): GameTimeRequest
92137
*/
93138
public function companies(): CompanyIndexRequest
94139
{
95-
return new CompanyIndexRequest();
140+
return new CompanyIndexRequest($this);
96141
}
97142

98143
/**
@@ -105,7 +150,7 @@ public function companies(): CompanyIndexRequest
105150
*/
106151
public function company(string $key): CompanyRequest
107152
{
108-
return new CompanyRequest($key);
153+
return new CompanyRequest($this, $key);
109154
}
110155

111156
/**
@@ -117,7 +162,7 @@ public function company(string $key): CompanyRequest
117162
*/
118163
public function version(): VersionRequest
119164
{
120-
return new VersionRequest();
165+
return new VersionRequest($this);
121166
}
122167

123168
/**
@@ -129,7 +174,7 @@ public function version(): VersionRequest
129174
*/
130175
public function rules(): RuleRequest
131176
{
132-
return new RuleRequest();
177+
return new RuleRequest($this);
133178
}
134179

135180
/**
@@ -141,7 +186,7 @@ public function rules(): RuleRequest
141186
*/
142187
public function events(): EventIndexRequest
143188
{
144-
return new EventIndexRequest();
189+
return new EventIndexRequest($this);
145190
}
146191

147192
/**
@@ -154,16 +199,6 @@ public function events(): EventIndexRequest
154199
*/
155200
public function event(int $id): EventRequest
156201
{
157-
return new EventRequest($id);
158-
}
159-
160-
/**
161-
* Get the configuration to use for Guzzle.
162-
*
163-
* @return array
164-
*/
165-
public static function config(): array
166-
{
167-
return self::$config;
202+
return new EventRequest($this, $id);
168203
}
169204
}

src/Models/Ban.php

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,80 +3,78 @@
33
namespace TruckersMP\APIClient\Models;
44

55
use Carbon\Carbon;
6+
use TruckersMP\APIClient\Client;
67

7-
class Ban
8+
class Ban extends Model
89
{
910
/**
1011
* The time the ban will expire.
1112
*
12-
* @var Carbon
13+
* @var Carbon|null
1314
*/
14-
protected $expiration;
15+
protected ?Carbon $expiration;
1516

1617
/**
1718
* The time the ban was issued.
1819
*
1920
* @var Carbon
2021
*/
21-
protected $timeAdded;
22+
protected Carbon $timeAdded;
2223

2324
/**
2425
* If the ban is still active.
2526
*
2627
* @var bool
2728
*/
28-
protected $active;
29+
protected bool $active;
2930

3031
/**
3132
* The reason for the ban.
3233
*
3334
* @var string
3435
*/
35-
protected $reason;
36+
protected string $reason;
3637

3738
/**
3839
* The name of the admin that banned the user.
3940
*
4041
* @var string
4142
*/
42-
protected $adminName;
43+
protected string $adminName;
4344

4445
/**
4546
* The TruckersMP ID for the admin that banned the user.
4647
*
4748
* @var int
4849
*/
49-
protected $adminId;
50+
protected int $adminId;
5051

5152
/**
5253
* Create a new Ban instance.
5354
*
55+
* @param Client $client
5456
* @param array $ban
5557
* @return void
5658
*/
57-
public function __construct(array $ban)
59+
public function __construct(Client $client, array $ban)
5860
{
59-
// Expiration
60-
if ($ban['expiration'] !== null) {
61-
$this->expiration = new Carbon($ban['expiration'], 'UTC');
62-
} else {
63-
$this->expiration = null;
64-
}
61+
parent::__construct($client, $ban);
62+
63+
$expiration = $this->getValue('expiration');
64+
$this->expiration = $expiration ? new Carbon($expiration, 'UTC') : null;
6565

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

69-
// Active
70-
$this->active = boolval($ban['active']);
71-
if (!is_null($this->expiration) && $this->active) {
72-
if (!$this->expiration->greaterThan(Carbon::now('UTC'))) {
69+
if ($this->expiration !== null && $this->active) {
70+
if ($this->expiration->lessThan(Carbon::now('UTC'))) {
7371
$this->active = false;
7472
}
7573
}
7674

77-
$this->reason = $ban['reason'];
78-
$this->adminName = $ban['adminName'];
79-
$this->adminId = intval($ban['adminID']);
75+
$this->reason = $this->getValue('reason');
76+
$this->adminName = $this->getValue('adminName');
77+
$this->adminId = $this->getValue('adminID');
8078
}
8179

8280
/**

src/Models/Checksum.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ class Checksum
99
*
1010
* @var string
1111
*/
12-
protected $dll;
12+
protected string $dll;
1313

1414
/**
1515
* The checksum ADB.
1616
*
1717
* @var string
1818
*/
19-
protected $adb;
19+
protected string $adb;
2020

2121
/**
2222
* Create a new Checksum instance.

0 commit comments

Comments
 (0)