Skip to content

Commit

Permalink
Add NoContentResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
jsor committed Oct 13, 2014
1 parent 8b7a161 commit e1ee111
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/Response/HalResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,9 @@ public function getContent()
return $this->hal->asJson($this->prettyPrint);
}
}

public function getHal()
{
return $this->hal;
}
}
53 changes: 53 additions & 0 deletions src/Response/NoContentResponse.php
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);
}
}
4 changes: 0 additions & 4 deletions src/Response/VndErrorResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@

class VndErrorResponse extends HalResponse
{
protected $hal;
protected $requestFormat;
protected $prettyPrint;

public function __construct(Hal $hal, $status = 500, $headers = array(), $prettyPrint = false)
{
parent::__construct($hal, $status, $headers, $prettyPrint);
Expand Down
2 changes: 1 addition & 1 deletion tests/Response/HalResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class HalResponseTest extends \PHPUnit_Framework_TestCase
{
use ResponseTestCase;
use HalResponseTestCase;

protected function provideResponse(Hal $hal = null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Nocarrier\Hal;

trait ResponseTestCase
trait HalResponseTestCase
{
/** @test */
public function it_allows_setting_hal_content()
Expand Down Expand Up @@ -62,6 +62,16 @@ public function is_sends_hal_content()
);
}

/** @test */
public function it_returns_hal_instance()
{
$hal = new Hal(null, ['message' => 'test']);

$response = $this->provideResponse($hal);

$this->assertSame($hal, $response->getHal());
}

/**
* @return HalResponse
*/
Expand Down
69 changes: 69 additions & 0 deletions tests/Response/NoContentResponseTest.php
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'));
}
}
2 changes: 1 addition & 1 deletion tests/Response/VndErrorResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class VndErrorResponseTest extends \PHPUnit_Framework_TestCase
{
use ResponseTestCase;
use HalResponseTestCase;

protected function provideResponse(Hal $hal = null)
{
Expand Down

0 comments on commit e1ee111

Please sign in to comment.