How can I get php code coverage to run phpt test cases?

I have a test environment that runs component tests for a product. I found that recently it has been difficult to test and successfully simulate php is_uploaded_file() and move_uploaded_file() , but after many searches and studies I came across PHPT. This helped me a lot in testing these methods and expectations for downloading files. This is not a question of uploading files, but of how to integrate phpt test tags into phpunit base test tags so that code coverage also applies to test methods. Below are some snippets of code:

files.php

 class prFiles { // Instance methods here not needed for the purpose of this question // ...... public function transfer(array $files, $target_directory, $new_filename, $old_filename = '') { if ( (isset($files['file']['tmp_name']) === true) && (is_uploaded_file($files['file']['tmp_name']) === true) ) { // Only check if old filename exists if ( (file_exists($target_directory . '/' . $old_filename) === true) && (empty($old_filename) === false) ) { unlink($target_directory . $old_filename); } $upload = move_uploaded_file( $files['file']['tmp_name'], $target_directory . '/' . $new_filename ); if ( $upload === true ) { return true; } else { return false; } } return false; } } 

file_upload_test.phpt

 --TEST-- Test the prFiles::transfer() the actual testing of the file uploading. --POST_RAW-- Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryfywL8UCjFtqUBTQn ------WebKitFormBoundaryfywL8UCjFtqUBTQn Content-Disposition: form-data; name="file"; filename="test.txt" Content-Type: text/plain This is some test text ------WebKitFormBoundaryfywL8UCjFtqUBTQn Content-Disposition: form-data; name="submit" Upload ------WebKitFormBoundaryfywL8UCjFtqUBTQn-- --FILE-- <?php require_once dirname(__FILE__) . '/../../../src/lib/utilities/files.php'; $prFiles = prFiles::getInstance()->transfer( $_FILES, dirname(__FILE__) . '/../_data/', 'test.txt' ); var_dump($prFiles); ?> --EXPECT-- bool(true) 

UtilitiesFilesTransferTest.php

 class UtilitiesFilesTransferTest extends PHPUnit_Extensions_PhptTestCase { /** * Constructs a new UtilitiesFilesTransferTest. * */ public function __construct() { parent::__construct(dirname(__FILE__) . '/_phpt/file_upload_test.phpt'); } } 

So everything works. But I can’t understand which transfer method I am testing. Please help me?

EDIT: my coverage command is as follows:

 @echo off echo. if not "%1"=="" goto location goto default :location set EXEC=phpunit --coverage-html %1 TestSuite goto execute :default set EXEC=phpunit --coverage-html c:\xampp\htdocs\workspace\coverage\project TestSuite :execute %EXEC% 
+4
source share
2 answers

Since PhpUnit has a custom implementation for running PHPT files that occurs in a separate process, getting code coverage integrated with PhpUnit can be quite difficult.

However, if all you need is coverage (or you are ready to do some post-processing yourself), it becomes pretty trivial.

In its simplest form, all you have to do is make xDebug calls from your PHPT files. Using PHP_CodeCoverage (and Composer to autoload classes), your --FILE-- section might look like this:

 --FILE-- <?php /* autoload classes */ require __DIR__ . '/../../../vendor/autoload.php'; /* Setup and start code coverage */ $coverage = new \PHP_CodeCoverage; $coverage->start('test'); /* run logic */ $prFiles = prFiles::getInstance()->transfer( $_FILES, __DIR__ . '/../_data/', 'test.txt' ); var_dump($prFiles); /* stop and output coverage data */ $coverage->stop(); $writer = new \PHP_CodeCoverage_Report_PHP; $writer->process($coverage, __DIR__ . '/../../../build/log/coverage-data.php'); ?> 

All collected coverage data will be placed in the coverage-data.php .

You can download this information and combine it with other coverage information (for example, from PhpUnit) to create an output in any format.

Coverage logic can be placed in a separate class, leaving you only two lines to add to each test that you want to cover:

 --FILE-- <?php /* autoload classes */ require __DIR__ . '/../../../vendor/autoload.php'; cover::start; /* run logic */ $prFiles = prFiles::getInstance()->transfer( $_FILES, __DIR__ . '/../_data/', 'test.txt' ); var_dump($prFiles); cover::stop; ?> 

And cover class:

 <?php class cover { private static $coverage; /* Setup and start code coverage */ public static function start() { self::$coverage = new \PHP_CodeCoverage; /* Make sure this file is not added to the coverage data */ $filter = self::$coverage->filter(); $filter->addFileToBlacklist(__FILE__); self::$coverage->start('test'); } /* stop and output coverage data */ public static function stop() { self::$coverage->stop(); $writer = new \PHP_CodeCoverage_Report_PHP; $writer->process(self::$coverage, __DIR__ . '/../build/log/coverage-data.php'); } } 

Since the coverage logic lives outside of the PHPT file, you can easily access the configuration files or add other logic.

+1
source

I do not know why PHPUnit will not collect coverage data for you. This may have something to do with how it uses (or doesn't use) XDebug.

You can get around this by using a testing tool that does not depend on how PHPUNit or XDebug works.

Our PHP Test Coverage will collect test coverage for any function on any PHP script that has been proposed to track, regardless of how this function is executed. He should have no problem providing coverage data to perform functions called by PHPT.

-3
source

Source: https://habr.com/ru/post/1347550/


All Articles