From 0355e83b51b957dd561b534e886f123f7cf6c836 Mon Sep 17 00:00:00 2001 From: Taufek Johar Date: Sat, 10 Feb 2018 19:16:49 +0800 Subject: [PATCH] Add PhpUnit Hook for PrePush Add `PhpUnit` test runner hook for `PrePush` context. --- config/default.yml | 7 +++++ lib/overcommit/hook/pre_push/php_unit.rb | 14 +++++++++ .../overcommit/hook/pre_push/php_unit_spec.rb | 29 +++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 lib/overcommit/hook/pre_push/php_unit.rb create mode 100644 spec/overcommit/hook/pre_push/php_unit_spec.rb diff --git a/config/default.yml b/config/default.yml index 12c01396..5cbea6ff 100644 --- a/config/default.yml +++ b/config/default.yml @@ -1119,6 +1119,13 @@ PrePush: command: ['ruby', '-Ilib:test', '-rminitest', "-e 'exit! Minitest.run'"] include: 'test/**/*_test.rb' + PhpUnit: + enabled: false + description: 'Run PhpUnit test suite' + command: 'vendor/bin/phpunit' + flags: ['--bootstrap', 'vendor/autoload.php', 'tests'] + install_command: 'composer require --dev phpunit/phpunit' + ProtectedBranches: enabled: false description: 'Check for illegal pushes to protected branches' diff --git a/lib/overcommit/hook/pre_push/php_unit.rb b/lib/overcommit/hook/pre_push/php_unit.rb new file mode 100644 index 00000000..758718d5 --- /dev/null +++ b/lib/overcommit/hook/pre_push/php_unit.rb @@ -0,0 +1,14 @@ +module Overcommit::Hook::PrePush + # Runs `phpunit` test suite before push + # + # @see https://phpunit.de/ + class PhpUnit < Base + def run + result = execute(command) + return :pass if result.success? + + output = result.stdout + result.stderr + [:fail, output] + end + end +end diff --git a/spec/overcommit/hook/pre_push/php_unit_spec.rb b/spec/overcommit/hook/pre_push/php_unit_spec.rb new file mode 100644 index 00000000..5c8bf0cc --- /dev/null +++ b/spec/overcommit/hook/pre_push/php_unit_spec.rb @@ -0,0 +1,29 @@ +require 'spec_helper' + +describe Overcommit::Hook::PrePush::PhpUnit do + let(:config) { Overcommit::ConfigurationLoader.default_configuration } + let(:context) { double('context') } + subject { described_class.new(config, context) } + + context 'when phpunit exits successfully' do + before do + result = double('result') + result.stub(:success?).and_return(true) + subject.stub(:execute).and_return(result) + end + + it { should pass } + end + + context 'when phpunit exits unsucessfully' do + before do + result = double('result') + result.stub(:success?).and_return(false) + result.stub(:stdout).and_return('Some error message') + result.stub(:stderr).and_return('') + subject.stub(:execute).and_return(result) + end + + it { should fail_hook 'Some error message' } + end +end