TYPO3 6.2 - automated test execution and CGL code analysis for ExtBase extensions with Jenkins CI
Some of my public repositories on GitHub are integrated in Travis CI and Scrutinizer CI, so each new commit automatically starts a predefined build process where tests and code analysis are executed. For open source projects, the combination of GitHub, Travis CI and Scrutinizer CI is perfect. For private repositories, you can also use those services if you pay for them or you could built up the CI environtment yourself.On work I've been using Jenkins CI for some years now to build our PHP, Java and Android projects. I'm also using Jenkins CI to process tests automation and code analysis for our TYPO3 extensions, which should be tested on different TYPO3 versions. This configuration was some kind of complicated, since each TYPO3 extension had to be installed in a working TYPO3 environment before it could be tested automatically by Jenkins CI. I've written a blogpost about this setup back in january 2013.
In this article I describe how I've setup a simple job in Jenkins CI for a TYPO3 6.2 ExtBase extension. The job executes the standalone unit tests and also does some TYPO3 CGL code analysis. Everything shown below should be seen as an example, since some analysis tasks like code coverage or pdepend are missing.
Prerequisites
- A running Jenkins CI server with at least the Git Plugin and the PHP Plugin
- ANT, composer, phpunit, phpmd and phpcs installed on the Jenkins CI server
- Jenkins workspace dir configured with ${ITEM_ROOTDIR}/workspace/
- The TYPO3 CMS CGL installed as a global standard
- An ExtBase extension with some working unit tests
Creating the ANT build file
As fas as I know it is still best to use ANT for PHP projects on Jenkins CI. Therefore I created an ANT build file, which contains some tasks for the build. The ANT build file is added to my ExtBase extension (e.g. my_extkey/Resources/Private/Build/build.xml).
The first task will just create some directories.
<target name="init">
<mkdir dir="${env.WORKSPACE}/build"/>
<mkdir dir="${env.WORKSPACE}/build/phpcs"/>
<mkdir dir="${env.WORKSPACE}/build/phpunit"/>
<mkdir dir="${env.WORKSPACE}/typo3_core"/>
</target>
The final structure of the workspace should look like shown below.
- build/ - contains build results
- jobname/ - the name of the Jenkins CI job. In this example the TYPO3 extension key
- typo3_core/ - path for the TYPO3 core
The next task I've created is the task for the unit tests execution. I'll clone the current TYPO3 6.2 master branch, install all dependencies with composer, create typo3conf, typo3temp and uploads folder, symlink the extbase extension to typo3conf/ext/ and finally execute the unit tests.
<target name="unittests">
<exec executable="git" failonerror="true">
<arg line="clone --single-branch --branch master --depth 1 https://github.com/TYPO3/TYPO3.CMS.git ${env.WORKSPACE}/typo3_core" />
</exec>
<exec executable="composer">
<arg line="install --working-dir ${env.WORKSPACE}/typo3_core" />
</exec>
<mkdir dir="${env.WORKSPACE}/typo3_core/uploads"/>
<mkdir dir="${env.WORKSPACE}/typo3_core/typo3temp"/>
<mkdir dir="${env.WORKSPACE}/typo3_core/typo3conf/ext"/>
<symlink link="${env.WORKSPACE}/typo3_core/typo3conf/ext/${env.JOB_NAME}" resource="${env.WORKSPACE}/${env.JOB_NAME}"/>
<exec executable="phpunit" dir="${env.WORKSPACE}/typo3_core">
<arg line="--log-junit ${env.WORKSPACE}/build/phpunit/unittests.xml --bootstrap typo3/sysext/core/Build/UnitTestsBootstrap.php typo3conf/ext/${env.JOB_NAME}/Tests/Unit" />
</exec>
</target>
Note that I'll set the task to fail on error if the TYPO3 core could not be cloned.
Finally I'll add some code analysis tasks and a cleanup task to remove some files from the build.
<target name="phpcs">
<exec executable="phpcs">
<arg line="--report=checkstyle --report-file=${env.WORKSPACE}/build/phpcs/checkstyle.xml --standard=TYPO3CMS --extensions=php,inc ${env.WORKSPACE}/${env.JOB_NAME}" />
</exec>
</target>
<target name="phpmd">
<exec executable="phpmd">
<arg line=" ${env.WORKSPACE}/${env.JOB_NAME} xml codesize,unusedcode,naming,design --reportfile ${env.WORKSPACE}/build/messdetector.xml --exclude Tests/" />
</exec>
</target>
<target name="phpcpd">
<exec executable="phpcpd">
<arg line=" --log-pmd ${env.WORKSPACE}/build/phpcpd.xml ${env.WORKSPACE}/${env.JOB_NAME}" />
</exec>
</target>
<target name="cleanup">
<delete dir="${env.WORKSPACE}/typo3_core"/>
</target>
Update 02.01.2015
I updated the ANT build file so it also supports the execution of functional tests.
Jenkins CI job setup
The job setup on Jenkins CI is as shown on the next screenshots.
The ANT script assumes, that the job name is equal to the TYPO3 extension key. So I'll setup the job in Jenkins CI as shown above.
The job name must be TYPO3 extension key |
The ExtBase extension should be cloned in a seperate directory |
ANT configuration |
Post build analysis |
And finally the results of the phpunit tests should be evaluated.
PHPUnit result analysis |
I've configured the build just to be instable, if a unit test fails (set threshold to zero). You can also configure the build to fail, if a certain amount of failed/incomplete tests are reached.
After some builds, you'll end up with a Jenkins CI job, that shows some graphs of the code analysis.
Code analysis graph |
Build with one failed test |
Outlook and conclusion
As written before, the Jenkins CI job I've shown above should be seen as an simple example. I did not add any build triggers and executed the jobs manually. In a production CI environment this task should be automated, so each new commit to the repository will trigger the build.
Also I did not add any functional tests to the TYPO3 ExtBase extension, because this would require MySQL database access and would make the ANT script and the job setup more complicated. Cloning the whole TYPO3 master branch each time the job executes could also be done more simple, if you have a clone of the repository locally somewhere and just copy all files from there.
Depending on you requirements, you could also add Selenium tests (follow this article on how to setup a Selenium Grid on Jenkins CI) to the job or you could take another step forward and completely automate the deployment of the ExtBase Extension.
Anyhow, I hope I could give you a little insight in using Jenkins CI to automate some tasks a TYPO3 developer should take care of.
By the way: When I wrote this article, I did a clean installation of Jenkins CI in my home network and configured everything from scratch to see, if the example shown in this article works as expected. This all just took me about 1-2 hours until I had a working CI environment.