First recognized PHPunit stub to simulate returned data, phpunitstub

Source: Internet
Author: User

First recognized PHPunit stub to simulate returned data, phpunitstub

This is a combination of the PHPunit document and the introduction from the audience. I want to record my learned knowledge, and I will provide some reference in the future to improve my learning experience.

We usually use a single test to test the code in the company's business. He will help you find out what you think is not comprehensive enough. (Although Daniel said that developers can write a single test before writing the implementation code, I feel that there are still a lot of distance from that one)

 

Stub (pile ):

The practical method to replace an object with (optional) A test proxy that returns the configured return value is calledStubbing)"-- This is the above explanation given in the official document.

I can only understand it after looking back. What should I do? Let's take a pear.

==== Prerequisite

I want to test the following method: switchClothes ($ username) ---- query the database by name. If the gender is 1, the trousers are returned. If the name is 0, the skirts are returned;

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

 

I have encapsulated find () in a Database class for Database query ()

 

==== Start write Test

First, I need to test the switchClothes class, but in this class, I need to use the select method to instantiate the database class, and then query the database to see whether I want pants or skirts. So, it is really too much trouble. I just want to test the logic of this method. If the database crashes, in case this username does not exist, is it too troublesome for me to create such a piece of data in a database. If you need to test the method for updating data, do you need to modify the data?

Stub is coming to Lili. Mom no longer has to worry that I am going to operate the database, and I am no longer worried that the interface cannot be connected.

    

I can pile up this class. To put it bluntly, I think it is a simulation of this class and a fake database class;

For example, A = switchClothes B = database Class D = database C = stub class

A should have called B and B to query the database.

But the existence of C follows the red line, and C won't check the database. C is under my control. I can specify find () in it () the return value of 1 or 0 is at least the same as that of B in A's opinion. Anyway, A value of 0 or 1 is returned to me. This means that C isolates A from B and D and reduces coupling;

Then, we can start to construct the C that I need.

    

<? Phpuse PHPUnit \ Framework \ TestCase; class StubTest extends TestCase {public function testStub () {// create a pile for the database class. $ Stub = $ this-> getMockBuilder ("database") // class name-> setMethods (array ('Find ')) // you can use multiple methods-> getMock (); // configure the pile. $ Stub-> method ('Find ') // method to set the return value-> willReturn (0); // set the return value // call $ stub-> select () now () the return value is 'skirt '. $ This-> assertEquals ('skirt ', $ stub-> find () ;}}?>

This is C.

During the single test, the red line is enough.

All

 

 

   

    

   

 

  

  

  

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.