From 0f1906dcc59110cde37298651329463c33079c5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Lup=C4=8D=C3=ADk?= Date: Sat, 18 Feb 2023 13:13:22 +0100 Subject: [PATCH] Add player's company history --- src/Models/Player.php | 22 +++- src/Models/PlayerCompanyHistory.php | 112 ++++++++++++++++++ .../Unit/Models/PlayerCompanyHistoryTest.php | 56 +++++++++ tests/Unit/Models/PlayerTest.php | 11 ++ .../fixtures/player.company.history.json | 7 ++ tests/Unit/Models/fixtures/player.json | 11 +- tests/Unit/Requests/fixtures/player.json | 11 +- 7 files changed, 227 insertions(+), 3 deletions(-) create mode 100644 src/Models/PlayerCompanyHistory.php create mode 100644 tests/Unit/Models/PlayerCompanyHistoryTest.php create mode 100644 tests/Unit/Models/fixtures/player.company.history.json diff --git a/src/Models/Player.php b/src/Models/Player.php index bfa9472..9f08962 100644 --- a/src/Models/Player.php +++ b/src/Models/Player.php @@ -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 */ @@ -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. * @@ -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); @@ -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. * diff --git a/src/Models/PlayerCompanyHistory.php b/src/Models/PlayerCompanyHistory.php new file mode 100644 index 0000000..c7a9b68 --- /dev/null +++ b/src/Models/PlayerCompanyHistory.php @@ -0,0 +1,112 @@ +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; + } +} diff --git a/tests/Unit/Models/PlayerCompanyHistoryTest.php b/tests/Unit/Models/PlayerCompanyHistoryTest.php new file mode 100644 index 0000000..ca69dd2 --- /dev/null +++ b/tests/Unit/Models/PlayerCompanyHistoryTest.php @@ -0,0 +1,56 @@ +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()); + } +} diff --git a/tests/Unit/Models/PlayerTest.php b/tests/Unit/Models/PlayerTest.php index f8cb7e3..eeed18c 100644 --- a/tests/Unit/Models/PlayerTest.php +++ b/tests/Unit/Models/PlayerTest.php @@ -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 { @@ -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'); diff --git a/tests/Unit/Models/fixtures/player.company.history.json b/tests/Unit/Models/fixtures/player.company.history.json new file mode 100644 index 0000000..2c250e7 --- /dev/null +++ b/tests/Unit/Models/fixtures/player.company.history.json @@ -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" +} diff --git a/tests/Unit/Models/fixtures/player.json b/tests/Unit/Models/fixtures/player.json index bdb8d35..e3072a5 100644 --- a/tests/Unit/Models/fixtures/player.json +++ b/tests/Unit/Models/fixtures/player.json @@ -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" + } + ] } diff --git a/tests/Unit/Requests/fixtures/player.json b/tests/Unit/Requests/fixtures/player.json index c02c89b..dd3f0c9 100644 --- a/tests/Unit/Requests/fixtures/player.json +++ b/tests/Unit/Requests/fixtures/player.json @@ -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" + } + ] } }