guard-phpunit 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (29) hide show
  1. data/CHANGELOG.md +37 -0
  2. data/LICENSE +19 -19
  3. data/README.md +106 -106
  4. data/lib/guard/phpunit.rb +112 -112
  5. data/lib/guard/phpunit/formatter.rb +64 -64
  6. data/lib/guard/phpunit/formatters/PHPUnit-Progress/PHPUnit/Extensions/Progress/ResultPrinter.php +498 -498
  7. data/lib/guard/phpunit/formatters/PHPUnit-Progress/README.markdown +44 -0
  8. data/lib/guard/phpunit/formatters/PHPUnit-Progress/Tests/_files/Number.php +78 -0
  9. data/lib/guard/phpunit/formatters/PHPUnit-Progress/Tests/_files/NumberTest.php +99 -0
  10. data/lib/guard/phpunit/formatters/PHPUnit-Progress/Tests/_files/emptyTest.php +6 -0
  11. data/lib/guard/phpunit/formatters/PHPUnit-Progress/Tests/printer_basic.phpt +22 -0
  12. data/lib/guard/phpunit/formatters/PHPUnit-Progress/Tests/printer_errors.phpt +27 -0
  13. data/lib/guard/phpunit/formatters/PHPUnit-Progress/Tests/printer_errors_variation.phpt +28 -0
  14. data/lib/guard/phpunit/formatters/PHPUnit-Progress/Tests/printer_failing.phpt +31 -0
  15. data/lib/guard/phpunit/formatters/PHPUnit-Progress/Tests/printer_failing_variation.phpt +32 -0
  16. data/lib/guard/phpunit/formatters/PHPUnit-Progress/Tests/printer_incomplete.phpt +19 -0
  17. data/lib/guard/phpunit/formatters/PHPUnit-Progress/Tests/printer_incomplete_variation.phpt +20 -0
  18. data/lib/guard/phpunit/formatters/PHPUnit-Progress/Tests/printer_passing.phpt +19 -0
  19. data/lib/guard/phpunit/formatters/PHPUnit-Progress/Tests/printer_passing_variation.phpt +20 -0
  20. data/lib/guard/phpunit/formatters/PHPUnit-Progress/Tests/printer_skipped.phpt +19 -0
  21. data/lib/guard/phpunit/formatters/PHPUnit-Progress/Tests/printer_skipped_variation.phpt +20 -0
  22. data/lib/guard/phpunit/formatters/PHPUnit-Progress/phpunit.xml +4 -0
  23. data/lib/guard/phpunit/formatters/PHPUnit-Progress/screenshot.png +0 -0
  24. data/lib/guard/phpunit/inspector.rb +54 -54
  25. data/lib/guard/phpunit/notifier.rb +68 -68
  26. data/lib/guard/phpunit/runner.rb +197 -194
  27. data/lib/guard/phpunit/templates/Guardfile +3 -3
  28. data/lib/guard/phpunit/version.rb +6 -6
  29. metadata +37 -3
