diff --git a/console/controllers/AccountQueueController.php b/console/controllers/AccountQueueController.php index 72e7bcb..2036e19 100644 --- a/console/controllers/AccountQueueController.php +++ b/console/controllers/AccountQueueController.php @@ -37,7 +37,7 @@ class AccountQueueController extends AmqpController { } public function routeUsernameChanged(UsernameChanged $body) { - $mojangApi = new MojangApi(); + $mojangApi = $this->createMojangApi(); try { $response = $mojangApi->usernameToUUID($body->newUsername); } catch (NoContentException $e) { @@ -68,4 +68,11 @@ class AccountQueueController extends AmqpController { return true; } + /** + * @return MojangApi + */ + protected function createMojangApi() : MojangApi { + return new MojangApi(); + } + } diff --git a/tests/codeception/console/unit.suite.yml b/tests/codeception/console/unit.suite.yml index a0582a5..3ac6f10 100644 --- a/tests/codeception/console/unit.suite.yml +++ b/tests/codeception/console/unit.suite.yml @@ -1,6 +1,8 @@ -# Codeception Test Suite Configuration - -# suite for unit (internal) tests. -# RUN `build` COMMAND AFTER ADDING/REMOVING MODULES. - class_name: UnitTester +modules: + enabled: + - Yii2: + part: [orm, email, fixtures] + config: + Yii2: + configFile: '../config/console/unit.php' diff --git a/tests/codeception/console/unit/DbTestCase.php b/tests/codeception/console/unit/DbTestCase.php deleted file mode 100644 index a841790..0000000 --- a/tests/codeception/console/unit/DbTestCase.php +++ /dev/null @@ -1,8 +0,0 @@ - [ - 'class' => AccountFixture::class, - 'dataFile' => '@tests/codeception/common/fixtures/data/accounts.php', - ], - 'mojangUsernames' => [ - 'class' => MojangUsernameFixture::class, - 'dataFile' => '@tests/codeception/common/fixtures/data/mojang-usernames.php', - ], + 'accounts' => AccountFixture::class, + 'mojangUsernames' => MojangUsernameFixture::class, ]; } - public function testRouteUsernameChanged() { - // TODO: пропустить тест, если у нас нету интернета - $controller = new AccountQueueController('account-queue', Yii::$app); - $this->specify('Update last_pulled_at time if username exists', function() use ($controller) { - $accountInfo = $this->accounts['admin']; - $body = new UsernameChanged([ - 'accountId' => $accountInfo['id'], - 'oldUsername' => $accountInfo['username'], - 'newUsername' => 'Notch', - ]); - $controller->routeUsernameChanged($body); - /** @var MojangUsername|null $mojangUsername */ - $mojangUsername = MojangUsername::findOne('Notch'); - expect($mojangUsername)->isInstanceOf(MojangUsername::class); - expect($mojangUsername->last_pulled_at)->greaterThan($this->mojangUsernames['Notch']['last_pulled_at']); - expect($mojangUsername->last_pulled_at)->lessOrEquals(time()); - }); + public function _before() { + parent::_before(); - $this->specify('Add new MojangUsername if don\'t exists', function() use ($controller) { - $accountInfo = $this->accounts['admin']; - $body = new UsernameChanged([ - 'accountId' => $accountInfo['id'], - 'oldUsername' => $accountInfo['username'], - 'newUsername' => 'Chest', - ]); - $controller->routeUsernameChanged($body); - /** @var MojangUsername|null $mojangUsername */ - $mojangUsername = MojangUsername::findOne('Chest'); - expect($mojangUsername)->isInstanceOf(MojangUsername::class); - }); + /** @var AccountQueueController|\PHPUnit_Framework_MockObject_MockObject $controller */ + $controller = $this->getMockBuilder(AccountQueueController::class) + ->setMethods(['createMojangApi']) + ->setConstructorArgs(['account-queue', Yii::$app]) + ->getMock(); - $this->specify('Remove MojangUsername, if now it\'s does\'t exists', function() use ($controller) { - $accountInfo = $this->accounts['admin']; - $username = $this->mojangUsernames['not-exists']['username']; - $body = new UsernameChanged([ - 'accountId' => $accountInfo['id'], - 'oldUsername' => $accountInfo['username'], - 'newUsername' => $username, - ]); - $controller->routeUsernameChanged($body); - /** @var MojangUsername|null $mojangUsername */ - $mojangUsername = MojangUsername::findOne($username); - expect($mojangUsername)->null(); - }); + /** @var Api|\PHPUnit_Framework_MockObject_MockObject $apiMock */ + $apiMock = $this->getMockBuilder(Api::class) + ->setMethods(['usernameToUUID']) + ->getMock(); - $this->specify('Update uuid if username for now owned by other player', function() use ($controller) { - $accountInfo = $this->accounts['admin']; - $mojangInfo = $this->mojangUsernames['uuid-changed']; - $username = $mojangInfo['username']; - $body = new UsernameChanged([ - 'accountId' => $accountInfo['id'], - 'oldUsername' => $accountInfo['username'], - 'newUsername' => $username, - ]); - $controller->routeUsernameChanged($body); - /** @var MojangUsername|null $mojangUsername */ - $mojangUsername = MojangUsername::findOne($username); - expect($mojangUsername)->isInstanceOf(MojangUsername::class); - expect($mojangUsername->uuid)->notEquals($mojangInfo['uuid']); - }); + $apiMock + ->expects($this->any()) + ->method('usernameToUUID') + ->willReturnCallback(function() { + if ($this->expectedResponse === false) { + throw new NoContentException(); + } else { + return $this->expectedResponse; + } + }); + + $controller + ->expects($this->any()) + ->method('createMojangApi') + ->willReturn($apiMock); + + $this->controller = $controller; + } + + public function testRouteUsernameChangedUsernameExists() { + $expectedResponse = new UsernameToUUIDResponse(); + $expectedResponse->id = '069a79f444e94726a5befca90e38aaf5'; + $expectedResponse->name = 'Notch'; + $this->expectedResponse = $expectedResponse; + + /** @var \common\models\Account $accountInfo */ + $accountInfo = $this->tester->grabFixture('accounts', 'admin'); + /** @var MojangUsername $mojangUsernameFixture */ + $mojangUsernameFixture = $this->tester->grabFixture('mojangUsernames', 'Notch'); + $body = new UsernameChanged([ + 'accountId' => $accountInfo->id, + 'oldUsername' => $accountInfo->username, + 'newUsername' => 'Notch', + ]); + $this->controller->routeUsernameChanged($body); + /** @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); + } + + public function testRouteUsernameChangedUsernameNotExists() { + $expectedResponse = new UsernameToUUIDResponse(); + $expectedResponse->id = '607153852b8c4909811f507ed8ee737f'; + $expectedResponse->name = 'Chest'; + $this->expectedResponse = $expectedResponse; + + /** @var \common\models\Account $accountInfo */ + $accountInfo = $this->tester->grabFixture('accounts', 'admin'); + $body = new UsernameChanged([ + 'accountId' => $accountInfo['id'], + 'oldUsername' => $accountInfo['username'], + 'newUsername' => 'Chest', + ]); + $this->controller->routeUsernameChanged($body); + /** @var MojangUsername|null $mojangUsername */ + $mojangUsername = MojangUsername::findOne('Chest'); + $this->assertInstanceOf(MojangUsername::class, $mojangUsername); + } + + public function testRouteUsernameChangedRemoveIfExistsNoMore() { + $this->expectedResponse = false; + + /** @var \common\models\Account $accountInfo */ + $accountInfo = $this->tester->grabFixture('accounts', 'admin'); + $username = $this->tester->grabFixture('mojangUsernames', 'not-exists')['username']; + $body = new UsernameChanged([ + 'accountId' => $accountInfo['id'], + 'oldUsername' => $accountInfo['username'], + 'newUsername' => $username, + ]); + $this->controller->routeUsernameChanged($body); + /** @var MojangUsername|null $mojangUsername */ + $mojangUsername = MojangUsername::findOne($username); + $this->assertNull($mojangUsername); + } + + public function testRouteUsernameChangedUuidUpdated() { + $expectedResponse = new UsernameToUUIDResponse(); + $expectedResponse->id = 'f498513ce8c84773be26ecfc7ed5185d'; + $expectedResponse->name = 'jeb'; + $this->expectedResponse = $expectedResponse; + + /** @var \common\models\Account $accountInfo */ + $accountInfo = $this->tester->grabFixture('accounts', 'admin'); + /** @var MojangUsername $mojangInfo */ + $mojangInfo = $this->tester->grabFixture('mojangUsernames', 'uuid-changed'); + $username = $mojangInfo['username']; + $body = new UsernameChanged([ + 'accountId' => $accountInfo['id'], + 'oldUsername' => $accountInfo['username'], + 'newUsername' => $username, + ]); + $this->controller->routeUsernameChanged($body); + /** @var MojangUsername|null $mojangUsername */ + $mojangUsername = MojangUsername::findOne($username); + $this->assertInstanceOf(MojangUsername::class, $mojangUsername); + $this->assertNotEquals($mojangInfo->uuid, $mojangUsername->uuid); } } diff --git a/tests/codeception/console/unit/fixtures/data/.gitkeep b/tests/codeception/console/unit/fixtures/data/.gitkeep deleted file mode 100644 index e69de29..0000000