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

Add the player's company history #51

Merged
merged 1 commit into from
Feb 25, 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
22 changes: 21 additions & 1 deletion src/Models/Player.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class Player extends Model
protected bool $isInCompany;

/**
* The player's company member id.
* The player's company member ID.
*
* @var int
*/
Expand All @@ -175,6 +175,13 @@ class Player extends Model
*/
protected ?string $discordSnowflake;

/**
* The player's history of company memberships sorted from the newest.
*
* @var Collection
*/
protected Collection $companyHistory;

/**
* Create a new Player instance.
*
Expand Down Expand Up @@ -205,6 +212,9 @@ public function __construct(Client $client, array $player)

$this->patreon = new Patreon($client, $this->getValue('patreon', []));

$history = new Collection($this->getValue('vtcHistory', []));
$this->companyHistory = $history->map(fn (array $entry) => new PlayerCompanyHistory($client, $entry));

$this->isStaff = $this->getValue('permissions.isStaff', false);
$this->isUpperStaff = $this->getValue('permissions.isUpperStaff', false);
$this->inGameAdmin = $this->getValue('permissions.isGameAdmin', false);
Expand Down Expand Up @@ -449,6 +459,16 @@ public function getCompanyMemberId(): int
return $this->companyMemberId;
}

/**
* Get the player's history of company memberships.
*
* @return Collection
*/
public function getCompanyHistory(): Collection
{
return $this->companyHistory;
}

/**
* Get the player's Discord Snowflake.
*
Expand Down
112 changes: 112 additions & 0 deletions src/Models/PlayerCompanyHistory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace TruckersMP\APIClient\Models;

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

class PlayerCompanyHistory extends Model
{
/**
* The ID of the company.
*
* @var int
*/
protected int $id;

/**
* The name of the company.
*
* @var string
*/
protected string $name;

/**
* If the company is verified.
*
* @var bool
*/
protected bool $verified;

/**
* The date and time the member joined the company (UTC).
*
* @var Carbon
*/
protected Carbon $joinDate;

/**
* The date and time the member left the company (UTC).
*
* @var Carbon
*/
protected Carbon $leftDate;

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

$this->id = $this->getValue('id');
$this->name = $this->getValue('name');
$this->verified = $this->getValue('verified', false);
$this->joinDate = new Carbon($this->getValue('joinDate'), 'UTC');
$this->leftDate = new Carbon($this->getValue('leftDate'), 'UTC');
}

/**
* Get the company's ID.
*
* @return int
*/
public function getId(): int
{
return $this->id;
}

/**
* Get the company's name.
*
* @return string
*/
public function getName(): string
{
return $this->name;
}

/**
* Check if the company is verified.
*
* @return bool
*/
public function isVerified(): bool
{
return $this->verified;
}

/**
* Get the date that the member joined the company.
*
* @return Carbon
*/
public function getJoinDate(): Carbon
{
return $this->joinDate;
}

/**
* Get the date that the member left the company.
*
* @return Carbon
*/
public function getLeftDate(): Carbon
{
return $this->leftDate;
}
}
56 changes: 56 additions & 0 deletions tests/Unit/Models/PlayerCompanyHistoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Tests\Unit\Models;

use Tests\TestCase;
use Tests\Unit\MockModelData;
use TruckersMP\APIClient\Models\PlayerCompanyHistory;

class PlayerCompanyHistoryTest extends TestCase
{
use MockModelData;

/**
* A PlayerCompanyHistory model instance filled with mocked data.
*
* @var PlayerCompanyHistory
*/
private PlayerCompanyHistory $companyHistory;

/**
* This method is called before each test.
*
* @return void
*/
protected function setUp(): void
{
$data = $this->getFixtureData('player.company.history.json');

$this->companyHistory = new PlayerCompanyHistory($this->client, $data);
}

public function testItHasAnId()
{
$this->assertSame(2, $this->companyHistory->getId());
}

public function testItHasAName()
{
$this->assertSame('TruckersMP Team', $this->companyHistory->getName());
}

public function testItIsNotVerified()
{
$this->assertFalse($this->companyHistory->isVerified());
}

public function testItHasAJoinDate()
{
$this->assertDate('2018-04-25 20:24:03', $this->companyHistory->getJoinDate());
}

public function testItHasALeftDate()
{
$this->assertDate('2019-07-13 15:39:58', $this->companyHistory->getLeftDate());
}
}
11 changes: 11 additions & 0 deletions tests/Unit/Models/PlayerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use TruckersMP\APIClient\Models\CompanyMember;
use TruckersMP\APIClient\Models\Patreon;
use TruckersMP\APIClient\Models\Player;
use TruckersMP\APIClient\Models\PlayerCompanyHistory;

class PlayerTest extends TestCase
{
Expand Down Expand Up @@ -112,6 +113,16 @@ public function testItHasACompany()
$this->assertSame(1, $this->player->getCompanyMemberId());
}

public function testItHasACompanyHistory()
{
$history = $this->player->getCompanyHistory();

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

$this->assertInstanceOf(PlayerCompanyHistory::class, $history->first());
}

public function testItCanGetBans()
{
$this->mockRequest('ban.json', 'bans/1287455');
Expand Down
7 changes: 7 additions & 0 deletions tests/Unit/Models/fixtures/player.company.history.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"id": 2,
"name": "TruckersMP Team",
"verified": false,
"joinDate": "2018-04-25 20:24:03",
"leftDate": "2019-07-13 15:39:58"
}
11 changes: 10 additions & 1 deletion tests/Unit/Models/fixtures/player.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,14 @@
"tag": "TMP-DEV",
"inVTC": true,
"memberID": 1
}
},
"vtcHistory": [
{
"id": 2,
"name": "TruckersMP Team",
"verified": false,
"joinDate": "2018-04-25 20:24:03",
"leftDate": "2019-07-13 15:39:58"
}
]
}
11 changes: 10 additions & 1 deletion tests/Unit/Requests/fixtures/player.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@
"tag": "TMP-DEV",
"inVTC": true,
"memberID": 1
}
},
"vtcHistory": [
{
"id": 2,
"name": "TruckersMP Team",
"verified": false,
"joinDate": "2018-04-25 20:24:03",
"leftDate": "2019-07-13 15:39:58"
}
]
}
}