Introduction
Before we discuss how to install PHPUnit on Ubuntu 22.04, let's first understand-What is PHPUnit?
PHPUnit is a widely-used testing framework for PHP, used to write and run unit tests for PHP code. Installing PHPUnit on Ubuntu 22.04 can greatly simplify the process of writing and executing tests, ensuring the quality and reliability of your PHP applications.
This tutorial will explain how to install PHPUnit on Ubuntu 22.04.
Advantages
- Automated Testing: PHPUnit enables you to define and run automated tests for your PHP code, helping to catch bugs and ensure the correctness of your application.
- Robust Test Suite: PHPUnit provides a comprehensive set of assertions and testing tools that allow you to thoroughly test different aspects of your PHP code, including functions, classes, and APIs.
- Test-Driven Development (TDD): PHPUnit supports Test-Driven Development, allowing you to write tests first and then develop code to fulfill those tests. This iterative approach helps produce more reliable and maintainable code.
- Code Coverage: PHPUnit can generate code coverage reports, which show which parts of your codebase are covered by tests. This helps you identify areas that need more testing and ensures maximum test coverage.
- Integration with Build Tools: PHPUnit seamlessly integrates with several build tools and Continuous Integration (CI) systems, enabling you to automate tests as part of your development workflow.
Steps to install PHPUnit on Ubuntu 22.04
Step 1. Update Ubuntu Linux
Go to your Ubuntu command terminal and start running the system update command. That will install the latest available security and application packages.
sudo apt update && sudo apt upgrade
Step 2. Install some PHP dependencies
Well to run PHPUnit to test code, PHP language must be installed on your system along with a few dependencies/extensions as shown in the below command:
sudo apt install php-cli php-json php-mbstring php-xml php-pcov php-xdebug
Method 1. Using APT Package Manager
Install PHPUnit on Ubuntu 22.04
Using Ubuntu's default system repository and APT packager manager, you can install the PHPUnit software testing framework's long-term supported version. Thus, all you have to do in your command prompt is type the following:
sudo apt install phpunit
Method 2. Using PHPUnit.phar
Download and configure Phar of PHPUnit
This solution allows us to execute PHPUnit on Linux systems, including Ubuntu, by downloading it directly in Phar format. In contrast to the APT method in this one, we are able to download and utilize both the most recent and older versions of PHPUnit as needed.
Enter the provided command into your terminal.
wget -O phpunit.phar https://phar.phpunit.de/phpunit-10.phar
Make the downloaded Phar file executable:
chmod +x phpunit.phar
Move it to the local bin directory, so that we can use it anywhere from the terminal.
sudo mv phpunit.phar /usr/local/bin/phpunit
Reload Bash session:
newgrp
Check PHPUnit version
phpunit --version
Method 3. Using PHP Composer
Install PHP-Composer
Download the Composer installation script:
curl -sS https://getcomposer.org/installer -o composer-setup.php
Start the installation process:
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
Install PHPUnit using Composer
Once the Composer is on your system, run:
composer global require phpunit/phpunit
To use PHPUnit globally after installing through composer, use:
echo 'export PATH="$PATH:$HOME/.config/composer/vendor/bin"' >> ~/.bashrc
reload session
newgrp
Test some PHP code
Let’s confirm PHPUnit is working by testing a simple piece of PHP code. Here is that:
nano first.php
Add the following code:
<?php
class Rectangle {
public $width;
public $height;
public function area() {
return $this->width * $this->height;
}
}
// PHPUnit test
class first extends \PHPUnit\Framework\TestCase {
public function testArea() {
$rectangle = new Rectangle();
$rectangle->width = 5;
$rectangle->height = 10;
$this->assertEquals(50, $rectangle->area());
}
}
Save the file by pressing Ctrl+X, Y, and then Enter key.
Now test your code using PHPUnit
phpunit first.php
Uninstallation
Want to completely remove PHPUnit from your Ubuntu Linux, then as per the method you have used to install it, go for the uninstallation as well.
For the APT method:
sudo apt autoremove --purge phpunit
For PHAR:
sudo rm /usr/local/bin/phpunit
newgrp
For Composer:
composer remove phpunit/phpunit
FAQs to Install PHPUnit on Ubuntu 22.04
Does PHPUnit support the latest PHP version?
Yes, PHPUnit supports the latest PHP versions and is regularly updated to maintain compatibility.
Can I use PHPUnit with any PHP framework?
PHPUnit is a versatile testing framework and can be used with any PHP framework, including Laravel, Symfony, CodeIgniter, and more.
How do I run PHPUnit tests?
PHPUnit tests are typically run through the command line using the phpunit
command. Navigate to your project's directory and run phpunit
to execute tests.
Where should I place my PHPUnit tests?
Conventionally, PHPUnit tests are placed in a separate directory, such as a tests
folder, within your project's directory. Tests should be organized into appropriate subdirectories.
Can I run specific tests or test suites with PHPUnit?
Yes, PHPUnit allows you to specify individual tests or test suites using the --filter
option while running the phpunit
command.
How do I generate a code coverage report with PHPUnit?
PHPUnit can generate code coverage reports by specifying the --coverage-html
or --coverage-clover
option along with the phpunit
command. This produces HTML or XML reports, respectively.
Can I mock or stub dependencies in PHPUnit tests?
Yes, PHPUnit provides functionalities for creating mocks and stubs, allowing you to isolate dependencies and make your tests more focused and reliable.
Conclusion
We have covered various methods and steps on how to install and uninstall PHPUnit on Ubuntu 22.04 in this tutorial.
If you have any queries, feel free to ask them in the comments section, and we would be happy to respond to them....