Module: Guard::PHPUnit::Runner

Defined in:
lib/guard/phpunit/runner.rb

Overview

The Guard::PHPUnit runner handles running the tests, displaying their output and notifying the user about the results.

Constant Summary

PHPUNIT_FAILURES_EXITCODE =

The exittcode phpunit returns when the tests contain failures

1
PHPUNIT_ERRORS_EXITCODE =

The exittcode phpunit returns when the tests contain errors

2

Class Method Summary (collapse)

Class Method Details

+ (Object) create_tests_folder_for(paths) {|String| ... } (private)

Creates a temporary folder which has links to the tests paths. This method is used because PHPUnit can't run multiple tests files at the same time and generate one result for them.

Yields:

  • (String)

    d the temporary dir for the tests



142
143
144
145
146
147
# File 'lib/guard/phpunit/runner.rb', line 142

def create_tests_folder_for(paths)
  Dir.mktmpdir('guard_phpunit') do |d|
    symlink_paths_to_tests_folder(paths, d)
    yield d
  end
end

+ (String) execute_command(command) (private)

Executes a system command and returns the output.

Parameters:

  • command (String)

    the command to be run

Returns:

  • (String)

    the output of the executed command



188
189
190
# File 'lib/guard/phpunit/runner.rb', line 188

def execute_command(command)
  %x{#{command}}
end

+ (Object) notify_failure(options) (private)

Displays a notification about failing to run the tests



111
112
113
114
# File 'lib/guard/phpunit/runner.rb', line 111

def notify_failure(options)
  return if options[:notification] == false
  Notifier.notify('Failed! Check the console', :title => 'PHPUnit results', :image => :failed)
end

+ (Object) notify_results(output, options) (private)

Displays a notification about the tests results.

Parameters:

  • output (String)

    the tests output



101
102
103
104
105
# File 'lib/guard/phpunit/runner.rb', line 101

def notify_results(output, options)
  return if options[:notification] == false
  results = Formatter.parse_output(output)
  Notifier.notify_results(results)
end

+ (Object) notify_start(paths, options) (private)

Displays the start testing notification.



91
92
93
94
# File 'lib/guard/phpunit/runner.rb', line 91

def notify_start(paths, options)
  message = options[:message] || "Running: #{paths.join(' ')}"
  UI.info(message, :reset => true)
end

+ (Object) phpunit_command(path, options) (private)

Generates the phpunit command for the tests paths.

See Also:

  • #run_tests


170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/guard/phpunit/runner.rb', line 170

def phpunit_command(path, options)
  formatter_path = File.join( File.dirname(__FILE__), 'formatters', 'PHPUnit-Progress')

  cmd_parts = []
  cmd_parts << "phpunit"
  cmd_parts << "--include-path #{formatter_path}"
  cmd_parts << "--printer PHPUnit_Extensions_Progress_ResultPrinter"
  cmd_parts << options[:cli] if options[:cli]
  cmd_parts << path

  cmd_parts.join(' ')
end

+ (Boolean) phpunit_exists? (private)

Checks that phpunit is installed on the user's machine.

Returns:

  • (Boolean)

    The status of phpunit



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

def phpunit_exists?
  system('which phpunit > /dev/null 2>&1')
end

+ (Boolean) run(paths, options = {})

Runs the PHPUnit tests and displays notifications about the results.

Parameters:

  • path (Array<Strings>)

    to the tests files.

  • watchers (Array<Guard::Watcher>)

    the watchers in the Guard block

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

    the options for the Guard

Returns:

  • (Boolean)

    whether the tests were run successfully



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/guard/phpunit/runner.rb', line 28

def run(paths, options = {})
  paths = Array(paths)
  
  return false if paths.empty?
  
  unless phpunit_exists?
    UI.error('phpunit is not installed on your machine.', :reset => true)
    return false
  end

  run_tests(paths, options)
end

+ (Object) run_tests(paths, options) (private)

Executes the testing command on the tests and returns the status of this process.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/guard/phpunit/runner.rb', line 58

def run_tests(paths, options)

  notify_start(paths, options)

  if paths.length == 1
    tests_path = paths.first
    output = execute_command phpunit_command(tests_path, options)
  else
    create_tests_folder_for(paths) do |tests_folder|
      output = execute_command phpunit_command(tests_folder, options)
    end
  end

  # print the output to the terminal
  UI.info output

  # return false in case the system call fails with no status!
  return false if $?.nil?

  if $?.success? or tests_contain_failures? or tests_contain_errors?
    notify_results(output, options)
  else
    notify_failure(options) 
  end

  $?.success?
end

Creates symbolic links inside the folder pointing back to the paths.

Parameters:

  • the (String)

    folder in which the links must be made

See Also:

  • #create_tests_folder_for


157
158
159
160
161
162
# File 'lib/guard/phpunit/runner.rb', line 157

def symlink_paths_to_tests_folder(paths, folder)
  paths.each do |p|
    FileUtils.mkdir_p( File.join(folder, File.dirname(p) ) ) unless File.dirname(p) == '.'
    FileUtils.ln_s(Pathname.new(p).realpath, File.join(folder, p))
  end
end

+ (Boolean) tests_contain_errors? (private)

Checks the exitstatus of the phpunit command for a sign of errors in the tests.

Returns:

  • (Boolean)

    whether the tests contain errors or not



130
131
132
# File 'lib/guard/phpunit/runner.rb', line 130

def tests_contain_errors?
  $?.exitstatus == PHPUNIT_ERRORS_EXITCODE
end

+ (Boolean) tests_contain_failures? (private)

Checks the exitstatus of the phpunit command for a sign of failures in the tests.

Returns:

  • (Boolean)

    whether the tests contain failures or not



121
122
123
# File 'lib/guard/phpunit/runner.rb', line 121

def tests_contain_failures?
  $?.exitstatus == PHPUNIT_FAILURES_EXITCODE
end