From 6af7b4325ccbee1e47f5d536a36c2b5ef8e2d26b Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Mon, 31 Oct 2016 02:11:52 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20MojangApi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/components/Mojang/Api.php | 11 ++-- .../common/unit/components/Mojang/ApiTest.php | 54 +++++++++++++++++++ 2 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 tests/codeception/common/unit/components/Mojang/ApiTest.php diff --git a/common/components/Mojang/Api.php b/common/components/Mojang/Api.php index 960a6bf..cb92122 100644 --- a/common/components/Mojang/Api.php +++ b/common/components/Mojang/Api.php @@ -4,7 +4,7 @@ namespace common\components\Mojang; use common\components\Mojang\exceptions\MojangApiException; use common\components\Mojang\exceptions\NoContentException; use common\components\Mojang\response\UsernameToUUIDResponse; -use GuzzleHttp\Client as GuzzleClient; +use Yii; class Api { @@ -23,7 +23,7 @@ class Api { $query['atTime'] = $atTime; } - $response = $this->createClient()->get($this->buildUsernameToUUIDRoute($username), $query); + $response = $this->getClient()->get($this->buildUsernameToUUIDRoute($username), $query); if ($response->getStatusCode() === 204) { throw new NoContentException('Username not found'); } elseif ($response->getStatusCode() !== 200) { @@ -40,8 +40,11 @@ class Api { return $responseObj; } - protected function createClient() { - return new GuzzleClient(); + /** + * @return \GuzzleHttp\Client + */ + protected function getClient() { + return Yii::$app->guzzle; } protected function buildUsernameToUUIDRoute($username) { diff --git a/tests/codeception/common/unit/components/Mojang/ApiTest.php b/tests/codeception/common/unit/components/Mojang/ApiTest.php new file mode 100644 index 0000000..821bebc --- /dev/null +++ b/tests/codeception/common/unit/components/Mojang/ApiTest.php @@ -0,0 +1,54 @@ +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->assertEquals('7125ba8b1c864508b92bb5c042ccfe2b', $response->id); + $this->assertEquals('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'); + } + +}