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
|
|
|
|
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;
|
2019-05-09 06:53:49 +05:30
|
|
|
use Ely\Mojang\Api as MojangApi;
|
|
|
|
use Ely\Mojang\Exception\NoContentException;
|
|
|
|
use Ely\Mojang\Response\ProfileInfo;
|
|
|
|
use GuzzleHttp\Psr7\Request;
|
|
|
|
use GuzzleHttp\Psr7\Response;
|
|
|
|
use Yii;
|
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
|
|
|
|
2019-05-09 06:53:49 +05:30
|
|
|
/** @var \PHPUnit\Framework\MockObject\Builder\InvocationMocker */
|
|
|
|
private $mockedMethod;
|
2016-10-31 05:17:18 +05:30
|
|
|
|
2019-05-14 04:28:29 +05:30
|
|
|
public function _fixtures(): array {
|
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
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function _before(): void {
|
2016-10-31 05:17:18 +05:30
|
|
|
parent::_before();
|
|
|
|
|
2019-05-09 06:53:49 +05:30
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject|MojangApi $mockApi */
|
|
|
|
$mockApi = $this->createMock(MojangApi::class);
|
|
|
|
$this->mockedMethod = $mockApi->method('usernameToUUID');
|
|
|
|
|
2019-05-13 23:20:15 +05:30
|
|
|
Yii::$container->set(MojangApi::class, $mockApi);
|
2018-07-08 20:50:19 +05:30
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testCreateFromAccount(): void {
|
2018-07-08 20:50:19 +05:30
|
|
|
$account = new Account();
|
|
|
|
$account->username = 'find-me';
|
|
|
|
$result = PullMojangUsername::createFromAccount($account);
|
|
|
|
$this->assertSame('find-me', $result->username);
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testExecuteUsernameExists(): void {
|
2019-05-09 06:53:49 +05:30
|
|
|
$this->mockedMethod->willReturn(new ProfileInfo('069a79f444e94726a5befca90e38aaf5', 'Notch'));
|
2018-07-08 20:50:19 +05:30
|
|
|
|
2019-08-02 18:27:17 +05:30
|
|
|
/** @var MojangUsername $mojangUsernameFixture */
|
2018-07-08 20:50:19 +05:30
|
|
|
$mojangUsernameFixture = $this->tester->grabFixture('mojangUsernames', 'Notch');
|
2019-05-09 06:53:49 +05:30
|
|
|
$task = new PullMojangUsername();
|
|
|
|
$task->username = 'Notch';
|
2019-12-14 02:46:05 +05:30
|
|
|
$task->execute($this->createMock(Queue::class));
|
2018-07-08 20:50:19 +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);
|
2016-10-31 05:17:18 +05:30
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testExecuteChangedUsernameExists(): void {
|
2019-05-09 06:53:49 +05:30
|
|
|
$this->mockedMethod->willReturn(new ProfileInfo('069a79f444e94726a5befca90e38aaf5', 'Notch'));
|
2016-10-31 05:17:18 +05:30
|
|
|
|
|
|
|
/** @var MojangUsername $mojangUsernameFixture */
|
|
|
|
$mojangUsernameFixture = $this->tester->grabFixture('mojangUsernames', 'Notch');
|
2019-05-09 06:53:49 +05:30
|
|
|
$task = new PullMojangUsername();
|
|
|
|
$task->username = 'Notch';
|
2019-12-14 02:46:05 +05:30
|
|
|
$task->execute($this->createMock(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);
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testExecuteChangedUsernameNotExists(): void {
|
2019-05-09 06:53:49 +05:30
|
|
|
$this->mockedMethod->willReturn(new ProfileInfo('607153852b8c4909811f507ed8ee737f', 'Chest'));
|
2016-10-31 05:17:18 +05:30
|
|
|
|
2019-05-09 06:53:49 +05:30
|
|
|
$task = new PullMojangUsername();
|
|
|
|
$task->username = 'Chest';
|
2019-12-14 02:46:05 +05:30
|
|
|
$task->execute($this->createMock(Queue::class));
|
2016-10-31 05:17:18 +05:30
|
|
|
/** @var MojangUsername|null $mojangUsername */
|
|
|
|
$mojangUsername = MojangUsername::findOne('Chest');
|
|
|
|
$this->assertInstanceOf(MojangUsername::class, $mojangUsername);
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testExecuteRemoveIfExistsNoMore(): void {
|
2019-08-02 18:27:17 +05:30
|
|
|
$this->mockedMethod->willThrowException(new NoContentException(new Request('GET', ''), new Response()));
|
2016-10-31 05:17:18 +05:30
|
|
|
|
|
|
|
$username = $this->tester->grabFixture('mojangUsernames', 'not-exists')['username'];
|
2019-05-09 06:53:49 +05:30
|
|
|
$task = new PullMojangUsername();
|
|
|
|
$task->username = $username;
|
2019-12-14 02:46:05 +05:30
|
|
|
$task->execute($this->createMock(Queue::class));
|
2016-10-31 05:17:18 +05:30
|
|
|
/** @var MojangUsername|null $mojangUsername */
|
|
|
|
$mojangUsername = MojangUsername::findOne($username);
|
|
|
|
$this->assertNull($mojangUsername);
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testExecuteUuidUpdated(): void {
|
2019-05-09 06:53:49 +05:30
|
|
|
$this->mockedMethod->willReturn(new ProfileInfo('f498513ce8c84773be26ecfc7ed5185d', 'jeb'));
|
2016-10-31 05:17:18 +05:30
|
|
|
|
|
|
|
/** @var MojangUsername $mojangInfo */
|
|
|
|
$mojangInfo = $this->tester->grabFixture('mojangUsernames', 'uuid-changed');
|
|
|
|
$username = $mojangInfo['username'];
|
2019-05-09 06:53:49 +05:30
|
|
|
$task = new PullMojangUsername();
|
|
|
|
$task->username = $username;
|
2019-12-14 02:46:05 +05:30
|
|
|
$task->execute($this->createMock(Queue::class));
|
2016-10-31 05:17:18 +05:30
|
|
|
/** @var MojangUsername|null $mojangUsername */
|
|
|
|
$mojangUsername = MojangUsername::findOne($username);
|
|
|
|
$this->assertInstanceOf(MojangUsername::class, $mojangUsername);
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertNotSame($mojangInfo->uuid, $mojangUsername->uuid);
|
2016-04-24 00:14:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|