From 46c0215add03876a9dedcc53c33077d1d09e9183 Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Thu, 11 Apr 2019 01:26:26 +0300 Subject: [PATCH] Implemented Change Skin endpoint --- CHANGELOG.md | 1 + src/Api.php | 23 +++++++++++++++++++++++ tests/ApiTest.php | 22 ++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c76d22..ec4d2da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [API Status](https://wiki.vg/Mojang_API#API_Status) endpoint. - [UUID to Name history](https://wiki.vg/Mojang_API#UUID_-.3E_Name_history) endpoint. - [Playernames -> UUIDs](https://wiki.vg/Mojang_API#Playernames_-.3E_UUIDs) endpoint. +- [Change Skin](https://wiki.vg/Mojang_API#Change_Skin) endpoint. ### Changed - The constructor no longer has arguments. diff --git a/src/Api.php b/src/Api.php index 2a364ab..228b533 100644 --- a/src/Api.php +++ b/src/Api.php @@ -177,6 +177,29 @@ class Api { return $result; } + /** + * @param string $accessToken + * @param string $accountUuid + * @param string $skinUrl + * @param bool $isSlim + * + * @throws \Ely\Mojang\Exception\MojangApiException + * @throws GuzzleException + * + * @url https://wiki.vg/Mojang_API#Change_Skin + */ + public function changeSkin(string $accessToken, string $accountUuid, string $skinUrl, bool $isSlim): void { + $this->getClient()->request('POST', "https://api.mojang.com/user/profile/{$accountUuid}/skin", [ + 'form_params' => [ + 'model' => $isSlim ? 'slim' : '', + 'url' => $skinUrl, + ], + 'headers' => [ + 'Authorization' => 'Bearer ' . $accessToken, + ], + ]); + } + /** * @param string $accessToken * @param string $accountUuid diff --git a/tests/ApiTest.php b/tests/ApiTest.php index 3c1a1dc..cf00956 100644 --- a/tests/ApiTest.php +++ b/tests/ApiTest.php @@ -251,11 +251,32 @@ class ApiTest extends TestCase { $this->assertStringStartsWith('https://sessionserver.mojang.com/session/minecraft/profile/86f6e3695b764412a29820cac1d4d0d6', (string)$request2->getUri()); } + public function testChangeSkinNotSlim() { + $this->mockHandler->append(new Response(200)); + $this->api->changeSkin('mocked access token', '86f6e3695b764412a29820cac1d4d0d6', 'http://localhost/skin.png', false); + /** @var \Psr\Http\Message\RequestInterface $request */ + $request = $this->history[0]['request']; + $this->assertSame('https://api.mojang.com/user/profile/86f6e3695b764412a29820cac1d4d0d6/skin', (string)$request->getUri()); + $this->assertSame('Bearer mocked access token', $request->getHeaderLine('Authorization')); + $body = urldecode($request->getBody()->getContents()); + $this->assertStringContainsString('url=http://localhost/skin.png', $body); + $this->assertStringNotContainsString('model=slim', $body); + } + + public function testChangeSkinSlim() { + $this->mockHandler->append(new Response(200)); + $this->api->changeSkin('mocked access token', '86f6e3695b764412a29820cac1d4d0d6', 'http://localhost/skin.png', true); + /** @var \Psr\Http\Message\RequestInterface $request */ + $request = $this->history[0]['request']; + $this->assertStringContainsString('model=slim', $request->getBody()->getContents()); + } + public function testUploadSkinNotSlim() { $this->mockHandler->append(new Response(200)); $this->api->uploadSkin('mocked access token', '86f6e3695b764412a29820cac1d4d0d6', 'skin contents', false); /** @var \Psr\Http\Message\RequestInterface $request */ $request = $this->history[0]['request']; + $this->assertSame('https://api.mojang.com/user/profile/86f6e3695b764412a29820cac1d4d0d6/skin', (string)$request->getUri()); $this->assertSame('Bearer mocked access token', $request->getHeaderLine('Authorization')); $this->assertStringNotContainsString('slim', $request->getBody()->getContents()); } @@ -265,6 +286,7 @@ class ApiTest extends TestCase { $this->api->uploadSkin('mocked access token', '86f6e3695b764412a29820cac1d4d0d6', 'skin contents', true); /** @var \Psr\Http\Message\RequestInterface $request */ $request = $this->history[0]['request']; + $this->assertSame('https://api.mojang.com/user/profile/86f6e3695b764412a29820cac1d4d0d6/skin', (string)$request->getUri()); $this->assertStringContainsString('slim', $request->getBody()->getContents()); }