PHP meets SCM-Manager Universe
To keep SCM-Manager Universe as lean as possible the appliance is geared for Java projects. To use it with some of the other programming languages it is necessary to perform some modifications. We want to show you how to adjust your SCM-Manager Universe for PHP projects.
To use SCM-Manager Universe for your PHP projects you only need to perform small changes to your appliance. For a start you need to install the PHP plugin for SonarQube and you need to provide a properties file for each project you want to perform a code analysis for. If you want to implement unit testing in Jenkins you need to configure some shell execution commands to the build process.
First of all you can install the PHP plugin in SonarQube. Go to the Update Center screen “Settings → System → Update Center”, select the Available Plugins tab and search for PHP. Click on Install. After the successful installation and a reload of the application you can see the plugin in the Installed Plugins screen.
The plugin implements coding rules specifically for PHP projects. The rules are used automatically every time a PHP project gets analyzed.
Jenkins - SonarQube Configuration
After SonarQube was prepared for PHP projects you can add a new Jenkins job for your PHP project (e.g. as “Freestyle Project”) and add the necessary configuration. Firstly you can add a SonarQube analysis to the build.
In addition to that you need a sonar-project.properties file in the top folder of your project. This file is required by SonarRunner to start the analysis. Before you can use it for your project, you need to adjust the file to your project.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 sonar.projectKey=org.codehaus.sonar:php-sonar-runner-unit-tests sonar.projectName=PHPdemoProject sonar.projectVersion=1.0 sonar.sources=path/to/code #sonar.tests=tests sonar.language=php sonar.sourceEncoding=UTF-8 # Reusing PHPUnit reports sonar.php.coverage.reportPath=path/to/phpunit.coverage.xml sonar.php.tests.reportPath=path/to/phpunit.xml
The file contains information like the language, encoding and paths to reports and unit tests. It ensures that SonarQube uses the previously installed PHP plugin. In this example there are no unit tests executed by SonarQube, because the row “sonar.tests” is commented out.
Now the configuration of SonarQube is done. The only thing left to do is to enable Jenkins to build your project and to perform your unit tests.
Build and Unit Testing
To build PHP projects with Jenkins you need a PHP build system. In this example we use Phing. For unit tests we use PHPunit. To keep your build and test system variable you can use the dependency management tool Composer. The implementation of those three tools in Jenkins is easy. You only need to add three shell commands to the build execution:
The first command installs Composer, the second one looks for updates and downloads the dependencies, the third one executes the build with Phing. Composer requires a Composer.json file in the top folder of your project that contains the dependencies of the project. This file could look like this:
It contains the requirements of the project including for example Phing and PHPunit. Each requirement consists of the vendor name, the project name and the version. You can use a local installation of Composer to generate the composer.json file.
Phing uses the build.xml of the project to execute the build and PHPunit will be used to execute the unit tests.
If you start a new project and you want to use the latest versions of the tools, you only need to adjust the versions in the composer.json file.
The benefit from using Composer with Jenkins is that the time for setting up the infrastructure is reduced and the requirements are very variable, because Composer installs all required dependencies automatically. SCM-Manager Universe can be modified to meet the requirements for PHP projects quite fast and without complicated installations or modifications. Depending on the frameworks you are using the implementation can be a bit different.
With kind regards,
your SCM-Manager Universe Team