scratch – Blame information for rev 87
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
87 | office | 1 | <?php |
2 | |||
3 | namespace GuzzleHttp\Tests\Subscriber; |
||
4 | |||
5 | use GuzzleHttp\Adapter\Transaction; |
||
6 | use GuzzleHttp\Client; |
||
7 | use GuzzleHttp\Event\BeforeEvent; |
||
8 | use GuzzleHttp\Exception\RequestException; |
||
9 | use GuzzleHttp\Message\MessageFactory; |
||
10 | use GuzzleHttp\Message\Request; |
||
11 | use GuzzleHttp\Message\Response; |
||
12 | use GuzzleHttp\Stream\Stream; |
||
13 | use GuzzleHttp\Subscriber\Mock; |
||
14 | |||
15 | /** |
||
16 | * @covers GuzzleHttp\Subscriber\Mock |
||
17 | */ |
||
18 | class MockTest extends \PHPUnit_Framework_TestCase |
||
19 | { |
||
20 | public function testDescribesSubscribedEvents() |
||
21 | { |
||
22 | $mock = new Mock(); |
||
23 | $this->assertInternalType('array', $mock->getEvents()); |
||
24 | } |
||
25 | |||
26 | public function testIsCountable() |
||
27 | { |
||
28 | $plugin = new Mock(); |
||
29 | $plugin->addResponse((new MessageFactory())->fromMessage("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n")); |
||
30 | $this->assertEquals(1, count($plugin)); |
||
31 | } |
||
32 | |||
33 | public function testCanClearQueue() |
||
34 | { |
||
35 | $plugin = new Mock(); |
||
36 | $plugin->addResponse((new MessageFactory())->fromMessage("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n")); |
||
37 | $plugin->clearQueue(); |
||
38 | $this->assertEquals(0, count($plugin)); |
||
39 | } |
||
40 | |||
41 | public function testRetrievesResponsesFromFiles() |
||
42 | { |
||
43 | $tmp = tempnam('/tmp', 'tfile'); |
||
44 | file_put_contents($tmp, "HTTP/1.1 201 OK\r\nContent-Length: 0\r\n\r\n"); |
||
45 | $plugin = new Mock(); |
||
46 | $plugin->addResponse($tmp); |
||
47 | unlink($tmp); |
||
48 | $this->assertEquals(1, count($plugin)); |
||
49 | $q = $this->readAttribute($plugin, 'queue'); |
||
50 | $this->assertEquals(201, $q[0]->getStatusCode()); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @expectedException \InvalidArgumentException |
||
55 | */ |
||
56 | public function testThrowsExceptionWhenInvalidResponse() |
||
57 | { |
||
58 | (new Mock())->addResponse(false); |
||
59 | } |
||
60 | |||
61 | public function testAddsMockResponseToRequestFromClient() |
||
62 | { |
||
63 | $response = new Response(200); |
||
64 | $t = new Transaction(new Client(), new Request('GET', '/')); |
||
65 | $m = new Mock([$response]); |
||
66 | $ev = new BeforeEvent($t); |
||
67 | $m->onBefore($ev); |
||
68 | $this->assertSame($response, $t->getResponse()); |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * @expectedException \OutOfBoundsException |
||
73 | */ |
||
74 | public function testUpdateThrowsExceptionWhenEmpty() |
||
75 | { |
||
76 | $p = new Mock(); |
||
77 | $ev = new BeforeEvent(new Transaction(new Client(), new Request('GET', '/'))); |
||
78 | $p->onBefore($ev); |
||
79 | } |
||
80 | |||
81 | public function testReadsBodiesFromMockedRequests() |
||
82 | { |
||
83 | $m = new Mock([new Response(200)]); |
||
84 | $client = new Client(['base_url' => 'http://test.com']); |
||
85 | $client->getEmitter()->attach($m); |
||
86 | $body = Stream::factory('foo'); |
||
87 | $client->put('/', ['body' => $body]); |
||
88 | $this->assertEquals(3, $body->tell()); |
||
89 | } |
||
90 | |||
91 | public function testCanMockBadRequestExceptions() |
||
92 | { |
||
93 | $client = new Client(['base_url' => 'http://test.com']); |
||
94 | $request = $client->createRequest('GET', '/'); |
||
95 | $ex = new RequestException('foo', $request); |
||
96 | $mock = new Mock([$ex]); |
||
97 | $this->assertCount(1, $mock); |
||
98 | $request->getEmitter()->attach($mock); |
||
99 | |||
100 | try { |
||
101 | $client->send($request); |
||
102 | $this->fail('Did not dequeue an exception'); |
||
103 | } catch (RequestException $e) { |
||
104 | $this->assertSame($e, $ex); |
||
105 | $this->assertSame($request, $ex->getRequest()); |
||
106 | } |
||
107 | } |
||
108 | } |