file upload - How do I get php code coverage to run over phpt test cases? -
i have test environment runs component tests product. found tough test , mock php's is_uploaded_file() , move_uploaded_file() after lot of searching , research came upon phpt. helped me lot testing methods , expectations file uploading. not question file uploading how integrate phpt testcases basic phpunit testcases code coverage run methods being tested well. here follows code extracts:
files.php
class prfiles { // instance methods here not needed purpose of 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) ) { // 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 prfiles::transfer() actual testing of 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 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 new utilitiesfilestransfertest. * */ public function __construct() { parent::__construct(dirname(__file__) . '/_phpt/file_upload_test.phpt'); } } so works. can't seem coverage of transfer method testing. please can me?
edit: coverage command looks :
@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%
as phpunit has custom implementation running phpt files occurs in separate process, getting code coverage integrated phpunit prove quite difficult indeed.
however, if need coverage (or willing post-processing yourself), becomes quite trivial.
in simple form, need make calls xdebug phpt files. using php_codecoverage (and composer class autoloading) --file-- section this:
--file-- <?php /* autoload classes */ require __dir__ . '/../../../vendor/autoload.php'; /* setup , 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 , output coverage data */ $coverage->stop(); $writer = new \php_codecoverage_report_php; $writer->process($coverage, __dir__ . '/../../../build/log/coverage-data.php'); ?> all gathered coverage data put in thecoverage-data.php file.
you load information , combine other coverage information (for instance phpunit) create output in format want.
the coverage logic put separate class leaving 2 lines add each test you'd want 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 , start code coverage */ public static function start() { self::$coverage = new \php_codecoverage; /* make sure file not added coverage data */ $filter = self::$coverage->filter(); $filter->addfiletoblacklist(__file__); self::$coverage->start('test'); } /* stop , 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'); } } as coverage logic lives outside of phpt file can access config files or add other logic.
Comments
Post a Comment