Open Journal Systems  3.3.0
MessageFormatterTest.php
1 <?php
2 
3 namespace Guzzle\Tests\Log;
4 
13 
18 {
19  protected $request;
20  protected $response;
21  protected $handle;
22 
23  public function setUp()
24  {
25  $this->request = new EntityEnclosingRequest('POST', 'http://foo.com?q=test', array(
26  'X-Foo' => 'bar',
27  'Authorization' => 'Baz'
28  ));
29  $this->request->setBody(EntityBody::factory('Hello'));
30 
31  $this->response = new Response(200, array(
32  'X-Test' => 'Abc'
33  ), 'Foo');
34 
35  $this->handle = $this->getMockBuilder('Guzzle\Http\Curl\CurlHandle')
36  ->disableOriginalConstructor()
37  ->setMethods(array('getError', 'getErrorNo', 'getStderr', 'getInfo'))
38  ->getMock();
39 
40  $this->handle->expects($this->any())
41  ->method('getError')
42  ->will($this->returnValue('e'));
43 
44  $this->handle->expects($this->any())
45  ->method('getErrorNo')
46  ->will($this->returnValue('123'));
47 
48  $this->handle->expects($this->any())
49  ->method('getStderr')
50  ->will($this->returnValue('testing'));
51 
52  $this->handle->expects($this->any())
53  ->method('getInfo')
54  ->will($this->returnValueMap(array(
55  array(CURLINFO_CONNECT_TIME, '123'),
56  array(CURLINFO_TOTAL_TIME, '456')
57  )));
58  }
59 
60  public function logProvider()
61  {
62  return array(
63  // Uses the cache for the second time
64  array('{method} - {method}', 'POST - POST'),
65  array('{url}', 'http://foo.com?q=test'),
66  array('{port}', '80'),
67  array('{resource}', '/?q=test'),
68  array('{host}', 'foo.com'),
69  array('{hostname}', gethostname()),
70  array('{protocol}/{version}', 'HTTP/1.1'),
71  array('{code} {phrase}', '200 OK'),
72  array('{req_header_Foo}', ''),
73  array('{req_header_X-Foo}', 'bar'),
74  array('{req_header_Authorization}', 'Baz'),
75  array('{res_header_foo}', ''),
76  array('{res_header_X-Test}', 'Abc'),
77  array('{req_body}', 'Hello'),
78  array('{res_body}', 'Foo'),
79  array('{curl_stderr}', 'testing'),
80  array('{curl_error}', 'e'),
81  array('{curl_code}', '123'),
82  array('{connect_time}', '123'),
83  array('{total_time}', '456')
84  );
85  }
86 
90  public function testFormatsMessages($template, $output)
91  {
92  $formatter = new MessageFormatter($template);
93  $this->assertEquals($output, $formatter->format($this->request, $this->response, $this->handle));
94  }
95 
97  {
98  $formatter = new MessageFormatter();
99  $formatter->setTemplate('{request}{response}');
100  $this->assertEquals($this->request . $this->response, $formatter->format($this->request, $this->response));
101  }
102 
103  public function testAddsTimestamp()
104  {
105  $formatter = new MessageFormatter('{ts}');
106  $this->assertNotEmpty($formatter->format($this->request, $this->response));
107  }
108 
110  {
111  $formatter = new MessageFormatter('{connect_time}/{total_time}');
112  $response = $this->getMockBuilder('Guzzle\Http\Message\Response')
113  ->setConstructorArgs(array(200))
114  ->setMethods(array('getInfo'))
115  ->getMock();
116  $response->expects($this->exactly(2))
117  ->method('getInfo')
118  ->will($this->returnValueMap(array(
119  array('connect_time', '1'),
120  array('total_time', '2'),
121  )));
122  $this->assertEquals('1/2', $formatter->format($this->request, $response));
123  }
124 
126  {
127  $formatter = new MessageFormatter('{connect_time}/{total_time}');
128  $this->assertEquals('/', $formatter->format($this->request));
129  }
130 
131  public function testInjectsTotalTime()
132  {
133  $out = '';
134  $formatter = new MessageFormatter('{connect_time}/{total_time}');
135  $adapter = new ClosureLogAdapter(function ($m) use (&$out) { $out .= $m; });
136  $log = new LogPlugin($adapter, $formatter);
137  $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nHI");
138  $client = new Client($this->getServer()->getUrl());
139  $client->addSubscriber($log);
140  $client->get('/')->send();
141  $this->assertNotEquals('/', $out);
142  }
143 }
Guzzle\Tests\Log\MessageFormatterTest\$handle
$handle
Definition: MessageFormatterTest.php:21
Guzzle\Plugin\Log\LogPlugin
Definition: LogPlugin.php:20
Guzzle\Tests\GuzzleTestCase
Definition: GuzzleTestCase.php:22
Guzzle\Tests\Log\MessageFormatterTest\testFormatsRequestsAndResponses
testFormatsRequestsAndResponses()
Definition: MessageFormatterTest.php:96
Guzzle\Http\EntityBody\factory
static factory($resource='', $size=null)
Definition: EntityBody.php:36
Guzzle\Http\Message\Response
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/Response.php:17
Guzzle\Tests\Log
Definition: ArrayLogAdapterTest.php:3
Guzzle\Log\MessageFormatter
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Log/MessageFormatter.php:39
Guzzle\Http\EntityBody
Definition: EntityBody.php:13
Guzzle\Tests\Log\MessageFormatterTest\$request
$request
Definition: MessageFormatterTest.php:19
Guzzle\Tests\Log\MessageFormatterTest\logProvider
logProvider()
Definition: MessageFormatterTest.php:60
Guzzle\Tests\Log\MessageFormatterTest\testFormatsMessages
testFormatsMessages($template, $output)
Definition: MessageFormatterTest.php:90
Guzzle\Http\Message\EntityEnclosingRequest
Definition: EntityEnclosingRequest.php:14
Guzzle\Log\ClosureLogAdapter
Definition: ClosureLogAdapter.php:8
Guzzle\Tests\Log\MessageFormatterTest\testInjectsTotalTime
testInjectsTotalTime()
Definition: MessageFormatterTest.php:131
Guzzle\Tests\Log\MessageFormatterTest\testUsesResponseWhenNoHandleAndGettingCurlInformation
testUsesResponseWhenNoHandleAndGettingCurlInformation()
Definition: MessageFormatterTest.php:109
Guzzle\Http\Client
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Client.php:24
Guzzle\Tests\Log\MessageFormatterTest\testAddsTimestamp
testAddsTimestamp()
Definition: MessageFormatterTest.php:103
Guzzle\Http\Curl\CurlHandle
Definition: CurlHandle.php:16
Guzzle\Tests\Log\MessageFormatterTest
Definition: MessageFormatterTest.php:17
Guzzle\Tests\Log\MessageFormatterTest\testUsesEmptyStringWhenNoHandleAndNoResponse
testUsesEmptyStringWhenNoHandleAndNoResponse()
Definition: MessageFormatterTest.php:125
Guzzle\Tests\Log\MessageFormatterTest\setUp
setUp()
Definition: MessageFormatterTest.php:23
Guzzle\Tests\GuzzleTestCase\getServer
static getServer()
Definition: GuzzleTestCase.php:36
Guzzle\Tests\Log\MessageFormatterTest\$response
$response
Definition: MessageFormatterTest.php:20