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
11c42fa7
Commit
11c42fa7
authored
Dec 31, 2013
by
Mark
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
tests improved, aspect mock changes reverted
parent
1afe198c
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
24 additions
and
53 deletions
+24
-53
LoginForm.php
apps/basic/models/LoginForm.php
+2
-1
_bootstrap.php
apps/basic/tests/_bootstrap.php
+4
-0
_bootstrap.php
apps/basic/tests/acceptance/_bootstrap.php
+0
-3
_bootstrap.php
apps/basic/tests/functional/_bootstrap.php
+0
-3
_bootstrap.php
apps/basic/tests/unit/_bootstrap.php
+1
-3
aspect_mock.php
apps/basic/tests/unit/aspect_mock.php
+0
-16
ContactFormTest.php
apps/basic/tests/unit/models/ContactFormTest.php
+3
-6
LoginFormTest.php
apps/basic/tests/unit/models/LoginFormTest.php
+14
-21
No files found.
apps/basic/models/LoginForm.php
View file @
11c42fa7
...
...
@@ -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/_bootstrap.php
View file @
11c42fa7
...
...
@@ -13,6 +13,10 @@ defined('YII_ENV') or define('YII_ENV', 'test');
require_once
(
__DIR__
.
'/../vendor/autoload.php'
);
require_once
(
__DIR__
.
'/../vendor/yiisoft/yii2/yii/Yii.php'
);
// set correct script paths
$_SERVER
[
'SCRIPT_FILENAME'
]
=
TEST_ENTRY_FILE
;
$_SERVER
[
'SCRIPT_NAME'
]
=
TEST_ENTRY_URL
;
Yii
::
setAlias
(
'@tests'
,
__DIR__
);
apps/basic/tests/acceptance/_bootstrap.php
View file @
11c42fa7
<?php
require_once
(
__DIR__
.
'/../../vendor/yiisoft/yii2/yii/Yii.php'
);
Yii
::
setAlias
(
'@tests'
,
__DIR__
.
'/../'
);
new
yii\web\Application
(
require
(
__DIR__
.
'/_config.php'
));
apps/basic/tests/functional/_bootstrap.php
View file @
11c42fa7
<?php
require_once
(
__DIR__
.
'/../../vendor/yiisoft/yii2/yii/Yii.php'
);
Yii
::
setAlias
(
'@tests'
,
__DIR__
.
'/../'
);
new
yii\web\Application
(
require
(
__DIR__
.
'/_config.php'
));
apps/basic/tests/unit/_bootstrap.php
View file @
11c42fa7
<?php
#aspect-mock should be included only once. Codeception calls this bootstrap file per each test file.
require_once
__DIR__
.
'/aspect_mock.php'
;
Yii
::
setAlias
(
'@tests'
,
__DIR__
.
'/../'
);
// add unit testing specific bootstrap code here
apps/basic/tests/unit/aspect_mock.php
deleted
100644 → 0
View file @
1afe198c
<?php
$kernel
=
AspectMock\Kernel
::
getInstance
();
$kernel
->
init
([
'debug'
=>
true
,
'excludePaths'
=>
[
__DIR__
.
'/../tests'
,
__DIR__
.
'/../mails'
,
__DIR__
.
'/../runtime'
,
__DIR__
.
'/../config'
,
__DIR__
.
'/../controllers'
,
__DIR__
.
'/../assets'
,
],
]);
$kernel
->
loadFile
(
__DIR__
.
'/../../vendor/yiisoft/yii2/yii/Yii.php'
);
apps/basic/tests/unit/models/ContactFormTest.php
View file @
11c42fa7
...
...
@@ -4,8 +4,6 @@ namespace tests\unit\models;
use
Yii
;
use
yii\codeception\TestCase
;
use
app\models\ContactForm
;
use
AspectMock\Test
as
test
;
class
ContactFormTest
extends
TestCase
{
...
...
@@ -23,16 +21,15 @@ class ContactFormTest extends TestCase
protected
function
tearDown
()
{
unlink
(
Yii
::
getAlias
(
Yii
::
$app
->
mail
->
fileTransportPath
)
.
'/testing_message.eml'
);
test
::
clean
();
unlink
(
$this
->
getMessageFile
());
parent
::
tearDown
();
}
public
function
testContact
()
{
test
::
double
(
'app\models\ContactForm'
,[
'validate'
=>
true
]);
$model
=
$this
->
getMock
(
'app\models\ContactForm'
,
[
'validate'
]);
$model
->
expects
(
$this
->
once
())
->
method
(
'validate'
)
->
will
(
$this
->
returnValue
(
true
));
$model
=
new
ContactForm
();
$model
->
attributes
=
[
'name'
=>
'Tester'
,
'email'
=>
'tester@example.com'
,
...
...
apps/basic/tests/unit/models/LoginFormTest.php
View file @
11c42fa7
...
...
@@ -2,69 +2,61 @@
namespace
tests\unit\models
;
use
Yii
;
use
yii\codeception\TestCase
;
use
app\models\LoginForm
;
use
app\models\User
;
use
AspectMock\Test
as
test
;
class
LoginFormTest
extends
TestCase
{
use
\Codeception\Specify
;
protected
function
tearDown
()
{
test
::
clean
();
parent
::
tearDown
();
}
public
function
testLoginNoUser
()
{
$
user
=
$this
->
mockUser
(
null
);
$
model
=
$this
->
mockUser
(
null
);
$model
=
new
LoginForm
();
$model
->
username
=
'some_username'
;
$model
->
password
=
'some_password'
;
$this
->
specify
(
'user should not be able to login, when there is no identity'
,
function
()
use
(
$
user
,
$
model
)
{
$this
->
specify
(
'user should not be able to login, when there is no identity'
,
function
()
use
(
$model
)
{
$this
->
assertFalse
(
$model
->
login
());
$
user
->
verifyInvoked
(
'findByUsername'
,[
'some_username'
]
);
$
this
->
assertTrue
(
Yii
::
$app
->
user
->
isGuest
,
'user should not be logged in'
);
});
}
public
function
testLoginWrongPassword
()
{
$this
->
mockUser
(
new
User
);
$
model
=
$
this
->
mockUser
(
new
User
);
$model
=
new
LoginForm
();
$model
->
username
=
'demo'
;
$model
->
password
=
'wrong-password'
;
$this
->
specify
(
'user should not be able to login with wrong password'
,
function
()
use
(
$model
){
$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
()
{
$this
->
mockUser
(
new
User
([
'password'
=>
'demo'
]));
$
model
=
$
this
->
mockUser
(
new
User
([
'password'
=>
'demo'
]));
$model
=
new
LoginForm
();
$model
->
username
=
'demo'
;
$model
->
password
=
'demo'
;
$this
->
specify
(
'user should
not be able to login with correct credentials'
,
function
()
use
(
$model
)
{
$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
)
{
return
test
::
double
(
'app\models\User'
,
[
'findByUsername'
=>
$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