This example describes the YII2 unit test usage. Share to everyone for your reference, specific as follows:
Install Yii2-app-basic (HTTPS://GITHUB.COM/YIISOFT/YII2-APP-BASIC/BLOB/MASTER/README.MD) using the composer method can be installed with both
Build a model file entryform.php in the models directory
<?php
namespace App\models;
Use Yii;
Use Yii\base\model;
Class Entryform extends Model
{public
$name;
public $email;
Public function rules ()
{return
[[
[' name ', ' email '], ' required '],
[' email ', ' email '],
];
}
}
Build a entryformtest.php and put it in the Tests/unit/models directory.
<?php
namespace Tests\models;
Use App\models\entryform;
Class Entryformtest extends \codeception\test\unit
{public
function testvalidinput ()
{
$model = new Entryform ();
$model->name = ' Harry Qin ';
$model->email = ' 15848778@qq.com ';
Expect_that ($model->validate ());
return $model;
}
Public Function Testinvalidinput ()
{
$model = new Entryform ();
$model->name = ' Harry Qin ';
$model->email = ' xxyy ';
Expect_not ($model->validate ());
$model = new Entryform ();
$model->name = ';
$model->email = ' 15848778@qq.com ';
Expect_not ($model->validate ());
/**
* The following line indicates that the parameter values entered here are from Testvalidinput output
* @depends testvalidinput/public
function Testmodelproperty ($model)
{
expect ($model->name)->equals (' Harry Qin ');
}
Run under Project root directory
Composer exec codecept run unit
Output
。。。。。。
✔entryformtest:valid input (0.00s)
✔entryformtest:invalid input (0.00s)
✔entryformtest:model Property ( 0.00s)
All of this is successful, and if the test fails, a specific failure message is displayed.
Here are basically 3 methods
Expect_that: Suppose to be true
Expect_not: False for Assumption
Expect: Suppose the target object, followed by the Verify method, the specific method list in the vendor/codeception/verify/src/codeception/verify.php file
For more information on YII-related content, readers who are interested in this site can view the topics: Introduction to YII Framework and summary of common skills, "Summary of PHP Excellent development framework", "Smarty Template Introductory Course", "Introduction to PHP object-oriented programming", "PHP string" Summary of Usage , "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"
I hope this article will help you with the PHP program design based on the YII framework.