如何让SonarQube PHP插件与PHPUnit一起使用?

时间:2015-10-09 08:10:53

标签: php phpunit sonarqube

我安装了jenkins和SonarQube Runner 2.4,SonarQube Server 5.1.2,php插件2.6,phpunit5.1 然后我用独立的sonarqube分析运行,这是我的配置:

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/

这是控制台输出:

$ /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

它运行成功,我可以看到声纳报告,但PHPUnit Test不起作用,看看这个,似乎找不到phpunit,但我已经在服务器上安装了。任何人都知道这是怎么回事?我如何让PHPunit工作?

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

4 个答案:

答案 0 :(得分:1)

从您的日志:

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

您应该按照日志和"PHP Unit Test and Coverage Results Import"上的文档中的说明配置sonar.php.tests.reportPath。

答案 1 :(得分:1)

使用phpunit生成声纳扫描仪的单位和覆盖率报告:

你必须在

之前安装xdebug
pecl install xdebug

然后

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

更改你的sonar-project.properties:

sonar.php.coverage.reportPath=phpunit.coverage.xml
sonar.php.tests.reportPath=phpunit.report.xml

答案 2 :(得分:0)

正如其他人回答的那样,您需要通过设置phpunit--coverage-clover生成覆盖率报告。

非常重要的一点是,仅设置该参数将无法使用,您需要设置白名单目录:

答案 3 :(得分:0)

当您在SonarQube中使用phpunit(在Symfony中) 您需要安装 -Xdebug:pecl安装xdebug && docker-php-ext-enable xdebug -作曲家需要nette / tokenizer

运行测试并将报告保存在文件中后,方法如下:

php bin / phpunit --coverage-clover resports.xml

完成运行Sonar Scanner的操作

供应商/箱/声纳扫描仪-Dsonar.sources = src -Dsonar.host.url = repository-sonar -Dsonar.projectKey = akileepay:backend -Dsonar.exclusions = src / Migrations / ** -Dsonar.php。 coverage.reportPaths = reports.xml