-
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
4 changed files
with
168 additions
and
1 deletion.
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,34 @@ | ||
<?php | ||
|
||
namespace Jsor\Stack\Hal\Response; | ||
|
||
use Nocarrier\Hal; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
class HalResponseTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
use ResponseTestCase; | ||
|
||
protected function provideResponse(Hal $hal = null) | ||
{ | ||
return HalResponse::create($hal ?: new Hal()); | ||
} | ||
|
||
/** @test */ | ||
public function it_sets_default_content_type_header() | ||
{ | ||
$response = $this->provideResponse(); | ||
$this->assertSame('application/hal+json', $response->headers->get('Content-Type')); | ||
} | ||
|
||
/** @test */ | ||
public function it_sets_content_type_header_depending_on_request_format() | ||
{ | ||
$response = $this->provideResponse(); | ||
|
||
$request = new Request(); | ||
$request->setRequestFormat('xml'); | ||
$response->prepare($request); | ||
$this->assertSame('application/hal+xml', $response->headers->get('Content-Type')); | ||
} | ||
} |
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; | ||
|
||
trait ResponseTestCase | ||
{ | ||
/** @test */ | ||
public function it_allows_setting_hal_content() | ||
{ | ||
$response = $this->provideResponse(); | ||
|
||
$response->setContent(new Hal(null, ['message' => 'test'])); | ||
|
||
$this->assertJsonStringEqualsJsonString( | ||
json_encode( | ||
[ | ||
'message' => 'test' | ||
] | ||
), | ||
$response->getContent() | ||
); | ||
} | ||
|
||
/** @test */ | ||
public function it_allows_setting_null_content() | ||
{ | ||
$response = $this->provideResponse(); | ||
|
||
$response->setContent(null); | ||
|
||
$this->assertSame('', $response->getContent()); | ||
} | ||
|
||
/** @test */ | ||
public function it_throws_exception_for_non_hal_content() | ||
{ | ||
$this->setExpectedException('\LogicException'); | ||
|
||
$response = $this->provideResponse(); | ||
|
||
$response->setContent(''); | ||
} | ||
|
||
/** @test */ | ||
public function is_sends_hal_content() | ||
{ | ||
$response = $this->provideResponse(new Hal(null, ['message' => 'test'])); | ||
|
||
ob_start(); | ||
$response->sendContent(); | ||
$string = ob_get_clean(); | ||
|
||
$this->assertJsonStringEqualsJsonString( | ||
json_encode( | ||
[ | ||
'message' => 'test' | ||
] | ||
), | ||
$string | ||
); | ||
} | ||
|
||
/** | ||
* @return HalResponse | ||
*/ | ||
abstract protected function provideResponse(); | ||
} |
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,64 @@ | ||
<?php | ||
|
||
namespace Jsor\Stack\Hal\Response; | ||
|
||
use Jsor\Stack\Hal\Exception\ErrorException; | ||
use Nocarrier\Hal; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\Validator\Constraints\Collection; | ||
|
||
class VndErrorResponseTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
use ResponseTestCase; | ||
|
||
protected function provideResponse(Hal $hal = null) | ||
{ | ||
return VndErrorResponse::create($hal ?: new Hal()); | ||
} | ||
|
||
/** @test */ | ||
public function it_sets_default_content_type_header() | ||
{ | ||
$response = $this->provideResponse(); | ||
$this->assertSame('application/vnd.error+json', $response->headers->get('Content-Type')); | ||
} | ||
|
||
/** @test */ | ||
public function it_sets_content_type_header_depending_on_request_format() | ||
{ | ||
$response = $this->provideResponse(); | ||
|
||
$request = new Request(); | ||
$request->setRequestFormat('xml'); | ||
$response->prepare($request); | ||
$this->assertSame('application/vnd.error+xml', $response->headers->get('Content-Type')); | ||
} | ||
|
||
/** @test */ | ||
public function it_accepts_hal_exception() | ||
{ | ||
$exception = new ErrorException(['Error'], 'Error', 100); | ||
|
||
$response = VndErrorResponse::fromException($exception); | ||
|
||
$this->assertSame(400, $response->getStatusCode()); | ||
$this->assertJsonStringEqualsJsonString( | ||
json_encode( | ||
[ | ||
'message' => 'Error', | ||
'logref' => 100, | ||
'_embedded' => | ||
[ | ||
'errors' => | ||
[ | ||
[ | ||
'message' => 'Error', | ||
], | ||
], | ||
], | ||
] | ||
), | ||
$response->getContent() | ||
); | ||
} | ||
} |