Skip to content

Commit

Permalink
Add response tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jsor committed Oct 13, 2014
1 parent 1e61308 commit 00a5329
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Response/HalResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function setContent($content)
public function getContent()
{
if (null === $this->hal) {
return null;
return '';
}

switch ($this->requestFormat) {
Expand Down
34 changes: 34 additions & 0 deletions tests/Response/HalResponseTest.php
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'));
}
}
69 changes: 69 additions & 0 deletions tests/Response/ResponseTestCase.php
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();
}
64 changes: 64 additions & 0 deletions tests/Response/VndErrorResponseTest.php
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()
);
}
}

0 comments on commit 00a5329

Please sign in to comment.