diff --git a/common/components/Mojang/Api.php b/common/components/Mojang/Api.php deleted file mode 100644 index 37a65a1..0000000 --- a/common/components/Mojang/Api.php +++ /dev/null @@ -1,56 +0,0 @@ -getClient()->get($this->buildUsernameToUUIDRoute($username), $query); - if ($response->getStatusCode() === 204) { - throw new NoContentException('Username not found'); - } - - if ($response->getStatusCode() !== 200) { - throw new MojangApiException('Unexpected request result'); - } - - $data = json_decode($response->getBody(), true); - $responseObj = new UsernameToUUIDResponse(); - $responseObj->id = $data['id']; - $responseObj->name = $data['name']; - $responseObj->legacy = isset($data['legacy']); - $responseObj->demo = isset($data['demo']); - - return $responseObj; - } - - /** - * @return \GuzzleHttp\Client - */ - protected function getClient() { - return Yii::$app->guzzle; - } - - protected function buildUsernameToUUIDRoute($username) { - return 'https://api.mojang.com/users/profiles/minecraft/' . $username; - } - -} diff --git a/common/components/Mojang/exceptions/MojangApiException.php b/common/components/Mojang/exceptions/MojangApiException.php deleted file mode 100644 index 954f76d..0000000 --- a/common/components/Mojang/exceptions/MojangApiException.php +++ /dev/null @@ -1,8 +0,0 @@ - [ 'definitions' => [ GuzzleHttp\ClientInterface::class => GuzzleHttp\Client::class, + Ely\Mojang\Api::class => Ely\Mojang\Api::class, ], ], 'components' => [ diff --git a/common/tasks/PullMojangUsername.php b/common/tasks/PullMojangUsername.php index 169ddfb..269e76b 100644 --- a/common/tasks/PullMojangUsername.php +++ b/common/tasks/PullMojangUsername.php @@ -4,12 +4,12 @@ declare(strict_types=1); namespace common\tasks; use api\exceptions\ThisShouldNotHappenException; -use common\components\Mojang\Api as MojangApi; -use common\components\Mojang\exceptions\MojangApiException; -use common\components\Mojang\exceptions\NoContentException; use common\models\Account; use common\models\MojangUsername; -use GuzzleHttp\Exception\RequestException; +use Ely\Mojang\Api as MojangApi; +use Ely\Mojang\Exception\MojangApiException; +use Ely\Mojang\Exception\NoContentException; +use GuzzleHttp\Exception\GuzzleException; use Yii; use yii\queue\JobInterface; @@ -31,14 +31,15 @@ class PullMojangUsername implements JobInterface { */ public function execute($queue) { Yii::$app->statsd->inc('queue.pullMojangUsername.attempt'); - $mojangApi = $this->createMojangApi(); + /** @var MojangApi $mojangApi */ + $mojangApi = Yii::$app->get(MojangApi::class); try { $response = $mojangApi->usernameToUUID($this->username); Yii::$app->statsd->inc('queue.pullMojangUsername.found'); } catch (NoContentException $e) { $response = false; Yii::$app->statsd->inc('queue.pullMojangUsername.not_found'); - } catch (RequestException | MojangApiException $e) { + } catch (GuzzleException | MojangApiException $e) { Yii::$app->statsd->inc('queue.pullMojangUsername.error'); return; } @@ -52,10 +53,10 @@ class PullMojangUsername implements JobInterface { } else { if ($mojangUsername === null) { $mojangUsername = new MojangUsername(); - $mojangUsername->username = $response->name; - $mojangUsername->uuid = $response->id; + $mojangUsername->username = $response->getName(); + $mojangUsername->uuid = $response->getId(); } else { - $mojangUsername->uuid = $response->id; + $mojangUsername->uuid = $response->getId(); $mojangUsername->touch('last_pulled_at'); } @@ -65,8 +66,4 @@ class PullMojangUsername implements JobInterface { } } - protected function createMojangApi(): MojangApi { - return new MojangApi(); - } - } diff --git a/common/tests/unit/components/Mojang/ApiTest.php b/common/tests/unit/components/Mojang/ApiTest.php deleted file mode 100644 index b958833..0000000 --- a/common/tests/unit/components/Mojang/ApiTest.php +++ /dev/null @@ -1,54 +0,0 @@ -handler = new MockHandler(); - $handler = HandlerStack::create($this->handler); - Yii::$app->set('guzzle', new GuzzleClient([ - 'handler' => $handler, - ])); - } - - public function testUsernameToUUID() { - $this->handler->append(new Response(200, [], '{"id": "7125ba8b1c864508b92bb5c042ccfe2b","name": "KrisJelbring"}')); - $response = (new Api())->usernameToUUID('KrisJelbring'); - $this->assertInstanceOf(UsernameToUUIDResponse::class, $response); - $this->assertSame('7125ba8b1c864508b92bb5c042ccfe2b', $response->id); - $this->assertSame('KrisJelbring', $response->name); - } - - /** - * @expectedException \common\components\Mojang\exceptions\NoContentException - */ - public function testUsernameToUUIDNoContent() { - $this->handler->append(new Response(204)); - (new Api())->usernameToUUID('some-non-exists-user'); - } - - /** - * @expectedException \GuzzleHttp\Exception\RequestException - */ - public function testUsernameToUUID404() { - $this->handler->append(new Response(404, [], '{"error":"Not Found","errorMessage":"The server has not found anything matching the request URI"}')); - (new Api())->usernameToUUID('#hashedNickname'); - } - -} diff --git a/common/tests/unit/tasks/PullMojangUsernameTest.php b/common/tests/unit/tasks/PullMojangUsernameTest.php index df97827..7ea11e6 100644 --- a/common/tests/unit/tasks/PullMojangUsernameTest.php +++ b/common/tests/unit/tasks/PullMojangUsernameTest.php @@ -3,14 +3,17 @@ declare(strict_types=1); namespace common\tests\unit\tasks; -use common\components\Mojang\Api; -use common\components\Mojang\exceptions\NoContentException; -use common\components\Mojang\response\UsernameToUUIDResponse; use common\models\Account; use common\models\MojangUsername; use common\tasks\PullMojangUsername; use common\tests\fixtures\MojangUsernameFixture; use common\tests\unit\TestCase; +use Ely\Mojang\Api as MojangApi; +use Ely\Mojang\Exception\NoContentException; +use Ely\Mojang\Response\ProfileInfo; +use GuzzleHttp\Psr7\Request; +use GuzzleHttp\Psr7\Response; +use Yii; use yii\queue\Queue; /** @@ -18,12 +21,8 @@ use yii\queue\Queue; */ class PullMojangUsernameTest extends TestCase { - private $expectedResponse; - - /** - * @var PullMojangUsername - */ - private $task; + /** @var \PHPUnit\Framework\MockObject\Builder\InvocationMocker */ + private $mockedMethod; public function _fixtures() { return [ @@ -34,33 +33,11 @@ class PullMojangUsernameTest extends TestCase { public function _before() { parent::_before(); - /** @var PullMojangUsername|\PHPUnit_Framework_MockObject_MockObject $task */ - $task = $this->getMockBuilder(PullMojangUsername::class) - ->setMethods(['createMojangApi']) - ->getMock(); + /** @var \PHPUnit\Framework\MockObject\MockObject|MojangApi $mockApi */ + $mockApi = $this->createMock(MojangApi::class); + $this->mockedMethod = $mockApi->method('usernameToUUID'); - /** @var Api|\PHPUnit_Framework_MockObject_MockObject $apiMock */ - $apiMock = $this->getMockBuilder(Api::class) - ->setMethods(['usernameToUUID']) - ->getMock(); - - $apiMock - ->expects($this->any()) - ->method('usernameToUUID') - ->willReturnCallback(function() { - if ($this->expectedResponse === false) { - throw new NoContentException(); - } - - return $this->expectedResponse; - }); - - $task - ->expects($this->any()) - ->method('createMojangApi') - ->willReturn($apiMock); - - $this->task = $task; + Yii::$app->set(MojangApi::class, $mockApi); } public function testCreateFromAccount() { @@ -71,15 +48,13 @@ class PullMojangUsernameTest extends TestCase { } public function testExecuteUsernameExists() { - $expectedResponse = new UsernameToUUIDResponse(); - $expectedResponse->id = '069a79f444e94726a5befca90e38aaf5'; - $expectedResponse->name = 'Notch'; - $this->expectedResponse = $expectedResponse; + $this->mockedMethod->willReturn(new ProfileInfo('069a79f444e94726a5befca90e38aaf5', 'Notch')); /** @var \common\models\MojangUsername $mojangUsernameFixture */ $mojangUsernameFixture = $this->tester->grabFixture('mojangUsernames', 'Notch'); - $this->task->username = 'Notch'; - $this->task->execute(mock(Queue::class)); + $task = new PullMojangUsername(); + $task->username = 'Notch'; + $task->execute(mock(Queue::class)); /** @var MojangUsername|null $mojangUsername */ $mojangUsername = MojangUsername::findOne('Notch'); $this->assertInstanceOf(MojangUsername::class, $mojangUsername); @@ -88,15 +63,13 @@ class PullMojangUsernameTest extends TestCase { } public function testExecuteChangedUsernameExists() { - $expectedResponse = new UsernameToUUIDResponse(); - $expectedResponse->id = '069a79f444e94726a5befca90e38aaf5'; - $expectedResponse->name = 'Notch'; - $this->expectedResponse = $expectedResponse; + $this->mockedMethod->willReturn(new ProfileInfo('069a79f444e94726a5befca90e38aaf5', 'Notch')); /** @var MojangUsername $mojangUsernameFixture */ $mojangUsernameFixture = $this->tester->grabFixture('mojangUsernames', 'Notch'); - $this->task->username = 'Notch'; - $this->task->execute(mock(Queue::class)); + $task = new PullMojangUsername(); + $task->username = 'Notch'; + $task->execute(mock(Queue::class)); /** @var MojangUsername|null $mojangUsername */ $mojangUsername = MojangUsername::findOne('Notch'); $this->assertInstanceOf(MojangUsername::class, $mojangUsername); @@ -105,40 +78,37 @@ class PullMojangUsernameTest extends TestCase { } public function testExecuteChangedUsernameNotExists() { - $expectedResponse = new UsernameToUUIDResponse(); - $expectedResponse->id = '607153852b8c4909811f507ed8ee737f'; - $expectedResponse->name = 'Chest'; - $this->expectedResponse = $expectedResponse; + $this->mockedMethod->willReturn(new ProfileInfo('607153852b8c4909811f507ed8ee737f', 'Chest')); - $this->task->username = 'Chest'; - $this->task->execute(mock(Queue::class)); + $task = new PullMojangUsername(); + $task->username = 'Chest'; + $task->execute(mock(Queue::class)); /** @var MojangUsername|null $mojangUsername */ $mojangUsername = MojangUsername::findOne('Chest'); $this->assertInstanceOf(MojangUsername::class, $mojangUsername); } public function testExecuteRemoveIfExistsNoMore() { - $this->expectedResponse = false; + $this->mockedMethod->willThrowException(new NoContentException(new Request('', ''), new Response())); $username = $this->tester->grabFixture('mojangUsernames', 'not-exists')['username']; - $this->task->username = $username; - $this->task->execute(mock(Queue::class)); + $task = new PullMojangUsername(); + $task->username = $username; + $task->execute(mock(Queue::class)); /** @var MojangUsername|null $mojangUsername */ $mojangUsername = MojangUsername::findOne($username); $this->assertNull($mojangUsername); } public function testExecuteUuidUpdated() { - $expectedResponse = new UsernameToUUIDResponse(); - $expectedResponse->id = 'f498513ce8c84773be26ecfc7ed5185d'; - $expectedResponse->name = 'jeb'; - $this->expectedResponse = $expectedResponse; + $this->mockedMethod->willReturn(new ProfileInfo('f498513ce8c84773be26ecfc7ed5185d', 'jeb')); /** @var MojangUsername $mojangInfo */ $mojangInfo = $this->tester->grabFixture('mojangUsernames', 'uuid-changed'); $username = $mojangInfo['username']; - $this->task->username = $username; - $this->task->execute(mock(Queue::class)); + $task = new PullMojangUsername(); + $task->username = $username; + $task->execute(mock(Queue::class)); /** @var MojangUsername|null $mojangUsername */ $mojangUsername = MojangUsername::findOne($username); $this->assertInstanceOf(MojangUsername::class, $mojangUsername); diff --git a/composer.json b/composer.json index bc5ad13..211b626 100644 --- a/composer.json +++ b/composer.json @@ -7,27 +7,28 @@ "require": { "php": "^7.2", "ext-json": "*", + "ext-libxml": "*", "ext-mbstring": "*", "ext-pdo": "*", - "ext-libxml": "*", "ext-simplexml": "*", - "yiisoft/yii2": "2.0.15.1", - "yiisoft/yii2-swiftmailer": "~2.1.0", - "ramsey/uuid": "^3.5", - "league/oauth2-server": "^4.1", - "yiisoft/yii2-redis": "~2.0.0", - "guzzlehttp/guzzle": "^6.0.0", + "bacon/bacon-qr-code": "^1.0", + "domnikl/statsd": "^2.6", + "ely/email-renderer": "dev-master#8aa2e71c5b3b8e4a726c3c090b2997030ba29f73", + "ely/mojang-api": "^0.2.0", "ely/yii2-tempmail-validator": "^2.0", "emarref/jwt": "~1.0.3", - "ely/email-renderer": "dev-master#8aa2e71c5b3b8e4a726c3c090b2997030ba29f73", - "mito/yii2-sentry": "^1.0", - "spomky-labs/otphp": "^9.0.2", - "bacon/bacon-qr-code": "^1.0", - "paragonie/constant_time_encoding": "^2.0", - "webmozart/assert": "^1.2.0", "goaop/framework": "^2.2.0", - "domnikl/statsd": "^2.6", - "yiisoft/yii2-queue": "~2.1.0" + "guzzlehttp/guzzle": "^6.0.0", + "league/oauth2-server": "^4.1", + "mito/yii2-sentry": "^1.0", + "paragonie/constant_time_encoding": "^2.0", + "ramsey/uuid": "^3.5", + "spomky-labs/otphp": "^9.0.2", + "webmozart/assert": "^1.2.0", + "yiisoft/yii2": "2.0.15.1", + "yiisoft/yii2-queue": "~2.1.0", + "yiisoft/yii2-redis": "~2.0.0", + "yiisoft/yii2-swiftmailer": "~2.1.0" }, "require-dev": { "codeception/codeception": "^2.5.3", diff --git a/composer.lock b/composer.lock index 41b6d86..0785767 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "029519509cc9ceb2a06e09ba680257b2", + "content-hash": "ae2318fe3bd54e8c670c5969ba5c3e82", "packages": [ { "name": "bacon/bacon-qr-code", @@ -118,8 +118,7 @@ "dist": { "type": "zip", "url": "https://api.github.com/repos/RobinHerbots/Inputmask/zipball/5e670ad62f50c738388d4dcec78d2888505ad77b", - "reference": "5e670ad62f50c738388d4dcec78d2888505ad77b", - "shasum": null + "reference": "5e670ad62f50c738388d4dcec78d2888505ad77b" }, "require": { "bower-asset/jquery": ">=1.7" @@ -140,8 +139,7 @@ "dist": { "type": "zip", "url": "https://api.github.com/repos/jquery/jquery-dist/zipball/77d2a51d0520d2ee44173afdf4e40a9201f5964e", - "reference": "77d2a51d0520d2ee44173afdf4e40a9201f5964e", - "shasum": null + "reference": "77d2a51d0520d2ee44173afdf4e40a9201f5964e" }, "type": "bower-asset", "license": [ @@ -159,8 +157,7 @@ "dist": { "type": "zip", "url": "https://api.github.com/repos/bestiejs/punycode.js/zipball/38c8d3131a82567bfef18da09f7f4db68c84f8a3", - "reference": "38c8d3131a82567bfef18da09f7f4db68c84f8a3", - "shasum": null + "reference": "38c8d3131a82567bfef18da09f7f4db68c84f8a3" }, "type": "bower-asset" }, @@ -175,8 +172,7 @@ "dist": { "type": "zip", "url": "https://api.github.com/repos/getsentry/raven-js-bower/zipball/c8b3a6040be6928e2f57fa5eec4d7afc31750235", - "reference": "c8b3a6040be6928e2f57fa5eec4d7afc31750235", - "shasum": null + "reference": "c8b3a6040be6928e2f57fa5eec4d7afc31750235" }, "type": "bower-asset" }, @@ -191,8 +187,7 @@ "dist": { "type": "zip", "url": "https://api.github.com/repos/yiisoft/jquery-pjax/zipball/aef7b953107264f00234902a3880eb50dafc48be", - "reference": "aef7b953107264f00234902a3880eb50dafc48be", - "shasum": null + "reference": "aef7b953107264f00234902a3880eb50dafc48be" }, "require": { "bower-asset/jquery": ">=1.8" @@ -651,6 +646,50 @@ ], "time": "2017-10-04T17:17:10+00:00" }, + { + "name": "ely/mojang-api", + "version": "0.2.0", + "source": { + "type": "git", + "url": "https://github.com/elyby/mojang-api.git", + "reference": "1bb4365e555ae6210ca6a7327d49948dbff857d9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/elyby/mojang-api/zipball/1bb4365e555ae6210ca6a7327d49948dbff857d9", + "reference": "1bb4365e555ae6210ca6a7327d49948dbff857d9", + "shasum": "" + }, + "require": { + "ext-json": "*", + "ext-mbstring": "*", + "guzzlehttp/guzzle": "^6.0.0", + "php": ">=7.1.0", + "ramsey/uuid": "^3.0.0" + }, + "require-dev": { + "ely/php-code-style": "^0.3.0", + "phpunit/phpunit": "^7.0.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Ely\\Mojang\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "ErickSkrauch", + "email": "erickskrauch@yandex.ru" + } + ], + "description": "Library for access to Mojang API.", + "time": "2019-05-07T08:31:39+00:00" + }, { "name": "ely/yii2-tempmail-validator", "version": "2.0.0", @@ -5697,9 +5736,9 @@ "platform": { "php": "^7.2", "ext-json": "*", + "ext-libxml": "*", "ext-mbstring": "*", "ext-pdo": "*", - "ext-libxml": "*", "ext-simplexml": "*" }, "platform-dev": []