Testing private and protected functions in php is normally done through testing the public function it calls. But sometimes you need to test the private or protected functions separately. How can we do this? Lets consider the following simple example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
When you try to run the test class you will get the following error.
PHP Fatal error: Call to private/protected method MyClass::myProtectedMethod() from context 'MyTestClass'
After version 5.3.2 PHP's "Reflection Class" makes it possible to temporarily work around access modifiers. This is done using the setAccessiblefunction. Then you need to change your test class as follows.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
For Google
Summer of Code (GSoC) 2012 I submitted a proposal on "AutomatedTesting" project listed by phpmyadmin (PMA). Michal Čihař, Marc
Delisle, Madhura Jayaratneand
Stas Zarubinwho
is the last year GSoC contributor helped me a lot when I
improve my patches and the proposal. Michal Čihařact
as my mentor for my project in this summer of code. PMA
is a free software tool written in PHP, intended to handle the
administration of MySQL over the web. Automated testing is a
project idea which continues from the last year GSoC session which
was partially done by Stas Zarubin. Expectation of the project is
automate the testing in PMA by writing uni test cases and selenium
test cases using phpunit and improve the code coverage. Last year
Stas Zarubin has written some test cases for the PMA libraries and
classes but couldn't complete all. In this time I am improving the
written test cases coverage and write new test cases for the
remaining libraries and classes.
In
the next series of blog post I will discuss more about my
project in this summer.
Hi All. We will discuss how to write your first unit test case using PHPUnit. If you are not configured your testing environment yet, refer my first post.
Unit testing is a method of testing that verifies the individual units of source code are working properly. Normally we are considering a function (method) as our unit of source code.
Assume we are writing a function to add two numbers in class operation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Copy the above code into a empty text file and name it as operation.php. Now we need to write a test case for function add(); Function add() accepting two parameters and returning the addition of those. So what we have to test is passing two values to the add method and check whether we are getting the expected output.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Create the operationTest.php file and copy the above code into it. The class should extend the PHPUnit_Framework_TestCase. In function testAdd(), it passes two values for the add method and getting the return value. As we passes 2 and 3 the return valued should be 5. We can verify it using assertions. We used assertEquals() to check the return value and the expected values are equals. If it equals the test will pass.
Running the Unit Test
Place the above two file in a same folder and run the following command.
$ phpunit operationTest.php
You will get the following.
PHPUnit 3.6.10 by Sebastian Bergmann.
.
Time: 0 seconds, Memory: 3.50Mb
OK (1 test, 1 assertion)
For each test run, the PHPUnit command-line tool prints one character to indicate progress.
. Printed when the test succeeds.
F Printed when an assertion fails while running the test method.
E printed when an error occurs while running the test method.
S Printed when the test has been skipped.
I Printed when the test is marked as being incomplete or not yet implemented.
Change the expected value into some other value and run the test. You will have test failure.
PHPUnit 3.6.10 by Sebastian Bergmann.
F
Time: 0 seconds, Memory: 3.50Mb
There was 1 failure:
1) operationTest::testAdd
Failed asserting that 2 matches expected 5.
/var/www/codemix/operationTest.php:15 FAILURES!
Change the method in to some other name and run the test. You will have fetal error.
PHPUnit 3.6.10 by Sebastian Bergmann.
PHP Fatal error: Call to undefined method operation::sum() in
/var/www/codemix/operationTest.php on line 14. .............
We can write multiple tests for one method.Add following test method to operationTest.php to test addition of decimals values.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In good test cases there should be test cases for testing the success scenarios as well as negative scenarios. Add following test cases to operationTest.php. It checks the return value is not equal to a invalid output.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hi All. Here I am going to discuss how to write unit tests using PHPUnit. In this post I am listing down the necessary steps to configure your testing environment. PHPUnit
is unit testing software frame work for php programming language developed by Sebastian
Bergmann. It is one of the xUnit family of framework.
Set
up the environment
PHPUnit
should be installed using the PEAR Installer.
Upgrading
PEAR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
If you have version less than 1.9 it is recommended to update the PEAR channel and upgrade the PEAR.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PHPUnit depends on package pear/XML_RPC2 that requires curl.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Need to install Xdebug to run the tests and generate code coverage.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters