Open Journal Systems  3.3.0
MockPluginTest.php
1 <?php
2 
4 
11 
16 {
18  {
19  $this->assertInternalType('array', MockPlugin::getSubscribedEvents());
20  }
21 
22  public function testDescribesEvents()
23  {
24  $this->assertInternalType('array', MockPlugin::getAllEvents());
25  }
26 
27  public function testCanBeTemporary()
28  {
29  $plugin = new MockPlugin();
30  $this->assertFalse($plugin->isTemporary());
31  $plugin = new MockPlugin(null, true);
32  $this->assertTrue($plugin->isTemporary());
33  }
34 
35  public function testIsCountable()
36  {
37  $plugin = new MockPlugin();
38  $plugin->addResponse(Response::fromMessage("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"));
39  $this->assertEquals(1, count($plugin));
40  }
41 
45  public function testCanClearQueue()
46  {
47  $plugin = new MockPlugin();
48  $plugin->addResponse(Response::fromMessage("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"));
49  $plugin->clearQueue();
50  $this->assertEquals(0, count($plugin));
51  }
52 
53  public function testCanInspectQueue()
54  {
55  $plugin = new MockPlugin();
56  $this->assertInternalType('array', $plugin->getQueue());
57  $plugin->addResponse(Response::fromMessage("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"));
58  $queue = $plugin->getQueue();
59  $this->assertInternalType('array', $queue);
60  $this->assertEquals(1, count($queue));
61  }
62 
64  {
65  $response = MockPlugin::getMockFile(__DIR__ . '/../../TestData/mock_response');
66  $this->assertInstanceOf('Guzzle\\Http\\Message\\Response', $response);
67  $this->assertEquals(200, $response->getStatusCode());
68  }
69 
74  {
75  MockPlugin::getMockFile('missing/filename');
76  }
77 
82  {
83  $p = new MockPlugin();
84  $p->addResponse($this);
85  }
86 
88  {
89  $p = new MockPlugin();
90  $response = Response::fromMessage("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
91  $p->addResponse($response);
92  $this->assertEquals(array($response), $p->getQueue());
93  }
94 
95  public function testAddsResponseFilesToQueue()
96  {
97  $p = new MockPlugin();
98  $p->addResponse(__DIR__ . '/../../TestData/mock_response');
99  $this->assertEquals(1, count($p));
100  }
101 
106  {
107  $p = new MockPlugin();
108  $response = MockPlugin::getMockFile(__DIR__ . '/../../TestData/mock_response');
109  $p->addResponse($response);
110 
111  $client = new Client('http://127.0.0.1:123/');
112  $client->getEventDispatcher()->addSubscriber($p, 9999);
113  $request = $client->get();
114  $request->send();
115 
116  $this->assertSame($response, $request->getResponse());
117  $this->assertEquals(0, count($p));
118  }
119 
125  {
126  $p = new MockPlugin();
127  $p->onRequestBeforeSend(new Event());
128  }
129 
134  {
135  $p = new MockPlugin(null, true);
136  $p->addResponse(MockPlugin::getMockFile(__DIR__ . '/../../TestData/mock_response'));
137  $client = new Client('http://127.0.0.1:123/');
138  $client->getEventDispatcher()->addSubscriber($p, 9999);
139  $request = $client->get();
140  $request->send();
141 
142  $this->assertFalse($this->hasSubscriber($client, $p));
143  }
144 
146  {
147  $p = new MockPlugin(array(new Response(200)));
148  $this->assertEquals(1, $p->count());
149  }
150 
151  public function testStoresMockedRequests()
152  {
153  $p = new MockPlugin(array(new Response(200), new Response(200)));
154  $client = new Client('http://127.0.0.1:123/');
155  $client->getEventDispatcher()->addSubscriber($p, 9999);
156 
157  $request1 = $client->get();
158  $request1->send();
159  $this->assertEquals(array($request1), $p->getReceivedRequests());
160 
161  $request2 = $client->get();
162  $request2->send();
163  $this->assertEquals(array($request1, $request2), $p->getReceivedRequests());
164 
165  $p->flush();
166  $this->assertEquals(array(), $p->getReceivedRequests());
167  }
168 
170  {
171  $p = new MockPlugin(array(new Response(200)));
172  $p->readBodies(true);
173  $client = new Client('http://127.0.0.1:123/');
174  $client->getEventDispatcher()->addSubscriber($p, 9999);
175 
176  $body = EntityBody::factory('foo');
177  $request = $client->put();
178  $request->setBody($body);
179  $request->send();
180  $this->assertEquals(3, $body->ftell());
181  }
182 
184  {
185  $client = new Client('http://127.0.0.1:123/');
186  $ex = new CurlException('Foo');
187  $mock = new MockPlugin(array($ex));
188  $client->addSubscriber($mock);
189  $request = $client->get('foo');
190 
191  try {
192  $request->send();
193  $this->fail('Did not dequeue an exception');
194  } catch (CurlException $e) {
195  $this->assertSame($e, $ex);
196  $this->assertSame($request, $ex->getRequest());
197  }
198  }
199 }
Guzzle\Tests\Plugin\Mock
Definition: MockPluginTest.php:3
Guzzle\Tests\Plugin\Mock\MockPluginTest\testLoadsResponsesFromConstructor
testLoadsResponsesFromConstructor()
Definition: MockPluginTest.php:145
Guzzle\Http\Exception\CurlException
Definition: CurlException.php:10
Guzzle\Tests\Plugin\Mock\MockPluginTest\testDetachesTemporaryWhenEmpty
testDetachesTemporaryWhenEmpty()
Definition: MockPluginTest.php:133
Guzzle\Plugin\Mock\MockPlugin\getSubscribedEvents
static getSubscribedEvents()
Definition: MockPlugin.php:63
Guzzle\Tests\Plugin\Mock\MockPluginTest\testInvalidResponsesThrowAnException
testInvalidResponsesThrowAnException()
Definition: MockPluginTest.php:81
Guzzle\Tests\GuzzleTestCase
Definition: GuzzleTestCase.php:22
Guzzle\Tests\Plugin\Mock\MockPluginTest
Definition: MockPluginTest.php:15
Guzzle\Tests\Plugin\Mock\MockPluginTest\testDescribesEvents
testDescribesEvents()
Definition: MockPluginTest.php:22
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\Mock\MockPluginTest\testThrowsExceptionWhenResponseFileIsNotFound
testThrowsExceptionWhenResponseFileIsNotFound()
Definition: MockPluginTest.php:73
Guzzle\Http\Message\Response\fromMessage
static fromMessage($message)
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/Response.php:123
Guzzle\Http\EntityBody
Definition: EntityBody.php:13
Guzzle\Plugin\Mock\MockPlugin\getMockFile
static getMockFile($path)
Definition: MockPlugin.php:82
Guzzle\Tests\Plugin\Mock\MockPluginTest\testCanInspectQueue
testCanInspectQueue()
Definition: MockPluginTest.php:53
Guzzle\Plugin\Mock\MockPlugin\getAllEvents
static getAllEvents()
Definition: MockPlugin.php:69
Guzzle\Tests\Plugin\Mock\MockPluginTest\testAddsResponseFilesToQueue
testAddsResponseFilesToQueue()
Definition: MockPluginTest.php:95
Guzzle\Tests\Plugin\Mock\MockPluginTest\testCanClearQueue
testCanClearQueue()
Definition: MockPluginTest.php:45
Guzzle\Tests\GuzzleTestCase\hasSubscriber
hasSubscriber(HasDispatcherInterface $dispatcher, EventSubscriberInterface $subscriber)
Definition: GuzzleTestCase.php:82
Guzzle\Tests\Plugin\Mock\MockPluginTest\testAddsMockResponseToRequestFromClient
testAddsMockResponseToRequestFromClient()
Definition: MockPluginTest.php:105
Guzzle\Tests\Plugin\Mock\MockPluginTest\testStoresMockedRequests
testStoresMockedRequests()
Definition: MockPluginTest.php:151
Guzzle\Common\Event
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Common/Event.php:10
Guzzle\Tests\Plugin\Mock\MockPluginTest\testDescribesSubscribedEvents
testDescribesSubscribedEvents()
Definition: MockPluginTest.php:17
Guzzle\Tests\Plugin\Mock\MockPluginTest\testUpdateThrowsExceptionWhenEmpty
testUpdateThrowsExceptionWhenEmpty()
Definition: MockPluginTest.php:124
Guzzle\Tests\Plugin\Mock\MockPluginTest\testCanBeTemporary
testCanBeTemporary()
Definition: MockPluginTest.php:27
Guzzle\Tests\Plugin\Mock\MockPluginTest\testReadsBodiesFromMockedRequests
testReadsBodiesFromMockedRequests()
Definition: MockPluginTest.php:169
Guzzle\Tests\Plugin\Mock\MockPluginTest\testIsCountable
testIsCountable()
Definition: MockPluginTest.php:35
Guzzle\Tests\Plugin\Mock\MockPluginTest\testCanMockBadRequestExceptions
testCanMockBadRequestExceptions()
Definition: MockPluginTest.php:183
Guzzle\Http\Client
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Client.php:24
Guzzle\Plugin\Mock\MockPlugin
Definition: MockPlugin.php:17
Guzzle\Tests\Plugin\Mock\MockPluginTest\testAddsResponseObjectsToQueue
testAddsResponseObjectsToQueue()
Definition: MockPluginTest.php:87
Guzzle\Tests\Plugin\Mock\MockPluginTest\testRetrievesResponsesFromFiles
testRetrievesResponsesFromFiles()
Definition: MockPluginTest.php:63