Skip to content

Commit

Permalink
Merge pull request #2 from autorusltd/release/v1.2.0
Browse files Browse the repository at this point in the history
v1.2.0
  • Loading branch information
fenric authored Apr 13, 2020
2 parents 7ef20d7 + 9fc6fec commit 3408b81
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/ResponseFactoryAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,23 @@ public function violations(ConstraintViolationListInterface $violations, int $st

return $this->json(new Errors(...$errors), $status);
}

/**
* @param array $violations
* @param int $status
*
* @return ResponseInterface
*/
public function jsonViolations(array $violations, int $status = 400) : ResponseInterface
{
$errors = [];
foreach ($violations as $violation) {
$errors[] = new Error(
$violation['message'],
$violation['property']
);
}

return $this->json(new Errors(...$errors), $status);
}
}
39 changes: 39 additions & 0 deletions tests/ResponseFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,43 @@ public function testCreateViolationsResponseWithCustomStatusCode() : void

$this->assertSame(500, $response->getStatusCode());
}

/**
* @return void
*/
public function testCreateJsonViolationsResponse() : void
{
$response = (new class () {
use ResponseFactoryAwareTrait;
})->jsonViolations([
[
'message' => 'foo',
'property' => 'bar',
],
[
'message' => 'baz',
'property' => 'qux',
],
]);

$this->assertSame(400, $response->getStatusCode());

$this->assertSame(
'{"errors":[{"code":null,"source":"bar","message":"foo"},' .
'{"code":null,"source":"qux","message":"baz"}]}',
$response->getBody()->__toString()
);
}

/**
* @return void
*/
public function testCreateJsonViolationsResponseWithCustomStatusCode() : void
{
$response = (new class () {
use ResponseFactoryAwareTrait;
})->jsonViolations([], 500);

$this->assertSame(500, $response->getStatusCode());
}
}

0 comments on commit 3408b81

Please sign in to comment.