mirror of
				https://github.com/elyby/accounts.git
				synced 2025-05-31 14:11:46 +05:30 
			
		
		
		
	Переписаны тесты для AccountQueueController, чтобы они не делали запросов в сеть
Забыл комитнуть изменения для консольных тестов
This commit is contained in:
		@@ -37,7 +37,7 @@ class AccountQueueController extends AmqpController {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function routeUsernameChanged(UsernameChanged $body) {
 | 
					    public function routeUsernameChanged(UsernameChanged $body) {
 | 
				
			||||||
        $mojangApi = new MojangApi();
 | 
					        $mojangApi = $this->createMojangApi();
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
            $response = $mojangApi->usernameToUUID($body->newUsername);
 | 
					            $response = $mojangApi->usernameToUUID($body->newUsername);
 | 
				
			||||||
        } catch (NoContentException $e) {
 | 
					        } catch (NoContentException $e) {
 | 
				
			||||||
@@ -68,4 +68,11 @@ class AccountQueueController extends AmqpController {
 | 
				
			|||||||
        return true;
 | 
					        return true;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * @return MojangApi
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    protected function createMojangApi() : MojangApi {
 | 
				
			||||||
 | 
					        return new MojangApi();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,8 @@
 | 
				
			|||||||
# Codeception Test Suite Configuration
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# suite for unit (internal) tests.
 | 
					 | 
				
			||||||
# RUN `build` COMMAND AFTER ADDING/REMOVING MODULES.
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
class_name: UnitTester
 | 
					class_name: UnitTester
 | 
				
			||||||
 | 
					modules:
 | 
				
			||||||
 | 
					    enabled:
 | 
				
			||||||
 | 
					        - Yii2:
 | 
				
			||||||
 | 
					            part: [orm, email, fixtures]
 | 
				
			||||||
 | 
					    config:
 | 
				
			||||||
 | 
					        Yii2:
 | 
				
			||||||
 | 
					            configFile: '../config/console/unit.php'
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,8 +0,0 @@
 | 
				
			|||||||
<?php
 | 
					 | 
				
			||||||
namespace tests\codeception\console\unit;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
class DbTestCase extends \yii\codeception\DbTestCase {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public $appConfig = '@tests/codeception/config/console/unit.php';
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@@ -1,11 +1,24 @@
 | 
				
			|||||||
<?php
 | 
					<?php
 | 
				
			||||||
 | 
					 | 
				
			||||||
namespace tests\codeception\console\unit;
 | 
					namespace tests\codeception\console\unit;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use Codeception\Test\Unit;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class TestCase extends Unit {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
 * @inheritdoc
 | 
					     * @var \tests\codeception\console\UnitTester
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
class TestCase extends \yii\codeception\TestCase
 | 
					    protected $tester;
 | 
				
			||||||
{
 | 
					
 | 
				
			||||||
    public $appConfig = '@tests/codeception/config/console/config.php';
 | 
					    /**
 | 
				
			||||||
 | 
					     * Список фикстур, что будут загружены перед тестом, но после зачистки базы данных
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @url http://codeception.com/docs/modules/Yii2#fixtures
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @return array
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public function _fixtures() {
 | 
				
			||||||
 | 
					        return [];
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,95 +1,146 @@
 | 
				
			|||||||
<?php
 | 
					<?php
 | 
				
			||||||
namespace codeception\console\unit\controllers;
 | 
					namespace codeception\console\unit\controllers;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use Codeception\Specify;
 | 
					use common\components\Mojang\Api;
 | 
				
			||||||
 | 
					use common\components\Mojang\exceptions\NoContentException;
 | 
				
			||||||
 | 
					use common\components\Mojang\response\UsernameToUUIDResponse;
 | 
				
			||||||
use common\models\amqp\UsernameChanged;
 | 
					use common\models\amqp\UsernameChanged;
 | 
				
			||||||
use common\models\MojangUsername;
 | 
					use common\models\MojangUsername;
 | 
				
			||||||
use console\controllers\AccountQueueController;
 | 
					use console\controllers\AccountQueueController;
 | 
				
			||||||
use tests\codeception\common\fixtures\AccountFixture;
 | 
					use tests\codeception\common\fixtures\AccountFixture;
 | 
				
			||||||
use tests\codeception\common\fixtures\MojangUsernameFixture;
 | 
					use tests\codeception\common\fixtures\MojangUsernameFixture;
 | 
				
			||||||
use tests\codeception\console\unit\DbTestCase;
 | 
					use tests\codeception\console\unit\TestCase;
 | 
				
			||||||
use Yii;
 | 
					use Yii;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					class AccountQueueControllerTest extends TestCase {
 | 
				
			||||||
 * @property array $accounts
 | 
					 | 
				
			||||||
 * @property array $mojangUsernames
 | 
					 | 
				
			||||||
 */
 | 
					 | 
				
			||||||
class AccountQueueControllerTest extends DbTestCase {
 | 
					 | 
				
			||||||
    use Specify;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function fixtures() {
 | 
					    /**
 | 
				
			||||||
 | 
					     * @var AccountQueueController
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    private $controller;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private $expectedResponse;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function _fixtures() {
 | 
				
			||||||
        return [
 | 
					        return [
 | 
				
			||||||
            'accounts' => [
 | 
					            'accounts' => AccountFixture::class,
 | 
				
			||||||
                'class' => AccountFixture::class,
 | 
					            'mojangUsernames' => MojangUsernameFixture::class,
 | 
				
			||||||
                'dataFile' => '@tests/codeception/common/fixtures/data/accounts.php',
 | 
					 | 
				
			||||||
            ],
 | 
					 | 
				
			||||||
            'mojangUsernames' => [
 | 
					 | 
				
			||||||
                'class' => MojangUsernameFixture::class,
 | 
					 | 
				
			||||||
                'dataFile' => '@tests/codeception/common/fixtures/data/mojang-usernames.php',
 | 
					 | 
				
			||||||
            ],
 | 
					 | 
				
			||||||
        ];
 | 
					        ];
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function testRouteUsernameChanged() {
 | 
					    public function _before() {
 | 
				
			||||||
        // TODO: пропустить тест, если у нас нету интернета
 | 
					        parent::_before();
 | 
				
			||||||
        $controller = new AccountQueueController('account-queue', Yii::$app);
 | 
					
 | 
				
			||||||
        $this->specify('Update last_pulled_at time if username exists', function() use ($controller) {
 | 
					        /** @var AccountQueueController|\PHPUnit_Framework_MockObject_MockObject $controller */
 | 
				
			||||||
            $accountInfo = $this->accounts['admin'];
 | 
					        $controller = $this->getMockBuilder(AccountQueueController::class)
 | 
				
			||||||
            $body = new UsernameChanged([
 | 
					            ->setMethods(['createMojangApi'])
 | 
				
			||||||
                'accountId' => $accountInfo['id'],
 | 
					            ->setConstructorArgs(['account-queue', Yii::$app])
 | 
				
			||||||
                'oldUsername' => $accountInfo['username'],
 | 
					            ->getMock();
 | 
				
			||||||
                'newUsername' => 'Notch',
 | 
					
 | 
				
			||||||
            ]);
 | 
					        /** @var Api|\PHPUnit_Framework_MockObject_MockObject $apiMock */
 | 
				
			||||||
            $controller->routeUsernameChanged($body);
 | 
					        $apiMock = $this->getMockBuilder(Api::class)
 | 
				
			||||||
            /** @var MojangUsername|null $mojangUsername */
 | 
					            ->setMethods(['usernameToUUID'])
 | 
				
			||||||
            $mojangUsername = MojangUsername::findOne('Notch');
 | 
					            ->getMock();
 | 
				
			||||||
            expect($mojangUsername)->isInstanceOf(MojangUsername::class);
 | 
					
 | 
				
			||||||
            expect($mojangUsername->last_pulled_at)->greaterThan($this->mojangUsernames['Notch']['last_pulled_at']);
 | 
					        $apiMock
 | 
				
			||||||
            expect($mojangUsername->last_pulled_at)->lessOrEquals(time());
 | 
					            ->expects($this->any())
 | 
				
			||||||
 | 
					            ->method('usernameToUUID')
 | 
				
			||||||
 | 
					            ->willReturnCallback(function() {
 | 
				
			||||||
 | 
					                if ($this->expectedResponse === false) {
 | 
				
			||||||
 | 
					                    throw new NoContentException();
 | 
				
			||||||
 | 
					                } else {
 | 
				
			||||||
 | 
					                    return $this->expectedResponse;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
            });
 | 
					            });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $this->specify('Add new MojangUsername if don\'t exists', function() use ($controller) {
 | 
					        $controller
 | 
				
			||||||
            $accountInfo = $this->accounts['admin'];
 | 
					            ->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([
 | 
					        $body = new UsernameChanged([
 | 
				
			||||||
            'accountId' => $accountInfo['id'],
 | 
					            'accountId' => $accountInfo['id'],
 | 
				
			||||||
            'oldUsername' => $accountInfo['username'],
 | 
					            'oldUsername' => $accountInfo['username'],
 | 
				
			||||||
            'newUsername' => 'Chest',
 | 
					            'newUsername' => 'Chest',
 | 
				
			||||||
        ]);
 | 
					        ]);
 | 
				
			||||||
            $controller->routeUsernameChanged($body);
 | 
					        $this->controller->routeUsernameChanged($body);
 | 
				
			||||||
        /** @var MojangUsername|null $mojangUsername */
 | 
					        /** @var MojangUsername|null $mojangUsername */
 | 
				
			||||||
        $mojangUsername = MojangUsername::findOne('Chest');
 | 
					        $mojangUsername = MojangUsername::findOne('Chest');
 | 
				
			||||||
            expect($mojangUsername)->isInstanceOf(MojangUsername::class);
 | 
					        $this->assertInstanceOf(MojangUsername::class, $mojangUsername);
 | 
				
			||||||
        });
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $this->specify('Remove MojangUsername, if now it\'s does\'t exists', function() use ($controller) {
 | 
					    public function testRouteUsernameChangedRemoveIfExistsNoMore() {
 | 
				
			||||||
            $accountInfo = $this->accounts['admin'];
 | 
					        $this->expectedResponse = false;
 | 
				
			||||||
            $username = $this->mojangUsernames['not-exists']['username'];
 | 
					
 | 
				
			||||||
 | 
					        /** @var \common\models\Account $accountInfo */
 | 
				
			||||||
 | 
					        $accountInfo = $this->tester->grabFixture('accounts', 'admin');
 | 
				
			||||||
 | 
					        $username = $this->tester->grabFixture('mojangUsernames', 'not-exists')['username'];
 | 
				
			||||||
        $body = new UsernameChanged([
 | 
					        $body = new UsernameChanged([
 | 
				
			||||||
            'accountId' => $accountInfo['id'],
 | 
					            'accountId' => $accountInfo['id'],
 | 
				
			||||||
            'oldUsername' => $accountInfo['username'],
 | 
					            'oldUsername' => $accountInfo['username'],
 | 
				
			||||||
            'newUsername' => $username,
 | 
					            'newUsername' => $username,
 | 
				
			||||||
        ]);
 | 
					        ]);
 | 
				
			||||||
            $controller->routeUsernameChanged($body);
 | 
					        $this->controller->routeUsernameChanged($body);
 | 
				
			||||||
        /** @var MojangUsername|null $mojangUsername */
 | 
					        /** @var MojangUsername|null $mojangUsername */
 | 
				
			||||||
        $mojangUsername = MojangUsername::findOne($username);
 | 
					        $mojangUsername = MojangUsername::findOne($username);
 | 
				
			||||||
            expect($mojangUsername)->null();
 | 
					        $this->assertNull($mojangUsername);
 | 
				
			||||||
        });
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $this->specify('Update uuid if username for now owned by other player', function() use ($controller) {
 | 
					    public function testRouteUsernameChangedUuidUpdated() {
 | 
				
			||||||
            $accountInfo = $this->accounts['admin'];
 | 
					        $expectedResponse = new UsernameToUUIDResponse();
 | 
				
			||||||
            $mojangInfo = $this->mojangUsernames['uuid-changed'];
 | 
					        $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'];
 | 
					        $username = $mojangInfo['username'];
 | 
				
			||||||
        $body = new UsernameChanged([
 | 
					        $body = new UsernameChanged([
 | 
				
			||||||
            'accountId' => $accountInfo['id'],
 | 
					            'accountId' => $accountInfo['id'],
 | 
				
			||||||
            'oldUsername' => $accountInfo['username'],
 | 
					            'oldUsername' => $accountInfo['username'],
 | 
				
			||||||
            'newUsername' => $username,
 | 
					            'newUsername' => $username,
 | 
				
			||||||
        ]);
 | 
					        ]);
 | 
				
			||||||
            $controller->routeUsernameChanged($body);
 | 
					        $this->controller->routeUsernameChanged($body);
 | 
				
			||||||
        /** @var MojangUsername|null $mojangUsername */
 | 
					        /** @var MojangUsername|null $mojangUsername */
 | 
				
			||||||
        $mojangUsername = MojangUsername::findOne($username);
 | 
					        $mojangUsername = MojangUsername::findOne($username);
 | 
				
			||||||
            expect($mojangUsername)->isInstanceOf(MojangUsername::class);
 | 
					        $this->assertInstanceOf(MojangUsername::class, $mojangUsername);
 | 
				
			||||||
            expect($mojangUsername->uuid)->notEquals($mojangInfo['uuid']);
 | 
					        $this->assertNotEquals($mojangInfo->uuid, $mojangUsername->uuid);
 | 
				
			||||||
        });
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user