Skip to content

Instantly share code, notes, and snippets.

@mgerasimchuk
Created October 26, 2016 15:04
Show Gist options
  • Save mgerasimchuk/f6c9e4f4688b4d7f24b106506a0fb078 to your computer and use it in GitHub Desktop.
Save mgerasimchuk/f6c9e4f4688b4d7f24b106506a0fb078 to your computer and use it in GitHub Desktop.
Test for UserAccount model
<?php
namespace tests\codeception\common\models;
use Codeception\Specify;
use common\models\AffiliateGroup;
use common\models\MarketplaceCategory;
use common\models\UserAccount;
use common\models\UserProfile;
use tests\codeception\common\fixtures\UserAccountFixture;
use tests\codeception\common\fixtures\UserProfileFixture;
use tests\codeception\common\unit\DbTestCase;
use Yii;
class UserAccountTest extends DbTestCase
{
use Specify;
/**
* @var \tests\codeception\common\UnitTester
*/
public function setUp()
{
parent::setUp();
Yii::configure(Yii::$app, [
'components' => [
'user' => [
'class' => 'yii\web\User',
'identityClass' => 'common\models\User',
],
],
]);
Yii::$app->db->createCommand()->checkIntegrity(false)->execute();
}
protected function tearDown()
{
Yii::$app->user->logout();
Yii::$app->db->createCommand()->checkIntegrity(true)->execute();
parent::tearDown();
}
public function fixtures()
{
return [
'profiles' => UserProfileFixture::class,
'accounts' => UserAccountFixture::class,
];
}
public function testStatusTrait()
{
$model = new UserAccount();
$this->specify('testing status trait integration', function () use ($model) {
expect('status label getter must return string', $model->getStatusLabel(''))->internalType('string');
});
}
public function testTypeTrait()
{
$model = new UserAccount();
$this->specify('testing type trait integration', function () use ($model) {
expect('status label getter must return string', $model->getTypeLabel(''))->internalType('string');
});
}
public function testValidation()
{
$model = new UserAccount();
$this->specify('testing model validation', function () use ($model) {
expect('validation on a new object must return boolean', $model->validate())->internalType('boolean');
});
}
public function testFieldsRequiredValidation()
{
/** @var UserAccountFixture $fixture */
$fixture = $this->getFixture('accounts');
/** @var UserAccount $model */
$model = $fixture->getModel(0);
$model->name = NULL;
$this->specify('testing name required rule', function () use ($model) {
expect('expecting name validation fail', $model->validate())->false();
});
$model = $fixture->getModel(1);
$model->email = NULL;
$this->specify('testing email required rule', function () use ($model) {
expect('expecting email validation fail', $model->validate())->false();
});
$model = $fixture->getModel(2);
$model->is_email_confirmed = NULL;
$this->specify('testing is_email_confirmed required rule', function () use ($model) {
expect('expecting is_email_confirmed validation fail', $model->validate())->false();
});
}
public function testEmailEmailValidation()
{
/** @var UserAccountFixture $fixture */
$fixture = $this->getFixture('accounts');
/** @var UserAccount $model */
$model = $fixture->getModel(0);
$model->email = "bademail";
$this->specify('testing email email rule', function () use ($model) {
expect('expecting email validation fail', $model->validate())->false();
});
}
public function testIsEmailConfirmedBooleanValidation()
{
/** @var UserAccountFixture $fixture */
$fixture = $this->getFixture('accounts');
/** @var UserAccount $model */
$model = $fixture->getModel(0);
$model->is_email_confirmed = "string";
$this->specify('testing email boolean rule', function () use ($model) {
expect('expecting email validation fail', $model->validate())->false();
});
}
public function testFieldsUniqueValidation()
{
/** @var UserAccountFixture $fixture */
$fixture = $this->getFixture('accounts');
/** @var UserAccount $model1 */
$model1 = $fixture->getModel(0);
/** @var UserAccount $model2 */
$model2 = $fixture->getModel(1);
$model2->email = $model1->email;
$this->specify('testing email unique rule', function () use ($model2) {
expect('expecting email validation fail', $model2->validate())->false();
});
$model2 = $fixture->getModel(2);
$model2->name = $model1->name;
$this->specify('testing name unique rule', function () use ($model2) {
expect('expecting name validation fail', $model2->validate())->false();
});
}
public function testProfile()
{
/** @var UserAccountFixture $fixture */
$fixture = $this->getFixture('accounts');
$model = $fixture->getModel(0);
$this->specify('testing profile relation', function () use ($model) {
expect('expecting profile to be an UserProfile instance', $model->profile)->isInstanceOf(UserProfile::class);
});
}
public function testAffiliateManagerRelations()
{
$fixture = new UserAccountFixture();
$fixture->dataFile = '@tests/codeception/common/fixtures/data/user_account__affiliate_relations.php';
$fixture->load();
$manager = $fixture->getModel(0);
$affiliate = $fixture->getModel(1);
$this->specify('testing profile relation', function () use ($manager, $affiliate) {
expect('expecting affiliates to be an array of UserAccount', $manager->affiliates)->internalType('array');
expect('expecting size of affiliates array to be 2', count($manager->affiliates))->equals(2);
expect('expecting affiliateManager to be an instance of UserAccount', $affiliate->affiliateManager)->isInstanceOf(UserAccount::class);
});
}
public function testMarketplaceCategoriesRelationsValidation()
{
/** @var UserAccountFixture $fixture */
$fixture = $this->getFixture('accounts');
$model = $fixture->getModel(0);
$this->specify('testing MarketplaceCategories relation', function () use ($model) {
expect('expecting marketplaceCategories to be an MarketplaceCategory instance', $model->marketplaceCategories)
->containsOnly(MarketplaceCategory::class);
});
}
public function testAffiliateGroupValidation()
{
/** @var UserAccountFixture $fixture */
$fixture = $this->getFixture('accounts');
$model = $fixture->getModel(0);
$this->specify('testing AffiliateGroup relation', function () use ($model) {
expect('expecting affiliateGroup to be an AffiliateGroup instance', $model->affiliateGroup)->isInstanceOf(AffiliateGroup::class);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment