SonarQube for PHP project

SonarQube is a code coverage tool that provides reasonable insight into the reports generated by your test suite and test cases. I have been planning on applying the DDL concept on my next PHP projects.

For that I needed a mainstream testing suite and I found that in PHPUnit. It is a remarkable tool if you want to write tests for PHP. 

I will not go into details about PHPUnit since it will be off topic but we will need coverage and test reports for our PHP project to work with SonarQube. You can generate coverage reports through the –coverage-clover flag and test log/report using –log-junit. Both will be in xml format.

Next wherever these reports are generated we will need a sonar-project.properties file which will contain following elements:

All the attributes are easily understood and those which need explaining are:

sonar.sources=src : This is the path of the actual PHP source code files for which the coverage is to be estimated.

sonar.tests=tests : The directory where PHPUnit tests are located.

sonar.php.coverage.reportPath=reports/phpunit.coverage.xml : Path where coverage reports generated through PHPUnit are located

sonar.php.tests.reportPath=reports/phpunit.xml : Path where logs generated through PHPUnit are located  

Next we have to download SonarQube and SonarQube Runner from this location:

http://www.sonarqube.org/downloads/

Unzip both of  them at some location like :

C:\sonarqube

C:\sonar-runner

Next you will have to start the Sonar Server from the command line. So in windows you navigate to C:\sonarqube\bin\windows-xxx-xx and run SonarStart like so:

After the server has started, do not close the console screen since the server will shut down with it too. Now go to http://localhost:9000 to see if sonar server is running (although you can check it is console too). If it is, you will see the GUI. If it’s not then one of the reasons might be that the port 9000 is being used by some other application and in that case you will have to change the default port from C:\sonarqube\conf]sonar.properties to some other and start the server again.

If it all worked out for you then the only step that remains is analyzing the project through SonarQube Runner. For that open up another console and navigate to project that has sonar-project.properties, tests directory, report.xml and coverage.xml. Run command sonar-runner and it will analyze the project and show execution results on console. Now, to see the analysis results, refresh http://localhost:9000 and you will see you project appearing there.

 

alexey

alexey

Application developer, movie buff, occasional reader and a huge Manchester United fan.
alexey