1
+ <?php
2
+
3
+ /**
4
+ * This file is part of the Altumo library.
5
+ *
6
+ * (c) Steve Sperandeo <[email protected] >
7
+ *
8
+ * For the full copyright and license information, please view the LICENSE
9
+ * file that was distributed with this source code.
10
+ */
11
+
12
+
13
+
14
+
15
+ namespace Altumo \Test ;
16
+
17
+ /**
18
+ * This class is the entry point the run all unit tests. It is called directly
19
+ * from the CLI (or web application).
20
+ *
21
+ * To run tests, you pass the directory that contains the tests to the
22
+ * constructor. Any file that has a filename that follows the pattern:
23
+ * xxxxTest.php and contains a class that inherits from \Altumo\Test\UnitTest
24
+ * will be run.
25
+ *
26
+ * Within each UnitTest class, if there is a "run()" method defined, the test
27
+ * suite will only invoke that one method. However, if no "run()" method is
28
+ * defined, each method whose name begins with "test" will be invoked in the
29
+ * order that they're defined.
30
+ *
31
+ * If the method "setup" exist, it'll be run before all of the tests.
32
+ * If the method "tearDown" exist, it'll be run after all of the tests.
33
+ *
34
+ * Each unit test is instantiated only once for all of the unit tests, so you
35
+ * can define protected members or methods to setup, tear down, or whatever.
36
+ *
37
+ *
38
+ * @author Steve Sperandeo <[email protected] >
39
+ */
40
+ class TestSuite{
41
+
42
+ protected $ results = array ();
43
+ protected $ counts = array ();
44
+
45
+ /**
46
+ * Runs all of the unit tests in the suppied directory (and all of the
47
+ * subdirectories).
48
+ *
49
+ * @param string $test_directory
50
+ * @return TestSuite
51
+ */
52
+ public function __construct ( $ test_directory ){
53
+
54
+ $ this ->counts ['pass ' ] = 0 ;
55
+ $ this ->counts ['fail ' ] = 0 ;
56
+ $ this ->counts ['error ' ] = 0 ;
57
+ $ this ->counts ['total ' ] = 0 ;
58
+
59
+ $ this ->runTests ( $ test_directory );
60
+
61
+ }
62
+
63
+ /**
64
+ * Runs all of the unit tests in the suppied directory (and all of the
65
+ * subdirectories).
66
+ *
67
+ * @param string $test_directory
68
+ */
69
+ protected function runTests ( $ test_directory ){
70
+
71
+ $ files = \Altumo \Utils \Finder::type ('file ' )->name ('*Test.php ' )->in ( $ test_directory );
72
+
73
+ foreach ( $ files as $ file ){
74
+
75
+ //determine the class name
76
+ $ class_name = '\\Altumo \\Test \\' . basename ( $ file , '.php ' );
77
+
78
+ //skip the base class
79
+ if ( $ class_name == '\\Altumo \\Test \\UnitTest ' ){
80
+ continue ;
81
+ }
82
+
83
+ //include the file
84
+ require_once ( $ file );
85
+
86
+ //display which unit test is being run
87
+ $ this ->output ( "\n" . $ file . ': ' );
88
+
89
+ //instantiate and run the unit test
90
+ $ unit_test = new $ class_name ( $ this );
91
+
92
+ if ( method_exists ( $ unit_test , 'setup ' ) ){
93
+ $ unit_test ->setup ();
94
+ }
95
+
96
+ if ( method_exists ( $ unit_test , 'run ' ) ){
97
+
98
+ $ unit_test ->run ();
99
+
100
+ }else {
101
+
102
+ $ methods = get_class_methods ( $ class_name );
103
+
104
+ foreach ( $ methods as $ method ){
105
+
106
+ if ( preg_match ('/^test(.*?)$/ ' , $ method , $ regs ) ){
107
+ $ test_name = $ regs [1 ];
108
+ $ this ->output ( "\n\t" . $ test_name . ': ' );
109
+ $ unit_test ->$ method ();
110
+ }
111
+
112
+ }
113
+
114
+ }
115
+
116
+ if ( method_exists ( $ unit_test , 'tearDown ' ) ){
117
+ $ unit_test ->setup ();
118
+ }
119
+
120
+ $ this ->output ( "\n" );
121
+
122
+ }
123
+
124
+ $ this ->displayReport ();
125
+
126
+ }
127
+
128
+
129
+ /**
130
+ * Writes the output to a location. Defaults to stdout.
131
+ *
132
+ * @param string $line
133
+ */
134
+ public function output ( $ line ){
135
+
136
+ echo $ line ;
137
+
138
+ }
139
+
140
+
141
+
142
+ /**
143
+ * Getter for the results field on this TestSuite.
144
+ *
145
+ * @return array
146
+ */
147
+ protected function getResults (){
148
+
149
+ return $ this ->results ;
150
+
151
+ }
152
+
153
+ /**
154
+ * Adds a test result to this TestSuite.
155
+ *
156
+ * @param \Altumo\Test\UnitTestResult $result
157
+ */
158
+ public function addResult ( $ result ){
159
+
160
+ $ this ->output ( '. ' );
161
+ $ this ->counts ['total ' ]++;
162
+
163
+
164
+ switch ( $ result ->getResult () ){
165
+
166
+ case \Altumo \Test \UnitTestResult::RESULT_SUCCESS :
167
+ $ this ->counts ['pass ' ]++;
168
+ break ;
169
+
170
+ case \Altumo \Test \UnitTestResult::RESULT_FAILURE :
171
+ $ this ->counts ['fail ' ]++;
172
+ $ this ->output ( "\n\t\tError: " . $ result ->getSummary () );
173
+ break ;
174
+
175
+ case \Altumo \Test \UnitTestResult::RESULT_ERROR :
176
+ $ this ->counts ['error ' ]++;
177
+ $ this ->output ( "\n\t\tError: " . $ result ->getSummary () );
178
+ break ;
179
+ };
180
+
181
+ $ this ->results [] = $ result ;
182
+
183
+ }
184
+
185
+ /**
186
+ * Adds a test result to this TestSuite.
187
+ *
188
+ */
189
+ public function displayReport (){
190
+
191
+ $ this ->output ( "\n" );
192
+ $ this ->output ( "\nPass: " . $ this ->counts ['pass ' ] . '/ ' . $ this ->counts ['total ' ] );
193
+ $ this ->output ( "\nFail: " . $ this ->counts ['fail ' ] );
194
+ $ this ->output ( "\nError: " . $ this ->counts ['error ' ] );
195
+
196
+ $ this ->output ( "\n\n" );
197
+
198
+ }
199
+
200
+
201
+ }
0 commit comments