热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

laravel黄昏的代码覆盖率

如何解决《laravel黄昏的代码覆盖率》经验,为你挑选了1个好方法。

运行Laravel Dusk时有没有办法获得代码覆盖?

我知道它运行浏览器测试,所以它不是仔细检查代码,但有没有办法添加一个监听器来检查所涵盖的代码?我现在没有看到关于这个主题的任何内容.



1> Darryl E. Cl..:

Conceptually, you need to bootstrap all your requests w/ PHP Unit's code coverage tools.

You can do this with phpunit libraries directly, or via xdebug's coverage tools (which use phpunit).

From this sample gist that I found, you can start coverage tools based on a couple of _GET parameters passed via the Dusk test.

public function testBasicExample()
{
      $this->browse(function (Browser $browser) {
          $browser->visit(route('test', [
              'test_name' => 'testBasicExample',
              'coverage_dir' => '/app/Http'
          ]))->assertSee('test');
      });
  }

The code that does the work is two parts 1. Start collecting based on parameters:

$test_name = $_GET['test_name'];
require __DIR__ . '/../vendor/autoload.php';
$current_dir = __DIR__;
$coverage = new SebastianBergmann\CodeCoverage\CodeCoverage;
$filter = $coverage->filter();
$filter->addDirectoryToWhitelist(
    $current_dir . '/..' . ((isset($_GET['coverage_dir']) && $_GET['coverage_dir'])
        ? $_GET['coverage_dir']
        : '/app')
);
$coverage->start($test_name);

And 2 end collecting and output:

function end_coverage()
{
    global $test_name;
    global $coverage;
    global $filter;
    global $current_dir;
    $coverageName = $current_dir . '/coverages/coverage-' . $test_name . '-' . microtime(true);
    try {
        $coverage->stop();
        $writer = new \SebastianBergmann\CodeCoverage\Report\Html\Facade;
        $writer->process($coverage, $current_dir . '/../public/report/' . $test_name);
        $writer = new SebastianBergmann\CodeCoverage\Report\PHP();
    } catch (Exception $ex) {
        file_put_contents($coverageName . '.ex', $ex);
    }
}

The end collection is called using a clever little trick where the class coverage_dumper has just a destructor, which gets called automatically when php ends the process.

The code itself can use a bit of tidy up as far as output paths, and variables go, but from a concept, it should work.


推荐阅读
author-avatar
撩人的醉了
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有