Skip to content

Commit

Permalink
Response Interface Changes
Browse files Browse the repository at this point in the history
Add Getters/Setters for Request Object on Response, to allow for
changing of Request after creation, and to allow for creation of
Request/Response in any order.
  • Loading branch information
MichaelJ2324 committed Mar 9, 2017
1 parent 13c94a4 commit 675ac6b
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 12 deletions.
57 changes: 45 additions & 12 deletions src/Response/AbstractResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,40 +23,73 @@ abstract class AbstractResponse implements ResponseInterface
* Extracted headers from cURL Response
* @var string
*/
protected $headers;
protected $headers = NULL;

/**
* Extracted body from cURL Response
* @var mixed
*/
protected $body;
protected $body = NULL;

/**
* The HTTP Status Code of Request
* @var string
*/
protected $status;
protected $status = NULL;

/**
* The cURL Resource information returned via curl_getinfo
* @var array
*/
protected $info;
protected $info = array();

public function __construct(RequestInterface $Request)
public function __construct(RequestInterface $Request = NULL)
{
$this->Request = $Request;
if ($Request !== NULL){
$this->setRequest($Request);
}
$this->extract();
}

/**
* @inheritdoc
*/
public function setRequest(RequestInterface $Request){
$this->Request = $Request;
$this->reset();
return $this;
}

/**
* Reset the Response Object
*/
public function reset(){
$this->status = NULL;
$this->body = NULL;
$this->headers = NULL;
$this->info = array();
}

/**
* @inheritdoc
*/
public function getRequest() {
return $this->Request;
}

/**
* @inheritdoc
*/
public function extract()
{
$error = $this->Request->getError();
if ($this->Request->getStatus() == Curl::STATUS_SENT && empty($error)){
$this->extractInfo($this->Request->getCurlResource());
$this->extractResponse($this->Request->getResponse());
$this->Request->close();
return TRUE;
if (is_object($this->Request)){
$error = $this->Request->getError();
if ($this->Request->getStatus() == Curl::STATUS_SENT && empty($error)){
$this->extractInfo($this->Request->getCurlResource());
$this->extractResponse($this->Request->getResponse());
$this->Request->close();
return TRUE;
}
}
return FALSE;
}
Expand Down
15 changes: 15 additions & 0 deletions src/Response/ResponseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,23 @@

namespace MRussell\Http\Response;

use MRussell\Http\Request\RequestInterface;

interface ResponseInterface
{
/**
* Set the Request Object that the Response is extracted from
* @param RequestInterface $Request
* @return mixed
*/
public function setRequest(RequestInterface $Request);

/**
* Get the current configured Request Object
* @return mixed
*/
public function getRequest();

/**
* Extract the Response information from the Request Object
* @return mixed
Expand Down
44 changes: 44 additions & 0 deletions tests/Response/AbstractResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,64 @@ public function tearDown()
* @group abstractResponse
*/
public function testConstructor(){
$Response = new Standard();
$this->assertEmpty($Response->getInfo());
$this->assertEmpty($Response->getBody());
$this->assertEmpty($Response->getHeaders());
$this->assertEmpty($Response->getStatus());
$this->assertEmpty($Response->getRequest());
$this->assertEquals(FALSE,$Response->extract());
unset($Response);
$Response = new Standard($this->Request);
$this->assertEmpty($Response->getInfo());
$this->assertEmpty($Response->getBody());
$this->assertEmpty($Response->getHeaders());
$this->assertEmpty($Response->getStatus());
$this->assertEquals($this->Request,$Response->getRequest());
$this->assertEquals(FALSE,$Response->extract());
$this->Request->send();
$this->assertEquals(TRUE,$Response->extract());
$this->assertNotEmpty($Response->getInfo());
$this->assertNotEmpty($Response->getBody());
$this->assertNotEmpty($Response->getHeaders());
$this->assertNotEmpty($Response->getStatus());
$this->assertEquals($this->Request,$Response->getRequest());
}

/**
* @covers ::setRequest
* @covers ::getRequest
* @covers ::reset
*/
public function testSetRequest(){
$Response = new Standard();
$this->assertEmpty($Response->getInfo());
$this->assertEmpty($Response->getBody());
$this->assertEmpty($Response->getHeaders());
$this->assertEmpty($Response->getStatus());
$this->assertEmpty($Response->getRequest());
$this->assertEquals(FALSE,$Response->extract());
$Response->setRequest($this->Request);
$this->assertEquals(FALSE,$Response->extract());
$this->Request->send();
$this->assertEquals(TRUE,$Response->extract());
$this->assertNotEmpty($Response->getInfo());
$this->assertNotEmpty($Response->getBody());
$this->assertNotEmpty($Response->getHeaders());
$this->assertNotEmpty($Response->getStatus());
$this->assertEquals($this->Request,$Response->getRequest());
$this->Request = new Curl('www.google.com');
$Response->setRequest($this->Request);
$this->assertEmpty($Response->getInfo());
$this->assertEmpty($Response->getBody());
$this->assertEmpty($Response->getHeaders());
$this->assertEmpty($Response->getStatus());
$this->assertEquals($this->Request,$Response->getRequest());
}

/**
* @covers ::extractInfo
*/
public function testExtraInfo(){
$Response = new ResponseTestStub($this->Request);
$this->Request->send();
Expand Down

0 comments on commit 675ac6b

Please sign in to comment.