Bu örneğimizde PHPUnit ile hem text hem de HTML kod kapsama raporu hazırlayacağız. Ne kadar fazla kod kapsanmışsa, o kadar iyi demektir.


Önkoşul


Sunucunuzda XDebug'ın kurulu olduğundan emin olun.


$ php -v
PHP 7.0.22-0ubuntu0.16.04.1 (cli) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.0.22-0ubuntu0.16.04.1, Copyright (c) 1999-2017, by Zend Technologies
with Xdebug v2.4.0, Copyright (c) 2002-2016, by Derick Rethans

Text versiyon


Bu report.txt ismindeki dosyayı tests/coverage klasörüne kaydeder.


$ vendor/bin/phpunit --coverage-text=tests/coverage/report.txt

# tests/coverage/report.txt

Code Coverage Report:
2017-10-22 20:16:39

Summary:
Classes: 83.33% (5/6)
Methods: 91.67% (11/12)
Lines: 96.97% (32/33)

\Application\Exception::PostcodesException
Methods: 100.00% ( 1/ 1) Lines: 100.00% ( 2/ 2)
\Application\Service::PostcodeService
Methods: 100.00% ( 2/ 2) Lines: 100.00% ( 3/ 3)
\Application\Service::TwitterService
Methods: 100.00% ( 2/ 2) Lines: 100.00% ( 3/ 3)
\Application\Service::UserService
Methods: 100.00% ( 2/ 2) Lines: 100.00% ( 5/ 5)
\Application\Util::ParameterUtil
Methods: 100.00% ( 2/ 2) Lines: 100.00% ( 8/ 8)
\Application\Util::Postcodes
Methods: 66.67% ( 2/ 3) Lines: 91.67% ( 11/ 12)

Terminal versiyon


Bu yukarıda gördüğümüz çıktıyı terminalde renkli olarak gösterecektir. Aşağıdaki kodu phpunit.xml.dist dosyasına eklemeniz gerekecek. Ama eğer phpunit.xml.dist dosyasını değiştirmek istemiyorsanız, vendor/bin/phpunit --coverage-text komutunu kullanmak yeterli olacaktır.


<logging>
<log lowUpperBound="35" highLowerBound="70" showUncoveredFiles="true" />
<log type="coverage-text" target="php://stdout" showUncoveredFiles="true"/>
</logging>

$ vendor/bin/phpunit


HTML versyion


Bu ilgili dosyaları tests/coverage klasörüne kaydeder.


$ vendor/bin/phpunit --coverage-html tests/coverage


Alternatif olarak aşağıdaki kodu phpunit.xml.dist dosyasına ekleyip vendor/bin/phpunit komutunu kullanabilirsiniz.


<logging>
<log type="coverage-html" target="tests/coverage" lowUpperBound="35" highLowerBound="70" />
</logging>