Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make SonarQube PHP plugin work with PHPUnit?

I had install jenkins and SonarQube Runner 2.4 ,SonarQube Server 5.1.2, php plugin 2.6,phpunit5.1 and then i run with standalone sonarqube analysis,this is my configuration:

sonar.language=php
sonar.projectVersion=1.0
sonar.sourceEncoding=UTF-8

sonar.phpCodesniffer.timeout=120
sonar.projectKey=xxx
sonar.projectName=xxxx
sonar.sources=.
sonar.tests=./tests/tests/

Here is the console output:

$ /apps/svr/sonar-runner/bin/sonar-runner -e -Dsonar.projectBaseDir=/home/apps/.jenkins/workspace/pc_dev_example -Dsonar.sourceEncoding=UTF-8 -Dsonar.sources=. -Dsonar.language=php -Dsonar.projectVersion=1.0 -Dsonar.projectKey=php:pc_dev_example -Dsonar.phpUnit.argumentLine="/apps/svr/sonar" -Dsonar.inclusions=applications/vipuser/public/passport.php -Dsonar.phpCodesniffer.timeout=120 -Dsonar.tests=./tests/tests/ -Dsonar.projectName=pc_dev_example
SonarQube Runner 2.4
Java 1.7.0_71 Oracle Corporation (64-bit)
Linux 2.6.32-504.23.4.el6.x86_64 amd64
INFO: Error stacktraces are turned on.
INFO: Runner configuration file: /apps/svr/sonar-runner/conf/sonar-runner.properties
INFO: Project configuration file: NONE
INFO: Default locale: "en_US", source code encoding: "UTF-8"
INFO: Work directory: /home/apps/.jenkins/workspace/pc_dev_example/.sonar
INFO: SonarQube Server 5.1.2
15:56:42.207 INFO  - Load global repositories
15:56:42.471 INFO  - Load global repositories (done) | time=267ms
15:56:42.474 INFO  - Server id: 20151009151309
15:56:42.477 INFO  - User cache: /home/apps/.sonar/cache
15:56:42.490 INFO  - Install plugins
15:56:42.634 INFO  - Install JDBC driver
15:56:42.642 INFO  - Create JDBC datasource for jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
15:56:44.209 INFO  - Initializing Hibernate
15:56:45.887 INFO  - Load project repositories
15:56:46.582 INFO  - Load project repositories (done) | time=695ms
15:56:46.582 INFO  - Load project settings
15:56:47.405 INFO  - Load technical debt model
15:56:47.432 INFO  - Apply project exclusions
15:56:47.697 INFO  - -------------  Scan pc_dev_vip_example
15:56:47.702 INFO  - Load module settings
15:56:47.862 INFO  - Language is forced to php
15:56:47.871 INFO  - Load rules
15:56:48.455 INFO  - Base dir: /home/apps/.jenkins/workspace/pc_dev_example
15:56:48.455 INFO  - Working dir: /home/apps/.jenkins/workspace/pc_dev_example/.sonar
15:56:48.456 INFO  - Source paths: .
15:56:48.456 INFO  - Test paths: tests/tests
15:56:48.457 INFO  - Source encoding: UTF-8, default locale: en_US
15:56:48.457 INFO  - Index files
15:56:48.470 INFO  - Included sources: 
15:56:48.470 INFO  -   applications/vipuser/public/passport.php
15:56:48.542 INFO  - 2 files indexed
15:56:48.542 INFO  - 470 files ignored because of inclusion/exclusion patterns
15:56:48.610 INFO  - Quality profile for php: Sonar way
15:56:48.736 INFO  - Sensor NoSonar and Commented out LOC Sensor
15:56:48.769 INFO  - Sensor NoSonar and Commented out LOC Sensor (done) | time=33ms
15:56:48.769 INFO  - Sensor Lines Sensor
15:56:48.771 INFO  - Sensor Lines Sensor (done) | time=2ms
15:56:48.771 INFO  - Sensor QProfileSensor
15:56:48.775 INFO  - Sensor QProfileSensor (done) | time=4ms
15:56:48.775 INFO  - Sensor InitialOpenIssuesSensor
15:56:48.798 INFO  - Sensor InitialOpenIssuesSensor (done) | time=23ms
15:56:48.798 INFO  - Sensor ProjectLinksSensor
15:56:48.806 INFO  - Sensor ProjectLinksSensor (done) | time=8ms
15:56:48.806 INFO  - Sensor VersionEventsSensor
15:56:48.833 INFO  - Sensor VersionEventsSensor (done) | time=27ms
15:56:48.834 INFO  - Sensor PHPSquidSensor
15:56:48.880 INFO  - 1 source files to be analyzed
15:56:49.109 INFO  - 1/1 source files have been analyzed
15:56:49.132 INFO  - Sensor PHPSquidSensor (done) | time=298ms
15:56:49.132 INFO  - Sensor PHPUnit Sensor
15:56:49.132 INFO  - No PHPUnit test report provided (see 'sonar.php.tests.reportPath' property)
15:56:49.133 INFO  - No PHPUnit unit test coverage report provided (see 'sonar.php.coverage.reportPath' property)
15:56:49.133 INFO  - No PHPUnit integration test coverage report provided (see 'sonar.php.coverage.itReportPath' property)
15:56:49.133 INFO  - No PHPUnit overall coverage report provided (see 'sonar.php.coverage.overallReportPath' property)
15:56:49.133 INFO  - Sensor PHPUnit Sensor (done) | time=1ms
15:56:49.133 INFO  - Sensor SCM Sensor
15:56:49.137 INFO  - Sensor SCM Sensor (done) | time=4ms
15:56:49.137 INFO  - Sensor CPD Sensor
15:56:49.137 INFO  - DefaultCpdEngine is used for php
15:56:49.137 INFO  - Cross-project analysis disabled
15:56:49.180 INFO  - Sensor CPD Sensor (done) | time=43ms
15:56:49.182 INFO  - No quality gate is configured.
15:56:49.240 INFO  - Compare to previous analysis (2015-10-09)
15:56:49.249 INFO  - Compare over 30 days (2015-09-09, analysis of Fri Oct 09 10:02:34 CST 2015)
15:56:49.618 INFO  - Execute decorators...
15:56:50.177 INFO  - Store results in database
15:56:50.494 INFO  - Analysis reports generated in 21ms, dir size=1 KB
15:56:50.504 INFO  - Analysis reports compressed in 10ms, zip size=2 KB
15:56:50.552 INFO  - Analysis reports sent to server in 48ms
15:56:50.552 INFO  - ANALYSIS SUCCESSFUL, you can browse http://10.199.250.171:9040/dashboard/index/php:pc_dev_vip_example
15:56:50.552 INFO  - Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report.
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
Total time: 8.930s
Final Memory: 17M/563M
INFO: ------------------------------------------------------------------------
Finished: SUCCESS

It run success,and I can see the sonar report,but PHPUnit Test is not work,look at this,it seems that it can not find phpunit,but i had install on the server.Any one knows how does this happen?How can i make PHPunit work?

15:56:49.132 INFO  - Sensor PHPUnit Sensor
    15:56:49.132 INFO  - No PHPUnit test report provided (see 'sonar.php.tests.reportPath' property)
    15:56:49.133 INFO  - No PHPUnit unit test coverage report provided (see 'sonar.php.coverage.reportPath' property)
    15:56:49.133 INFO  - No PHPUnit integration test coverage report provided (see 'sonar.php.coverage.itReportPath' property)
    15:56:49.133 INFO  - No PHPUnit overall coverage report provided (see 'sonar.php.coverage.overallReportPath' property)
    15:56:49.133 INFO  - Sensor PHPUnit Sensor (done) | time=1ms
like image 566
钟艺文 zywkeven Avatar asked Oct 09 '15 08:10

钟艺文 zywkeven


3 Answers

to generate unit and coverage reports for sonar-scanner with phpunit :

you have to install xdebug before

pecl install xdebug

then

php -dzend_extension=xdebug.so phpunit.phar --configuration phpunit.xml --coverage-clover phpunit.coverage.xml --log-junit phpunit.report.xml

Changes in your sonar-project.properties file:

sonar.php.coverage.reportPaths=phpunit.coverage.xml
sonar.php.tests.reportPath=phpunit.report.xml
like image 68
rouet Avatar answered Nov 15 '22 00:11

rouet


From your logs:

No PHPUnit test report provided (see 'sonar.php.tests.reportPath' property)

You should configure sonar.php.tests.reportPath as mentioned in the logs and in the documentation on "PHP Unit Test and Coverage Results Import".

like image 40
Pierre-Yves Avatar answered Nov 14 '22 23:11

Pierre-Yves


As others answered, you need to generate coverage reports from phpunit by setting --coverage-clover.

One very important point is that it won't work by only setting that parameter and you need to set whitelist directories:

  • Whitelist for phpunit
  • Docs
like image 21
Moradnejad Avatar answered Nov 15 '22 00:11

Moradnejad