-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
140 additions
and
7 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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace Jsor\Stack\Hal\Response; | ||
|
||
use Nocarrier\Hal; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class NoContentResponse extends Response | ||
{ | ||
protected $location; | ||
|
||
public function __construct($location = null, $headers = array()) | ||
{ | ||
parent::__construct(null, 204, $headers); | ||
|
||
if (!empty($location)) { | ||
$this->headers->set('Location', $location); | ||
} | ||
} | ||
|
||
public static function create($location = null, $status = 204, $headers = array()) | ||
{ | ||
return new static($location, $headers); | ||
} | ||
|
||
public static function convert($value) | ||
{ | ||
if ($value instanceof Hal) { | ||
return new static($value->getUri()); | ||
} | ||
|
||
if ($value instanceof Response) { | ||
$uri = $value instanceof HalResponse ? $value->getHal()->getUri() : null; | ||
|
||
return new static($uri, $value->headers->all()); | ||
} | ||
|
||
if (is_string($value) && filter_var($value, FILTER_VALIDATE_URL)) { | ||
return new static($value); | ||
} | ||
|
||
return new static(); | ||
} | ||
|
||
public static function convertIfSuccessful($value) | ||
{ | ||
if ($value instanceof Response && !$value->isSuccessful()) { | ||
return $value; | ||
} | ||
|
||
return static::convert($value); | ||
} | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace Jsor\Stack\Hal\Response; | ||
|
||
use Nocarrier\Hal; | ||
|
||
class NoContentResponseTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** @test */ | ||
public function it_sets_location_header() | ||
{ | ||
$response = NoContentResponse::create('http://example.com'); | ||
$this->assertSame('http://example.com', $response->headers->get('Location')); | ||
} | ||
|
||
/** @test */ | ||
public function it_converts_hal_instance() | ||
{ | ||
$hal = new Hal('http://example.com'); | ||
|
||
$response = NoContentResponse::convert($hal); | ||
$this->assertSame('http://example.com', $response->headers->get('Location')); | ||
} | ||
|
||
/** @test */ | ||
public function it_converts_hal_response_instance() | ||
{ | ||
$halResponse = new HalResponse(new Hal('http://example.com')); | ||
|
||
$response = NoContentResponse::convert($halResponse); | ||
$this->assertSame('http://example.com', $response->headers->get('Location')); | ||
} | ||
|
||
/** @test */ | ||
public function it_converts_string_url() | ||
{ | ||
$url = 'http://example.com'; | ||
|
||
$response = NoContentResponse::convert($url); | ||
$this->assertSame('http://example.com', $response->headers->get('Location')); | ||
} | ||
|
||
/** @test */ | ||
public function it_ignores_invalid_string_url() | ||
{ | ||
$url = 'foo'; | ||
|
||
$response = NoContentResponse::convert($url); | ||
$this->assertNull($response->headers->get('Location')); | ||
} | ||
|
||
/** @test */ | ||
public function it_converts_response_if_successful() | ||
{ | ||
$halResponse = new HalResponse(new Hal('http://example.com')); | ||
|
||
$response = NoContentResponse::convertIfSuccessful($halResponse); | ||
$this->assertSame('http://example.com', $response->headers->get('Location')); | ||
} | ||
|
||
/** @test */ | ||
public function it_ignores_response_if_not_successful() | ||
{ | ||
$errorResponse = new VndErrorResponse(new Hal('http://example.com')); | ||
|
||
$response = NoContentResponse::convertIfSuccessful($errorResponse); | ||
$this->assertNull($response->headers->get('Location')); | ||
} | ||
} |
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