Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
53daaf81
Commit
53daaf81
authored
Dec 31, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1714 from Ragazzo/tests_improved
modified tests, improved configuration, introduced aspect
parents
c3c642dd
139fbcbc
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
109 additions
and
4 deletions
+109
-4
composer.json
apps/basic/composer.json
+3
-1
LoginForm.php
apps/basic/models/LoginForm.php
+2
-1
ContactFormTest.php
apps/basic/tests/unit/models/ContactFormTest.php
+50
-1
LoginFormTest.php
apps/basic/tests/unit/models/LoginFormTest.php
+54
-1
No files found.
apps/basic/composer.json
View file @
53daaf81
...
...
@@ -20,7 +20,9 @@
"yiisoft/yii2-codeception"
:
"*"
,
"yiisoft/yii2-debug"
:
"*"
,
"yiisoft/yii2-gii"
:
"*"
,
"yiisoft/yii2-swiftmailer"
:
"*"
"yiisoft/yii2-swiftmailer"
:
"*"
,
"codeception/codeception"
:
"*"
,
"codeception/specify"
:
"*"
},
"scripts"
:
{
"post-create-project-cmd"
:
[
...
...
apps/basic/models/LoginForm.php
View file @
53daaf81
...
...
@@ -38,6 +38,7 @@ class LoginForm extends Model
public
function
validatePassword
()
{
$user
=
$this
->
getUser
();
if
(
!
$user
||
!
$user
->
validatePassword
(
$this
->
password
))
{
$this
->
addError
(
'password'
,
'Incorrect username or password.'
);
}
...
...
@@ -61,7 +62,7 @@ class LoginForm extends Model
*
* @return User|null
*/
p
rivate
function
getUser
()
p
ublic
function
getUser
()
{
if
(
$this
->
_user
===
false
)
{
$this
->
_user
=
User
::
findByUsername
(
$this
->
username
);
...
...
apps/basic/tests/unit/models/ContactFormTest.php
View file @
53daaf81
...
...
@@ -2,9 +2,58 @@
namespace
tests\unit\models
;
use
Yii
;
use
yii\codeception\TestCase
;
class
ContactFormTest
extends
TestCase
{
// TODO add test methods here
use
\Codeception\Specify
;
protected
function
setUp
()
{
parent
::
setUp
();
Yii
::
$app
->
mail
->
fileTransportCallback
=
function
(
$mailer
,
$message
)
{
return
'testing_message.eml'
;
};
}
protected
function
tearDown
()
{
unlink
(
$this
->
getMessageFile
());
parent
::
tearDown
();
}
public
function
testContact
()
{
$model
=
$this
->
getMock
(
'app\models\ContactForm'
,
[
'validate'
]);
$model
->
expects
(
$this
->
once
())
->
method
(
'validate'
)
->
will
(
$this
->
returnValue
(
true
));
$model
->
attributes
=
[
'name'
=>
'Tester'
,
'email'
=>
'tester@example.com'
,
'subject'
=>
'very important letter subject'
,
'body'
=>
'body of current message'
,
];
$model
->
contact
(
'admin@example.com'
);
$this
->
specify
(
'email should be send'
,
function
()
{
$this
->
assertFileExists
(
$this
->
getMessageFile
(),
'email file should exist'
);
});
$this
->
specify
(
'message should contain correct data'
,
function
()
use
(
$model
)
{
$emailMessage
=
file_get_contents
(
$this
->
getMessageFile
());
$this
->
assertContains
(
$model
->
name
,
$emailMessage
,
'email should contain user name'
);
$this
->
assertContains
(
$model
->
email
,
$emailMessage
,
'email should contain sender email'
);
$this
->
assertContains
(
$model
->
subject
,
$emailMessage
,
'email should contain subject'
);
$this
->
assertContains
(
$model
->
body
,
$emailMessage
,
'email should contain body'
);
});
}
private
function
getMessageFile
()
{
return
Yii
::
getAlias
(
Yii
::
$app
->
mail
->
fileTransportPath
)
.
'/testing_message.eml'
;
}
}
apps/basic/tests/unit/models/LoginFormTest.php
View file @
53daaf81
...
...
@@ -2,9 +2,61 @@
namespace
tests\unit\models
;
use
Yii
;
use
yii\codeception\TestCase
;
use
app\models\User
;
class
LoginFormTest
extends
TestCase
{
// TODO add test methods here
use
\Codeception\Specify
;
public
function
testLoginNoUser
()
{
$model
=
$this
->
mockUser
(
null
);
$model
->
username
=
'some_username'
;
$model
->
password
=
'some_password'
;
$this
->
specify
(
'user should not be able to login, when there is no identity'
,
function
()
use
(
$model
)
{
$this
->
assertFalse
(
$model
->
login
());
$this
->
assertTrue
(
Yii
::
$app
->
user
->
isGuest
,
'user should not be logged in'
);
});
}
public
function
testLoginWrongPassword
()
{
$model
=
$this
->
mockUser
(
new
User
);
$model
->
username
=
'demo'
;
$model
->
password
=
'wrong-password'
;
$this
->
specify
(
'user should not be able to login with wrong password'
,
function
()
use
(
$model
)
{
$this
->
assertFalse
(
$model
->
login
());
$this
->
assertArrayHasKey
(
'password'
,
$model
->
errors
);
$this
->
assertTrue
(
Yii
::
$app
->
user
->
isGuest
,
'user should not be logged in'
);
});
}
public
function
testLoginCorrect
()
{
$model
=
$this
->
mockUser
(
new
User
([
'password'
=>
'demo'
]));
$model
->
username
=
'demo'
;
$model
->
password
=
'demo'
;
$this
->
specify
(
'user should be able to login with correct credentials'
,
function
()
use
(
$model
)
{
$this
->
assertTrue
(
$model
->
login
());
$this
->
assertArrayNotHasKey
(
'password'
,
$model
->
errors
);
$this
->
assertFalse
(
Yii
::
$app
->
user
->
isGuest
,
'user should be logged in'
);
});
}
private
function
mockUser
(
$user
)
{
$loginForm
=
$this
->
getMock
(
'app\models\LoginForm'
,[
'getUser'
]);
$loginForm
->
expects
(
$this
->
any
())
->
method
(
'getUser'
)
->
will
(
$this
->
returnValue
(
$user
));
return
$loginForm
;
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment