This is a combination of PHPunit documentation and the explanations of experts during this period. I want to record the knowledge I have learned and supplement it for future reference to improve what I have learned
We generally use single testing to test the code in the company's business, and it will help find every little place where your thinking is not comprehensive enough. (Although Daniel said that you can write unit tests first and then write the implementation code, but now I feel that there is still a long way to go)
Stub (stub):
"The practical method of replacing an object with a test double that (optionally) returns a configured return value is called stubbing" - This is the explanation of stubbing given by the official documentation
Now that I look back at it, I understand it. How should I put 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;
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">Class</span> <span style="color: #0000ff;">Switch</span><span style="color: #000000;">{ </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> switchClothes(<span style="color: #800080;">$username</span><span style="color: #000000;">){ </span><span style="color: #800080;">$database</span>=<span style="color: #0000ff;">new</span><span style="color: #000000;"> database(); </span><span style="color: #800080;">$gender</span>=<span style="color: #800080;">$databse</span>->find("id=<span style="color: #800080;">$username</span>")<span style="color: #000000;">; </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$gender</span>==0<span style="color: #000000;">){ </span><span style="color: #0000ff;">return</span> "裙子"<span style="color: #000000;">; }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #0000ff;">return</span> "裤子"<span style="color: #000000;">; } } }</span>
To query the database, I encapsulated a Database class: find()
====Start writing tests
The first thing I need to test is the switchClothes class, but in this class I need to instantiate the database class and use the select method, query the database and then 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.
I can stub this class. To put it simply, I think it is a simulation of this class and a fake database class;
As above A=switchClothes B=database class D=database C=stub class
A should originally call B, and B should query the database.
But the existence of C is marked by the red line. C will not check the database. C is controlled by me. I can specify the find() method inside to return 1 or 0. At least from the perspective of A, it is 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.
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">use</span><span style="color: #000000;"> PHPUnit\Framework\TestCase; </span><span style="color: #0000ff;">class</span> StubTest <span style="color: #0000ff;">extends</span><span style="color: #000000;"> TestCase { </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> testStub() { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 为database类建立桩件。</span> <span style="color: #800080;">$stub</span> = <span style="color: #800080;">$this</span>->getMockBuilder("database"<span style="color: #000000;">)//类名 </span>->setMethods(<span style="color: #0000ff;">array</span>('find')) <span style="color: #008000;">//</span><span style="color: #008000;">可以是多个方法</span> -><span style="color: #000000;">getMock(); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 配置桩件。</span> <span style="color: #800080;">$stub</span>->method('find'<span style="color: #000000;">)//想要设置返回值的方法 </span>->willReturn(0<span style="color: #000000;">);//设置返回值 </span><span style="color: #008000;">//</span><span style="color: #008000;"> 现在调用 $stub->select() 将返回 '裙子'。</span> <span style="color: #800080;">$this</span>->assertEquals('裙子', <span style="color: #800080;">$stub</span>-><span style="color: #000000;">find()); } } </span>?>
This is C.
When taking a single test, just take the red path.
all