Skip to content
This repository has been archived by the owner on Dec 20, 2021. It is now read-only.

Commit

Permalink
Merge pull request #22 from dpobel/csv_format
Browse files Browse the repository at this point in the history
feat: add partial support text/csv response
  • Loading branch information
RETFU authored May 23, 2019
2 parents 6138ec5 + ef41a18 commit 6d96ed1
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ Based on the APISpec description, **RREST** validate i/o:
`Accept` is required for every request.
`Content-Type` is required for `POST` & `PUT` method.

> **RREST** only support JSON & XML for input/output, so valid mime-types are: `application/json`, `application/x-json`, `text/xml`, `application/xml` & `application/x-xml`.
> **RREST** supports JSON & XML for input/output and partially supports CSV for
> output, so valid mime-types are: `application/json`, `application/x-json`,
> `text/xml`, `application/xml`, `application/x-xml`, 'text/csv' and
> 'application/csv'.
#### Protocol

Expand All @@ -103,6 +106,7 @@ This will ensure that data input is valid.
Depending of the `Accept` header, **RREST** will validate:
* if the format (`JSON`or `XML`) is valid
* if it follow the schema (`JSON Schema` or `XML Schema`)
* there's no validation for `CSV`

A schema is not required by **RREST** for a response. But this is a security to be sure that your response
respect your APISpec and your documentation based on it.
Expand Down Expand Up @@ -188,6 +192,7 @@ class Resource
##### Format input/output
* JSON
* XML
* CSV (output only)

##### APISPec
* RAML
Expand Down
1 change: 1 addition & 0 deletions src/RREST.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class RREST
public static $supportedMimeTypes = [
'json' => ['application/json', 'application/x-json'],
'xml' => ['text/xml', 'application/xml', 'application/x-xml'],
'csv' => ['text/csv', 'application/csv'],
];

/**
Expand Down
13 changes: 12 additions & 1 deletion src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Response
/**
* @var string[]
*/
protected $supportedFormat = ['json', 'xml'];
protected $supportedFormat = ['json', 'xml', 'csv'];

/**
* @var RouterInterface
Expand Down Expand Up @@ -251,6 +251,14 @@ public function serialize($data, $format)
$data = json_decode(json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), true);

return $serializer->serialize($data, $format);
} elseif ($format === 'csv') {
if (!is_string($data)) {
throw new \RuntimeException(
'auto serialization for CSV format is not supported'
);
}

return $data;
} else {
throw new \RuntimeException(
'format not supported, only are '.implode(', ', $this->supportedFormat).' availables'
Expand Down Expand Up @@ -280,6 +288,9 @@ public function assertReponseSchema($format, $schema, $value)
case strpos($format, 'xml') !== false:
$this->assertResponseXML($value, $schema);
break;
case strpos($format, 'csv') !== false:
// no validation for CSV
break;
default:
throw new \RuntimeException(
'format not supported, only are '.implode(', ', $this->supportedFormat).' availables'
Expand Down
28 changes: 28 additions & 0 deletions tests/units/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,34 @@ function () {
;
}

public function testNoCsvSerialize()
{
$this->newTestedInstance($this->router, 'csv', 200);

$this
->given($this->testedInstance)
->exception(
function () {
$this->testedInstance->serialize([], 'csv');
}
)
->isInstanceOf('\RuntimeException')
->message->contains('auto serialization for CSV format is not supported');
}

public function testSerializeForCsv()
{
$this->newTestedInstance($this->router, 'csv', 200);

$csv = "The Black Keys;El Camino\nFoo Fighters;Sonic Highways";

$this
->given($this->testedInstance)
->string($this->testedInstance->serialize($csv, 'csv'))
->isEqualTo($csv);
;
}

public function testAssertReponseSchema()
{
$this->newTestedInstance($this->router, 'json', 200);
Expand Down

0 comments on commit 6d96ed1

Please sign in to comment.