Download
We distribute a PHP Arhive (PHAR) that contains everything you need in order to use PHPUnit. Simply download it from here, make it executable, and put it into your $PATH, for instance:
➜ wget https://phar.phpunit.de/phpunit.phar
➜ chmod +x phpunit.phar
➜ mv phpunit.phar /usr/local/bin/phpunit
➜ phpunit --version
PHPUnit 4.0.0 by Sebastian Bergmann.
You can also immediately use the PHAR after you have downloaded it, of course:
➜ wget https://phar.phpunit.de/phpunit.phar
➜ php phpunit.phar --version
PHPUnit 4.0.0 by Sebastian Bergmann
Alternatively, you may use Composer or the PEAR Installer to download and install PHPUnit as well as its dependencies. Please refer to the documentation for details on how to do this.
<?php
class Money
{
private $amount;
public function __construct($amount)
{
$this->amount = $amount;
}
public function getAmount()
{
return $this->amount;
}
public function negate()
{
return new Money(-1 * $this->amount);
}
// ...
}
<?php
class MoneyTest extends PHPUnit_Framework_TestCase
{
// ...
public function testCanBeNegated()
{
// Arrange
$a = new Money(1);
// Act
$b = $a->negate();
// Assert
$this->assertEquals(-1, $b->getAmount());
}
// ...
}
Test Execution
➜ phpunit --bootstrap src/autoload.php tests/MoneyTest
PHPUnit 4.0.0 by Sebastian Bergmann.
....................
Time: 121 ms, Memory: 4.50Mb
OK (20 tests, 39 assertions)
Lets have a look at what the three parts of above’s invokation mean:
phpunitinvokes the PHPUnit command-line test runner. We assume that you have downloadedphpunit.phar(see above) and put it into your$PATHasphpunit.--bootstrap src/autoload.phpinstructs the PHPUnit command-line test runner to includesrc/autoload.phpbefore the test execution. Such a bootstrap script is commonly used to set up autoloading for the classes that are to be tested.tests/MoneyTestinstructs the PHPUnit command-line test runner to execute the tests of theMoneyTestclass that is declared intests/MoneyTest.php.Usingtestsinstead oftests/MoneyTestwould instruct the PHPUnit command-line test runner to execute all tests found declared in*Test.phpsourcecode files in thetestsdirectory.