<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>coverage &#8211; Rafael Bernard Araujo</title>
	<atom:link href="https://rafael.bernard-araujo.com/tag/coverage/feed" rel="self" type="application/rss+xml" />
	<link>https://rafael.bernard-araujo.com</link>
	<description>desenvolvendo... while(!success){  try(); }</description>
	<lastBuildDate>Thu, 05 Sep 2024 00:22:47 +0000</lastBuildDate>
	<language>pt-BR</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
<site xmlns="com-wordpress:feed-additions:1">21941730</site>	<item>
		<title>PHP Test Coverage Using Bitbucket and Codacy</title>
		<link>https://rafael.bernard-araujo.com/php-test-coverage-using-bitbucket-and-codacy.php</link>
					<comments>https://rafael.bernard-araujo.com/php-test-coverage-using-bitbucket-and-codacy.php#respond</comments>
		
		<dc:creator><![CDATA[rafael]]></dc:creator>
		<pubDate>Fri, 21 Feb 2020 12:23:22 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[coverage]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[qa]]></category>
		<category><![CDATA[testing]]></category>
		<guid isPermaLink="false">https://rafael.bernard-araujo.com/?p=1347</guid>

					<description><![CDATA[Wikipedia: In computer science, code coverage is a measure used to describe the degree to which the source code of a program is tested by a particular test suite. A program with high code coverage has been more thoroughly tested and has a lower chance of containing software bugs than a program with low code [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Wikipedia:</p>
<blockquote><p>
In computer science, code coverage is a measure used to describe the degree to which the source code of a program is tested by a particular test suite. A program with high code coverage has been more thoroughly tested and has a lower chance of containing software bugs than a program with low code coverage.
</p></blockquote>
<p>Testing is an unavoidable process for building a trustful software. Unfortunately, in PHP world we have a massive number of legacy software still running today that are very valuable but born in an age where testing was skipped for various reasons.</p>
<p>As today we are refactoring those untested systems into tested ones or we are creating new projects already focusing on having tests, we can go one step further and measure the code coverage, leveraging bug protection and code quality.</p>
<p>You can use these steps for a legacy project, a new project, a well-covered project, a poorly covered project; no matter the state of your project.</p>
<p>We are considering a PHP project using Bitbucket Pipelines as our CI and Codacy for monitoring our test coverage reports but the main concepts could be easily used when using other tools.</p>
<p>Table of contents:</p>
<ol>
<li>Dependencies installation</li>
<li>PHPUnit configuration</li>
<li>Set up Codacy project API token</li>
<li>Pipelines configuration</li>
</ol>
<h1>1. Dependencies installation</h1>
<p>Use composer to install dependencies:</p>
<pre><code class="language-sh">composer require --dev phpunit/php-code-coverage codacy/coverage</code></pre>
<p>Installation results would be similar to:</p>
<pre><code class="language-sh">Using version ^1.4 for codacy/coverage
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 3 installs, 0 updates, 0 removals
  - Installing symfony/process (v5.0.4): Downloading (100%)
  - Installing gitonomy/gitlib (v1.2.0): Downloading (100%)
  - Installing codacy/coverage (1.4.3): Downloading (100%)
Writing lock file
Generating autoload files
ocramius/package-versions:  Generating version class...
ocramius/package-versions: ...done generating version class</code></pre>
<h1>2. PHPUnit configuration</h1>
<p>We need to configure at least <code>whitelist</code> and <code>logging</code> sections. They are required to code coverage.</p>
<p><strong>Whitelist</strong> is the section that determines which files will be considered as your available code and how existent tests cover this code.</p>
<p>As I want that all my code loaded and analyzes by PHPUnit, I will set <code>processUncoveredFilesFromWhitelist</code>. Considering that all my code is under <code>./src</code> folder:</p>
<pre><code class="language-xml">&lt;phpunit&gt;
    &lt;!-- ... --&gt;
    &lt;filter&gt;
        &lt;!-- ... --&gt;
        &lt;whitelist processUncoveredFilesFromWhitelist=&quot;true&quot;&gt;
            &lt;directory suffix=&quot;.php&quot;&gt;./src&lt;/directory&gt;
        &lt;/whitelist&gt;
    &lt;/filter&gt;
&lt;/phpunit&gt;</code></pre>
<p><strong>Logging</strong> is where we configure logging of the test execution. Clover configuration is enough for now:</p>
<pre><code class="language-xml">&lt;phpunit&gt;
    &lt;!-- ... --&gt;
    &lt;logging&gt;
      &lt;log type=&quot;coverage-clover&quot; target=&quot;/tmp/coverage.xml&quot;/&gt;
    &lt;/logging&gt;
&lt;/phpunit&gt;</code></pre>
<p>You should now run your tests locally to ensure that you can fix everything that will be analyzed by PHPUnit. All errors have to be fixed. One example that might appear:</p>
<pre><code class="language-sh">Fatal error: Interface &#039;InterfaceClass&#039; not found in /var/www/src/Example/Application/ClassService.php on line 5</code></pre>
<p>Oops.</p>
<pre><code class="language-php">&lt;?php

namespace Example;

class ClassService implements InterfaceClass {
    /* */
}</code></pre>
<p>I have a class extending from another but I missed the import for the parent class.</p>
<pre><code class="language-php">&lt;?php

namespace Example;

use Example\Domain\InterfaceClass;

class ClassService implements InterfaceClass {
    /* */
}</code></pre>
<p>And now I am good. After fixing all errors that might appear (and discovering some dead classes...), we test results and report generation message:</p>
<pre><code>OK (10 tests, 17 assertions)

Generating code coverage report in Clover XML format ... done [5.71 seconds]</code></pre>
<p>This has already configured code coverage. We will use Codacy as a tool to keep track of code coverage status, representing them in a beautiful dashboard and some other tools such as a check for new pull requests.</p>
<h1>3. Set up Codacy project API token</h1>
<p>For sending coverage results to Codacy, we need the <a href="https://docs.codacy.com/repositories-configure/integrations/project-api/" title="project API token">project API token</a>. This is located at <em>Settings &gt; Integrations</em> tab.</p>
<p>If there is already a code, you can use it. Otherwise, generate one. A project token would look like something as:</p>
<pre><code>a9564ebc3289b7a14551baf8ad5ec60a // not real</code></pre>
<p>We will use this as an environment variable at Bitbucket. At your project in Bitbucket, go to <em>Configurations &gt; Pipelines &gt; Repository Variables</em>. In my case, I used:</p>
<p>Name: <code>CODACY_PROJECT_TOKEN</code><br />
Value: <code>a9564ebc3289b7a14551baf8ad5ec60a</code></p>
<p>I want the value securely encrypted. Then I mark &quot;Secure&quot;.</p>
<p>Right now we have Codacy token and the value enabled to use as an environment variable at our pipeline.</p>
<h1>4. Pipelines configuration</h1>
<p>For Pipelines now you should provide API token as an environment variable:</p>
<pre><code class="language-yaml">variables:
  - CODACY_PROJECT_TOKEN: $CODACY_PROJECT_TOKEN</code></pre>
<p>Enable xdebug:</p>
<pre><code class="language-yaml">  - pecl install xdebug-2.9.2 &amp;&amp; docker-php-ext-enable xdebug</code></pre>
<p>And execute <code>codacycoverage</code> to send saved report to Codacy:</p>
<pre><code class="language-yaml">  - src/vendor/bin/codacycoverage clover /tmp/coverage.xml</code></pre>
<p>Considering one of my legacy projects that I am adding code coverage, this could be my unit test step:</p>
<ul>
<li>using alpine</li>
<li>source code (whitelisted) at <code>site/src</code></li>
<li>logfile generated at <code>/tmp/unit-clover.xml</code></li>
<li><code>g++ gcc make git php7-dev</code> are required to install and enable xdebug</li>
</ul>
<pre><code class="language-yaml">    - step: &amp;step-unit-tests
        name: unit tests
        image: php:7.2-alpine
        variables:
          - CODACY_PROJECT_TOKEN: $CODACY_PROJECT_TOKEN
        caches:
          - composer
        script:
          - apk add --no-cache g++ gcc make git php7-dev
          - pecl install xdebug-2.9.2 &amp;&amp; docker-php-ext-enable xdebug
          - site/src/vendor/bin/phpunit -c tests/Unit/phpunit.xml
          - site/src/vendor/bin/codacycoverage clover /tmp/unit-clover.xml</code></pre>
<p>After being successfully executed by pipelines, you may see the results at your Codacy dashboard.</p>
<p>Now code with love.<br />
And coverage.</p>
<hr />
<p>Useful links:</p>
<ul>
<li><a href="https://en.wikipedia.org/wiki/Code_coverage">https://en.wikipedia.org/wiki/Code_coverage</a></li>
<li><a href="https://xdebug.org/">https://xdebug.org/</a></li>
<li><a href="https://docs.phpunit.de/en/11.3/code-coverage.html">https://docs.phpunit.de/en/11.3/code-coverage.html</a></li>
<li><a href="https://phpunit.readthedocs.io/en/8.5/code-coverage-analysis.html#whitelisting-files">https://phpunit.readthedocs.io/en/8.5/code-coverage-analysis.html#whitelisting-files</a></li>
<li><a href="https://phpunit.readthedocs.io/en/8.5/configuration.html#the-logging-element">https://phpunit.readthedocs.io/en/8.5/configuration.html#the-logging-element</a></li>
<li><a href="https://en.wikipedia.org/wiki/Test-driven_development">https://en.wikipedia.org/wiki/Test-driven_development</a></li>
</ul>
]]></content:encoded>
					
					<wfw:commentRss>https://rafael.bernard-araujo.com/php-test-coverage-using-bitbucket-and-codacy.php/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1347</post-id>	</item>
	</channel>
</rss>
