1
0
mirror of https://github.com/guzzle/guzzle.git synced 2025-02-24 18:13:00 +01:00

Cleaning up debug output for the stream adapter

This commit is contained in:
Michael Dowling 2014-03-07 13:56:07 -08:00
parent 84b0176d42
commit 41a2224022
2 changed files with 30 additions and 40 deletions

View File

@ -250,44 +250,34 @@ class StreamAdapter implements AdapterInterface
private function visit_debug(RequestInterface $request, &$options, $value, &$params)
{
static $map = [
STREAM_NOTIFY_CONNECT => 'CONNECT',
STREAM_NOTIFY_AUTH_REQUIRED => 'AUTH_REQUIRED',
STREAM_NOTIFY_AUTH_RESULT => 'AUTH_RESULT',
STREAM_NOTIFY_MIME_TYPE_IS => 'MIME_TYPE_IS',
STREAM_NOTIFY_FILE_SIZE_IS => 'FILE_SIZE_IS',
STREAM_NOTIFY_REDIRECTED => 'REDIRECTED',
STREAM_NOTIFY_PROGRESS => 'PROGRESS',
STREAM_NOTIFY_FAILURE => 'FAILURE',
STREAM_NOTIFY_COMPLETED => 'COMPLETED',
STREAM_NOTIFY_RESOLVE => 'RESOLVE'
];
static $args = ['severity', 'message', 'message_code',
'bytes_transferred', 'bytes_max'];
if (!is_resource($value)) {
$value = fopen('php://output', 'w');
}
$params['notification'] = function (
$code,
$severity,
$message,
$message_code,
$bytes_transferred,
$bytes_max
) use ($request, $value) {
fwrite($value, '<' . $request->getUrl() . '>: ');
switch ($code) {
case STREAM_NOTIFY_COMPLETED:
fwrite($value, 'Completed request to ' . $request->getUrl() . "\n");
break;
case STREAM_NOTIFY_FAILURE:
fwrite($value, "Failure: {$message_code} {$message} \n");
break;
case STREAM_NOTIFY_RESOLVE:
case STREAM_NOTIFY_AUTH_REQUIRED:
case STREAM_NOTIFY_AUTH_RESULT:
var_dump($code, $severity, $message, $message_code, $bytes_transferred, $bytes_max);
break;
case STREAM_NOTIFY_CONNECT:
fputs($value, "Connected...\n");
break;
case STREAM_NOTIFY_FILE_SIZE_IS:
fputs($value, "Got the filesize: {$bytes_max}\n");
break;
case STREAM_NOTIFY_MIME_TYPE_IS:
fputs($value, "Found the mime-type: {$message}\n");
break;
case STREAM_NOTIFY_PROGRESS:
fputs($value, "Downloaded {$bytes_transferred} bytes\n");
break;
$params['notification'] = function () use ($request, $value, $map, $args) {
$passed = func_get_args();
$code = array_shift($passed);
fprintf($value, '<%s> [%s] ', $request->getUrl(), $map[$code]);
foreach (array_filter($passed) as $i => $v) {
fwrite($value, $args[$i] . ': "' . $v . '" ');
}
fwrite($value, "\n");
};
}
}

View File

@ -307,9 +307,9 @@ class StreamAdapterTest extends \PHPUnit_Framework_TestCase
ob_start();
$client->get('/', ['debug' => true]);
$contents = ob_get_clean();
$this->assertContains('<http://127.0.0.1:8124/>: Connected', $contents);
$this->assertContains('<http://127.0.0.1:8124/>: Got the filesize: 8', $contents);
$this->assertContains('<http://127.0.0.1:8124/>: Downloaded 0 bytes', $contents);
$this->assertContains('<http://127.0.0.1:8124/> [CONNECT]', $contents);
$this->assertContains('<http://127.0.0.1:8124/> [FILE_SIZE_IS]', $contents);
$this->assertContains('<http://127.0.0.1:8124/> [PROGRESS]', $contents);
}
public function testDebugAttributeWritesStreamInfoToBuffer()
@ -324,9 +324,9 @@ class StreamAdapterTest extends \PHPUnit_Framework_TestCase
$client->get('/', ['debug' => $buffer]);
fseek($buffer, 0);
$contents = stream_get_contents($buffer);
$this->assertContains('<http://127.0.0.1:8124/>: Connected', $contents);
$this->assertContains('<http://127.0.0.1:8124/>: Got the filesize: 8', $contents);
$this->assertContains('<http://127.0.0.1:8124/>: Downloaded 0 bytes', $contents);
$this->assertContains('<http://127.0.0.1:8124/>: Found the mime-type: text/plain', $contents);
$this->assertContains('<http://127.0.0.1:8124/> [CONNECT]', $contents);
$this->assertContains('<http://127.0.0.1:8124/> [FILE_SIZE_IS] message: "Content-Length: 8"', $contents);
$this->assertContains('<http://127.0.0.1:8124/> [PROGRESS] bytes_max: "8"', $contents);
$this->assertContains('<http://127.0.0.1:8124/> [MIME_TYPE_IS] message: "text/plain"', $contents);
}
}