Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement an ErrorTransport #1529

Merged
merged 5 commits into from
Oct 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ All notable changes to this project will be documented in this file based on the

### Added

* Added a transport class for mocking a HTTP 403 error codes, useful for testing response failures in inheriting clients
* [Field](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-random) param for `Elastica\Query\FunctionScore::addRandomScoreFunction`

### Improvements
Expand Down
4 changes: 3 additions & 1 deletion lib/Elastica/Transport/Guzzle.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ public function exec(Request $request, array $params)
throw new GuzzleException($ex, $request, new Response($ex->getMessage()));
}

$response = new Response((string) $res->getBody(), $res->getStatusCode());
$responseBody = (string) $res->getBody();
$response = new Response($responseBody, $res->getStatusCode());
$response->setQueryTime($end - $start);
if ($connection->hasConfig('bigintConversion')) {
$response->setJsonBigintConversion($connection->getConfig('bigintConversion'));
Expand All @@ -93,6 +94,7 @@ public function exec(Request $request, array $params)
[
'request_header' => $request->getMethod(),
'http_code' => $res->getStatusCode(),
'body' => $responseBody,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm looking at this line and wondering if it could have any other side effects. Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ruflin unlikely but your heart is in the right place: nothing apart from tests was accessing the array passed to 'setTransportInfo'

]
);

Expand Down
60 changes: 55 additions & 5 deletions lib/Elastica/Transport/NullTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,50 @@
* host but still returns a valid response object
*
* @author James Boehmer <james.boehmer@jamesboehmer.com>
* @author Jan Domanski <jandom@gmail.com>
*/
class NullTransport extends AbstractTransport
{
/**
* Null transport.
* Response you want to get from the transport
*
* @var Response Response
*/
protected $_response = null;

/**
* Set response object the transport returns
*
* @param \Elastica\Response $response
*
* @return $this
*/
public function getResponse()
{
return $this->_response;
}

/**
* Set response object the transport returns
*
* @param \Elastica\Response $response
*
* @return $this
*/
public function setResponse(Response $response)
{
$this->_response = $response;
return $this->_response;
}

/**
* Generate an example response object
*
* @param \Elastica\Request $request
* @param array $params Hostname, port, path, ...
*
* @return \Elastica\Response Response empty object
* @return \Elastica\Response $response
*/
public function exec(Request $request, array $params)
public function generateDefaultResponse(array $params)
{
$response = [
'took' => 0,
Expand All @@ -40,7 +72,25 @@ public function exec(Request $request, array $params)
],
'params' => $params,
];

return new Response(JSON::stringify($response));
}

/**
* Null transport.
*
* @param \Elastica\Request $request
* @param array $params Hostname, port, path, ...
*
* @return \Elastica\Response Response empty object
*/
public function exec(Request $request, array $params)
{
$response = $this->getResponse();

if (!$response) {
$response = $this->generateDefaultResponse($params);
}

return $response;
}
}
36 changes: 36 additions & 0 deletions test/Elastica/Transport/NullTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,20 @@
* Elastica Null Transport Test.
*
* @author James Boehmer <james.boehmer@jamesboehmer.com>
* @author Jan Domanski <jandom@gmail.com>
*/
class NullTransportTest extends BaseTest
{

/** @var NullTransport NullTransport */
protected $transport;

public function setUp()
{
parent::setUp();
$this->transport = new NullTransport();
}

/**
* @group functional
*/
Expand Down Expand Up @@ -74,4 +85,29 @@ public function testExec()
$data = $response->getData();
$this->assertEquals($params, $data['params']);
}

/**
* @group unit
*/
public function testResponse()
{
$resposeString = '';
$response = new Response($resposeString);
$this->transport->setResponse($response);
$this->assertEquals($response, $this->transport->getResponse());
}

/**
* @group unit
*/
public function testGenerateDefaultResponse()
{
$params = [ 'blah' => 123 ];
$response = $this->transport->generateDefaultResponse($params);
$this->assertEquals([], $response->getTransferInfo());

$responseData = $response->getData();
$this->assertContains('params', $responseData);
$this->assertEquals($params, $responseData['params']);
}
}