mirror of
https://github.com/elyby/accounts.git
synced 2024-11-30 02:32:26 +05:30
Добавлены тесты для MojangApi
This commit is contained in:
parent
465a5289a0
commit
6af7b4325c
@ -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) {
|
||||
|
54
tests/codeception/common/unit/components/Mojang/ApiTest.php
Normal file
54
tests/codeception/common/unit/components/Mojang/ApiTest.php
Normal file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
namespace codeception\common\unit\components\Mojang;
|
||||
|
||||
use common\components\Mojang\Api;
|
||||
use common\components\Mojang\response\UsernameToUUIDResponse;
|
||||
use GuzzleHttp\Client as GuzzleClient;
|
||||
use GuzzleHttp\Handler\MockHandler;
|
||||
use GuzzleHttp\HandlerStack;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use tests\codeception\common\unit\TestCase;
|
||||
use Yii;
|
||||
|
||||
class ApiTest extends TestCase {
|
||||
|
||||
/**
|
||||
* @var MockHandler
|
||||
*/
|
||||
private $handler;
|
||||
|
||||
public function _before() {
|
||||
parent::_before();
|
||||
|
||||
$this->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');
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user