YII2 unit Test Usage Example _php instance

Source: Internet
Author: User
Tags php and smarty template yii

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.