mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Get rid of mockery library. Still have some troubles with functional tests for api
This commit is contained in:
@@ -1,31 +0,0 @@
|
||||
<?php
|
||||
namespace common\tests\_support;
|
||||
|
||||
use Codeception\Module;
|
||||
use Codeception\TestInterface;
|
||||
|
||||
class Mockery extends Module {
|
||||
|
||||
/**
|
||||
* @var bool Run mockery expectations after test or not
|
||||
*/
|
||||
private $assert_mocks = true;
|
||||
|
||||
public function _before(TestInterface $test) {
|
||||
\Mockery::globalHelpers();
|
||||
}
|
||||
|
||||
public function _after(TestInterface $test) {
|
||||
if ($this->assert_mocks) {
|
||||
\Mockery::close();
|
||||
} else {
|
||||
\Mockery::getContainer()->mockery_close();
|
||||
\Mockery::resetContainer();
|
||||
}
|
||||
}
|
||||
|
||||
public function _failed(TestInterface $test, $fail) {
|
||||
$this->assert_mocks = false;
|
||||
}
|
||||
|
||||
}
|
28
common/tests/helpers/ExtendedPHPMock.php
Normal file
28
common/tests/helpers/ExtendedPHPMock.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace common\tests\helpers;
|
||||
|
||||
use phpmock\phpunit\PHPMock;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use ReflectionClass;
|
||||
|
||||
trait ExtendedPHPMock {
|
||||
use PHPMock {
|
||||
getFunctionMock as private getOriginalFunctionMock;
|
||||
defineFunctionMock as private defineOriginalFunctionMock;
|
||||
}
|
||||
|
||||
public function getFunctionMock($namespace, $name): MockObject {
|
||||
return $this->getOriginalFunctionMock(static::getClassNamespace($namespace), $name);
|
||||
}
|
||||
|
||||
public static function defineFunctionMock($namespace, $name) {
|
||||
static::defineOriginalFunctionMock(static::getClassNamespace($namespace), $name);
|
||||
}
|
||||
|
||||
private static function getClassNamespace(string $className): string {
|
||||
return (new ReflectionClass($className))->getNamespaceName();
|
||||
}
|
||||
|
||||
}
|
@@ -6,4 +6,3 @@ modules:
|
||||
part: [orm, email, fixtures]
|
||||
configFile: tests/config/unit.php
|
||||
- common\tests\_support\queue\CodeceptionQueueHelper
|
||||
- common\tests\_support\Mockery
|
||||
|
@@ -4,19 +4,13 @@ declare(strict_types=1);
|
||||
namespace common\tests\unit;
|
||||
|
||||
use Codeception\Test\Unit;
|
||||
use Mockery;
|
||||
use common\tests\helpers\ExtendedPHPMock;
|
||||
|
||||
/**
|
||||
* @property \common\tests\UnitTester $tester
|
||||
*/
|
||||
class TestCase extends Unit {
|
||||
|
||||
/**
|
||||
* @var \common\tests\UnitTester
|
||||
*/
|
||||
protected $tester;
|
||||
|
||||
protected function tearDown(): void {
|
||||
parent::tearDown();
|
||||
Mockery::close();
|
||||
}
|
||||
use ExtendedPHPMock;
|
||||
|
||||
/**
|
||||
* A list of fixtures that will be loaded before the test, but after the database is cleaned up
|
||||
|
@@ -35,7 +35,7 @@ class ClearAccountSessionsTest extends TestCase {
|
||||
$bannedAccount = $this->tester->grabFixture('accounts', 'banned-account');
|
||||
$task = new ClearAccountSessions();
|
||||
$task->accountId = $bannedAccount->id;
|
||||
$task->execute(mock(Queue::class));
|
||||
$task->execute($this->createMock(Queue::class));
|
||||
$this->assertEmpty($bannedAccount->sessions);
|
||||
$this->assertEmpty($bannedAccount->minecraftAccessKeys);
|
||||
$this->assertEmpty($bannedAccount->oauthSessions);
|
||||
|
@@ -36,20 +36,20 @@ class ClearOauthSessionsTest extends TestCase {
|
||||
$task = new ClearOauthSessions();
|
||||
$task->clientId = 'deleted-oauth-client-with-sessions';
|
||||
$task->notSince = 1519510065;
|
||||
$task->execute(mock(Queue::class));
|
||||
$task->execute($this->createMock(Queue::class));
|
||||
|
||||
$this->assertFalse(OauthSession::find()->andWhere(['legacy_id' => 3])->exists());
|
||||
$this->assertTrue(OauthSession::find()->andWhere(['legacy_id' => 4])->exists());
|
||||
|
||||
$task = new ClearOauthSessions();
|
||||
$task->clientId = 'deleted-oauth-client-with-sessions';
|
||||
$task->execute(mock(Queue::class));
|
||||
$task->execute($this->createMock(Queue::class));
|
||||
|
||||
$this->assertFalse(OauthSession::find()->andWhere(['legacy_id' => 4])->exists());
|
||||
|
||||
$task = new ClearOauthSessions();
|
||||
$task->clientId = 'some-not-exists-client-id';
|
||||
$task->execute(mock(Queue::class));
|
||||
$task->execute($this->createMock(Queue::class));
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -69,7 +69,7 @@ class CreateWebHooksDeliveriesTest extends TestCase {
|
||||
'status' => 0,
|
||||
],
|
||||
];
|
||||
$task->execute(mock(Queue::class));
|
||||
$task->execute($this->createMock(Queue::class));
|
||||
/** @var DeliveryWebHook[] $tasks */
|
||||
$tasks = $this->tester->grabQueueJobs();
|
||||
$this->assertCount(2, $tasks);
|
||||
|
@@ -45,7 +45,7 @@ class DeliveryWebHookTest extends TestCase {
|
||||
'key' => 'value',
|
||||
'another' => 'value',
|
||||
];
|
||||
$task->execute(mock(Queue::class));
|
||||
$task->execute($this->createMock(Queue::class));
|
||||
/** @var Request $request */
|
||||
$request = $this->historyContainer[0]['request'];
|
||||
$this->assertSame('http://localhost:81/webhooks/ely', (string)$request->getUri());
|
||||
@@ -66,7 +66,7 @@ class DeliveryWebHookTest extends TestCase {
|
||||
'key' => 'value',
|
||||
'another' => 'value',
|
||||
];
|
||||
$task->execute(mock(Queue::class));
|
||||
$task->execute($this->createMock(Queue::class));
|
||||
/** @var Request $request */
|
||||
$request = $this->historyContainer[0]['request'];
|
||||
$this->assertSame('http://localhost:81/webhooks/ely', (string)$request->getUri());
|
||||
@@ -87,7 +87,7 @@ class DeliveryWebHookTest extends TestCase {
|
||||
'key' => 'value',
|
||||
'another' => 'value',
|
||||
];
|
||||
$task->execute(mock(Queue::class));
|
||||
$task->execute($this->createMock(Queue::class));
|
||||
}
|
||||
|
||||
public function testExecuteUnhandledException() {
|
||||
@@ -102,7 +102,7 @@ class DeliveryWebHookTest extends TestCase {
|
||||
'key' => 'value',
|
||||
'another' => 'value',
|
||||
];
|
||||
$task->execute(mock(Queue::class));
|
||||
$task->execute($this->createMock(Queue::class));
|
||||
}
|
||||
|
||||
private function createMockedTask(): DeliveryWebHook {
|
||||
|
@@ -54,7 +54,7 @@ class PullMojangUsernameTest extends TestCase {
|
||||
$mojangUsernameFixture = $this->tester->grabFixture('mojangUsernames', 'Notch');
|
||||
$task = new PullMojangUsername();
|
||||
$task->username = 'Notch';
|
||||
$task->execute(mock(Queue::class));
|
||||
$task->execute($this->createMock(Queue::class));
|
||||
/** @var MojangUsername|null $mojangUsername */
|
||||
$mojangUsername = MojangUsername::findOne('Notch');
|
||||
$this->assertInstanceOf(MojangUsername::class, $mojangUsername);
|
||||
@@ -69,7 +69,7 @@ class PullMojangUsernameTest extends TestCase {
|
||||
$mojangUsernameFixture = $this->tester->grabFixture('mojangUsernames', 'Notch');
|
||||
$task = new PullMojangUsername();
|
||||
$task->username = 'Notch';
|
||||
$task->execute(mock(Queue::class));
|
||||
$task->execute($this->createMock(Queue::class));
|
||||
/** @var MojangUsername|null $mojangUsername */
|
||||
$mojangUsername = MojangUsername::findOne('Notch');
|
||||
$this->assertInstanceOf(MojangUsername::class, $mojangUsername);
|
||||
@@ -82,7 +82,7 @@ class PullMojangUsernameTest extends TestCase {
|
||||
|
||||
$task = new PullMojangUsername();
|
||||
$task->username = 'Chest';
|
||||
$task->execute(mock(Queue::class));
|
||||
$task->execute($this->createMock(Queue::class));
|
||||
/** @var MojangUsername|null $mojangUsername */
|
||||
$mojangUsername = MojangUsername::findOne('Chest');
|
||||
$this->assertInstanceOf(MojangUsername::class, $mojangUsername);
|
||||
@@ -94,7 +94,7 @@ class PullMojangUsernameTest extends TestCase {
|
||||
$username = $this->tester->grabFixture('mojangUsernames', 'not-exists')['username'];
|
||||
$task = new PullMojangUsername();
|
||||
$task->username = $username;
|
||||
$task->execute(mock(Queue::class));
|
||||
$task->execute($this->createMock(Queue::class));
|
||||
/** @var MojangUsername|null $mojangUsername */
|
||||
$mojangUsername = MojangUsername::findOne($username);
|
||||
$this->assertNull($mojangUsername);
|
||||
@@ -108,7 +108,7 @@ class PullMojangUsernameTest extends TestCase {
|
||||
$username = $mojangInfo['username'];
|
||||
$task = new PullMojangUsername();
|
||||
$task->username = $username;
|
||||
$task->execute(mock(Queue::class));
|
||||
$task->execute($this->createMock(Queue::class));
|
||||
/** @var MojangUsername|null $mojangUsername */
|
||||
$mojangUsername = MojangUsername::findOne($username);
|
||||
$this->assertInstanceOf(MojangUsername::class, $mojangUsername);
|
||||
|
@@ -4,7 +4,6 @@ declare(strict_types=1);
|
||||
namespace common\tests\unit\validators;
|
||||
|
||||
use common\tests\fixtures\AccountFixture;
|
||||
use common\tests\helpers\Mock;
|
||||
use common\tests\unit\TestCase;
|
||||
use common\validators\EmailValidator;
|
||||
use yii\base\Model;
|
||||
@@ -20,15 +19,15 @@ class EmailValidatorTest extends TestCase {
|
||||
public function _before() {
|
||||
parent::_before();
|
||||
|
||||
Mock::define(YiiEmailValidator::class, 'checkdnsrr');
|
||||
Mock::define(YiiEmailValidator::class, 'dns_get_record');
|
||||
self::defineFunctionMock(YiiEmailValidator::class, 'checkdnsrr');
|
||||
self::defineFunctionMock(YiiEmailValidator::class, 'dns_get_record');
|
||||
|
||||
$this->validator = new EmailValidator();
|
||||
}
|
||||
|
||||
public function testValidateTrimming() {
|
||||
// Prevent it to access to db
|
||||
Mock::func(YiiEmailValidator::class, 'checkdnsrr')->andReturn(false);
|
||||
$this->getFunctionMock(YiiEmailValidator::class, 'checkdnsrr')->expects($this->any())->willReturn(false);
|
||||
|
||||
$model = $this->createModel("testemail@ely.by\u{feff}"); // Zero width no-break space (U+FEFF)
|
||||
$this->validator->validateAttribute($model, 'field');
|
||||
@@ -47,7 +46,7 @@ class EmailValidatorTest extends TestCase {
|
||||
}
|
||||
|
||||
public function testValidateAttributeLength() {
|
||||
Mock::func(YiiEmailValidator::class, 'checkdnsrr')->andReturn(false);
|
||||
$this->getFunctionMock(YiiEmailValidator::class, 'checkdnsrr')->expects($this->any())->willReturn(false);
|
||||
|
||||
$model = $this->createModel(
|
||||
'emailemailemailemailemailemailemailemailemailemailemailemailemailemailemailemailemail' .
|
||||
@@ -64,8 +63,8 @@ class EmailValidatorTest extends TestCase {
|
||||
}
|
||||
|
||||
public function testValidateAttributeEmailCaseNotExistsDomain() {
|
||||
Mock::func(YiiEmailValidator::class, 'checkdnsrr')->andReturn(false);
|
||||
Mock::func(YiiEmailValidator::class, 'dns_get_record')->times(0);
|
||||
$this->getFunctionMock(YiiEmailValidator::class, 'checkdnsrr')->expects($this->any())->willReturn(false);
|
||||
$this->getFunctionMock(YiiEmailValidator::class, 'dns_get_record')->expects($this->never());
|
||||
|
||||
$model = $this->createModel('non-email@this-domain-does-not-exists.de');
|
||||
$this->validator->validateAttribute($model, 'field');
|
||||
@@ -73,8 +72,8 @@ class EmailValidatorTest extends TestCase {
|
||||
}
|
||||
|
||||
public function testValidateAttributeEmailCaseExistsDomainButWithoutMXRecord() {
|
||||
Mock::func(YiiEmailValidator::class, 'checkdnsrr')->andReturnValues([false, true]);
|
||||
Mock::func(YiiEmailValidator::class, 'dns_get_record')->andReturn(['127.0.0.1']);
|
||||
$this->getFunctionMock(YiiEmailValidator::class, 'checkdnsrr')->expects($this->exactly(2))->willReturnOnConsecutiveCalls(false, true);
|
||||
$this->getFunctionMock(YiiEmailValidator::class, 'dns_get_record')->expects($this->any())->willReturn(['127.0.0.1']);
|
||||
|
||||
$model = $this->createModel('non-email@this-domain-has-no-mx-record.de');
|
||||
$this->validator->validateAttribute($model, 'field');
|
||||
@@ -82,8 +81,8 @@ class EmailValidatorTest extends TestCase {
|
||||
}
|
||||
|
||||
public function testValidateAttributeEmailCaseExistsDomainWithMXRecord() {
|
||||
Mock::func(YiiEmailValidator::class, 'checkdnsrr')->andReturn(true);
|
||||
Mock::func(YiiEmailValidator::class, 'dns_get_record')->andReturn(['mx.google.com']);
|
||||
$this->getFunctionMock(YiiEmailValidator::class, 'checkdnsrr')->expects($this->any())->willReturn(true);
|
||||
$this->getFunctionMock(YiiEmailValidator::class, 'dns_get_record')->expects($this->any())->willReturn(['mx.google.com']);
|
||||
|
||||
$model = $this->createModel('valid-email@gmail.com');
|
||||
$this->validator->validateAttribute($model, 'field');
|
||||
@@ -91,8 +90,8 @@ class EmailValidatorTest extends TestCase {
|
||||
}
|
||||
|
||||
public function testValidateAttributeTempmail() {
|
||||
Mock::func(YiiEmailValidator::class, 'checkdnsrr')->andReturn(true);
|
||||
Mock::func(YiiEmailValidator::class, 'dns_get_record')->andReturn(['127.0.0.1']);
|
||||
$this->getFunctionMock(YiiEmailValidator::class, 'checkdnsrr')->expects($this->any())->willReturn(true);
|
||||
$this->getFunctionMock(YiiEmailValidator::class, 'dns_get_record')->expects($this->any())->willReturn(['127.0.0.1']);
|
||||
|
||||
$model = $this->createModel('ibrpycwyjdnt@dropmail.me');
|
||||
$this->validator->validateAttribute($model, 'field');
|
||||
@@ -104,8 +103,8 @@ class EmailValidatorTest extends TestCase {
|
||||
}
|
||||
|
||||
public function testValidateAttributeIdna() {
|
||||
Mock::func(YiiEmailValidator::class, 'checkdnsrr')->andReturn(true);
|
||||
Mock::func(YiiEmailValidator::class, 'dns_get_record')->andReturn(['127.0.0.1']);
|
||||
$this->getFunctionMock(YiiEmailValidator::class, 'checkdnsrr')->expects($this->any())->willReturn(true);
|
||||
$this->getFunctionMock(YiiEmailValidator::class, 'dns_get_record')->expects($this->any())->willReturn(['127.0.0.1']);
|
||||
|
||||
$model = $this->createModel('qdushyantasunassm@❕.gq');
|
||||
$this->validator->validateAttribute($model, 'field');
|
||||
@@ -117,8 +116,8 @@ class EmailValidatorTest extends TestCase {
|
||||
}
|
||||
|
||||
public function testValidateAttributeUnique() {
|
||||
Mock::func(YiiEmailValidator::class, 'checkdnsrr')->andReturn(true);
|
||||
Mock::func(YiiEmailValidator::class, 'dns_get_record')->andReturn(['127.0.0.1']);
|
||||
$this->getFunctionMock(YiiEmailValidator::class, 'checkdnsrr')->expects($this->any())->willReturn(true);
|
||||
$this->getFunctionMock(YiiEmailValidator::class, 'dns_get_record')->expects($this->any())->willReturn(['127.0.0.1']);
|
||||
|
||||
$this->tester->haveFixtures([
|
||||
'accounts' => AccountFixture::class,
|
||||
|
Reference in New Issue
Block a user