Class: Guard::PHPUnit

Inherits:
Guard
  • Object
show all
Defined in:
lib/guard/phpunit.rb,
lib/guard/phpunit/runner.rb,
lib/guard/phpunit/notifier.rb,
lib/guard/phpunit/inspector.rb,
lib/guard/phpunit/formatter.rb

Overview

The PHPUnit guard gets notified about system events.

Defined Under Namespace

Modules: Formatter, Inspector, Notifier, Runner

Constant Summary

DEFAULT_OPTIONS =
{
  :all_on_start   => true,
  :all_after_pass => true,
  :keep_failed    => true,
  :cli            => nil,
  :tests_path     => Dir.pwd
}

Instance Method Summary (collapse)

Constructor Details

- (PHPUnit) initialize(watchers = [], options = {})

Initialize Guard::PHPUnit.

Parameters:

  • watchers (Array<Guard::Watcher>) (defaults to: [])

    the watchers in the Guard block

  • options (Hash) (defaults to: {})

    the options for the Guard

Options Hash (options):

  • :all_on_start (Boolean)

    run all tests on start

  • :cli (String)

    The CLI arguments passed to phpunit

  • :tests_path (String)

    the path where all tests exist



32
33
34
35
36
37
38
39
40
41
# File 'lib/guard/phpunit.rb', line 32

def initialize(watchers = [], options = {})
  defaults = DEFAULT_OPTIONS.clone
  @options = defaults.merge(options)
  super(watchers, @options)

  @failed_paths     = []
  @previous_failed = false

  Inspector.tests_path = @options[:tests_path]
end

Instance Method Details

- (Object) run_all

Gets called when all tests should be run.

Raises:

  • (:task_has_failed)

    when stop has failed



55
56
57
58
59
60
61
62
# File 'lib/guard/phpunit.rb', line 55

def run_all
  success = Runner.run(options[:tests_path], options.merge(
    :message => 'Running all tests'
  ))

  @previous_failed = !success
  throw :task_has_failed unless success
end

- (Object) run_all_after_pass(tests_passed) (private)

Runs all tests after the failed tests pass.



100
101
102
103
104
105
106
107
108
# File 'lib/guard/phpunit.rb', line 100

def run_all_after_pass(tests_passed)
  return unless @options[:all_after_pass]

  if tests_passed
    run_all if @previous_failed
  else
    @previous_failed = true
  end
end

- (Object) run_on_change(paths)

Gets called when the watched tests have changes.

Parameters:

  • paths (Array<String>)

    to the changed tests

Raises:

  • (:task_has_failed)

    when stop has failed



69
70
71
72
73
74
75
76
# File 'lib/guard/phpunit.rb', line 69

def run_on_change(paths)
  paths = Inspector.clean(paths + @failed_paths)
  success = Runner.run(paths, options)

  update_failed_paths(success, paths)
  run_all_after_pass(success)
  throw :task_has_failed unless success
end

- (Object) start

Gets called once when Guard starts.

Raises:

  • (:task_has_failed)

    when stop has failed



47
48
49
# File 'lib/guard/phpunit.rb', line 47

def start
  run_all if options[:all_on_start]
end

- (Object) update_failed_paths(tests_passed, paths) (private)

Adds or removes path to the failed_paths bassed on the tests result.

Parameters:

  • tests_passed (Boolean)

    whether the tests passed or not

  • paths (Array<String>)

    the tests paths



86
87
88
89
90
91
92
93
94
# File 'lib/guard/phpunit.rb', line 86

def update_failed_paths(tests_passed, paths)
  return unless @options[:keep_failed]

  if tests_passed
    @failed_paths -= paths
  else
    @failed_paths += paths 
  end
end