search
Home类库下载PHP类库First introduction to PHPunit stub simulation return data

This is a combination of PHPunit documentation and the explanations of experts during this period. I want to record the knowledge I have learned, supplement it for future reference, and improve what I have learned

We generally use single testing to test the code in the company's business , he will help find every little place where your thinking is not comprehensive enough. (Although Daniel said that development can write unit tests first and then write implementation code, but now I feel that I am still far away from that)

  Stub (stub):

   Replace the object with ( Optionally) The practical method of returning a test double with a configured return value is called stubbing" - this is the explanation of stubbing given in the official documentation

  I only understand it now when I look back at it, how should I say it? Take a pear.

====Premise

 What I want to test is this method: switchClothes($username) ---- Query the database by name. If the gender is 1, it will return pants, if it is 0, it will return skirts;

<?php
        Class Switch{
            public function switchClothes($username){
                $database=new database();
                $gender=$databse->find("id=$username");
                if($gender==0){
                        return "裙子";
                }else{
                        return "裤子";
                }
            }
        }

Query database I encapsulated a DataBase class: find ()

==== Start writing test

First, I need to test the Switchclothes class, but in this category, I need to instantiated through this class. The database class uses the select method to query the database and find out whether I want pants or a skirt. So, it’s really too troublesome. I just want to test the logic of this method. What if the database hangs up? If the username does not exist, do I have to go to the database to create such a piece of data? Too much trouble and not good enough. If you need to test a method that includes updating data, do you really need to modify the data?

The stub has arrived gorgeously. Mom no longer has to worry about me operating the database, nor does she have to worry about the interface being incomprehensible.

First introduction to PHPunit stub simulation return data

I can stub this class. To put it more simply, I think it is a simulation of this class and a fake database class;

   It is as above A=switchClothes B=database class D=database C=stub The class

   It should be called by A B, B queries the database.

  But the existence of C is the red line. C will not check the database. C is under my control. I can specify the find() method inside to return 1 or 0, at least in A seems to be the same as B. Anyway, it will return 0 or 1 to me. This means that C isolates the system A from B and D, reducing coupling;

   Then, I can start constructing the C I need.

  

<?php
use PHPUnit\Framework\TestCase;

class StubTest extends TestCase
{
    

    public function testStub()
    {
        // 为database类建立桩件。
        $stub = $this->getMockBuilder("database")//类名
                             ->setMethods(array(&#39;find&#39;)) //可以是多个方法
                              ->getMock();

        // 配置桩件。
        $stub->method(&#39;find&#39;)//想要设置返回值的方法
             ->willReturn(0);//设置返回值 

        // 现在调用 $stub->select() 将返回 &#39;裙子&#39;。
        $this->assertEquals(&#39;裙子&#39;, $stub->find());
    }
}
?>

This is C.

When taking a single test, just take the red path.

all


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.