Difference between revisions of "PHPUnit"

From ArchWiki
Jump to: navigation, search
(Additional)
(Replace PEAR installation instructions with PHAR package instructions. http://phpunit.de/manual/current/en/installation.html#installation.phar)
(9 intermediate revisions by 6 users not shown)
Line 1: Line 1:
[[Category:Software (English)]]
+
[[Category:System administration]]
 
== Installing ==
 
== Installing ==
  
First of all, you need PEAR to install PHPUnit.
+
By far the easiest way to install is using the PHP Archive ([http://php.net/phar PHAR]) package provided by the project. The PHAR package contains all dependencies as well as some of the optional dependencies for PHPUnit. The latest version can be retrieved from the project's site:
  pacman -S php-pear
+
  wget https://phar.phpunit.de/phpunit.phar
  
Now you can do just like the [http://www.phpunit.de/manual/current/en/installation.html ''official documentation'']. As root, just type
+
You can then run PHPUnit using {{ic|php phpunit.phar}}. You can also make the PHP Archive executable ({{ic|chmod +x phpunit.phar}}) and move it to /usr/local/bin or ~/bin or somewhere else you have in your {{ic|$PATH}}.
pear channel-discover pear.phpunit.de
+
pear channel-discover components.ez.no
+
pear channel-discover pear.symfony-project.com
+
pear install phpunit/PHPUnit
+
  
Done. <br>
+
{{Note|You have to enable {{ic|1=extension=phar.so}} in your php.ini for PHP to be able to run PHP Archive/PHAR files.}}
  
== Running ==
+
== Example ==
  
For running PHPUnit properly you must have the /usr/bin/ directory listed in the open_basedir option at the /etc/php/php.ini
+
This section gives beginners a very brief introduction to how to use PHPUnit to run test cases. It won't explain how to write them but if you want to get more information about this have a look at the references.
open_basedir = /srv/http/:/home/:/tmp/:/usr/share/pear/:/usr/bin/
+
Or just unset it
+
open_basedir =
+
  
== Additional ==
+
As the application to be tested we use in this example a [https://github.com/hasbridge/php-json-schema| JSON schema validator]. In the directory ''tests'' you'll see the directory ''mock'' and three files.
  
In some php framework, you will meet the error of missing Selenium. Just install the selenium package with pear ( make sure you have root right )
+
* mock
  pear install phpunit/PHPUnit_Selenium
+
* JsonValidatorTest.php
 +
* bootstrap.php
 +
* phpunit.xml
 +
 
 +
''mock'' contains JSON schemas which have nothing to do with PHPUnit itself, it's application-specific here, you won't find it in other applications.
 +
 
 +
''phpunit.xml is a [http://www.phpunit.de/manual/current/en/appendixes.configuration.html configuration file] where you
 +
 
 +
* configure PHPUnit's core functionality,
 +
* compose a test suite out of test suites and test cases,
 +
* select groups of tests from a suite of tests that should (not) be run,
 +
* configure the blacklist and whitelist for the code coverage reporting,
 +
* configure the logging of the test execution,
 +
* attach additional test listeners to the test execution,
 +
* configure PHP settings, constants, and global variables or
 +
* configure a list of Selenium RC servers.
 +
 
 +
In ''bootstrap.php'' you put code to be run before tests are executed. Here you could register your autoloading functions or include other php scripts. Though there's one limitation, only one bootstrap can be defined per PHPUnit configuration file.
 +
 
 +
''JsonValidatorTest.php'' is the PHP file with the test cases. It's beyond the scope to explain the content of this file in depth. As a beginner you are most likely interested in how to run the test cases so what you need to execute is simply
 +
 
 +
<pre>phpunit JsonValidatorTest JsonValidatorTest.php</pre>
 +
 
 +
You pass it the class with the test cases and the file where they're defined. That's it.
 +
 
 +
== References ==
 +
 
 +
* [http://www.phpunit.de/manual/current/en/index.html Official PHPUnit Manual ]
 +
* [http://www.slideshare.net/DragonBe/introduction-to-unit-testing-with-phpunit-presentation-705447 Introduction to Unit Testing with PHPUnit ]

Revision as of 18:49, 20 October 2013

Installing

By far the easiest way to install is using the PHP Archive (PHAR) package provided by the project. The PHAR package contains all dependencies as well as some of the optional dependencies for PHPUnit. The latest version can be retrieved from the project's site:

wget https://phar.phpunit.de/phpunit.phar

You can then run PHPUnit using php phpunit.phar. You can also make the PHP Archive executable (chmod +x phpunit.phar) and move it to /usr/local/bin or ~/bin or somewhere else you have in your $PATH.

Note: You have to enable extension=phar.so in your php.ini for PHP to be able to run PHP Archive/PHAR files.

Example

This section gives beginners a very brief introduction to how to use PHPUnit to run test cases. It won't explain how to write them but if you want to get more information about this have a look at the references.

As the application to be tested we use in this example a JSON schema validator. In the directory tests you'll see the directory mock and three files.

  • mock
  • JsonValidatorTest.php
  • bootstrap.php
  • phpunit.xml

mock contains JSON schemas which have nothing to do with PHPUnit itself, it's application-specific here, you won't find it in other applications.

phpunit.xml is a configuration file where you

  • configure PHPUnit's core functionality,
  • compose a test suite out of test suites and test cases,
  • select groups of tests from a suite of tests that should (not) be run,
  • configure the blacklist and whitelist for the code coverage reporting,
  • configure the logging of the test execution,
  • attach additional test listeners to the test execution,
  • configure PHP settings, constants, and global variables or
  • configure a list of Selenium RC servers.

In bootstrap.php you put code to be run before tests are executed. Here you could register your autoloading functions or include other php scripts. Though there's one limitation, only one bootstrap can be defined per PHPUnit configuration file.

JsonValidatorTest.php is the PHP file with the test cases. It's beyond the scope to explain the content of this file in depth. As a beginner you are most likely interested in how to run the test cases so what you need to execute is simply

phpunit JsonValidatorTest JsonValidatorTest.php

You pass it the class with the test cases and the file where they're defined. That's it.

References