/** * @param array $responseCollection * @return \CanalTP\AbstractGuzzle\Guzzle */ public function getMock(array $responseCollection) { $plugin = new MockPlugin(); foreach ($responseCollection as $response) { if ($response instanceof Psr7Response) { $response = new Response($response->getStatusCode(), $response->getHeaders(), $response->getBody()); } $plugin->addResponse($response); } $client = GuzzleFactory::createClient(''); $client->addSubscriber($plugin); return $client; }
/** * @param Response[] $responseCollection * @return \CanalTP\AbstractGuzzle\Guzzle */ public function getMock(array $responseCollection) { $version5ResponseCollection = []; $client = GuzzleFactory::createClient(''); foreach ($responseCollection as $response) { if ($response instanceof Psr7Response) { $version5ResponseCollection[] = new Response($response->getStatusCode(), $response->getHeaders(), Stream::factory($response->getBody()->getContents())); } else { $version5ResponseCollection[] = new Response(); } } $mock = new Mock($version5ResponseCollection); $client->getEmitter()->attach($mock); return $client; }
public function testGetMock() { $clientMock = GuzzleFactory::createClientMock([new Response(200, ['content-type' => 'application/json', 'content-length' => 26, 'canaltp' => 42]), new Response(200, [], '{"lines":"expected-lines"}'), new Response(404, ['Content-Length' => 0])]); $this->assertInstanceOf('CanalTP\\AbstractGuzzle\\Guzzle', $clientMock); $firstCall = $clientMock->send(new Request('get', 'github')); $firstCallHeaders = $firstCall->getHeaders(); $this->assertInstanceOf('GuzzleHttp\\Psr7\\Response', $firstCall); $this->assertEquals(200, $firstCall->getStatusCode()); $this->assertEquals(42, $firstCallHeaders['canaltp'][0]); $this->assertEquals('application/json', $firstCallHeaders['content-type'][0]); $secondCall = $clientMock->send(new Request('post', 'packagist')); $this->assertEquals('{"lines":"expected-lines"}', $secondCall->getBody()->getContents()); // since guzzle 6, exception will be throw in case of http errors try { $thirdCall = $clientMock->send(new Request('get', 'php/404')); $this->assertEquals(404, $thirdCall->getStatusCode()); } catch (ClientException $e) { $this->assertEquals(404, $e->getResponse()->getStatusCode()); } }
public function testClientMockForVersion6() { $clientMock = GuzzleFactory::createClientMock([]); $this->assertInstanceOf('CanalTP\\AbstractGuzzle\\Version\\Guzzle6', $clientMock); }
/** * Guzzle6Mock . * * @param array $responseCollection new Response(200, ['X-Foo' => 'Bar']), * new Response(202, ['Content-Length' => 0]), * new RequestException("Error Communicating with Server", new Request('GET', 'test')) * @return \CanalTP\AbstractGuzzle\Guzzle */ public function getMock(array $responseCollection) { $mock = new MockHandler($responseCollection); $handler = HandlerStack::create($mock); return GuzzleFactory::createClient('', ['handler' => $handler]); }