Skip to content

Commit 058f565

Browse files
committed
Several Changes:
- Created new simple unit testing framework - Removed phpunit as the unit testing framework - Added Finder from symfony - Converted all existing unit tests to new testing framework - Fixed errors with PUT in OutgoingHttpRequest
1 parent 49143b5 commit 058f565

8 files changed

+1445
-58
lines changed

source/php/Http/OutgoingHttpRequest.php

+36-5
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,25 @@ public function send( $return_response_header = false, $populate_curl_info = fal
127127
curl_setopt( $curl_handle, CURLOPT_CUSTOMREQUEST, "PUT" );
128128
curl_setopt( $curl_handle, CURLOPT_PUT, 1 );
129129
$message_body = $this->getMessageBody();
130+
//$temp_file_name = tempnam( sys_get_temp_dir(), 'http_put_' );
131+
//$temp_file = fopen($temp_file_name, 'w+');
130132
$temp_file = tmpfile();
131133
fwrite($temp_file, $message_body);
132134
fseek($temp_file, 0);
133135
curl_setopt( $curl_handle, CURLOPT_INFILE, $temp_file );
134-
curl_setopt( $curl_handle, CURLOPT_INFILESIZE, strlen($message_body));
136+
curl_setopt( $curl_handle, CURLOPT_INFILESIZE, strlen($message_body) );
137+
//var_dump($message_body);
138+
//curl_setopt( $curl_handle, CURLOPT_READFUNCTION, array( &$this, 'putReadFunctionCallback') );
135139
}
136140

137141
if( $this->isDeleteRequest() ){
138142
curl_setopt( $curl_handle, CURLOPT_CUSTOMREQUEST, "DELETE" );
139143
}
140144

145+
if( $this->isGetRequest() ){
146+
//curl_setopt( $curl_handle, CURLOPT_CUSTOMREQUEST, false );
147+
}
148+
141149

142150
curl_setopt( $curl_handle, CURLOPT_URL, $full_url );
143151

@@ -252,8 +260,11 @@ public function send( $return_response_header = false, $populate_curl_info = fal
252260
}
253261

254262
//clean up the temp file, if this was a PUT
255-
if( isset($temp_file) ){
256-
fclose($temp_file); //this also deletes the file
263+
if( isset($temp_file_name) ){
264+
//fclose($temp_file);
265+
//unlink($temp_file_name);
266+
//unset($temp_file);
267+
//unset($temp_file_name);
257268
}
258269

259270
if( $response === false ){
@@ -267,6 +278,26 @@ public function send( $return_response_header = false, $populate_curl_info = fal
267278

268279
}
269280

281+
/**
282+
* The read function for HTTP PUT calls.
283+
*
284+
* @param resource $curl_handle
285+
* @param string $filename
286+
* @param integer $length
287+
*
288+
* @see http://stackoverflow.com/questions/5619429/help-me-understand-curlopt-readfunction
289+
* @return string
290+
*/
291+
/*
292+
protected function putReadFunctionCallback( $curl_handle, $filename, $length ){
293+
294+
return $this->getMessageBody();
295+
//return 'booo';
296+
297+
}
298+
*/
299+
300+
270301
/**
271302
* Sends the HTTP Request and returns the response as a HttpResponseMessage.
272303
*
@@ -895,8 +926,8 @@ public function __destruct(){
895926

896927
$this->closeCurlHandle();
897928
if( file_exists($this->getCookieFilename()) ){
898-
unlink( $this->getCookieFilename() );
899-
}
929+
//unlink( $this->getCookieFilename() );
930+
}
900931

901932
}
902933

source/php/Test/TestSuite.php

+201
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
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

Comments
 (0)