PHPUnit is a unit testing framework for the PHP programming language. It provides a structure for writing automated tests to ensure that PHP code functions as expected. Unit tests are a common practice in software development, where small units of code are tested individually to ensure that each one functions correctly.
The main function of PHPUnit is to automate the testing process, allowing developers to write tests that can be executed repeatedly whenever there is a modification to the code. This helps ensure that changes to the code do not break existing functionalities.
PHPUnit is primarily used to test PHP classes and methods. It allows you to create tests to verify whether a particular class or method produces the expected result with different inputs. This is done by defining test cases, where the inputs to be provided to the code and the expected results are specified. PHPUnit then executes these test cases and reports whether the code is functioning as expected or if there are any issues.
In addition to unit tests, PHPUnit this project supports integration tests. This allows to test not only individual units of code but also the interaction between different components and ensures that changes made to the code do not introduce new bugs.
In summary, PHPUnit is an essential tool for ensuring the quality of PHP code, automating the testing process, and allowing to quickly identify and fix issues in the code. It plays a fundamental role in the development of high-quality and reliable software.
<?php
namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function test_example()
{
$this->assertTrue(true);
}
}
<?php
namespace Tests\Feature;
use Tests\TestCase;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function test_example()
{
$response = $this->get('/');
$response->assertOk();
}
}
php artisan test
vendor/bin/phpunit --coverage-html storage/coverage-report/
How to check if xdebug module is loaded:
php -m
How to install xdebug from mac:
pecl install xdebug
How to install xdebug:
apt-get -y install php-xdebug
How to know where is the php.init file:
php --ini | grep "Loaded Configuration File"
Edit php.ini file: nano /opt/homebrew/etc/php/{version}/php.ini
zend_extension="xdebug.so"
xdebug.mode=coverage