init
This commit is contained in:
3
tests/unit/_bootstrap.php
Normal file
3
tests/unit/_bootstrap.php
Normal file
@@ -0,0 +1,3 @@
|
||||
<?php
|
||||
|
||||
// add unit testing specific bootstrap code here
|
||||
43
tests/unit/models/ContactFormTest.php
Normal file
43
tests/unit/models/ContactFormTest.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace tests\models;
|
||||
|
||||
class ContactFormTest extends \Codeception\Test\Unit
|
||||
{
|
||||
private $model;
|
||||
/**
|
||||
* @var \UnitTester
|
||||
*/
|
||||
public $tester;
|
||||
|
||||
public function testEmailIsSentOnContact()
|
||||
{
|
||||
/** @var ContactForm $model */
|
||||
$this->model = $this->getMockBuilder('app\models\ContactForm')
|
||||
->setMethods(['validate'])
|
||||
->getMock();
|
||||
|
||||
$this->model->expects($this->once())
|
||||
->method('validate')
|
||||
->will($this->returnValue(true));
|
||||
|
||||
$this->model->attributes = [
|
||||
'name' => 'Tester',
|
||||
'email' => 'tester@example.com',
|
||||
'subject' => 'very important letter subject',
|
||||
'body' => 'body of current message',
|
||||
];
|
||||
|
||||
expect_that($this->model->contact('admin@example.com'));
|
||||
|
||||
// using Yii2 module actions to check email was sent
|
||||
$this->tester->seeEmailIsSent();
|
||||
|
||||
$emailMessage = $this->tester->grabLastSentEmail();
|
||||
expect('valid email is sent', $emailMessage)->isInstanceOf('yii\mail\MessageInterface');
|
||||
expect($emailMessage->getTo())->hasKey('admin@example.com');
|
||||
expect($emailMessage->getFrom())->hasKey('tester@example.com');
|
||||
expect($emailMessage->getSubject())->equals('very important letter subject');
|
||||
expect($emailMessage->toString())->contains('body of current message');
|
||||
}
|
||||
}
|
||||
51
tests/unit/models/LoginFormTest.php
Normal file
51
tests/unit/models/LoginFormTest.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace tests\models;
|
||||
|
||||
use app\models\LoginForm;
|
||||
|
||||
class LoginFormTest extends \Codeception\Test\Unit
|
||||
{
|
||||
private $model;
|
||||
|
||||
protected function _after()
|
||||
{
|
||||
\Yii::$app->user->logout();
|
||||
}
|
||||
|
||||
public function testLoginNoUser()
|
||||
{
|
||||
$this->model = new LoginForm([
|
||||
'username' => 'not_existing_username',
|
||||
'password' => 'not_existing_password',
|
||||
]);
|
||||
|
||||
expect_not($this->model->login());
|
||||
expect_that(\Yii::$app->user->isGuest);
|
||||
}
|
||||
|
||||
public function testLoginWrongPassword()
|
||||
{
|
||||
$this->model = new LoginForm([
|
||||
'username' => 'demo',
|
||||
'password' => 'wrong_password',
|
||||
]);
|
||||
|
||||
expect_not($this->model->login());
|
||||
expect_that(\Yii::$app->user->isGuest);
|
||||
expect($this->model->errors)->hasKey('password');
|
||||
}
|
||||
|
||||
public function testLoginCorrect()
|
||||
{
|
||||
$this->model = new LoginForm([
|
||||
'username' => 'demo',
|
||||
'password' => 'demo',
|
||||
]);
|
||||
|
||||
expect_that($this->model->login());
|
||||
expect_not(\Yii::$app->user->isGuest);
|
||||
expect($this->model->errors)->hasntKey('password');
|
||||
}
|
||||
|
||||
}
|
||||
44
tests/unit/models/UserTest.php
Normal file
44
tests/unit/models/UserTest.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace tests\models;
|
||||
|
||||
use app\models\User;
|
||||
|
||||
class UserTest extends \Codeception\Test\Unit
|
||||
{
|
||||
public function testFindUserById()
|
||||
{
|
||||
expect_that($user = User::findIdentity(100));
|
||||
expect($user->username)->equals('admin');
|
||||
|
||||
expect_not(User::findIdentity(999));
|
||||
}
|
||||
|
||||
public function testFindUserByAccessToken()
|
||||
{
|
||||
expect_that($user = User::findIdentityByAccessToken('100-token'));
|
||||
expect($user->username)->equals('admin');
|
||||
|
||||
expect_not(User::findIdentityByAccessToken('non-existing'));
|
||||
}
|
||||
|
||||
public function testFindUserByUsername()
|
||||
{
|
||||
expect_that($user = User::findByUsername('admin'));
|
||||
expect_not(User::findByUsername('not-admin'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testFindUserByUsername
|
||||
*/
|
||||
public function testValidateUser($user)
|
||||
{
|
||||
$user = User::findByUsername('admin');
|
||||
expect_that($user->validateAuthKey('test100key'));
|
||||
expect_not($user->validateAuthKey('test102key'));
|
||||
|
||||
expect_that($user->validatePassword('admin'));
|
||||
expect_not($user->validatePassword('123456'));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user