Skip to content

Instantly share code, notes, and snippets.

@laracasts
Created August 20, 2014 20:47
Show Gist options
  • Select an option

  • Save laracasts/52a9f085408605a06400 to your computer and use it in GitHub Desktop.

Select an option

Save laracasts/52a9f085408605a06400 to your computer and use it in GitHub Desktop.
PHPSpec auto-testing Gulpfile
var gulp = require('gulp');
var phpspec = require('gulp-phpspec');
var run = require('gulp-run');
var notify = require('gulp-notify');
gulp.task('test', function() {
gulp.src('spec/**/*.php')
.pipe(run('clear'))
.pipe(phpspec('', { notify: true }))
.on('error', notify.onError({
title: 'Dangit',
message: 'Your tests failed!',
icon: __dirname + '/fail.png'
}))
.pipe(notify({
title: 'Success',
message: 'All tests have returned green!'
}));
});
gulp.task('watch', function() {
gulp.watch(['spec/**/*.php', 'src/**/*.php'], ['test']);
});
gulp.task('default', ['test', 'watch']);
@carnar

carnar commented Sep 19, 2014

Copy link
Copy Markdown

I had to change line 8 for this:
.pipe(run('clear').exec())

@frcaliga

Copy link
Copy Markdown

I had to change line 8 for this:
.pipe(shell('clear'))
and conseguntely
insert the following line
var shell = require('gulp-shell');

@jfmercer

jfmercer commented May 2, 2015

Copy link
Copy Markdown

Like @carnar I too had to change line 8 to .pipe(run('clear').exec())

@mishbah

mishbah commented Aug 21, 2015

Copy link
Copy Markdown

ghost commented Jan 6, 2017

Copy link
Copy Markdown

Yes I had to change line 8 as well .pipe(run('clear').exec()).

@niels-numbers

Copy link
Copy Markdown

THis does not work anymore with Gulp4

@4unkur

4unkur commented Mar 13, 2019

Copy link
Copy Markdown

@cmancre

cmancre commented Jun 27, 2019

Copy link
Copy Markdown

Gulp 4

var gulp = require('gulp');
var phpspec = require('gulp-phpspec');
var run = require('gulp-run');
var notify = require('gulp-notify');

gulp.task('test', function(done){
	gulp.src('spec/**/*.php')
		.pipe(run('clear').exec())
		.pipe(phpspec('', {notify:true}))
		.on('error', notify.onError({
			'title': 'Failed!!!',
			'message': 'Your test failed'
			//'icon': __dirname + '/fail.png',
		}))
		.pipe(notify({
			'title': 'Success',
			'message': 'All tests have returned green'
		}));
	done();
});

gulp.task('watch', function(){
	gulp.watch(['spec/**/*.php', 'src/**/*.php'], gulp.series(['test']));
});

gulp.task('default', gulp.series(['test','watch']));

@cofirazak

cofirazak commented Dec 3, 2019

Copy link
Copy Markdown

Gulp 4

var gulp = require('gulp');
var phpspec = require('gulp-phpspec');
var run = require('gulp-run');
var notify = require('gulp-notify');

gulp.task('test', function(done){
	gulp.src('spec/**/*.php')
		.pipe(run('clear').exec())
		.pipe(phpspec('', {notify:true}))
		.on('error', notify.onError({
			'title': 'Failed!!!',
			'message': 'Your test failed'
			//'icon': __dirname + '/fail.png',
		}))
		.pipe(notify({
			'title': 'Success',
			'message': 'All tests have returned green'
		}));
	done();
});

gulp.task('watch', function(){
	gulp.watch(['spec/**/*.php', 'src/**/*.php'], gulp.series(['test']));
});

gulp.task('default', gulp.series(['test','watch']));

Great! Thanks!
But on macOS Catalina you don't need .exec() in line 8.
Maybe .exec() is for windows? If somebody knows pls put in the comments.

By the way you don't need the gulp-run here, because gulp-phpspec has:
API options.clear
Type: Boolean (Default: false)
Clear console before executing command

If you need an example or more phpspec options, i have a gist with my gulpfile here: https://gist.github.com/cofirazak/8dcf9b5204c7bdafd0f4bcff636a55ee

As a minor fix i would also convert var to const.
And no need in extra quotes around title, message, icon and message json keys.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment