Last Updated: April 08, 2016
·
6.126K
· arnaudbreton

PHP continuous testing with GruntJS and PHPUnit

Grunt (http://gruntjs.com/) is our preferred tool here at mention (https://mention.com) when it comes to task automation.
It’s a Javascript task runner, offering both a lot of bundled plugins for common tasks, while still being very extensible, giving you the option to write all kinds of tasks to suit your need. The best comes when you can combine tasks together to create even more powerful ones!

Grunt’s scope goes beyond simply automating front-end related tasks. We use it, for example, to continuously test in PHP while we develop:

 var log, terminal;

terminal = require('color-terminal');

log = function(error, stdout, stderr, cb) {
  if (error) {
    terminal.color('red').write(stdout);
  } else {
    terminal.color('green').write(stdout);
  }
  return cb();
};

module.exports = function(grunt) {
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-shell');
  grunt.initConfig({
    testFilepath: null,
    watch: {
      php: {
        options: {
          event: 'changed',
          spawn: false
        },
        files: ['foo/bar/**/*.php', 'foo/bar/**/*Test.php'],
        tasks: 'shell:phpunit'
      }
    },
    shell: {
      phpunit: {
        options: {
          callback: log
        },
        command: 'echo <%= testFilepath %> && phpunit -c app <%= testFilepath %>'
      }
    }
  });
  return grunt.event.on('watch', function(action, filepath, ext) {
    var regex, testFilepath;
    regex = new RegExp("foo/bar/([a-z0-9]+)/([a-z0-9/]+)", "i");
    if (filepath.match(regex)) {
      if (filepath.indexOf('Test') === -1) {
        testFilepath = filepath.replace(regex, "foo/bar/$1/Tests/$2Test");
      } else {
        testFilepath = filepath;
      }
      return grunt.config('testFilepath', testFilepath);
    }
  });
};

This simple Grunt task watch *.php and *Test.php files, and run PHPUnit with the correct test class each time it is modified. Simple but really efficient while in an intense coding session.

Want to learn more ? Here's the list of our favorite front-end tools: http://blog.mention.com/the-5-best-front-end-developer-tools/

1 Response
Add your response

Hey, how's it going! I was wondering if you could help me debug my own implementation of this, which happens to be in a wordpress plugin. I find that when I run phpunit, my tests run just fine. However, when I run grunt, although it does seem to attempt to run the tests, it gives me the output:

scotts-MacBook-Pro:lxb-zendesk scottfennell$ grunt
Running "phpunit-runner:fast" (phpunit-runner) task

Running "shell:phpunit-runner-0" (shell) task

Installing...
Installing network...
Running as multisite...
Not running ajax tests. To execute these, use --group ajax.
Not running ms-files tests. To execute these, use --group ms-files.
Not running external-http tests. To execute these, use --group external-http.
PHPUnit 5.2.3 by Sebastian Bergmann and contributors.

Time: 1.14 seconds, Memory: 40.75Mb

No tests executed!

Any advice? I'm not sure how to debug this.

over 1 year ago ·