scratch – Blame information for rev 87

Subversion Repositories:
Rev:
Rev Author Line No. Line
87 office 1 <?php
2  
3 namespace GuzzleHttp\Tests\Adapter;
4  
5 use GuzzleHttp\Adapter\MockAdapter;
6 use GuzzleHttp\Adapter\Transaction;
7 use GuzzleHttp\Adapter\TransactionInterface;
8 use GuzzleHttp\Client;
9 use GuzzleHttp\Event\CompleteEvent;
10 use GuzzleHttp\Event\ErrorEvent;
11 use GuzzleHttp\Exception\RequestException;
12 use GuzzleHttp\Message\Request;
13 use GuzzleHttp\Message\Response;
14 use GuzzleHttp\Stream\Stream;
15  
16 /**
17 * @covers GuzzleHttp\Adapter\MockAdapter
18 */
19 class MockAdapterTest extends \PHPUnit_Framework_TestCase
20 {
21 public function testYieldsMockResponse()
22 {
23 $response = new Response(200);
24 $m = new MockAdapter();
25 $m->setResponse($response);
26 $this->assertSame($response, $m->send(new Transaction(new Client(), new Request('GET', 'http://httbin.org'))));
27 }
28  
29 public function testMocksWithCallable()
30 {
31 $response = new Response(200);
32 $r = function (TransactionInterface $trans) use ($response) {
33 $this->assertEquals(0, $trans->getRequest()->getBody()->tell());
34 return $response;
35 };
36 $m = new MockAdapter($r);
37 $body = Stream::factory('foo');
38 $request = new Request('GET', 'http://httbin.org', [], $body);
39 $trans = new Transaction(new Client(), $request);
40 $this->assertSame($response, $m->send($trans));
41 $this->assertEquals(3, $body->tell());
42 }
43  
44 /**
45 * @expectedException \RuntimeException
46 */
47 public function testValidatesResponses()
48 {
49 $m = new MockAdapter();
50 $m->setResponse('foo');
51 $m->send(new Transaction(new Client(), new Request('GET', 'http://httbin.org')));
52 }
53  
54 public function testHandlesErrors()
55 {
56 $m = new MockAdapter();
57 $m->setResponse(new Response(404));
58 $request = new Request('GET', 'http://httbin.org');
59 $c = false;
60 $request->getEmitter()->once('complete', function (CompleteEvent $e) use (&$c) {
61 $c = true;
62 throw new RequestException('foo', $e->getRequest());
63 });
64 $request->getEmitter()->on('error', function (ErrorEvent $e) {
65 $e->intercept(new Response(201));
66 });
67 $r = $m->send(new Transaction(new Client(), $request));
68 $this->assertTrue($c);
69 $this->assertEquals(201, $r->getStatusCode());
70 }
71  
72 /**
73 * @expectedException \GuzzleHttp\Exception\RequestException
74 */
75 public function testThrowsUnhandledErrors()
76 {
77 $m = new MockAdapter();
78 $m->setResponse(new Response(404));
79 $request = new Request('GET', 'http://httbin.org');
80 $request->getEmitter()->once('complete', function (CompleteEvent $e) {
81 throw new RequestException('foo', $e->getRequest());
82 });
83 $m->send(new Transaction(new Client(), $request));
84 }
85  
86 public function testReadsRequestBody()
87 {
88 $response = new Response(200);
89 $m = new MockAdapter($response);
90 $m->setResponse($response);
91 $body = Stream::factory('foo');
92 $request = new Request('PUT', 'http://httpbin.org/put', [], $body);
93 $this->assertSame($response, $m->send(new Transaction(new Client(), $request)));
94 $this->assertEquals(3, $body->tell());
95 }
96  
97 public function testEmitsHeadersEvent()
98 {
99 $m = new MockAdapter(new Response(404));
100 $request = new Request('GET', 'http://httbin.org');
101 $called = false;
102 $request->getEmitter()->once('headers', function () use (&$called) {
103 $called = true;
104 });
105 $m->send(new Transaction(new Client(), $request));
106 $this->assertTrue($called);
107 }
108 }