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 Ely\Mojang\Middleware\ResponseConverterMiddleware;
use Ely\Mojang\Middleware\RetryMiddleware;
use Ely\Mojang\Response\AnswerResponse;
use Ely\Mojang\Response\QuestionResponse;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\ClientInterface;
@ -461,8 +460,11 @@ class Api {
* @url https://wiki.vg/Mojang_API#Check_if_security_questions_are_needed
*/
public function isSecurityQuestionsNeeded(string $accessToken): void {
$uri = new Uri('https://api.mojang.com/user/security/location');
$request = new Request('GET', $uri, ['Authorization' => 'Bearer ' . $accessToken]);
$request = new Request(
'GET',
'https://api.mojang.com/user/security/location',
['Authorization' => 'Bearer ' . $accessToken]
);
$response = $this->getClient()->send($request);
$rawBody = $response->getBody()->getContents();
if (!empty($rawBody)) {
@ -479,21 +481,16 @@ class Api {
* @url https://wiki.vg/Mojang_API#Get_list_of_questions
*/
public function questions(string $accessToken): array {
$uri = new Uri('https://api.mojang.com/user/security/challenges');
$request = new Request('GET', $uri, ['Authorization' => 'Bearer ' . $accessToken]);
$request = new Request(
'GET',
'https://api.mojang.com/user/security/challenges',
['Authorization' => 'Bearer ' . $accessToken]
);
$response = $this->getClient()->send($request);
$rawBody = $response->getBody()->getContents();
if (empty($rawBody)) {
throw new Exception\NoContentException($request, $response);
}
$result = [];
$body = $this->decode($rawBody);
$body = $this->decode($response->getBody()->getContents());
foreach ($body as $question) {
$result[] = [
'answer' => new AnswerResponse($question['answer']['id']),
'question' => new QuestionResponse($question['question']['id'], $question['question']['question']),
];
$result[] = new QuestionResponse($question['question']['id'], $question['question']['question'], $question['answer']['id']);
}
return $result;
@ -503,18 +500,21 @@ class Api {
* @param string $accessToken
* @param array $answers
* @throws GuzzleException
* @return bool
*
* @url https://wiki.vg/Mojang_API#Send_back_the_answers
*/
public function answer(string $accessToken, array $answers): void {
$uri = new Uri('https://api.mojang.com/user/security/location');
$request = new Request('POST', $uri, ['Authorization' => 'Bearer ' . $accessToken], json_encode($answers));
public function answer(string $accessToken, array $answers): bool {
$request = new Request(
'POST',
'https://api.mojang.com/user/security/location',
['Authorization' => 'Bearer ' . $accessToken],
json_encode($answers)
);
$response = $this->getClient()->send($request);
$rawBody = $response->getBody()->getContents();
if (!empty($rawBody)) {
$body = $this->decode($rawBody);
throw new Exception\OperationException($body['errorMessage'], $request, $response);
}
return empty($rawBody);
}
/**
@ -526,7 +526,9 @@ class Api {
*/
public function statistics(array $metricKeys) {
$response = $this->getClient()->request('POST', 'https://api.mojang.com/orders/statistics', [
'json' => $metricKeys,
'json' => [
'metricKeys' => $metricKeys,
],
]);
$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
*/
private $id;
private $questionId;
/**
* @var string
*/
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->answerId = $answerId;
}
public function getId(): int {
return $this->id;
public function getQuestionId(): int {
return $this->questionId;
}
public function getQuestion(): string {
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\Middleware\ResponseConverterMiddleware;
use Ely\Mojang\Middleware\RetryMiddleware;
use Ely\Mojang\Response\AnswerResponse;
use Ely\Mojang\Response\ApiStatus;
use Ely\Mojang\Response\NameHistoryItem;
use Ely\Mojang\Response\ProfileInfo;
@ -663,37 +662,25 @@ class ApiTest extends TestCase {
$this->assertSame('Bearer mocked access token', $request->getHeaderLine('Authorization'));
foreach ($result as $question) {
$this->assertArrayHasKey('answer', $question);
$this->assertArrayHasKey('question', $question);
$this->assertInstanceOf(AnswerResponse::class, $question['answer']);
$this->assertInstanceOf(QuestionResponse::class, $question['question']);
$this->assertInstanceOf(QuestionResponse::class, $question);
}
/** @var AnswerResponse $firstAnswer */
$firstAnswer = $result[0]['answer'];
/** @var QuestionResponse $firstQuestion */
$firstQuestion = $result[0]['question'];
$this->assertSame(123, $firstAnswer->getId());
$this->assertNull($firstAnswer->getAnswer());
$this->assertSame(1, $firstQuestion->getId());
$firstQuestion = $result[0];
$this->assertSame(123, $firstQuestion->getAnswerId());
$this->assertSame(1, $firstQuestion->getQuestionId());
$this->assertSame('What is your favorite pet\'s name?', $firstQuestion->getQuestion());
/** @var AnswerResponse $secondAnswer */
$secondAnswer = $result[1]['answer'];
/** @var QuestionResponse $secondQuestion */
$secondQuestion = $result[1]['question'];
$this->assertSame(456, $secondAnswer->getId());
$this->assertNull($secondAnswer->getAnswer());
$this->assertSame(2, $secondQuestion->getId());
$secondQuestion = $result[1];
$this->assertSame(456, $secondQuestion->getAnswerId());
$this->assertSame(2, $secondQuestion->getQuestionId());
$this->assertSame('What is your favorite movie?', $secondQuestion->getQuestion());
/** @var AnswerResponse $thirdAnswer */
$thirdAnswer = $result[2]['answer'];
/** @var QuestionResponse $thirdQuestion */
$thirdQuestion = $result[2]['question'];
$this->assertSame(789, $thirdAnswer->getId());
$this->assertNull($thirdAnswer->getAnswer());
$this->assertSame(3, $thirdQuestion->getId());
$thirdQuestion = $result[2];
$this->assertSame(789, $thirdQuestion->getAnswerId());
$this->assertSame(3, $thirdQuestion->getQuestionId());
$this->assertSame('What is your favorite author\'s last name?', $thirdQuestion->getQuestion());
}
@ -702,8 +689,7 @@ class ApiTest extends TestCase {
'error' => 'ForbiddenOperationException',
'errorMessage' => 'At least one answer was incorrect',
]));
$this->expectException(OperationException::class);
$this->api->answer('mocked access token', [
$result = $this->api->answer('mocked access token', [
[
'id' => 123,
'answer' => 'foo',
@ -713,6 +699,7 @@ class ApiTest extends TestCase {
'answer' => 'bar',
],
]);
$this->assertFalse($result);
}
public function testAnswer() {
@ -728,12 +715,10 @@ class ApiTest extends TestCase {
'saleVelocityPerSeconds' => 1.32,
]));
$result = $this->api->statistics([
'metricKeys' => [
'item_sold_minecraft',
'prepaid_card_redeemed_minecraft',
'item_sold_cobalt',
'item_sold_scrolls',
],
'item_sold_minecraft',
'prepaid_card_redeemed_minecraft',
'item_sold_cobalt',
'item_sold_scrolls',
]);
/** @var \Psr\Http\Message\RequestInterface $request */
$request = $this->history[0]['request'];