Skip to content

Commit

Permalink
More clean code.
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Jul 11, 2023
1 parent 69875eb commit 7b69072
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 31 deletions.
10 changes: 5 additions & 5 deletions models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

final class User extends \yii\base\BaseObject implements IdentityInterface
{
public string $id;
public string $username;
public string $password;
public string $authKey;
public string $accessToken;
public string $id = '';
public string $username = '';
public string $password = '';
public string $authKey = '';
public string $accessToken = '';

private static array $users = [
'100' => [
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/models/ContactFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
namespace tests\unit\models;

use app\models\ContactForm;
use UnitTester;
use yii\mail\MessageInterface;

final class ContactFormTest extends \Codeception\Test\Unit
{
public \UnitTester $tester;
public UnitTester $tester;

public function testEmailIsSentOnContact(): void
{
Expand Down
14 changes: 8 additions & 6 deletions tests/Unit/models/LoginFormTest.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
<?php

declare(strict_types=1);

namespace tests\unit\models;

use app\models\LoginForm;

class LoginFormTest extends \Codeception\Test\Unit
final class LoginFormTest extends \Codeception\Test\Unit
{
private \app\models\LoginForm|null $model = null;
private LoginForm|null $model = null;

protected function _after()
protected function _after(): void
{
\Yii::$app->user->logout();
}

public function testLoginNoUser()
public function testLoginNoUser(): void
{
$this->model = new LoginForm([
'username' => 'not_existing_username',
Expand All @@ -24,7 +26,7 @@ public function testLoginNoUser()
verify(\Yii::$app->user->isGuest)->true();
}

public function testLoginWrongPassword()
public function testLoginWrongPassword(): void
{
$this->model = new LoginForm([
'username' => 'demo',
Expand All @@ -36,7 +38,7 @@ public function testLoginWrongPassword()
verify($this->model->errors)->arrayHasKey('password');
}

public function testLoginCorrect()
public function testLoginCorrect(): void
{
$this->model = new LoginForm([
'username' => 'demo',
Expand Down
12 changes: 7 additions & 5 deletions tests/Unit/models/UserTest.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
<?php

declare(strict_types=1);

namespace tests\unit\models;

use app\models\User;

class UserTest extends \Codeception\Test\Unit
final class UserTest extends \Codeception\Test\Unit
{
public function testFindUserById()
public function testFindUserById(): void
{
verify($user = User::findIdentity(100))->notEmpty();
verify($user->username)->equals('admin');

verify(User::findIdentity(999))->empty();
}

public function testFindUserByAccessToken()
public function testFindUserByAccessToken(): void
{
verify($user = User::findIdentityByAccessToken('100-token'))->notEmpty();
verify($user->username)->equals('admin');

verify(User::findIdentityByAccessToken('non-existing'))->empty();
}

public function testFindUserByUsername()
public function testFindUserByUsername(): void
{
verify($user = User::findByUsername('admin'))->notEmpty();
verify(User::findByUsername('not-admin'))->empty();
Expand All @@ -31,7 +33,7 @@ public function testFindUserByUsername()
/**
* @depends testFindUserByUsername
*/
public function testValidateUser()
public function testValidateUser(): void
{
$user = User::findByUsername('admin');
verify($user->validateAuthKey('test100key'))->notEmpty();
Expand Down
32 changes: 18 additions & 14 deletions tests/Unit/widgets/AlertTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php

declare(strict_types=1);

namespace tests\unit\widgets;

use app\widgets\Alert;
use Yii;

class AlertTest extends \Codeception\Test\Unit
final class AlertTest extends \Codeception\Test\Unit
{
public function testSingleErrorMessage()
public function testSingleErrorMessage(): void
{
$message = 'This is an error message';

Expand All @@ -23,7 +25,7 @@ public function testSingleErrorMessage()
verify($renderingResult)->stringNotContainsString('alert-warning');
}

public function testMultipleErrorMessages()
public function testMultipleErrorMessages(): void
{
$firstMessage = 'This is the first error message';
$secondMessage = 'This is the second error message';
Expand All @@ -41,7 +43,7 @@ public function testMultipleErrorMessages()
verify($renderingResult)->stringNotContainsString('alert-warning');
}

public function testSingleDangerMessage()
public function testSingleDangerMessage(): void
{
$message = 'This is a danger message';

Expand All @@ -57,7 +59,7 @@ public function testSingleDangerMessage()
verify($renderingResult)->stringNotContainsString('alert-warning');
}

public function testMultipleDangerMessages()
public function testMultipleDangerMessages(): void
{
$firstMessage = 'This is the first danger message';
$secondMessage = 'This is the second danger message';
Expand All @@ -75,7 +77,7 @@ public function testMultipleDangerMessages()
verify($renderingResult)->stringNotContainsString('alert-warning');
}

public function testSingleSuccessMessage()
public function testSingleSuccessMessage(): void
{
$message = 'This is a success message';

Expand All @@ -91,7 +93,7 @@ public function testSingleSuccessMessage()
verify($renderingResult)->stringNotContainsString('alert-warning');
}

public function testMultipleSuccessMessages()
public function testMultipleSuccessMessages(): void
{
$firstMessage = 'This is the first danger message';
$secondMessage = 'This is the second danger message';
Expand All @@ -109,7 +111,7 @@ public function testMultipleSuccessMessages()
verify($renderingResult)->stringNotContainsString('alert-warning');
}

public function testSingleInfoMessage()
public function testSingleInfoMessage(): void
{
$message = 'This is an info message';

Expand All @@ -125,7 +127,7 @@ public function testSingleInfoMessage()
verify($renderingResult)->stringNotContainsString('alert-warning');
}

public function testMultipleInfoMessages()
public function testMultipleInfoMessages(): void
{
$firstMessage = 'This is the first info message';
$secondMessage = 'This is the second info message';
Expand All @@ -143,7 +145,7 @@ public function testMultipleInfoMessages()
verify($renderingResult)->stringNotContainsString('alert-warning');
}

public function testSingleWarningMessage()
public function testSingleWarningMessage(): void
{
$message = 'This is a warning message';

Expand All @@ -159,7 +161,7 @@ public function testSingleWarningMessage()
verify($renderingResult)->stringNotContainsString('alert-info');
}

public function testMultipleWarningMessages()
public function testMultipleWarningMessages(): void
{
$firstMessage = 'This is the first warning message';
$secondMessage = 'This is the second warning message';
Expand All @@ -177,7 +179,8 @@ public function testMultipleWarningMessages()
verify($renderingResult)->stringNotContainsString('alert-info');
}

public function testSingleMixedMessages() {
public function testSingleMixedMessages(): void
{
$errorMessage = 'This is an error message';
$dangerMessage = 'This is a danger message';
$successMessage = 'This is a success message';
Expand All @@ -204,7 +207,8 @@ public function testSingleMixedMessages() {
verify($renderingResult)->stringContainsString('alert-warning');
}

public function testMultipleMixedMessages() {
public function testMultipleMixedMessages(): void
{
$firstErrorMessage = 'This is the first error message';
$secondErrorMessage = 'This is the second error message';
$firstDangerMessage = 'This is the first danger message';
Expand Down Expand Up @@ -241,7 +245,7 @@ public function testMultipleMixedMessages() {
verify($renderingResult)->stringContainsString('alert-warning');
}

public function testFlashIntegrity()
public function testFlashIntegrity(): void
{
$errorMessage = 'This is an error message';
$unrelatedMessage = 'This is a message that is not related to the alert widget';
Expand Down
2 changes: 2 additions & 0 deletions widgets/Alert.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ final class Alert extends \yii\bootstrap5\Widget
*/
public function run()
{
/** @var \yii\web\Session $session */
$session = Yii::$app->session;

$appendClass = isset($this->options['class']) ? ' ' . $this->options['class'] : '';

foreach (array_keys($this->alertTypes) as $type) {
Expand Down

0 comments on commit 7b69072

Please sign in to comment.