Remove usage of codeception/specify and fzaninotto/faker

This commit is contained in:
ErickSkrauch
2019-12-21 02:26:06 +03:00
parent 666213afc7
commit 43a4a58053
19 changed files with 217 additions and 613 deletions

View File

@@ -1,20 +0,0 @@
<?php
/**
* @var $faker \Faker\Generator
* @var $index integer
*/
$security = Yii::$app->getSecurity();
return [
'uuid' => $faker->uuid,
'username' => $faker->userName,
'email' => $faker->email,
'password_hash' => $security->generatePasswordHash('password_' . $index),
'password_hash_strategy' => \common\models\Account::PASS_HASH_STRATEGY_YII2,
'lang' => 'en',
'auth_key' => $security->generateRandomString(),
'status' => \common\models\Account::STATUS_ACTIVE,
'created_at' => time(),
'updated_at' => time(),
];

View File

@@ -1,70 +1,46 @@
<?php
declare(strict_types=1);
namespace common\tests\unit\behaviors;
use Codeception\Specify;
use common\behaviors\PrimaryKeyValueBehavior;
use common\tests\unit\TestCase;
use yii\db\ActiveRecord;
class PrimaryKeyValueBehaviorTest extends TestCase {
use Specify;
public function testRefreshPrimaryKeyValue() {
$this->specify('method should generate value for primary key field on call', function() {
$model = new DummyModel();
/** @var PrimaryKeyValueBehavior|\PHPUnit\Framework\MockObject\MockObject $behavior */
$behavior = $this->getMockBuilder(PrimaryKeyValueBehavior::class)
->setMethods(['isValueExists'])
->setConstructorArgs([[
'value' => function() {
return 'mock';
},
]])
->getMock();
public function testGenerateValueForThePrimaryKey() {
$model = $this->createDummyModel();
$behavior = $this->createPartialMock(PrimaryKeyValueBehavior::class, ['isValueExists']);
$behavior->method('isValueExists')->willReturn(false);
$behavior->value = function() {
return 'mock';
};
$behavior->expects($this->once())
->method('isValueExists')
->will($this->returnValue(false));
$model->attachBehavior('primary-key-value-behavior', $behavior);
$behavior->setPrimaryKeyValue();
$this->assertSame('mock', $model->id);
}
$model->attachBehavior('primary-key-value-behavior', $behavior);
$behavior->setPrimaryKeyValue();
$this->assertSame('mock', $model->id);
});
public function testShouldRegenerateValueWhenGeneratedAlreadyExists() {
$model = $this->createDummyModel();
$behavior = $this->createPartialMock(PrimaryKeyValueBehavior::class, ['isValueExists', 'generateValue']);
$behavior->expects($this->exactly(3))->method('generateValue')->willReturnOnConsecutiveCalls('1', '2', '3');
$behavior->expects($this->exactly(3))->method('isValueExists')->willReturnOnConsecutiveCalls(true, true, false);
$this->specify('method should repeat value generation if generated value duplicate with exists', function() {
$model = new DummyModel();
/** @var PrimaryKeyValueBehavior|\PHPUnit\Framework\MockObject\MockObject $behavior */
$behavior = $this->getMockBuilder(PrimaryKeyValueBehavior::class)
->setMethods(['isValueExists', 'generateValue'])
->setConstructorArgs([[
'value' => function() {
return 'this was mocked, but let be passed';
},
]])
->getMock();
$model->attachBehavior('primary-key-value-behavior', $behavior);
$behavior->setPrimaryKeyValue();
$this->assertSame('3', $model->id);
}
$behavior->expects($this->exactly(3))
->method('generateValue')
->will($this->onConsecutiveCalls('1', '2', '3'));
private function createDummyModel() {
return new class extends ActiveRecord {
public $id;
$behavior->expects($this->exactly(3))
->method('isValueExists')
->will($this->onConsecutiveCalls(true, true, false));
$model->attachBehavior('primary-key-value-behavior', $behavior);
$behavior->setPrimaryKeyValue();
$this->assertSame('3', $model->id);
});
}
}
class DummyModel extends ActiveRecord {
public $id;
public static function primaryKey() {
return ['id'];
public static function primaryKey() {
return ['id'];
}
};
}
}

View File

@@ -3,20 +3,16 @@ declare(strict_types=1);
namespace common\tests\unit\models;
use Codeception\Specify;
use common\components\UserPass;
use common\models\Account;
use common\tasks\CreateWebHooksDeliveries;
use common\tests\fixtures\MojangUsernameFixture;
use common\tests\unit\TestCase;
use Yii;
use const common\LATEST_RULES_VERSION;
/**
* @covers \common\models\Account
*/
class AccountTest extends TestCase {
use Specify;
public function testSetPassword() {
$model = new Account();
@@ -27,57 +23,47 @@ class AccountTest extends TestCase {
}
public function testValidatePassword() {
$this->specify('old Ely password should work', function() {
$model = new Account([
'email' => 'erick@skrauch.net',
'password_hash' => UserPass::make('erick@skrauch.net', '12345678'),
]);
$this->assertTrue($model->validatePassword('12345678', Account::PASS_HASH_STRATEGY_OLD_ELY), 'valid password should pass');
$this->assertFalse($model->validatePassword('87654321', Account::PASS_HASH_STRATEGY_OLD_ELY), 'invalid password should fail');
});
// Use old hashing algorithm
$model = new Account();
$model->email = 'erick@skrauch.net';
$model->password_hash = '2cfdb29eb354af970865a923335d17d9'; // 12345678
$model->password_hash_strategy = null; // To be sure it's not set
$this->assertTrue($model->validatePassword('12345678', Account::PASS_HASH_STRATEGY_OLD_ELY), 'valid password should pass');
$this->assertFalse($model->validatePassword('87654321', Account::PASS_HASH_STRATEGY_OLD_ELY), 'invalid password should fail');
$this->specify('modern hash algorithm should work', function() {
$model = new Account([
'password_hash' => Yii::$app->security->generatePasswordHash('12345678'),
]);
$this->assertTrue($model->validatePassword('12345678', Account::PASS_HASH_STRATEGY_YII2), 'valid password should pass');
$this->assertFalse($model->validatePassword('87654321', Account::PASS_HASH_STRATEGY_YII2), 'invalid password should fail');
});
// Modern hash algorithm should also work
$model = new Account();
$model->password_hash = '$2y$04$N0q8DaHzlYILCnLYrpZfEeWKEqkPZzbawiS07GbSr/.xbRNweSLU6'; // 12345678
$model->password_hash_strategy = null; // To be sure it's not set
$this->assertTrue($model->validatePassword('12345678', Account::PASS_HASH_STRATEGY_YII2), 'valid password should pass');
$this->assertFalse($model->validatePassword('87654321', Account::PASS_HASH_STRATEGY_YII2), 'invalid password should fail');
$this->specify('if second argument is not pass model value should be used', function() {
$model = new Account([
'email' => 'erick@skrauch.net',
'password_hash_strategy' => Account::PASS_HASH_STRATEGY_OLD_ELY,
'password_hash' => UserPass::make('erick@skrauch.net', '12345678'),
]);
$this->assertTrue($model->validatePassword('12345678'), 'valid password should pass');
$this->assertFalse($model->validatePassword('87654321'), 'invalid password should fail');
// If the second arg isn't passed model's value should be used
$model = new Account();
$model->email = 'erick@skrauch.net';
$model->password_hash = '2cfdb29eb354af970865a923335d17d9'; // 12345678
$model->password_hash_strategy = Account::PASS_HASH_STRATEGY_OLD_ELY;
$this->assertTrue($model->validatePassword('12345678'), 'valid password should pass');
$this->assertFalse($model->validatePassword('87654321'), 'invalid password should fail');
$model = new Account([
'password_hash_strategy' => Account::PASS_HASH_STRATEGY_YII2,
'password_hash' => Yii::$app->security->generatePasswordHash('12345678'),
]);
$this->assertTrue($model->validatePassword('12345678'), 'valid password should pass');
$this->assertFalse($model->validatePassword('87654321'), 'invalid password should fail');
});
// The same case for modern algorithm
$model = new Account();
$model->password_hash = '$2y$04$N0q8DaHzlYILCnLYrpZfEeWKEqkPZzbawiS07GbSr/.xbRNweSLU6'; // 12345678
$model->password_hash_strategy = Account::PASS_HASH_STRATEGY_YII2;
$this->assertTrue($model->validatePassword('12345678'), 'valid password should pass');
$this->assertFalse($model->validatePassword('87654321'), 'invalid password should fail');
}
public function testHasMojangUsernameCollision() {
$model = new Account();
$model->username = 'ErickSkrauch';
$this->assertFalse($model->hasMojangUsernameCollision());
$this->tester->haveFixtures([
'mojangUsernames' => MojangUsernameFixture::class,
]);
$this->specify('Expect true if collision with current username', function() {
$model = new Account();
$model->username = 'ErickSkrauch';
$this->assertTrue($model->hasMojangUsernameCollision());
});
$this->specify('Expect false if some rare username without any collision on Mojang', function() {
$model = new Account();
$model->username = 'rare-username';
$this->assertFalse($model->hasMojangUsernameCollision());
});
$this->assertTrue($model->hasMojangUsernameCollision());
}
public function testGetProfileLink() {
@@ -87,22 +73,14 @@ class AccountTest extends TestCase {
}
public function testIsAgreedWithActualRules() {
$this->specify('get false, if rules field set in null', function() {
$model = new Account();
$this->assertFalse($model->isAgreedWithActualRules());
});
$model = new Account();
$this->assertFalse($model->isAgreedWithActualRules(), 'field is null');
$this->specify('get false, if rules field have version less, then actual', function() {
$model = new Account();
$model->rules_agreement_version = 0;
$this->assertFalse($model->isAgreedWithActualRules());
});
$model->rules_agreement_version = 0;
$this->assertFalse($model->isAgreedWithActualRules(), 'actual version is greater than zero');
$this->specify('get true, if rules field have equals rules version', function() {
$model = new Account();
$model->rules_agreement_version = LATEST_RULES_VERSION;
$this->assertTrue($model->isAgreedWithActualRules());
});
$model->rules_agreement_version = LATEST_RULES_VERSION;
$this->assertTrue($model->isAgreedWithActualRules());
}
public function testSetRegistrationIp() {

View File

@@ -1,77 +0,0 @@
<?php
namespace common\tests\unit\validators;
use Codeception\Specify;
use common\tests\unit\TestCase;
use common\validators\UuidValidator;
use Faker\Provider\Uuid;
use yii\base\Model;
class UuidValidatorTest extends TestCase {
use Specify;
public function testValidateAttribute() {
$this->specify('expected error if passed empty value', function() {
$validator = new UuidValidator();
$model = $this->createModel();
$validator->validateAttribute($model, 'attribute');
$this->assertTrue($model->hasErrors());
$this->assertSame(['Attribute must be valid uuid'], $model->getErrors('attribute'));
});
$this->specify('expected error if passed invalid string', function() {
$validator = new UuidValidator();
$model = $this->createModel();
$model->attribute = '123456789';
$validator->validateAttribute($model, 'attribute');
$this->assertTrue($model->hasErrors());
$this->assertSame(['Attribute must be valid uuid'], $model->getErrors('attribute'));
});
$this->specify('no errors if passed nil uuid and allowNil is set to true', function() {
$validator = new UuidValidator();
$model = $this->createModel();
$model->attribute = '00000000-0000-0000-0000-000000000000';
$validator->validateAttribute($model, 'attribute');
$this->assertFalse($model->hasErrors());
});
$this->specify('no errors if passed nil uuid and allowNil is set to false', function() {
$validator = new UuidValidator();
$validator->allowNil = false;
$model = $this->createModel();
$model->attribute = '00000000-0000-0000-0000-000000000000';
$validator->validateAttribute($model, 'attribute');
$this->assertTrue($model->hasErrors());
$this->assertSame(['Attribute must be valid uuid'], $model->getErrors('attribute'));
});
$this->specify('no errors if passed valid uuid', function() {
$validator = new UuidValidator();
$model = $this->createModel();
$model->attribute = Uuid::uuid();
$validator->validateAttribute($model, 'attribute');
$this->assertFalse($model->hasErrors());
});
$this->specify('no errors if passed uuid string without dashes and converted to standart value', function() {
$validator = new UuidValidator();
$model = $this->createModel();
$originalUuid = Uuid::uuid();
$model->attribute = str_replace('-', '', $originalUuid);
$validator->validateAttribute($model, 'attribute');
$this->assertFalse($model->hasErrors());
$this->assertSame($originalUuid, $model->attribute);
});
}
/**
* @return Model
*/
public function createModel() {
return new class extends Model {
public $attribute;
};
}
}

View File

@@ -1,29 +0,0 @@
<?php
namespace common\validators;
use InvalidArgumentException;
use Ramsey\Uuid\Uuid;
use yii\validators\Validator;
class UuidValidator extends Validator {
public $allowNil = true;
public $skipOnEmpty = false;
public $message = '{attribute} must be valid uuid';
public function validateAttribute($model, $attribute) {
try {
$uuid = Uuid::fromString($model->$attribute)->toString();
$model->$attribute = $uuid;
} catch (InvalidArgumentException $e) {
$this->addError($model, $attribute, $this->message, []);
}
if (isset($uuid) && $this->allowNil === false && $uuid === Uuid::NIL) {
$this->addError($model, $attribute, $this->message, []);
}
}
}