I have talked a lot about using Mockery in PHP for test-driven Development (TDD) testing-driven development on the Internet, and some articles on PHP are also found in Baidu and Google, I don't seem to have seen several articles about Mock (disguised object) technology. I will write an article here.
First, let's take a look at the basic idea of Test-Driven Development: write test cases first (generally, this test case is an automated unit test case to facilitate fast rollback and execution ), then, the product code is supplemented by gradually repairing the test cases. after the test cases are repaired, the product is finished.
In my own practice, I think using the test-driven development technology during class library development is a good solution for the following reasons:
The ability to write test cases indicates that you have a clear understanding of the problem domain,
This saves time for writing documents. the test case is the sample code called by the class library.
The code quality is ensured. because the process of writing a class library is the process of repairing test cases, the class library will be completed after the test cases are repaired.
When it is easy to estimate, the problem of estimating the development time of the class library is simplified to estimating the time for fixing the test case, which is easier to estimate.
We will illustrate the idea of test-driven development by writing a string-to-number function, and then introduce Mock technology. Before you start, you need to install the PHPUnit and Mockery libraries (this article does not use the Mock library that comes with PHPUnit ):
# Install PHPUnitpear config-set auto_discover 1 pear install plugin install the Mock library sudo pear channel-discover pear. unzip vethedeepend. comsudo pear channel-discover inclupear install -- alldeps deepend/Mockery
Let's write test cases first from the idea of test-driven development? Parseinttest. php:
assertEquals(12345, $v); $v = parse_int("-12345"); $this->assertEquals(-12345, $v); /* $v = parse_int("abcd"); $this->assertEquals(0, $v); $v = parse_int("0xab12"); $this->assertEquals(0xab12, $v); $v = parse_int("01b"); $this->assertEquals(1, $v); */ } }?>
In the above code, we use unit test cases to specify the need for the parse_int function to be implemented, that is, we can parse integers, signed integers, and so on, because of time and resource restrictions, then we removed the hexadecimal and binary support.
Run the following command to run the test case:
phpunit --verbose parseinttest
What is the expected error because there is no code at this time? PHPUnit tells us that the parse_int function cannot be found:
PHPUnit 3.6.12 by Sebastian Bergmann.
PHP Fatal error: Call to undefined function parse_int () in/var/www/pmdemo/parseinttest2.php on line 6
So we should first create an empty parse_int function? Parseint. php:
In the above code, we do not implement any logic of the parse_int function, and then run the test case to obtain:
Shiyimin @ ubuntu:/var/www/pmdemo $ phpunit -- verbose parseinttest
PHPUnit 3.6.12 by Sebastian Bergmann.
F
Time: 0 seconds, Memory: 2.75 Mb
There was 1 failure:
1) ParseIntTest: testParseIntBasic
Failed asserting that 0 matches expected 12345.
/Var/www/pmdemo/parseinttest. php: 7
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
From the error message highlighted in red above, we know that the first judgment in the test case fails, that is, the following judgment statement is not successfully executed:
$this->assertEquals(12345, $v);
This is because our parse_int function always returns the value 0. when this error is found, we can complete this logic to fix the test case:
Parseint. php
Run the test case again:
Shiyimin @ ubuntu:/var/www/pmdemo $ phpunit -- verbose parseinttest
PHPUnit 3.6.12 by Sebastian Bergmann.
F
Time: 0 seconds, Memory: 2.75 Mb
There was 1 failure:
1) ParseIntTest: testParseIntBasic
Failed asserting that 12345 matches expected-12345.
/Var/www/pmdemo/parseinttest. php: 10
FAILURES!
Tests: 1, Assertions: 2, Failures: 1.
From the above results, we can see that the first test case has been fixed. now the problem occurs when processing the signed string. continue to fix the code:
Parseint. php
Run the test case again:
Shiyimin @ ubuntu:/var/www/pmdemo $ phpunit -- verbose parseinttest
PHPUnit 3.6.12 by Sebastian Bergmann.
.
Time: 0 seconds, Memory: 2.75 Mb
OK (1 test, 2 assertions)
Well, all the test cases passed this time, so the implementation of our product code has come to an end. of course, the above code is not a complete implementation for the convenience of the article, for example, it cannot process the string "+ 12345.
The next article explains how to apply Mock technology in PHPUnit.