Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code coverage elseif not marked as executed #531

Closed
frankvanhest opened this issue May 31, 2017 · 6 comments
Closed

Code coverage elseif not marked as executed #531

frankvanhest opened this issue May 31, 2017 · 6 comments

Comments

@frankvanhest
Copy link

Q A
php-code-coverage version 5.2.1
PHP version 7.1.5
Driver Xdebug
Xdebug version (if used) 2.6.0-dev
Installation Method Composer
Usage Method PHPUnit
PHPUnit version (if used) 6.1.4

When testing the following class the elseif is marked in the code coverage as not executed, but the lines inside the elseif are marked as executed.

namespace Acme\Test;

class Test
{
    private $test;

    public function __construct(string $json)
    {
        $this->test = json_decode($json);
    }

    public function doSomething(): string
    {
        if (isset($this->test->foo)) {
            return 'foo';
        } elseif (isset($this->test->bar)) {
            return 'bar';
        }

        return '';
    }
}

The test class:

namespace Acme\Tests\Test;

use Acme\Test\Test;
use PHPUnit\Framework\TestCase;

class TestTest extends TestCase
{
    public function testCheckWithFoo(): void
    {
        $test = new Test(json_encode(['foo' => 1]));
        $this->assertSame('foo', $test->doSomething());
    }

    public function testCheckWithBar(): void
    {
        $test = new Test(json_encode(['bar' => 1]));
        $this->assertSame('bar', $test->doSomething());
    }

    public function testCheckWithNothing(): void
    {
        $test = new Test(json_encode(['nothing' => 1]));
        $this->assertSame('', $test->doSomething());
    }
}

The output of Code coverage:
screen shot 2017-05-31 at 15 39 37

Is this a bug or is it something I did wrong?

@sebastianbergmann
Copy link
Owner

@derickr

@derickr
Copy link
Contributor

derickr commented May 31, 2017

@sebastianbergmann
Copy link
Owner

@derickr I have created a "saved reply" based on your comment that I will use from now on. Thanks!

@sebastianbergmann
Copy link
Owner

There can be many reasons why a specific line is not covered. Each of them is likely a different issue. In order to be able to reproduce anything and not get bogged down with details, you need to provide the following in ONE new issue per case.

That means:

  • The exact versions of (this is what the template requires too, but more):
    • PHP
    • PHP Code Coverage
    • Xdebug
    • PHPUnit
  • php -v output
  • Either a .tar.gz archive or a GitHub repository with a reproduce case. With one file that shows the problem and one test case file (with the exception that sometimes, you might need two source files to show a problem.

This information should be filed as one specific case per issue.

@frankvanhest
Copy link
Author

@sebastianbergmann The versions of PHP, PHP Code Coverage, Xdebug and PHPUnit are found in the table at the top of this issue.
The output of php -v is

PHP 7.1.5-1+deb.sury.org~xenial+1 (cli) (built: May 11 2017 14:07:52) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.1.5-1+deb.sury.org~xenial+1, Copyright (c) 1999-2017, by Zend Technologies
    with Xdebug v2.6.0-dev, Copyright (c) 2002-2017, by Derick Rethans

The case is attached here in a .tar.gz file. It contains a simple class with a method which does the else if and a test class which covers the method with the elseif in it.
phpunit.tar.gz

If you need more information, let me know.

@derickr
Copy link
Contributor

derickr commented Jun 1, 2017

https://bugs.xdebug.org/view.php?id=1446 (fixed)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants