Codeception / module-yii2

Codeception module for Yii2 framework
MIT License
17 stars 35 forks source link

REST module tests report 0% coverage #46

Closed vitorhorta closed 3 years ago

vitorhorta commented 9 years ago

Hi,

I'm having the same problem that was reported on Codeception/Codeception#1622 but that solution didn't work for me. I'm using Yii2 basic template and i'm trying to run Api tests that uses the REST module. The tests passes and the html files are created but I get 0 reports from coverage.

As mentioned on Codeception/Codeception#1622 I tried to downgrade the PHPUnit and Codeception version but it didn't work. Is there any other way to fix this issue? If not, what version should I use for both packages?

Thanks in advance

thecatontheflat commented 9 years ago

+1

greentopsecret commented 9 years ago

+1

zelf commented 9 years ago

+1

danielintloop commented 9 years ago

+1

Naktibalda commented 9 years ago

So many people are interested. Could one of you reproduce and fix the issue?

rogoit commented 8 years ago

+1

Naktibalda commented 8 years ago

Please provide instructions how to reproduce this issue and I will reopen this ticket.

rogoit commented 8 years ago

Just try to make an api test and check the code coverage

Naktibalda commented 8 years ago

Which version of codeception are you using?

rogoit commented 8 years ago

2.2.0

Naktibalda commented 8 years ago

@samdark This is a Yii issue, could you test it?

Naktibalda commented 8 years ago

@rogoit have you enabled code coverage and whitelist (not white_list) in codeception.yml?

rogoit commented 8 years ago

Yes i also try remote true and false on a vagrant. Try to run it from mac and vagrant. Xdebug settings are running fine on development.

valoricDe commented 7 years ago

I had the REST module enabled and got 0/46 methods covered. A removed REST module and executed codeception build afterwards it gave me code coverage.

In one scenario $this->getTestResultObject()->getCodeCoverage() in codeception/codeception/src/Codeception/Test/Feature/CodeCoverage.php:16 returns null and in the other a SebastianBergmann\CodeCoverage\CodeCoverage class.

Naktibalda commented 7 years ago

@valorize What module does REST module use for connecting to API in your test suite?

valoricDe commented 7 years ago
        - PhpBrowser:
            url: http://mview.example.local/
        - REST:
            depends: PhpBrowser
            url: http://mview.example.local
valoricDe commented 7 years ago

In one scenario \Codeception\Coverage\SuiteSubscriber::applyFilter:84 is called in the other not.

Naktibalda commented 7 years ago

I will try to reproduce it.

valoricDe commented 7 years ago

I think I found the flaw. Without REST+PhpBrowser is the \Codeception\Coverage\Subscriber\Local module active (see \Codeception\Coverage\Subscriber\Local::beforeSuite:34) if so the applyFilter method directly after will $result->setCodeCoverage($this->coverage);. If REST is activated Local is not an active module, but \Codeception\Coverage\Subscriber\LocalServer or RemoteServer are. But these do not execute $result->setCodeCoverage($this->coverage);.

So I guress the fix would be: add $this->applyFilter($e->getResult()); to LocalServer::beforeSuite

Naktibalda commented 7 years ago

Please make that change and check if it fixes your issue.

valoricDe commented 7 years ago

The change activates successfully the code coverage, but the report is still empty. I also had to adapt the LocalServer::afterSuite to the behaviour of Local::afterSuite. Soo I had to add: $this->mergeToPrint($e->getResult()->getCodeCoverage()); in LocalServer. It know reports just fine.

Sidequestion: When is codeception.yml used and when api.suite.yml? Is this overwritten when I supply the run command with the -c param?

Edit: setAddUncoveredFilesFromWhitelist is never been called making the coverage: show_uncovered: false option / setting kind of useless. Therefore I will add self::$coverage->setAddUncoveredFilesFromWhitelist($this->settings['show_uncovered']); in the first line of \Codeception\Coverage\Subscriber\Printer::printConsole, but I guess that's not the best place for that.

valoricDe commented 7 years ago

So basically we shouldn't consider only remote coverage when activating the REST module. So LocalServer and RemoteServer should integrate all functionality of \Codeception\Coverage\Subscriber\Local.

davidjeddy commented 7 years ago

Code coverage would be really awesome for API only applications.

arustamov commented 7 years ago

@Naktibalda @valorize any updates on this? btw it looks like Yii label can be removed since the issue is not framework specific

Naktibalda commented 7 years ago

I don't use PhpBrowser in my tests, but I use REST a lot, so I tried to generate code coverage report for one of my applications and I got empty report.

In order to get non-empty report I had to whitelist application code with this configuration:

coverage:
    enabled: true
    include:
      - 'application/*'
valoricDe commented 7 years ago

@arustamov I do not maintain this project. I stepped through all relevant code of codeception and updated the code as described in the upper comments. For me the code worked quite well then.

davidjeddy commented 7 years ago

@valorize Would you mind providing a PR for your changes? Would be really awesome to have code coverage working for API/REST apps.

valoricDe commented 7 years ago

@davidjeddy I'm sorry but I won't be able to. Timewise and because I left the company where I used codeception and where I adapted the code.

davidjeddy commented 7 years ago

@valorize NNNNnnooooooo :(. Man, was really hoping for a fix :(

valoricDe commented 7 years ago

@davidjeddy the fix is described in https://github.com/Codeception/Codeception/issues/2220#issuecomment-305147001 and https://github.com/Codeception/Codeception/issues/2220#issuecomment-305151819 It is not to difficult to go into these classes with xdebug and check which parts need to be adapted once one knows where to look.

igorabrandao commented 6 years ago

Hello @valorize, I've applied the changes in LocalServer as you told, however my coverage test just grab the data related to the /tests folder and none from the Controllers.

Is really necessary to downgrade the PhpUnit and Codeception versions? Because when I do it, all my tests crash.

Do you have any idea what can be happening?

Note: I'm testing a REST in another server.

Thanks in advance!

screenshot from 2017-11-26 12-00-28

igorabrandao commented 6 years ago

@davidjeddy here you are the changes @valorize mentioned above.

BEFORESUITE `public function beforeSuite(SuiteEvent $e) { $this->module = $this->getServerConnectionModule($e->getSuite()->getModules()); $this->applySettings($e->getSettings()); $this->applyFilter($e->getResult()); if (!$this->isEnabled()) { return; }

    $this->suiteName = $e->getSuite()->getBaseName();

    if ($this->settings['remote_config']) {
        $this->addC3AccessHeader(self::COVERAGE_HEADER_CONFIG, $this->settings['remote_config']);
    }

    $knock = $this->c3Request('clear');
    if ($knock === false) {
        throw new RemoteException(
            '
            CodeCoverage Error.
            Check the file "c3.php" is included in your application.
            We tried to access "/c3/report/clear" but this URI was not accessible.
            You can review actual error messages in c3tmp dir.
            '
        );
    }
}`

AFTERSUITE `public function afterSuite(SuiteEvent $e) { if (!$this->isEnabled()) { return; } $this->mergeToPrint($e->getResult()->getCodeCoverage()); $coverageFile = Configuration::outputDir() . 'c3tmp/codecoverage.serialized';

    $retries = 5;
    while (!file_exists($coverageFile) && --$retries >= 0) {
        usleep(0.5 * 1000000); // 0.5 sec
    }

    if (!file_exists($coverageFile)) {
        if (file_exists(Configuration::outputDir() . 'c3tmp/error.txt')) {
            throw new \RuntimeException(file_get_contents(Configuration::outputDir() . 'c3tmp/error.txt'));
        }
        return;
    }

    $contents = file_get_contents($coverageFile);
    $coverage = @unserialize($contents);
    if ($coverage === false) {
        return;
    }
    $this->mergeToPrint($coverage);
}`
igorabrandao commented 6 years ago

Somehow in RemoteServer::retrieveAndPrintHtml, I'm getting the following error that prevents from getting the coverage from Controllers:

[UnexpectedValueException]
phar error: "/tmp/C3syXhG1.tar" is a corrupted tar file (checksum mismatch of file <!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 3.2 Final//EN>Index of /api//c3/re")</p> </blockquote> <p><strong>protected function retrieveAndPrintHtml($suite)</strong></p> <p>`protected function retrieveAndPrintHtml($suite) { $tempFile = tempnam(sys_get_temp_dir(), 'C3') . '.tar'; file_put_contents($tempFile, $this->c3Request('html'));</p> <pre><code> $destDir = Configuration::outputDir() . $suite . '.remote.coverage'; if (is_dir($destDir)) { FileSystem::doEmptyDir($destDir); } else { mkdir($destDir, 0777, true); } $phar = new \PharData($tempFile); $phar->extractTo($destDir); unlink($tempFile); }`</code></pre> <p><code>Exception trace: () at /var/www/.../codeception/base/src/Codeception/Coverage/Subscriber/RemoteServer.php:52 PharData->__construct() at /var/www/.../codeception/base/src/Codeception/Coverage/Subscriber/RemoteServer.php:52 </code></p> <p>Line 52 refers to this code: <code>$phar = new \PharData($tempFile);</code></p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/rogoit"><img src="https://avatars.githubusercontent.com/u/3101439?v=4" />rogoit</a> commented <strong> 6 years ago</strong> </div> <div class="markdown-body"> <p>We need something like the PhPStorm debug session key i think</p> <p>Am 26.11.2017 20:24 schrieb "Igor Augusto Brandão" <notifications@github.com</p> <blockquote> <p>:</p> </blockquote> <p>Somehow in RemoteServer::retrieveAndPrintHtml, I'm getting the following error that prevents from getting the coverage from Controllers:</p> <p>[UnexpectedValueException] phar error: "/tmp/C3syXhG1.tar" is a corrupted tar file (checksum mismatch of file <title>Index of /api//c3/re")</p> <p><em>protected function retrieveAndPrintHtml($suite)</em></p> <p>`protected function retrieveAndPrintHtml($suite) { $tempFile = tempnam(sys_get_temp_dir(), 'C3') . '.tar'; file_put_contents($tempFile, $this->c3Request('html'));</p> <pre><code>$destDir = Configuration::outputDir() . $suite . '.remote.coverage'; if (is_dir($destDir)) { FileSystem::doEmptyDir($destDir); } else { mkdir($destDir, 0777, true); } $phar = new \PharData($tempFile); $phar->extractTo($destDir); unlink($tempFile);</code></pre> <p>}`</p> <p>Exception trace: () at /var/www/.../codeception/base/ src/Codeception/Coverage/Subscriber/RemoteServer.php:52 PharData->__construct() at /var/www/.../codeception/base/ src/Codeception/Coverage/Subscriber/RemoteServer.php:52</p> <p>Line 52 refers to this code: $phar = new \PharData($tempFile);</p> <p>— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <a href="https://github.com/Codeception/Codeception/issues/2220#issuecomment-347031760">https://github.com/Codeception/Codeception/issues/2220#issuecomment-347031760</a>, or mute the thread <a href="https://github.com/notifications/unsubscribe-auth/AC9S_3nh2laS2Rpl1uKZba0oApRTSaz3ks5s6brygaJpZM4FhN7y">https://github.com/notifications/unsubscribe-auth/AC9S_3nh2laS2Rpl1uKZba0oApRTSaz3ks5s6brygaJpZM4FhN7y</a> .</p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/horace-velmont"><img src="https://avatars.githubusercontent.com/u/3744320?v=4" />horace-velmont</a> commented <strong> 6 years ago</strong> </div> <div class="markdown-body"> <p>+1</p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/metalagman"><img src="https://avatars.githubusercontent.com/u/1983796?v=4" />metalagman</a> commented <strong> 6 years ago</strong> </div> <div class="markdown-body"> <p>Still 0% +1 from 2018. I'm using REST module within the Yii2 module but without the PhpBrowser - all the api calls are being performed locally. Got zero coverage on all the methods. </p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/SamMousa"><img src="https://avatars.githubusercontent.com/u/547021?v=4" />SamMousa</a> commented <strong> 6 years ago</strong> </div> <div class="markdown-body"> <p>@russianlagman are you using php-debug as your coverage driver? I'm getting coverage just fine.</p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/metalagman"><img src="https://avatars.githubusercontent.com/u/1983796?v=4" />metalagman</a> commented <strong> 6 years ago</strong> </div> <div class="markdown-body"> <p>@SamMousa <em>codecept --coverage</em> just won't work if the coverage driver is missing. <img src="https://user-images.githubusercontent.com/1983796/41987840-922217d4-7a64-11e8-9607-ab08403ac135.png" alt="image" /></p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/SamMousa"><img src="https://avatars.githubusercontent.com/u/547021?v=4" />SamMousa</a> commented <strong> 6 years ago</strong> </div> <div class="markdown-body"> <p>I know, but there are multiple drivers; xdebug and phpdbg</p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/jonny7"><img src="https://avatars.githubusercontent.com/u/6732997?v=4" />jonny7</a> commented <strong> 6 years ago</strong> </div> <div class="markdown-body"> <p>@SamMousa How should we be defining phpdbg as the driver to use?</p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/SamMousa"><img src="https://avatars.githubusercontent.com/u/547021?v=4" />SamMousa</a> commented <strong> 6 years ago</strong> </div> <div class="markdown-body"> <p>Google it, it depends on your environment; it's not hard</p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/metalagman"><img src="https://avatars.githubusercontent.com/u/1983796?v=4" />metalagman</a> commented <strong> 6 years ago</strong> </div> <div class="markdown-body"> <p>@SamMousa in any case I was using xdebug. </p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/jonny7"><img src="https://avatars.githubusercontent.com/u/6732997?v=4" />jonny7</a> commented <strong> 6 years ago</strong> </div> <div class="markdown-body"> <p>@russianlagman How did you set it to use phpdbg. I can't find any docs on Codeception about drivers. I've tried building it via phpunit directly but no luck :|</p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/SamMousa"><img src="https://avatars.githubusercontent.com/u/547021?v=4" />SamMousa</a> commented <strong> 3 years ago</strong> </div> <div class="markdown-body"> <p>Closing this, it's old and about xdebug which you shouldn't use for code coverage.</p> </div> </div> <div class="page-bar-simple"> </div> <div class="footer"> <ul class="body"> <li>© <script> document.write(new Date().getFullYear()) </script> Githubissues.</li> <li>Githubissues is a development platform for aggregating issues.</li> </ul> </div> <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script> <script src="/githubissues/assets/js.js"></script> <script src="/githubissues/assets/markdown.js"></script> <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/highlight.min.js"></script> <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/go.min.js"></script> <script> hljs.highlightAll(); </script> </body> </html>