-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose RestApiContext::request and RestApiContext::response
In order to allow deep testing from other contexts Rename RestApiContext::headers into RestApiContext::requestHeaders BC keep RestApiContext::getHeaders as alias but deprecated
- Loading branch information
Guillaume MOREL
committed
Mar 24, 2015
1 parent
1195e71
commit 32646ef
Showing
2 changed files
with
164 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,23 +6,20 @@ | |
use Rezzza\JsonApiBehatExtension\RestApiContext as SUT; | ||
|
||
/** | ||
* Description of RestApiContext | ||
* | ||
* @author Mikaël FIMA <[email protected]> | ||
* @author Guillaume MOREL <[email protected]> | ||
*/ | ||
class RestApiContext extends atoum | ||
{ | ||
|
||
/** | ||
* Adding headers | ||
* @dataProvider addHeaderDataProvider | ||
*/ | ||
public function testAddHeader(array $addHeadersSteps, array $expectedHeaders) | ||
public function testAddRequestHeader(array $addHeadersSteps, array $expectedHeaders) | ||
{ | ||
$this | ||
->given( | ||
$httpClient = new \mock\Guzzle\Http\Client(), | ||
$httpClient->getMockController()->send = new \Guzzle\Http\Message\Response(200) | ||
$httpClient = $this->mockHttpClient(200) | ||
) | ||
->and($sut = new SUT($httpClient, null, false)) | ||
; | ||
|
@@ -34,7 +31,7 @@ public function testAddHeader(array $addHeadersSteps, array $expectedHeaders) | |
} | ||
|
||
$this | ||
->array($sut->getHeaders())->isIdenticalTo($expectedHeaders) | ||
->array($sut->getRequestHeaders())->isIdenticalTo($expectedHeaders) | ||
; | ||
} | ||
|
||
|
@@ -51,12 +48,11 @@ public function addHeaderDataProvider() | |
* Setting headers | ||
* @dataProvider setHeaderDataProvider | ||
*/ | ||
public function testSetHeader(array $setHeadersSteps, array $expectedHeaders) | ||
public function testSetRequestHeader(array $setHeadersSteps, array $expectedHeaders) | ||
{ | ||
$this | ||
->given( | ||
$httpClient = new \mock\Guzzle\Http\Client(), | ||
$httpClient->getMockController()->send = new \Guzzle\Http\Message\Response(200) | ||
$httpClient = $this->mockHttpClient(200) | ||
) | ||
->and($sut = new SUT($httpClient, null, false)) | ||
; | ||
|
@@ -68,7 +64,7 @@ public function testSetHeader(array $setHeadersSteps, array $expectedHeaders) | |
} | ||
|
||
$this | ||
->array($sut->getHeaders())->isIdenticalTo($expectedHeaders) | ||
->array($sut->getRequestHeaders())->isIdenticalTo($expectedHeaders) | ||
; | ||
} | ||
|
||
|
@@ -80,4 +76,101 @@ public function setHeaderDataProvider() | |
array(array(array("name" => "value"), array("name" => "value2")), array("name" => "value2")), | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider requestDataProvider | ||
* @param array $requestHeaders | ||
*/ | ||
public function test_get_request(array $requestHeaders) | ||
{ | ||
// Given | ||
$mockHttpClient = $this->mockHttpClient(200, array()); | ||
|
||
$restApiContext = new SUT($mockHttpClient, null, false); | ||
foreach ($requestHeaders as $requestHeaderKey => $requestHeaderValue) { | ||
$restApiContext->iAddHeaderEqualTo($requestHeaderKey, $requestHeaderValue); | ||
} | ||
|
||
// When | ||
$restApiContext->iSendARequest('GET', 'http://verylastroom.com/'); | ||
|
||
// Then | ||
$request = $restApiContext->getRequest(); | ||
$intersect = array_intersect_key($requestHeaders, $request->getHeaders()->toArray()); | ||
|
||
$this->array($requestHeaders)->isEqualTo($intersect); | ||
} | ||
|
||
public function requestDataProvider() | ||
{ | ||
return array( | ||
array( | ||
'requestHeaders' => array( | ||
array("name" => "value") | ||
) | ||
), | ||
array( | ||
'requestHeaders' => array( | ||
array("name1" => "value1"), array("name2" => "value2") | ||
) | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider responseDataProvider | ||
* @param int $statusCode | ||
* @param array $responseHeaders | ||
*/ | ||
public function test_get_response($statusCode, array $responseHeaders) | ||
{ | ||
// Given | ||
$mockHttpClient = $this->mockHttpClient($statusCode, $responseHeaders); | ||
|
||
$restApiContext = new SUT($mockHttpClient, null, false); | ||
|
||
// When | ||
$restApiContext->iSendARequest('GET', 'http://verylastroom.com/'); | ||
|
||
// Then | ||
$response = $restApiContext->getResponse(); | ||
$intersect = array_intersect_key($responseHeaders, $response->getHeaders()->toArray()); | ||
|
||
$this->array($responseHeaders)->isEqualTo($intersect); | ||
} | ||
|
||
public function responseDataProvider() | ||
{ | ||
return array( | ||
array( | ||
'statusCode' => 200, | ||
'requestHeaders' => array( | ||
array("name" => "value") | ||
) | ||
), | ||
array( | ||
'statusCode' => 400, | ||
'requestHeaders' => array( | ||
array("name1" => "value1"), array("name2" => "value2") | ||
) | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* @param int $responseStatusCode | ||
* @param array $headers | ||
* | ||
* @return \Guzzle\Http\Client | ||
*/ | ||
private function mockHttpClient($responseStatusCode, array $headers = array()) | ||
{ | ||
$mockHttpClient = new \mock\Guzzle\Http\Client(); | ||
$mockHttpClient->getMockController()->send = new \Guzzle\Http\Message\Response( | ||
$responseStatusCode, | ||
$headers | ||
); | ||
|
||
return $mockHttpClient; | ||
} | ||
} |