Setting up GrumPHP with Laravel

First of lets go through what is GrumPHP and how it can help in our workflow to make pull requests less “Code Check Styles” and more proper architecture review. There are specifically built tools for code style checks such as nitpick or styleCI, but even tools like these will only help you after you push your code to GitHub.

GrumPHP will do all the style checks and other tasks, such as run PHPunit before you do a commit.


Getting started

Prerequisites

This guide expects you have the following tools installed

Create a new Laravel project

$ composer create-project laravel/laravel grumphp

Setting up development environment (optional)

After the installation is done, go inside the project directory and install homestead for this project to set up a development environment for this specific project.

$ cd /path/to/project
$ composer require laravel/homestead --dev
$ ./vendor/bin/homestead make
$ vagrant up

Now that everything is provisioned launch your browser and open the IP address of the homestead box (192.168.10.10 default value) 192.168.10.10.xip.io where we will see Laravel welcoming screen.

Fresh Laravel 5 Installation

Install GrumPHP

Now that we have Laravel set up let’s install GrumPHP.

$ composer require phpro/grumphp --dev

Setting up GrumPHP

Since GrumPHP is a package that hooks into your git commit, we need to initialize git on this project, and add everything to stage.

$ git init
$ git add .

Now that is done, we can run GrumPHP CLI to see if everything is set. Open up a terminal and run the following commands.

$ cd /path/to/project
$ ./vendor/bin/grumphp run

After you run that you should see something like the following, if so you are on the right track.

Terminal output after running ./vendor/bin/grumphp run

Adding GrumPHP task

With GrumPHP you have the ability to run multiple tasks, such as gulp, PHPUnit, and PHPcs. A complete list of tasks which you may run may be found here.

Adding PSR-2 checks

When making a pull request to a project at work or in open source for code review most of the time it will end up as a style check, so why not just prevent the user from committing if they don’t follow PSR-2 standards?

First we need to install another php package that sniffs your code and checks for any code that doesn’t follow PSR-2 (other settings available). You might have this package installed in your global vendor file, but for this article I’m going to show you how to install it individually to keep your projects isolated just in case you might want to use different versions.

$ code /path/to/project
$ composer require squizlabs/php_codesniffer --dev

Now lets open grumphp.yml with your editor of choice and add the following.

parameters:
git_dir: .
bin_dir: vendor/bin
tasks:
phpcs:
standard: PSR2
ignore_patterns: [./tests/*, ./resources/*, ./database/*, ./bootstrap/*]

As you can see we added the PHPcs task containing some parameters. First we need to set a standard which we shall use to check, we will be going with PSR-2 for this article. We also added some ignore_patterns as we don’t want to run PSR-2 checks in any of those directories since they don’t need to follow the PSR-2 standard, such as the tests and the blade views. You can take a look at all of the available configurations here

Run GrumPHP again from the terminal to see if we still get green in your terminal.

$ ./vendor/bin/grumphp run

Now that we get green lets see if it actually works, go app/user.php and put the curly brace of the class next to the deceleration, like the following

class User extends Authenticatable {

Now if we run GrumPHP from the CLI again we should get an error.

Even when we try to commit, it won’t let us do the commit until we fix the issue.

$ git commit -m 'Initial Commit'

Add PHPUnit

Lets add unit tests as well to run before we do every commit so we are sure that everything is green before we commit. Add the following inside the grumphp.yml under tasks

parameters:
git_dir: .
bin_dir: vendor/bin
tasks:
phpcs:
standard: PSR2
ignore_patterns: [./tests/*, ./resources/*, ./database/*, ./bootstrap/*]
phpunit:

Run GrumPHP again and you should see that now PHPUnit is running and get green.

$ ./vendor/bin/grumphp run

To see this in action, we are going to break the example test that Laravel gives us as an example. The test can be found in tests/ExampleTest.php. Make the following changes

Now try to commit, as you see if will stop us from going any further.

Adding a gulp task

To run tests, especially accepted tests, we might want to run gulp so everything is built for the frontend. GrumPHP is smart enough to run build tasks first before running the tests.

First we need to run npm install so that we have gulp installed for our project and globally.

$ npm install
$ npm install --global gulp-cli

After that add the gulp task inside of your grumphp.yml file

parameters:
git_dir: .
bin_dir: vendor/bin
tasks:
phpcs:
standard: PSR2
ignore_patterns: [./tests/*, ./resources/*, ./database/*, ./bootstrap/*]
gulp:
phpunit:

This will run all of the tasks found inside of your gulpfile.js, if you wish to simply run certain tasks you can easily do this, but adding some configuration can be extremely helpful, more information can be found in the docs

Wrap up

There are tons of other tasks which you may run. Head over to the documentation in order to see a list of them and any configurations for each task that is needed. I hope this article helps you in any way possible. You can find a Github repository as a demo to help you out, which you see here

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.