2016-04-24 00:14:10 +05:30
|
|
|
<?php
|
2018-07-08 20:50:19 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-02-21 01:28:52 +05:30
|
|
|
namespace common\tests\unit\tasks;
|
2016-04-24 00:14:10 +05:30
|
|
|
|
2016-10-31 05:17:18 +05:30
|
|
|
use common\components\Mojang\Api;
|
|
|
|
use common\components\Mojang\exceptions\NoContentException;
|
|
|
|
use common\components\Mojang\response\UsernameToUUIDResponse;
|
2018-07-08 20:50:19 +05:30
|
|
|
use common\models\Account;
|
2016-04-24 00:14:10 +05:30
|
|
|
use common\models\MojangUsername;
|
2018-07-08 20:50:19 +05:30
|
|
|
use common\tasks\PullMojangUsername;
|
2019-02-21 01:28:52 +05:30
|
|
|
use common\tests\fixtures\MojangUsernameFixture;
|
|
|
|
use common\tests\unit\TestCase;
|
2018-07-08 20:50:19 +05:30
|
|
|
use yii\queue\Queue;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \common\tasks\PullMojangUsername
|
|
|
|
*/
|
|
|
|
class PullMojangUsernameTest extends TestCase {
|
2016-04-24 00:14:10 +05:30
|
|
|
|
2018-07-08 20:50:19 +05:30
|
|
|
private $expectedResponse;
|
2016-04-24 00:14:10 +05:30
|
|
|
|
2016-10-31 05:17:18 +05:30
|
|
|
/**
|
2018-07-08 20:50:19 +05:30
|
|
|
* @var PullMojangUsername
|
2016-10-31 05:17:18 +05:30
|
|
|
*/
|
2018-07-08 20:50:19 +05:30
|
|
|
private $task;
|
2016-10-31 05:17:18 +05:30
|
|
|
|
|
|
|
public function _fixtures() {
|
2016-04-24 00:14:10 +05:30
|
|
|
return [
|
2016-10-31 05:17:18 +05:30
|
|
|
'mojangUsernames' => MojangUsernameFixture::class,
|
2016-04-24 00:14:10 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-10-31 05:17:18 +05:30
|
|
|
public function _before() {
|
|
|
|
parent::_before();
|
|
|
|
|
2018-07-08 20:50:19 +05:30
|
|
|
/** @var PullMojangUsername|\PHPUnit_Framework_MockObject_MockObject $task */
|
|
|
|
$task = $this->getMockBuilder(PullMojangUsername::class)
|
2016-10-31 05:17:18 +05:30
|
|
|
->setMethods(['createMojangApi'])
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
/** @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();
|
|
|
|
}
|
2018-04-18 02:17:25 +05:30
|
|
|
|
|
|
|
return $this->expectedResponse;
|
2016-10-31 05:17:18 +05:30
|
|
|
});
|
|
|
|
|
2018-07-08 20:50:19 +05:30
|
|
|
$task
|
2016-10-31 05:17:18 +05:30
|
|
|
->expects($this->any())
|
|
|
|
->method('createMojangApi')
|
|
|
|
->willReturn($apiMock);
|
|
|
|
|
2018-07-08 20:50:19 +05:30
|
|
|
$this->task = $task;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateFromAccount() {
|
|
|
|
$account = new Account();
|
|
|
|
$account->username = 'find-me';
|
|
|
|
$result = PullMojangUsername::createFromAccount($account);
|
|
|
|
$this->assertSame('find-me', $result->username);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testExecuteUsernameExists() {
|
|
|
|
$expectedResponse = new UsernameToUUIDResponse();
|
|
|
|
$expectedResponse->id = '069a79f444e94726a5befca90e38aaf5';
|
|
|
|
$expectedResponse->name = 'Notch';
|
|
|
|
$this->expectedResponse = $expectedResponse;
|
|
|
|
|
|
|
|
/** @var \common\models\MojangUsername $mojangUsernameFixture */
|
|
|
|
$mojangUsernameFixture = $this->tester->grabFixture('mojangUsernames', 'Notch');
|
|
|
|
$this->task->username = 'Notch';
|
|
|
|
$this->task->execute(mock(Queue::class));
|
|
|
|
/** @var MojangUsername|null $mojangUsername */
|
|
|
|
$mojangUsername = MojangUsername::findOne('Notch');
|
|
|
|
$this->assertInstanceOf(MojangUsername::class, $mojangUsername);
|
|
|
|
$this->assertGreaterThan($mojangUsernameFixture->last_pulled_at, $mojangUsername->last_pulled_at);
|
|
|
|
$this->assertLessThanOrEqual(time(), $mojangUsername->last_pulled_at);
|
2016-10-31 05:17:18 +05:30
|
|
|
}
|
|
|
|
|
2018-07-08 20:50:19 +05:30
|
|
|
public function testExecuteChangedUsernameExists() {
|
2016-10-31 05:17:18 +05:30
|
|
|
$expectedResponse = new UsernameToUUIDResponse();
|
|
|
|
$expectedResponse->id = '069a79f444e94726a5befca90e38aaf5';
|
|
|
|
$expectedResponse->name = 'Notch';
|
|
|
|
$this->expectedResponse = $expectedResponse;
|
|
|
|
|
|
|
|
/** @var MojangUsername $mojangUsernameFixture */
|
|
|
|
$mojangUsernameFixture = $this->tester->grabFixture('mojangUsernames', 'Notch');
|
2018-07-08 20:50:19 +05:30
|
|
|
$this->task->username = 'Notch';
|
|
|
|
$this->task->execute(mock(Queue::class));
|
2016-10-31 05:17:18 +05:30
|
|
|
/** @var MojangUsername|null $mojangUsername */
|
|
|
|
$mojangUsername = MojangUsername::findOne('Notch');
|
|
|
|
$this->assertInstanceOf(MojangUsername::class, $mojangUsername);
|
|
|
|
$this->assertGreaterThan($mojangUsernameFixture->last_pulled_at, $mojangUsername->last_pulled_at);
|
|
|
|
$this->assertLessThanOrEqual(time(), $mojangUsername->last_pulled_at);
|
|
|
|
}
|
|
|
|
|
2018-07-08 20:50:19 +05:30
|
|
|
public function testExecuteChangedUsernameNotExists() {
|
2016-10-31 05:17:18 +05:30
|
|
|
$expectedResponse = new UsernameToUUIDResponse();
|
|
|
|
$expectedResponse->id = '607153852b8c4909811f507ed8ee737f';
|
|
|
|
$expectedResponse->name = 'Chest';
|
|
|
|
$this->expectedResponse = $expectedResponse;
|
|
|
|
|
2018-07-08 20:50:19 +05:30
|
|
|
$this->task->username = 'Chest';
|
|
|
|
$this->task->execute(mock(Queue::class));
|
2016-10-31 05:17:18 +05:30
|
|
|
/** @var MojangUsername|null $mojangUsername */
|
|
|
|
$mojangUsername = MojangUsername::findOne('Chest');
|
|
|
|
$this->assertInstanceOf(MojangUsername::class, $mojangUsername);
|
|
|
|
}
|
|
|
|
|
2018-07-08 20:50:19 +05:30
|
|
|
public function testExecuteRemoveIfExistsNoMore() {
|
2016-10-31 05:17:18 +05:30
|
|
|
$this->expectedResponse = false;
|
|
|
|
|
|
|
|
$username = $this->tester->grabFixture('mojangUsernames', 'not-exists')['username'];
|
2018-07-08 20:50:19 +05:30
|
|
|
$this->task->username = $username;
|
|
|
|
$this->task->execute(mock(Queue::class));
|
2016-10-31 05:17:18 +05:30
|
|
|
/** @var MojangUsername|null $mojangUsername */
|
|
|
|
$mojangUsername = MojangUsername::findOne($username);
|
|
|
|
$this->assertNull($mojangUsername);
|
|
|
|
}
|
|
|
|
|
2018-07-08 20:50:19 +05:30
|
|
|
public function testExecuteUuidUpdated() {
|
2016-10-31 05:17:18 +05:30
|
|
|
$expectedResponse = new UsernameToUUIDResponse();
|
|
|
|
$expectedResponse->id = 'f498513ce8c84773be26ecfc7ed5185d';
|
|
|
|
$expectedResponse->name = 'jeb';
|
|
|
|
$this->expectedResponse = $expectedResponse;
|
|
|
|
|
|
|
|
/** @var MojangUsername $mojangInfo */
|
|
|
|
$mojangInfo = $this->tester->grabFixture('mojangUsernames', 'uuid-changed');
|
|
|
|
$username = $mojangInfo['username'];
|
2018-07-08 20:50:19 +05:30
|
|
|
$this->task->username = $username;
|
|
|
|
$this->task->execute(mock(Queue::class));
|
2016-10-31 05:17:18 +05:30
|
|
|
/** @var MojangUsername|null $mojangUsername */
|
|
|
|
$mojangUsername = MojangUsername::findOne($username);
|
|
|
|
$this->assertInstanceOf(MojangUsername::class, $mojangUsername);
|
|
|
|
$this->assertNotEquals($mojangInfo->uuid, $mojangUsername->uuid);
|
2016-04-24 00:14:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|