sebastianbergmann / php-code-coverage

Library that provides collection, processing, and rendering functionality for PHP code coverage information.
BSD 3-Clause "New" or "Revised" License
8.82k stars 376 forks source link

Code-coverage cannot capture closure or anonymous functions #612

Closed ajcastro closed 6 years ago

ajcastro commented 6 years ago
Q A
PHPUnit version PHPUnit 7.0.3
PHP version PHP 7.2.4
Installation Method Composer

image

sebastianbergmann commented 6 years ago

Thank you for your report.

Please provide a minimal, self-contained, reproducing test case that shows the problem you are reporting.

Without such a minimal, self-contained, reproducing test case I will not be able to investigate this issue.

sebastianbergmann commented 6 years ago

Also note that PHPUnit 7.0 is no longer supported. Please try to reproduce the issue with the latest version of PHPUnit 7.1 as well as the latest version of php-code-coverage.

hartono-sulaiman-kaskus commented 6 years ago

i also had this problem

PHPUnit 7.1.5
phpunit/php-code-coverage 6.0.4

PHP 7.1.16 (cli) (built: Mar 31 2018 02:27:47) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.1.16, Copyright (c) 1999-2018, by Zend Technologies
    with Xdebug v2.5.1, Copyright (c) 2002-2017, by Derick Rethans

my code is like this

$cacheKey = Article::CACHE_KEY_LIST . '_' .
            $cursorParameter->getParameter('last_published_date') . '_' .
            $cursorParameter->getParameter('last_id') . '_' .
            $limit;

        return CollectionWithMeta::make(
            Cache::tags(Article::CACHE_KEY_LIST)
                ->remember(
                    $cacheKey,
                    config('cache.lifetime'),
                    function () use ($cursorParameter, $limit) {
                        return $this->repository->readManyByCursorAndStatus(
                            $cursorParameter,
                            $limit,
                            Article::STATUS_PUBLISHED
                        );
                    }
                )
        );

i got this coverage

screen shot 2018-05-07 at 9 23 13 am

but if i make it to one line, i got this screen shot 2018-05-07 at 9 24 33 am

but this is the weirdest

screen shot 2018-05-07 at 11 08 25 am

sebastianbergmann commented 6 years ago

@hartono-sulaiman-kaskus That, too, is not a minimal, self-contained, reproducing test case that I (or @derickr) can use to debug this issue in php-code-coverage (or Xdebug).

hartono-sulaiman-kaskus commented 6 years ago

okay this is my folder structure

project project/src project/src/Product.php

<?php
class Product
{
    public function run()
    {
        $c = 'a' . 'b' . 'c';
        $this->methodA(
            '1',
            '2',
            $c,
            [],
            []
        );
        return true;
    }

    private function methodA(
        string $a,
        string $b,
        ?string $c = null,
        array $d = [],
        array $e = []
    ) {
        echo "$a $b $c\n";
    }
}

project/src/tests/ProductTest.php

<?php

use PHPUnit\Framework\TestCase;

class ProductTest extends TestCase
{
    public function testRun()
    {
        $product = new \Product();
        $this->assertTrue($product->run());
    }
}

project/composer.json

{
  "require-dev": {
    "phpunit/phpunit": "~7.0"
  },
  "autoload": {
    "classmap": [
      "src"
    ]
  }
}

project/phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="vendor/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./src</directory>
        </whitelist>
    </filter>
</phpunit>

is it enough?

thanks

stale[bot] commented 6 years ago

This issue has been automatically marked as stale because it has not had activity within the last 60 days. It will be closed after 7 days if no further activity occurs. Thank you for your contributions.

CJDennis commented 6 years ago

@sebastianbergmann Isn't it a bit unfair to push this back onto the users? How are we supposed to produce a minimal example? I've never seen this bug in tiny code and no-one is going to submit their entire project (i.e. a breach of confidentiality) for you to look at, which you'd reject anyway for being too big.

stale[bot] commented 6 years ago

This issue has been automatically marked as stale because it has not had activity within the last 60 days. It will be closed after 7 days if no further activity occurs. Thank you for your contributions.

stale[bot] commented 6 years ago

This issue has been automatically closed because it has not had activity since it was marked as stale. Thank you for your contributions.