@@ -0,0 +1,19 @@
1
+ --TEST--
2
+ phpunit Number - Only skipped tests
3
+ --FILE--
4
+ <?php
5
+ $group = 'skipped';
6
+ $testClass = 'NumberTest';
7
+
8
+ $_SERVER['argv'][1] = '--group';
9
+ $_SERVER['argv'][2] = $group;
10
+ $_SERVER['argv'][3] = dirname(__FILE__) . "/_files/${testClass}.php";
11
+
12
+ require_once 'PHPUnit/Autoload.php';
13
+ PHPUnit_TextUI_Command::main();
14
+ ?>
15
+ --EXPECTF--
16
+ %rS+%r
17
+
18
+ Finished in %i second%S
19
+ %i tests, 0 assertions, %i skipped
@@ -0,0 +1,20 @@
1
+ --TEST--
2
+ phpunit Number - Only skipped tests and colors enabled
3
+ --FILE--
4
+ <?php
5
+ $group = 'skipped';
6
+ $testClass = 'NumberTest';
7
+
8
+ $_SERVER['argv'][1] = '--group';
9
+ $_SERVER['argv'][2] = $group;
10
+ $_SERVER['argv'][3] = '--colors';
11
+ $_SERVER['argv'][4] = dirname(__FILE__) . "/_files/${testClass}.php";
12
+
13
+ require_once 'PHPUnit/Autoload.php';
14
+ PHPUnit_TextUI_Command::main();
15
+ ?>
16
+ --EXPECTF--
17
+ SS
18
+
19
+ Finished in %i second%S
20
+ 2 tests, 0 assertions, 2 skipped
@@ -0,0 +1,4 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+
3
+ <phpunit printerClass="PHPUnit_Extensions_Progress_ResultPrinter">
4
+ </phpunit>
@@ -1,54 +1,54 @@
1
- module Guard
2
- class PHPUnit
3
-
4
- # The Guard::PHPUnit inspector verfies that the changed paths
5
- # are valid for Guard::PHPUnit.
6
- #
7
- module Inspector
8
- class << self
9
-
10
- attr_accessor :tests_path
11
-
12
- # Clean the changed paths and return only valid
13
- # PHPUnit tests files.
14
- #
15
- # @param [Array<String>] paths the changed paths
16
- # @return [Array<String>] the valid tests files
17
- #
18
- def clean(paths)
19
- paths.uniq!
20
- paths.compact!
21
- paths = paths.select { |p| test_file?(p) }
22
- clear_tests_files_list
23
- paths
24
- end
25
-
26
- private
27
-
28
- # Checks if the paths is a valid test file.
29
- #
30
- # @param [String] path the test path
31
- # @return [Boolean] whether the path a valid test or not
32
- #
33
- def test_file?(path)
34
- tests_files.include?(path)
35
- end
36
-
37
- # Scans the tests path and keeps a list of all
38
- # tests paths.
39
- #
40
- def tests_files
41
- @tests_files ||= Dir.glob( File.join(tests_path, '**', '*Test.php') )
42
- end
43
-
44
- # Clears the list of PHPUnit tests.
45
- #
46
- # @see #clean
47
- #
48
- def clear_tests_files_list
49
- @tests_files = nil
50
- end
51
- end
52
- end
53
- end
54
- end
1
+ module Guard
2
+ class PHPUnit
3
+
4
+ # The Guard::PHPUnit inspector verfies that the changed paths
5
+ # are valid for Guard::PHPUnit.
6
+ #
7
+ module Inspector
8
+ class << self
9
+
10
+ attr_accessor :tests_path
11
+
12
+ # Clean the changed paths and return only valid
13
+ # PHPUnit tests files.
14
+ #
15
+ # @param [Array<String>] paths the changed paths
16
+ # @return [Array<String>] the valid tests files
17
+ #
18
+ def clean(paths)
19
+ paths.uniq!
20
+ paths.compact!
21
+ paths = paths.select { |p| test_file?(p) }
22
+ clear_tests_files_list
23
+ paths
24
+ end
25
+
26
+ private
27
+
28
+ # Checks if the paths is a valid test file.
29
+ #
30
+ # @param [String] path the test path
31
+ # @return [Boolean] whether the path a valid test or not
32
+ #
33
+ def test_file?(path)
34
+ tests_files.include?(path)
35
+ end
36
+
37
+ # Scans the tests path and keeps a list of all
38
+ # tests paths.
39
+ #
40
+ def tests_files
41
+ @tests_files ||= Dir.glob( File.join(tests_path, '**', '*Test.php') )
42
+ end
43
+
44
+ # Clears the list of PHPUnit tests.
45
+ #
46
+ # @see #clean
47
+ #
48
+ def clear_tests_files_list
49
+ @tests_files = nil
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -1,68 +1,68 @@
1
- module Guard
2
- class PHPUnit
3
-
4
- # The Guard::PHPUnit notifier displays a notification pop-up
5
- # with the tests results.
6
- #
7
- module Notifier
8
- class << self
9
-
10
- # Displays a system notification.
11
- #
12
- # @param [String] message the message to show
13
- # @param [Hash] options the notifier options
14
- #
15
- def notify(message, options)
16
- ::Guard::Notifier.notify(message, options)
17
- end
18
-
19
- # Displays a notification about the tests results.
20
- #
21
- # @param [Hash] test_results the parsed tests results
22
- # @option test_results [Integer] :tests tests count
23
- # @option test_results [Integer] :failures failures count
24
- # @option test_results [Integer] :errors count count
25
- # @option test_results [Integer] :pending pending tests count
26
- # @option test_results [Integer] :duration tests duration
27
- #
28
- def notify_results(test_results)
29
- notify(message(test_results), {
30
- :title => 'PHPUnit results',
31
- :image => image(test_results)
32
- })
33
- end
34
-
35
- private
36
-
37
- # Formats the message for the tests results notifier.
38
- #
39
- # @param (see .notify)
40
- # @return [String] the message
41
- #
42
- def message(results)
43
- message = "#{results[:tests]} tests, #{results[:failures]} failures"
44
- message << "\n#{results[:errors]} errors" if results[:errors] > 0
45
- message << " (#{results[:pending]} pending)" if results[:pending] > 0
46
- message << "\nin #{results[:duration]} seconds"
47
- message
48
- end
49
-
50
- # Returns the appropriate image for the tests results.
51
- #
52
- # @param (see .notify)
53
- # @return [Symbol] the image symbol
54
- #
55
- def image(results)
56
- case
57
- when results[:failures] + results[:errors] > 0
58
- :failed
59
- when results[:pending] > 0
60
- :pending
61
- else
62
- :success
63
- end
64
- end
65
- end
66
- end
67
- end
68
- end
1
+ module Guard
2
+ class PHPUnit
3
+
4
+ # The Guard::PHPUnit notifier displays a notification pop-up
5
+ # with the tests results.
6
+ #
7
+ module Notifier
8
+ class << self
9
+
10
+ # Displays a system notification.
11
+ #
12
+ # @param [String] message the message to show
13
+ # @param [Hash] options the notifier options
14
+ #
15
+ def notify(message, options)
16
+ ::Guard::Notifier.notify(message, options)
17
+ end
18
+
19
+ # Displays a notification about the tests results.
20
+ #
21
+ # @param [Hash] test_results the parsed tests results
22
+ # @option test_results [Integer] :tests tests count
23
+ # @option test_results [Integer] :failures failures count
24
+ # @option test_results [Integer] :errors count count
25
+ # @option test_results [Integer] :pending pending tests count
26
+ # @option test_results [Integer] :duration tests duration
27
+ #
28
+ def notify_results(test_results)
29
+ notify(message(test_results), {
30
+ :title => 'PHPUnit results',
31
+ :image => image(test_results)
32
+ })
33
+ end
34
+
35
+ private
36
+
37
+ # Formats the message for the tests results notifier.
38
+ #
39
+ # @param (see .notify)
40
+ # @return [String] the message
41
+ #
42
+ def message(results)
43
+ message = "#{results[:tests]} tests, #{results[:failures]} failures"
44
+ message << "\n#{results[:errors]} errors" if results[:errors] > 0
45
+ message << " (#{results[:pending]} pending)" if results[:pending] > 0
46
+ message << "\nin #{results[:duration]} seconds"
47
+ message
48
+ end
49
+
50
+ # Returns the appropriate image for the tests results.
51
+ #
52
+ # @param (see .notify)
53
+ # @return [Symbol] the image symbol
54
+ #
55
+ def image(results)
56
+ case
57
+ when results[:failures] + results[:errors] > 0
58
+ :failed
59
+ when results[:pending] > 0
60
+ :pending
61
+ else
62
+ :success
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -1,194 +1,197 @@
1
- require 'tmpdir'
2
- require 'fileutils'
3
-
4
- module Guard
5
- class PHPUnit
6
-
7
- # The Guard::PHPUnit runner handles running the tests, displaying
8
- # their output and notifying the user about the results.
9
- #
10
- module Runner
11
- class << self
12
-
13
- # The exittcode phpunit returns when the tests contain failures
14
- #
15
- PHPUNIT_FAILURES_EXITCODE = 1
16
-
17
- # The exittcode phpunit returns when the tests contain errors
18
- #
19
- PHPUNIT_ERRORS_EXITCODE = 2
20
-
21
- # Runs the PHPUnit tests and displays notifications
22
- # about the results.
23
- #
24
- # @param [Array<Strings>] path to the tests files.
25
- # @param (see PHPUnit#initialize)
26
- # @return [Boolean] whether the tests were run successfully
27
- #
28
- def run(paths, options = {})
29
- paths = Array(paths)
30
-
31
- return false if paths.empty?
32
-
33
- unless phpunit_exists?
34
- UI.error('phpunit is not installed on your machine.', :reset => true)
35
- return false
36
- end
37
-
38
- run_tests(paths, options)
39
- end
40
-
41
- private
42
-
43
- # Checks that phpunit is installed on the user's
44
- # machine.
45
- #
46
- # @return [Boolean] The status of phpunit
47
- #
48
- def phpunit_exists?
49
- system('which phpunit > /dev/null 2>&1')
50
- end
51
-
52
- # Executes the testing command on the tests
53
- # and returns the status of this process.
54
- #
55
- # @param (see #run)
56
- # @param (see #run)
57
- #
58
- def run_tests(paths, options)
59
-
60
- notify_start(paths, options)
61
-
62
- if paths.length == 1
63
- tests_path = paths.first
64
- output = execute_command phpunit_command(tests_path, options)
65
- else
66
- create_tests_folder_for(paths) do |tests_folder|
67
- output = execute_command phpunit_command(tests_folder, options)
68
- end
69
- end
70
-
71
- # print the output to the terminal
72
- UI.info output
73
-
74
- # return false in case the system call fails with no status!
75
- return false if $?.nil?
76
-
77
- if $?.success? or tests_contain_failures? or tests_contain_errors?
78
- notify_results(output, options)
79
- else
80
- notify_failure(options)
81
- end
82
-
83
- $?.success?
84
- end
85
-
86
- # Displays the start testing notification.
87
- #
88
- # @param (see #run)
89
- # @param (see #run)
90
- #
91
- def notify_start(paths, options)
92
- message = options[:message] || "Running: #{paths.join(' ')}"
93
- UI.info(message, :reset => true)
94
- end
95
-
96
- # Displays a notification about the tests results.
97
- #
98
- # @param [String] output the tests output
99
- # @param (see #run)
100
- #
101
- def notify_results(output, options)
102
- return if options[:notification] == false
103
- results = Formatter.parse_output(output)
104
- Notifier.notify_results(results)
105
- end
106
-
107
- # Displays a notification about failing to run the tests
108
- #
109
- # @param (see #run)
110
- #
111
- def notify_failure(options)
112
- return if options[:notification] == false
113
- Notifier.notify('Failed! Check the console', :title => 'PHPUnit results', :image => :failed)
114
- end
115
-
116
- # Checks the exitstatus of the phpunit command
117
- # for a sign of failures in the tests.
118
- #
119
- # @return [Boolean] whether the tests contain failures or not
120
- #
121
- def tests_contain_failures?
122
- $?.exitstatus == PHPUNIT_FAILURES_EXITCODE
123
- end
124
-
125
- # Checks the exitstatus of the phpunit command
126
- # for a sign of errors in the tests.
127
- #
128
- # @return [Boolean] whether the tests contain errors or not
129
- #
130
- def tests_contain_errors?
131
- $?.exitstatus == PHPUNIT_ERRORS_EXITCODE
132
- end
133
-
134
- # Creates a temporary folder which has links to
135
- # the tests paths. This method is used because PHPUnit
136
- # can't run multiple tests files at the same time and generate
137
- # one result for them.
138
- #
139
- # @param (see #run)
140
- # @yield [String] d the temporary dir for the tests
141
- #
142
- def create_tests_folder_for(paths)
143
- Dir.mktmpdir('guard_phpunit') do |d|
144
- symlink_paths_to_tests_folder(paths, d)
145
- yield d
146
- end
147
- end
148
-
149
- # Creates symbolic links inside the folder pointing
150
- # back to the paths.
151
- #
152
- # @see #create_tests_folder_for
153
- #
154
- # @param (see #run)
155
- # @param [String] the folder in which the links must be made
156
- #
157
- def symlink_paths_to_tests_folder(paths, folder)
158
- paths.each do |p|
159
- FileUtils.mkdir_p( File.join(folder, File.dirname(p) ) ) unless File.dirname(p) == '.'
160
- FileUtils.ln_s(Pathname.new(p).realpath, File.join(folder, p))
161
- end
162
- end
163
-
164
- # Generates the phpunit command for the tests paths.
165
- #
166
- # @param (see #run)
167
- # @param (see #run)
168
- # @see #run_tests
169
- #
170
- def phpunit_command(path, options)
171
- formatter_path = File.join( File.dirname(__FILE__), 'formatters', 'PHPUnit-Progress')
172
-
173
- cmd_parts = []
174
- cmd_parts << "phpunit"
175
- cmd_parts << "--include-path #{formatter_path}"
176
- cmd_parts << "--printer PHPUnit_Extensions_Progress_ResultPrinter"
177
- cmd_parts << options[:cli] if options[:cli]
178
- cmd_parts << path
179
-
180
- cmd_parts.join(' ')
181
- end
182
-
183
- # Executes a system command and returns the output.
184
- #
185
- # @param [String] command the command to be run
186
- # @return [String] the output of the executed command
187
- #
188
- def execute_command(command)
189
- %x{#{command}}
190
- end
191
- end
192
- end
193
- end
194
- end
1
+ require 'tmpdir'
2
+ require 'fileutils'
3
+
4
+ module Guard
5
+ class PHPUnit
6
+
7
+ # The Guard::PHPUnit runner handles running the tests, displaying
8
+ # their output and notifying the user about the results.
9
+ #
10
+ module Runner
11
+ class << self
12
+
13
+ # The exittcode phpunit returns when the tests contain failures
14
+ #
15
+ PHPUNIT_FAILURES_EXITCODE = 1
16
+
17
+ # The exittcode phpunit returns when the tests contain errors
18
+ #
19
+ PHPUNIT_ERRORS_EXITCODE = 2
20
+
21
+ # Runs the PHPUnit tests and displays notifications
22
+ # about the results.
23
+ #
24
+ # @param [Array<Strings>] path to the tests files.
25
+ # @param (see PHPUnit#initialize)
26
+ # @return [Boolean] whether the tests were run successfully
27
+ #
28
+ def run(paths, options = {})
29
+ paths = Array(paths)
30
+
31
+ return false if paths.empty?
32
+
33
+ unless phpunit_exists?
34
+ UI.error('phpunit is not installed on your machine.', :reset => true)
35
+ return false
36
+ end
37
+
38
+ run_tests(paths, options)
39
+ end
40
+
41
+ private
42
+
43
+ # Checks that phpunit is installed on the user's
44
+ # machine.
45
+ #
46
+ # @return [Boolean] The status of phpunit
47
+ #
48
+ def phpunit_exists?
49
+ `phpunit --version`
50
+ true
51
+ rescue Errno::ENOENT
52
+ false
53
+ end
54
+
55
+ # Executes the testing command on the tests
56
+ # and returns the status of this process.
57
+ #
58
+ # @param (see #run)
59
+ # @param (see #run)
60
+ #
61
+ def run_tests(paths, options)
62
+
63
+ notify_start(paths, options)
64
+
65
+ if paths.length == 1
66
+ tests_path = paths.first
67
+ output = execute_command phpunit_command(tests_path, options)
68
+ else
69
+ create_tests_folder_for(paths) do |tests_folder|
70
+ output = execute_command phpunit_command(tests_folder, options)
71
+ end
72
+ end
73
+
74
+ # print the output to the terminal
75
+ UI.info output
76
+
77
+ # return false in case the system call fails with no status!
78
+ return false if $?.nil?
79
+
80
+ if $?.success? or tests_contain_failures? or tests_contain_errors?
81
+ notify_results(output, options)
82
+ else
83
+ notify_failure(options)
84
+ end
85
+
86
+ $?.success?
87
+ end
88
+
89
+ # Displays the start testing notification.
90
+ #
91
+ # @param (see #run)
92
+ # @param (see #run)
93
+ #
94
+ def notify_start(paths, options)
95
+ message = options[:message] || "Running: #{paths.join(' ')}"
96
+ UI.info(message, :reset => true)
97
+ end
98
+
99
+ # Displays a notification about the tests results.
100
+ #
101
+ # @param [String] output the tests output
102
+ # @param (see #run)
103
+ #
104
+ def notify_results(output, options)
105
+ return if options[:notification] == false
106
+ results = Formatter.parse_output(output)
107
+ Notifier.notify_results(results)
108
+ end
109
+
110
+ # Displays a notification about failing to run the tests
111
+ #
112
+ # @param (see #run)
113
+ #
114
+ def notify_failure(options)
115
+ return if options[:notification] == false
116
+ Notifier.notify('Failed! Check the console', :title => 'PHPUnit results', :image => :failed)
117
+ end
118
+
119
+ # Checks the exitstatus of the phpunit command
120
+ # for a sign of failures in the tests.
121
+ #
122
+ # @return [Boolean] whether the tests contain failures or not
123
+ #
124
+ def tests_contain_failures?
125
+ $?.exitstatus == PHPUNIT_FAILURES_EXITCODE
126
+ end
127
+
128
+ # Checks the exitstatus of the phpunit command
129
+ # for a sign of errors in the tests.
130
+ #
131
+ # @return [Boolean] whether the tests contain errors or not
132
+ #
133
+ def tests_contain_errors?
134
+ $?.exitstatus == PHPUNIT_ERRORS_EXITCODE
135
+ end
136
+
137
+ # Creates a temporary folder which has links to
138
+ # the tests paths. This method is used because PHPUnit
139
+ # can't run multiple tests files at the same time and generate
140
+ # one result for them.
141
+ #
142
+ # @param (see #run)
143
+ # @yield [String] d the temporary dir for the tests
144
+ #
145
+ def create_tests_folder_for(paths)
146
+ Dir.mktmpdir('guard_phpunit') do |d|
147
+ symlink_paths_to_tests_folder(paths, d)
148
+ yield d
149
+ end
150
+ end
151
+
152
+ # Creates symbolic links inside the folder pointing
153
+ # back to the paths.
154
+ #
155
+ # @see #create_tests_folder_for
156
+ #
157
+ # @param (see #run)
158
+ # @param [String] the folder in which the links must be made
159
+ #
160
+ def symlink_paths_to_tests_folder(paths, folder)
161
+ paths.each do |p|
162
+ FileUtils.mkdir_p( File.join(folder, File.dirname(p) ) ) unless File.dirname(p) == '.'
163
+ FileUtils.ln_s(Pathname.new(p).realpath, File.join(folder, p))
164
+ end
165
+ end
166
+
167
+ # Generates the phpunit command for the tests paths.
168
+ #
169
+ # @param (see #run)
170
+ # @param (see #run)
171
+ # @see #run_tests
172
+ #
173
+ def phpunit_command(path, options)
174
+ formatter_path = File.join( File.dirname(__FILE__), 'formatters', 'PHPUnit-Progress')
175
+
176
+ cmd_parts = []
177
+ cmd_parts << "phpunit"
178
+ cmd_parts << "--include-path #{formatter_path}"
179
+ cmd_parts << "--printer PHPUnit_Extensions_Progress_ResultPrinter"
180
+ cmd_parts << options[:cli] if options[:cli]
181
+ cmd_parts << path
182
+
183
+ cmd_parts.join(' ')
184
+ end
185
+
186
+ # Executes a system command and returns the output.
187
+ #
188
+ # @param [String] command the command to be run
189
+ # @return [String] the output of the executed command
190
+ #
191
+ def execute_command(command)
192
+ %x{#{command}}
193
+ end
194
+ end
195
+ end
196
+ end
197
+ end