Skip to content
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
19 changes: 19 additions & 0 deletions src/Models/Player.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use TruckersMP\APIClient\Requests\Company\MemberRequest;
use TruckersMP\APIClient\Requests\Company\RoleRequest;
use TruckersMP\APIClient\Requests\CompanyRequest;
use TruckersMP\APIClient\Requests\EventUserRequest;

class Player extends Model
{
Expand Down Expand Up @@ -472,6 +473,24 @@ public function getBans(): Collection
return (new BanRequest($this->client, $this->id))->get();
}

/**
* Get events created by the player.
*
* @return Collection
*
* @throws ApiErrorException
* @throws ClientExceptionInterface
*/
public function getEvents(): Collection
{
$request = new EventUserRequest(
$this->client,
$this->id,
);

return $request->get();
}

/**
* Get the player's company.
*
Expand Down
57 changes: 57 additions & 0 deletions src/Requests/EventUserRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace TruckersMP\APIClient\Requests;

use Illuminate\Support\Collection;
use Psr\Http\Client\ClientExceptionInterface;
use TruckersMP\APIClient\Client;
use TruckersMP\APIClient\Exceptions\ApiErrorException;
use TruckersMP\APIClient\Models\Event;

class EventUserRequest extends Request
{
/**
* The ID of the requested user.
*
* @var int
*/
protected int $userId;

/**
* Create a new EventUserRequest instance.
*
* @param Client $client
* @param int $userId
* @return void
*/
public function __construct(Client $client, int $userId)
{
parent::__construct($client);

$this->userId = $userId;
}

/**
* Get the endpoint of the request.
*
* @return string
*/
public function getEndpoint(): string
{
return 'events/user/' . $this->userId;
}

/**
* Get the data for the request.
*
* @return Collection
*
* @throws ClientExceptionInterface
* @throws ApiErrorException
*/
public function get(): Collection
{
return (new Collection($this->send()['response']))
->map(fn (array $event) => new Event($this->client, $event));
}
}
13 changes: 13 additions & 0 deletions src/Requests/PlayerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,17 @@ public function bans(): BanRequest
$this->id
);
}

/**
* Get all events created by the player.
*
* @return EventUserRequest
*/
public function events(): EventUserRequest
{
return new EventUserRequest(
$this->client,
$this->id,
);
}
}
13 changes: 13 additions & 0 deletions tests/Unit/Models/PlayerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use TruckersMP\APIClient\Models\Ban;
use TruckersMP\APIClient\Models\Company;
use TruckersMP\APIClient\Models\CompanyMember;
use TruckersMP\APIClient\Models\Event;
use TruckersMP\APIClient\Models\Patreon;
use TruckersMP\APIClient\Models\Player;

Expand Down Expand Up @@ -124,6 +125,18 @@ public function testItCanGetBans()
$this->assertInstanceOf(Ban::class, $bans->first());
}

public function testItCanGetEvents()
{
$this->mockRequest('event.user.json', 'events/user/1287455');

$events = $this->player->getEvents();

$this->assertInstanceOf(Collection::class, $events);
$this->assertNotEmpty($events);

$this->assertInstanceOf(Event::class, $events->first());
}

public function testItCanGetACompany()
{
$this->mockRequest('company.json', 'vtc/1');
Expand Down
25 changes: 25 additions & 0 deletions tests/Unit/Requests/EventUserRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Tests\Unit\Requests;

use Illuminate\Support\Collection;
use Tests\TestCase;
use Tests\Unit\MockAPIRequests;
use TruckersMP\APIClient\Models\Event;

class EventUserRequestTest extends TestCase
{
use MockAPIRequests;

public function testItCanGetUserEvents()
{
$this->mockRequest('event.user.json', 'events/user/1287455');

$events = $this->client->player(1287455)->events()->get();

$this->assertInstanceOf(Collection::class, $events);
$this->assertCount(1, $events);

$this->assertInstanceOf(Event::class, $events->first());
}
}
53 changes: 53 additions & 0 deletions tests/Unit/Requests/fixtures/event.user.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"error": false,
"response": [
{
"id": 777,
"event_type": {
"key": "convoy",
"name": "Convoy"
},
"name": "February Convoy",
"slug": "777-february-convoy",
"game": "ETS2",
"server": {
"id": 66,
"name": "Event Server"
},
"language": "English",
"departure": {
"location": "Hotel",
"city": "Groningen"
},
"arrive": {
"location": "City",
"city": "Verona"
},
"start_at": "2023-02-19 18:30:00",
"banner": "https://static.truckersmp.com/images/event/cover/cover.png",
"map": "https://static.truckersmp.com/images/event/map/map.png",
"description": "Description",
"rule": "Rules",
"voice_link": "https://discord.gg/truckersmp",
"external_link": "https://truckersmp.com",
"featured": "Featured",
"vtc": {
"id": 0,
"name": null
},
"user": {
"id": 1287455,
"username": "Name"
},
"attendances": {
"confirmed": 228,
"unsure": 53,
"vtcs": 32
},
"dlcs": [],
"url": "/events/777-february-convoy",
"created_at": "2023-01-22 18:48:47",
"updated_at": "2023-01-24 00:57:01"
}
]
}