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
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Rotua Panjaitan
yii2
Commits
1afe198c
Commit
1afe198c
authored
Dec 31, 2013
by
Mark
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
modified tests, improved configuration, introduced aspect mock and specify.
parent
dabeea7a
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
145 additions
and
8 deletions
+145
-8
composer.json
apps/basic/composer.json
+4
-1
_bootstrap.php
apps/basic/tests/_bootstrap.php
+0
-4
_bootstrap.php
apps/basic/tests/acceptance/_bootstrap.php
+3
-0
_bootstrap.php
apps/basic/tests/functional/_bootstrap.php
+3
-0
_bootstrap.php
apps/basic/tests/unit/_bootstrap.php
+3
-1
aspect_mock.php
apps/basic/tests/unit/aspect_mock.php
+16
-0
ContactFormTest.php
apps/basic/tests/unit/models/ContactFormTest.php
+54
-1
LoginFormTest.php
apps/basic/tests/unit/models/LoginFormTest.php
+62
-1
No files found.
apps/basic/composer.json
View file @
1afe198c
...
...
@@ -20,7 +20,10 @@
"yiisoft/yii2-codeception"
:
"*"
,
"yiisoft/yii2-debug"
:
"*"
,
"yiisoft/yii2-gii"
:
"*"
,
"yiisoft/yii2-swiftmailer"
:
"*"
"yiisoft/yii2-swiftmailer"
:
"*"
,
"codeception/codeception"
:
"*"
,
"codeception/aspect-mock"
:
"*"
,
"codeception/specify"
:
"*"
},
"scripts"
:
{
"post-create-project-cmd"
:
[
...
...
apps/basic/tests/_bootstrap.php
View file @
1afe198c
...
...
@@ -13,10 +13,6 @@ 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 @
1afe198c
<?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 @
1afe198c
<?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 @
1afe198c
<?php
// add unit testing specific bootstrap code here
#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__
.
'/../'
);
apps/basic/tests/unit/aspect_mock.php
0 → 100644
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 @
1afe198c
...
...
@@ -2,9 +2,62 @@
namespace
tests\unit\models
;
use
Yii
;
use
yii\codeception\TestCase
;
use
app\models\ContactForm
;
use
AspectMock\Test
as
test
;
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
(
Yii
::
getAlias
(
Yii
::
$app
->
mail
->
fileTransportPath
)
.
'/testing_message.eml'
);
test
::
clean
();
parent
::
tearDown
();
}
public
function
testContact
()
{
test
::
double
(
'app\models\ContactForm'
,[
'validate'
=>
true
]);
$model
=
new
ContactForm
();
$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 @
1afe198c
...
...
@@ -3,8 +3,68 @@
namespace
tests\unit\models
;
use
yii\codeception\TestCase
;
use
app\models\LoginForm
;
use
app\models\User
;
use
AspectMock\Test
as
test
;
class
LoginFormTest
extends
TestCase
{
// TODO add test methods here
use
\Codeception\Specify
;
protected
function
tearDown
()
{
test
::
clean
();
parent
::
tearDown
();
}
public
function
testLoginNoUser
()
{
$user
=
$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
->
assertFalse
(
$model
->
login
());
$user
->
verifyInvoked
(
'findByUsername'
,[
'some_username'
]);
});
}
public
function
testLoginWrongPassword
()
{
$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
->
assertFalse
(
$model
->
login
());
$this
->
assertArrayHasKey
(
'password'
,
$model
->
errors
);
});
}
public
function
testLoginCorrect
()
{
$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
->
assertTrue
(
$model
->
login
());
$this
->
assertArrayNotHasKey
(
'password'
,
$model
->
errors
);
});
}
private
function
mockUser
(
$user
)
{
return
test
::
double
(
'app\models\User'
,
[
'findByUsername'
=>
$user
,
]);
}
}
\ 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