Skip to content

Instantly share code, notes, and snippets.

@rymai
Created October 11, 2011 22:16
Show Gist options
  • Save rymai/1279627 to your computer and use it in GitHub Desktop.
Save rymai/1279627 to your computer and use it in GitHub Desktop.
Guard::Vagrant + Guard::PhpUnit
guard :vagrant, :cwd => '/custom/cwd'
guard :phpunit, :vagrant => true do
watch %r{^website/application/(.+)\.php}
watch %r{^website/tests/unit/(.+)\.php}
watch 'website/tests/unit/phpunit.xml'
end
require 'guard/guard'
module ::Guard
# Runs the phpunit test suite whenever a .php file in lib/ or test changes
#
class PhpUnit < ::Guard::Guard
def initialize(watchers = [], options = {})
super
@vagrant_env = .cwd if options[:vagrant]
guard_vagrant = ::Guard.guards(:vagrant)
unless guard_vagrant.nil?
@vagrant_env = Vagrant::Environment.new(:cwd => guard_vagrant.cwd)
end
end
def vagrant?
@vagrant_env
end
def run_on_change(paths)
UI.info 'Running phpunit', :reset => true
return if vagrant? && !vagrant_ready?
if success = run_tests
::Guard::Notifier.notify(success, :title => 'PHPUnit', :image => :success)
else
::Guard::Notifier.notify('Tests failed!', :title => 'PHPUnit', :image => :failed)
end
end
def run_tests
if vagrant?
@vagrant_env.primary_vm.ssh.execute do |ssh|
ssh.exec!(php_unit_command, :error_check => false) do |ch, type, data|
!(type == :exit_status)
end
end
else
system(php_unit_command)
end
end
def php_unit_command
tests_dir = File.dirname(File.expand_path(__FILE__)) + "/website/tests/unit"
"cd #{tests_dir}; phpunit"
end
def vagrant_ready?
if !@vagrant_env.primary_vm.created?
::Guard::Notifier.notify("Must run `vagrant up`", :title => 'PHPUnit')
false
elsif !@vagrant_env.primary_vm.vm.running?
::Guard::Notifier.notify("Vagrant must be running!", :title => 'PHPUnit')
false
else
true
end
end
end
end
require 'guard/guard'
require 'vagrant'
module ::Guard
# Start, stop and reload Vagrant VMs on file modifications
#
class Vagrant < ::Guard::Guard
attr_accessor :cwd
def initialize(watchers = [], options = {})
super
@cwd = File.expand_path(options[:cwd] || "~/vagrant/my-development")
end
def start
# Start your Vagrant VMs here
end
def stop
# Stop your Vagrant VMs here
end
def reload
# Reload your Vagrant VMs here
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment