Document
Check php/phpunit/laravel... version
PHP version
php -v PHP 7.3.30 (cli)
PhpUnit
composer.json ->require -> "phpunit/php-code-coverage": "3.3.3"
Update composer
composer update
Define XML
Xml name: phpunit.xml
Xml example:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="bootstrap/app.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false">
<testsuites>
<testsuite name="Application Test Suite">
//test.php files
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<!-- <directory suffix=".php">./app/</directory> -->
//Cover php files
<file>./app/Http/Controllers/QueueController.php</file>
</whitelist>
</filter>
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="DB_DATABASE" value="queue_ut"/>
</php>
</phpunit>
Install xdebug & enable coverage
4.1 Enable xdebug
Dowload xdebug.dll and put on directory
extension_dir -> D:\Xampp\php\ext
4.2 Enable xdebug & coverage
D:\Xampp\php\php.ini
[xdebug]
zend_extension=xdebug
xdebug.model=debug,coverage
xdebug.remote_enable = 1
xdebug.remote_host = "127.0.0.1"
xdebug.remote_port = 9003
xdebug.remote_handler = "dbgp"
xdebug.remote_mode = req
xdebug.remote_connect_back = 1
xdebug.remote_log="D:\Xampp7.3\php\logs\xdebug.log"
4.3 Check phpinfo()
Tests directory
Test file example
/project_name/tests/AbcControllerTest.php
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use App\Models\Email;
use Illuminate\Foundation\Testing\DatabaseMigrations;
class AbcControllerTest extends TestCase
{
use WithoutMiddleware;
public function testModel()
{
$request = [
"mobile" => "0764300533",
"message"=> "thong tin"
];
$this->post('/process', $request)
->see('OK');
}
}
Run phpunit
just run phpunit
./vendor/bin/phpunit
Run phpunit with coverage
./verdor/bin/phpunit --coverage-html folder_contain_html_files
Example, hình dưới