Open Journal Systems  3.3.0
LogPluginTest.php
1 <?php
2 
4 
12 
18 {
19  protected $adapter;
20 
21  public function setUp()
22  {
23  $this->adapter = new ClosureLogAdapter(function ($message) {
24  echo $message;
25  });
26  }
27 
29  {
30  $p = new LogPlugin($this->adapter);
31  $this->assertNotEmpty($p->getSubscribedEvents());
32  $event = new Event(array('request' => new Request('GET', 'http://foo.com')));
33  $p->onCurlRead($event);
34  $p->onCurlWrite($event);
35  $p->onRequestBeforeSend($event);
36  }
37 
38  public function testLogsWhenComplete()
39  {
40  $output = '';
41  $p = new LogPlugin(new ClosureLogAdapter(function ($message) use (&$output) {
42  $output = $message;
43  }), '{method} {resource} | {code} {res_body}');
44 
45  $p->onRequestSent(new Event(array(
46  'request' => new Request('GET', 'http://foo.com'),
47  'response' => new Response(200, array(), 'Foo')
48  )));
49 
50  $this->assertEquals('GET / | 200 Foo', $output);
51  }
52 
53  public function testWiresBodiesWhenNeeded()
54  {
55  $client = new Client($this->getServer()->getUrl());
56  $plugin = new LogPlugin($this->adapter, '{req_body} | {res_body}', true);
57  $client->getEventDispatcher()->addSubscriber($plugin);
58  $request = $client->put();
59 
60  // Send the response from the dummy server as the request body
61  $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\nsend");
62  $stream = fopen($this->getServer()->getUrl(), 'r');
63  $request->setBody(EntityBody::factory($stream, 4));
64 
65  $tmpFile = tempnam(sys_get_temp_dir(), 'non_repeatable');
66  $request->setResponseBody(EntityBody::factory(fopen($tmpFile, 'w')));
67 
68  $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 8\r\n\r\nresponse");
69 
70  ob_start();
71  $request->send();
72  $message = ob_get_clean();
73 
74  unlink($tmpFile);
75  $this->assertContains("send", $message);
76  $this->assertContains("response", $message);
77  }
78 
80  {
81  $s = fopen('php://temp', 'r+');
82  $client = new Client();
83  $client->addSubscriber(LogPlugin::getDebugPlugin(true, $s));
84  $request = $client->put('http://foo.com', array('Content-Type' => 'Foo'), 'Bar');
85  $request->setresponse(new Response(200), true);
86  $request->send();
87  rewind($s);
88  $contents = stream_get_contents($s);
89  $this->assertContains('# Request:', $contents);
90  $this->assertContainsIns('PUT / HTTP/1.1', $contents);
91  $this->assertContains('# Response:', $contents);
92  $this->assertContainsIns('HTTP/1.1 200 OK', $contents);
93  $this->assertContains('# Errors:', $contents);
94  }
95 }
Guzzle\Plugin\Log\LogPlugin
Definition: LogPlugin.php:20
Guzzle\Tests\Plugin\Log\LogPluginTest\$adapter
$adapter
Definition: LogPluginTest.php:19
Guzzle\Tests\GuzzleTestCase
Definition: GuzzleTestCase.php:22
Guzzle\Tests\Plugin\Log\LogPluginTest\testLogsWhenComplete
testLogsWhenComplete()
Definition: LogPluginTest.php:38
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\Plugin\Log\LogPluginTest\setUp
setUp()
Definition: LogPluginTest.php:21
Guzzle\Plugin\Log\LogPlugin\getDebugPlugin
static getDebugPlugin($wireBodies=true, $stream=null)
Definition: LogPlugin.php:64
Guzzle\Http\EntityBody
Definition: EntityBody.php:13
Guzzle\Tests\Plugin\Log\LogPluginTest\testHasHelpfulStaticFactoryMethod
testHasHelpfulStaticFactoryMethod()
Definition: LogPluginTest.php:79
Guzzle\Tests\Plugin\Log
Definition: LogPluginTest.php:3
Guzzle\Common\Event
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Common/Event.php:10
Guzzle\Http\Message\Request
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/Request.php:25
Guzzle\Tests\Plugin\Log\LogPluginTest\testIgnoresCurlEventsWhenNotWiringBodies
testIgnoresCurlEventsWhenNotWiringBodies()
Definition: LogPluginTest.php:28
Guzzle\Log\ClosureLogAdapter
Definition: ClosureLogAdapter.php:8
Guzzle\Tests\GuzzleTestCase\assertContainsIns
assertContainsIns($needle, $haystack, $message=null)
Definition: GuzzleTestCase.php:231
Guzzle\Http\Client
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Client.php:24
Guzzle\Tests\Plugin\Log\LogPluginTest\testWiresBodiesWhenNeeded
testWiresBodiesWhenNeeded()
Definition: LogPluginTest.php:53
Guzzle\Tests\GuzzleTestCase\getServer
static getServer()
Definition: GuzzleTestCase.php:36
Guzzle\Tests\Plugin\Log\LogPluginTest
Definition: LogPluginTest.php:17