class Guard::PHPUnit2::Runner

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

Constants

PHPUNIT_ERRORS_EXITCODE

The exittcode phpunit returns when the tests contain errors

PHPUNIT_FAILURES_EXITCODE

The exittcode phpunit returns when the tests contain failures

Public Class Methods

run(paths, options) click to toggle source
# File lib/guard/phpunit2/runner.rb, line 12
def self.run(paths, options)
  self.new.run(paths, options)
end

Public Instance Methods

run(paths, options = {}) click to toggle source

Runs the PHPUnit tests and displays notifications about the results.

@param [Array<Strings>] path to the tests files. @param (see PHPUnit#initialize) @return [Boolean] whether the tests were run successfully

# File lib/guard/phpunit2/runner.rb, line 31
def run(paths, options = {})
  paths = Array(paths)

  return false if paths.empty?

  unless phpunit_exists?(options)
    Compat::UI.error('the provided php unit command is invalid or phpunit is not installed on your machine.', :reset => true)
    return false
  end

  run_tests(paths, options)
end

Protected Instance Methods

create_tests_folder_for(paths) { |d| ... } click to toggle source

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.

@param (see run) @yield [String] d the temporary dir for the tests

# File lib/guard/phpunit2/runner.rb, line 155
def create_tests_folder_for(paths)
  Dir.mktmpdir('guard_phpunit') do |d|
    symlink_paths_to_tests_folder(paths, d)
    yield d
  end
end
execute_command(command) click to toggle source

Executes a system command and returns the output.

@param [String] command the command to be run @return [String] the output of the executed command

# File lib/guard/phpunit2/runner.rb, line 209
def execute_command(command)
  %x{#{command}}
end
execute_phpunit(tests_folder, options) click to toggle source
# File lib/guard/phpunit2/runner.rb, line 213
def execute_phpunit(tests_folder, options)
  output = execute_command phpunit_command(tests_folder, options)
  puts output

  output
end
notify_failure(options) click to toggle source

Displays a notification about failing to run the tests

@param (see run)

# File lib/guard/phpunit2/runner.rb, line 125
def notify_failure(options)
  Notifier.notify('Failed! Check the console', :title => 'PHPUnit results', :image => :failed)
end
notify_results(output, options) click to toggle source

Displays a notification about the tests results.

@param [String] output the tests output @param (see run)

# File lib/guard/phpunit2/runner.rb, line 111
def notify_results(output, options)
  results = parse_output(output)
  Notifier.notify_results(results)
end
notify_start(paths, options) click to toggle source

Displays the start testing notification.

@param (see run) @param (see run)

# File lib/guard/phpunit2/runner.rb, line 101
def notify_start(paths, options)
  message = options[:message] || "Running: #{paths.join(' ')}"
  Compat::UI.info(message, :reset => true)
end
parse_output(output) click to toggle source

Parses the output into the hash Guard expects

# File lib/guard/phpunit2/runner.rb, line 117
def parse_output(output)
  Formatter.parse_output(output)
end
phpunit_command(path, options) { |cmd_parts| ... } click to toggle source

Generates the phpunit command for the tests paths.

@param (see run) @param (see run) @see run_tests

# File lib/guard/phpunit2/runner.rb, line 183
def phpunit_command(path, options)
  formatter_path = File.expand_path( File.join( File.dirname(__FILE__), '..', 'phpunit', 'formatters', 'PHPUnit-Progress') )
  
  command = "phpunit"
  command = options[:command] if options[:command]

  cmd_parts = []
  cmd_parts << command
  cmd_parts << "--include-path #{formatter_path}"
  cmd_parts << "--printer PHPUnit_Extensions_Progress_ResultPrinter"

  # Allow callers to inject some parts if needed
  yield cmd_parts if block_given?

  cmd_parts << options[:cli] if options[:cli]
  cmd_parts << path


  cmd_parts.join(' ')
end
phpunit_exists?(options) click to toggle source

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

@return [Boolean] The status of phpunit

# File lib/guard/phpunit2/runner.rb, line 51
def phpunit_exists?(options)
  command = "phpunit"
  command = options[:command] if options[:command]

  `#{command} --version`
  true
rescue Errno::ENOENT
  false
end
run_tests(paths, options) click to toggle source

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

@param (see run) @param (see run)

# File lib/guard/phpunit2/runner.rb, line 67
def run_tests(paths, options)

  notify_start(paths, options)

  if paths.length == 1
    tests_path = paths.first
    output = execute_phpunit(tests_path, options)
  else
    create_tests_folder_for(paths) do |tests_folder|
      output = execute_phpunit(tests_folder, options)
    end
  end
  
  
  # return false in case the system call fails with no status!
  return false if $?.nil?

  # capture success so that if notifications alter the status stored in $? we still return the correct value
  success = $?.success?

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

  success
end
tests_contain_errors?() click to toggle source

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

@return [Boolean] whether the tests contain errors or not

# File lib/guard/phpunit2/runner.rb, line 143
def tests_contain_errors?
  $?.exitstatus == PHPUNIT_ERRORS_EXITCODE
end
tests_contain_failures?() click to toggle source

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

@return [Boolean] whether the tests contain failures or not

# File lib/guard/phpunit2/runner.rb, line 134
def tests_contain_failures?
  $?.exitstatus == PHPUNIT_FAILURES_EXITCODE
end