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 XML report generate empty files #692

Closed
aviator-ua opened this issue Aug 15, 2019 · 6 comments
Closed

Code Coverage XML report generate empty files #692

aviator-ua opened this issue Aug 15, 2019 · 6 comments

Comments

@aviator-ua
Copy link

Q A
php-code-coverage version 7.0.7
PHP version 7.3.6
Driver Xdebug
Xdebug version (if used) 2.7.2
Installation Method Composer
Usage Method PHPUnit
PHPUnit version (if used) 8.3.4

Target Class

<?php
declare(strict_types=1);

namespace App\Service;

class SoapClientService
{
    /**
     * @const string
     */
    const URL = 'http://example.com/service.jws?wsdl';

    /**
     * @var \SoapClient
     */
    protected $soapClient = null;

    /**
     * @param string $url
     * @return \SoapClient
     * @throws \InvalidArgumentException
     */
    public function getSoapClient(string $url = null)
    {
        if (!isset($this->soapClient)) {
            try {
                $this->soapClient = new \SoapClient(
                    $url ?? self::URL
                );
            } catch (\Throwable $error) {
                throw new \InvalidArgumentException('Failed to initialize Soap Client');
            }
        }

        return $this->soapClient;
    }
}

Target TestCase

<?php
declare(strict_types=1);

namespace App\Tests\Service;

use App\Service\SoapClientService;
use PHPUnit\Framework\TestCase;

/**
 * Unit test for SoapClientService
 */
class SoapClientServiceTest extends TestCase
{

    public function testGetSoapClient(): void
    {
        $client = new SoapClientService();

        $this->expectException(\InvalidArgumentException::class);
        $client->getSoapClient('no-wsdl');
    }
}

Command for generate Code Coverage XML Report

vendor/bin/phpunit -v --coverage-xml build/coverage-xml

After executing comand, folder build/coverage-xml contains empty files.

Other reports (HTML, Clover, PHP) works as expected.

Also reproduced on the previous version of sebastianbergmann/php-code-coverage.

@duncan3dc
Copy link
Contributor

Looks like it's just the SoapClient construction, here's a smaller reproduction case:

src/Service.php

namespace Coverage\Example;

class Service
{
    public function __construct()
    {
        new \SoapClient("api/.asmx?wsdl");
    }
}

tests/ServiceTest.php

namespace Coverage\ExampleTests;

class ServiceTest extends \PHPUnit\Framework\TestCase
{
    public function testGet(): void
    {
        $this->expectException(\Exception::class);
        new \Coverage\Example\Service();
    }
}

@theseer
Copy link
Contributor

theseer commented Jan 29, 2020

This is indeed unrelated to PHPUnit (and Coverage) but a general PHP problem (imho, bug):

try {
  new \SoapClient("not-found.wsdl");
} catch (Throwable $t) {}

$dom = new DOMDocument();
$dom->loadxml('<?xml version="1.0" ?><root />');
var_dump($dom->save('/tmp/test.xml'));

Running the above code outputs bool(false) with no information in error state and it seems to be limited to SoapClient. Any other class I checked does not seem to exhibit this behavior.

@theseer
Copy link
Contributor

theseer commented Jan 29, 2020

@sebastianbergmann I think for the time being the proposed workaround (read: PR) makes sense :)

@sebastianbergmann
Copy link
Owner

I have opened https://bugs.php.net/bug.php?id=79191 for the bug in PHP.

@duncan3dc
Copy link
Contributor

If you plan to move back to saveXml() in the future then I suggest including #700 otherwise it can be closed

@theseer
Copy link
Contributor

theseer commented Feb 21, 2020

For reference: The underlying PHP Bug has been fixed with PHP 7.4.3 and 7.3.15, respectively. Since 7.2 is in security bugs only mode, no fix there. No fixes for older versions of PHP as those are EOL.

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

4 participants