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

feat(xlsx): add supports for xlsx #38

Merged
merged 1 commit into from
Nov 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/RREST.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class RREST
'json' => ['application/json', 'application/x-json'],
'xml' => ['text/xml', 'application/xml', 'application/x-xml'],
'csv' => ['text/csv', 'application/csv'],
'xlsx' => ['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'],
];

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Response
/**
* @var string[]
*/
protected $supportedFormat = ['json', 'xml', 'csv'];
protected $supportedFormat = ['json', 'xml', 'csv', 'xlsx'];

/**
* @var RouterInterface
Expand Down Expand Up @@ -264,10 +264,10 @@ 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') {
} elseif ($format === 'csv' || $format === 'xlsx') {
if (!is_string($data)) {
throw new \RuntimeException(
'auto serialization for CSV format is not supported'
'auto serialization for '.strtoupper($format).' format is not supported'
);
}

Expand Down
28 changes: 28 additions & 0 deletions tests/units/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,34 @@ public function testSerializeForCsv()
;
}

public function testNoXlsxSerialize()
{
$this->newTestedInstance($this->router, 'xlsx', 200);

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

public function testSerializeForXlsx()
{
$this->newTestedInstance($this->router, 'xlsx', 200);

$xlsx = "Placeholder for binary content";

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

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