Fixed bugs

This commit is contained in:
valentinpahusko 2020-02-10 17:29:39 +03:00
parent ff6ef65a5e
commit 3ce60beaf1
4 changed files with 56 additions and 90 deletions

View File

@ -6,7 +6,6 @@ namespace Ely\Mojang;
use DateTime; use DateTime;
use Ely\Mojang\Middleware\ResponseConverterMiddleware; use Ely\Mojang\Middleware\ResponseConverterMiddleware;
use Ely\Mojang\Middleware\RetryMiddleware; use Ely\Mojang\Middleware\RetryMiddleware;
use Ely\Mojang\Response\AnswerResponse;
use Ely\Mojang\Response\QuestionResponse; use Ely\Mojang\Response\QuestionResponse;
use GuzzleHttp\Client as GuzzleClient; use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\ClientInterface; use GuzzleHttp\ClientInterface;
@ -461,8 +460,11 @@ class Api {
* @url https://wiki.vg/Mojang_API#Check_if_security_questions_are_needed * @url https://wiki.vg/Mojang_API#Check_if_security_questions_are_needed
*/ */
public function isSecurityQuestionsNeeded(string $accessToken): void { public function isSecurityQuestionsNeeded(string $accessToken): void {
$uri = new Uri('https://api.mojang.com/user/security/location'); $request = new Request(
$request = new Request('GET', $uri, ['Authorization' => 'Bearer ' . $accessToken]); 'GET',
'https://api.mojang.com/user/security/location',
['Authorization' => 'Bearer ' . $accessToken]
);
$response = $this->getClient()->send($request); $response = $this->getClient()->send($request);
$rawBody = $response->getBody()->getContents(); $rawBody = $response->getBody()->getContents();
if (!empty($rawBody)) { if (!empty($rawBody)) {
@ -479,21 +481,16 @@ class Api {
* @url https://wiki.vg/Mojang_API#Get_list_of_questions * @url https://wiki.vg/Mojang_API#Get_list_of_questions
*/ */
public function questions(string $accessToken): array { public function questions(string $accessToken): array {
$uri = new Uri('https://api.mojang.com/user/security/challenges'); $request = new Request(
$request = new Request('GET', $uri, ['Authorization' => 'Bearer ' . $accessToken]); 'GET',
'https://api.mojang.com/user/security/challenges',
['Authorization' => 'Bearer ' . $accessToken]
);
$response = $this->getClient()->send($request); $response = $this->getClient()->send($request);
$rawBody = $response->getBody()->getContents();
if (empty($rawBody)) {
throw new Exception\NoContentException($request, $response);
}
$result = []; $result = [];
$body = $this->decode($rawBody); $body = $this->decode($response->getBody()->getContents());
foreach ($body as $question) { foreach ($body as $question) {
$result[] = [ $result[] = new QuestionResponse($question['question']['id'], $question['question']['question'], $question['answer']['id']);
'answer' => new AnswerResponse($question['answer']['id']),
'question' => new QuestionResponse($question['question']['id'], $question['question']['question']),
];
} }
return $result; return $result;
@ -503,18 +500,21 @@ class Api {
* @param string $accessToken * @param string $accessToken
* @param array $answers * @param array $answers
* @throws GuzzleException * @throws GuzzleException
* @return bool
* *
* @url https://wiki.vg/Mojang_API#Send_back_the_answers * @url https://wiki.vg/Mojang_API#Send_back_the_answers
*/ */
public function answer(string $accessToken, array $answers): void { public function answer(string $accessToken, array $answers): bool {
$uri = new Uri('https://api.mojang.com/user/security/location'); $request = new Request(
$request = new Request('POST', $uri, ['Authorization' => 'Bearer ' . $accessToken], json_encode($answers)); 'POST',
'https://api.mojang.com/user/security/location',
['Authorization' => 'Bearer ' . $accessToken],
json_encode($answers)
);
$response = $this->getClient()->send($request); $response = $this->getClient()->send($request);
$rawBody = $response->getBody()->getContents(); $rawBody = $response->getBody()->getContents();
if (!empty($rawBody)) {
$body = $this->decode($rawBody); return empty($rawBody);
throw new Exception\OperationException($body['errorMessage'], $request, $response);
}
} }
/** /**
@ -526,7 +526,9 @@ class Api {
*/ */
public function statistics(array $metricKeys) { public function statistics(array $metricKeys) {
$response = $this->getClient()->request('POST', 'https://api.mojang.com/orders/statistics', [ $response = $this->getClient()->request('POST', 'https://api.mojang.com/orders/statistics', [
'json' => $metricKeys, 'json' => [
'metricKeys' => $metricKeys,
],
]); ]);
$body = $this->decode($response->getBody()->getContents()); $body = $this->decode($response->getBody()->getContents());

View File

@ -1,31 +0,0 @@
<?php
declare(strict_types=1);
namespace Ely\Mojang\Response;
class AnswerResponse {
/**
* @var int
*/
public $id;
/**
* @var string|null
*/
public $answer;
public function __construct(int $id, ?string $answer = null) {
$this->id = $id;
$this->answer = $answer;
}
public function getId(): int {
return $this->id;
}
public function getAnswer(): ?string {
return $this->answer;
}
}

View File

@ -8,24 +8,34 @@ class QuestionResponse {
/** /**
* @var int * @var int
*/ */
private $id; private $questionId;
/** /**
* @var string * @var string
*/ */
private $question; private $question;
public function __construct(int $id, string $question) { /**
$this->id = $id; * @var int
*/
private $answerId;
public function __construct(int $questionId, string $question, int $answerId) {
$this->questionId = $questionId;
$this->question = $question; $this->question = $question;
$this->answerId = $answerId;
} }
public function getId(): int { public function getQuestionId(): int {
return $this->id; return $this->questionId;
} }
public function getQuestion(): string { public function getQuestion(): string {
return $this->question; return $this->question;
} }
public function getAnswerId(): int {
return $this->answerId;
}
} }

View File

@ -9,7 +9,6 @@ use Ely\Mojang\Exception\NoContentException;
use Ely\Mojang\Exception\OperationException; use Ely\Mojang\Exception\OperationException;
use Ely\Mojang\Middleware\ResponseConverterMiddleware; use Ely\Mojang\Middleware\ResponseConverterMiddleware;
use Ely\Mojang\Middleware\RetryMiddleware; use Ely\Mojang\Middleware\RetryMiddleware;
use Ely\Mojang\Response\AnswerResponse;
use Ely\Mojang\Response\ApiStatus; use Ely\Mojang\Response\ApiStatus;
use Ely\Mojang\Response\NameHistoryItem; use Ely\Mojang\Response\NameHistoryItem;
use Ely\Mojang\Response\ProfileInfo; use Ely\Mojang\Response\ProfileInfo;
@ -663,37 +662,25 @@ class ApiTest extends TestCase {
$this->assertSame('Bearer mocked access token', $request->getHeaderLine('Authorization')); $this->assertSame('Bearer mocked access token', $request->getHeaderLine('Authorization'));
foreach ($result as $question) { foreach ($result as $question) {
$this->assertArrayHasKey('answer', $question); $this->assertInstanceOf(QuestionResponse::class, $question);
$this->assertArrayHasKey('question', $question);
$this->assertInstanceOf(AnswerResponse::class, $question['answer']);
$this->assertInstanceOf(QuestionResponse::class, $question['question']);
} }
/** @var AnswerResponse $firstAnswer */
$firstAnswer = $result[0]['answer'];
/** @var QuestionResponse $firstQuestion */ /** @var QuestionResponse $firstQuestion */
$firstQuestion = $result[0]['question']; $firstQuestion = $result[0];
$this->assertSame(123, $firstAnswer->getId()); $this->assertSame(123, $firstQuestion->getAnswerId());
$this->assertNull($firstAnswer->getAnswer()); $this->assertSame(1, $firstQuestion->getQuestionId());
$this->assertSame(1, $firstQuestion->getId());
$this->assertSame('What is your favorite pet\'s name?', $firstQuestion->getQuestion()); $this->assertSame('What is your favorite pet\'s name?', $firstQuestion->getQuestion());
/** @var AnswerResponse $secondAnswer */
$secondAnswer = $result[1]['answer'];
/** @var QuestionResponse $secondQuestion */ /** @var QuestionResponse $secondQuestion */
$secondQuestion = $result[1]['question']; $secondQuestion = $result[1];
$this->assertSame(456, $secondAnswer->getId()); $this->assertSame(456, $secondQuestion->getAnswerId());
$this->assertNull($secondAnswer->getAnswer()); $this->assertSame(2, $secondQuestion->getQuestionId());
$this->assertSame(2, $secondQuestion->getId());
$this->assertSame('What is your favorite movie?', $secondQuestion->getQuestion()); $this->assertSame('What is your favorite movie?', $secondQuestion->getQuestion());
/** @var AnswerResponse $thirdAnswer */
$thirdAnswer = $result[2]['answer'];
/** @var QuestionResponse $thirdQuestion */ /** @var QuestionResponse $thirdQuestion */
$thirdQuestion = $result[2]['question']; $thirdQuestion = $result[2];
$this->assertSame(789, $thirdAnswer->getId()); $this->assertSame(789, $thirdQuestion->getAnswerId());
$this->assertNull($thirdAnswer->getAnswer()); $this->assertSame(3, $thirdQuestion->getQuestionId());
$this->assertSame(3, $thirdQuestion->getId());
$this->assertSame('What is your favorite author\'s last name?', $thirdQuestion->getQuestion()); $this->assertSame('What is your favorite author\'s last name?', $thirdQuestion->getQuestion());
} }
@ -702,8 +689,7 @@ class ApiTest extends TestCase {
'error' => 'ForbiddenOperationException', 'error' => 'ForbiddenOperationException',
'errorMessage' => 'At least one answer was incorrect', 'errorMessage' => 'At least one answer was incorrect',
])); ]));
$this->expectException(OperationException::class); $result = $this->api->answer('mocked access token', [
$this->api->answer('mocked access token', [
[ [
'id' => 123, 'id' => 123,
'answer' => 'foo', 'answer' => 'foo',
@ -713,6 +699,7 @@ class ApiTest extends TestCase {
'answer' => 'bar', 'answer' => 'bar',
], ],
]); ]);
$this->assertFalse($result);
} }
public function testAnswer() { public function testAnswer() {
@ -728,12 +715,10 @@ class ApiTest extends TestCase {
'saleVelocityPerSeconds' => 1.32, 'saleVelocityPerSeconds' => 1.32,
])); ]));
$result = $this->api->statistics([ $result = $this->api->statistics([
'metricKeys' => [ 'item_sold_minecraft',
'item_sold_minecraft', 'prepaid_card_redeemed_minecraft',
'prepaid_card_redeemed_minecraft', 'item_sold_cobalt',
'item_sold_cobalt', 'item_sold_scrolls',
'item_sold_scrolls',
],
]); ]);
/** @var \Psr\Http\Message\RequestInterface $request */ /** @var \Psr\Http\Message\RequestInterface $request */
$request = $this->history[0]['request']; $request = $this->history[0]['request'];