I have a action that returns an Excel file. Accessing that route in a browser will automatically download my Excel file.
I wrote a test for that action, but all that I can assert is the data from headers. Also, the test outputs the response directly to the console with text like w֑m�J�9���9�e�줽o��
.
Is there a way to make asserts on data that comes from that Excel file output? The file is not saved, only the content is displayed in response. I'm using Symfony2.
EDIT
I found a way to read that data by saving the response in output buffer, saving the content in a .xlsx file, converting into a .csv file and get contents from there.
ob_start();
$this->client->request(
'GET',
'/myUrl',
array(),
array(),
array()
);
$content = ob_get_clean();
file_put_contents('MyFile.xlsx', $content);
$reader = \PHPExcel_IOFactory::createReader('Excel2007');
$excel = $reader->load('MyFile.xlsx');
$writer = \PHPExcel_IOFactory::createWriter($excel, 'CSV');
$writer->save('MyFileCsv.csv');
$newContent = fgetcsv(fopen("MyFileCsv.csv", "r"));
Here is just a sketch. I stopped here, didn't try to read all content and make asserts, but I'm sure it can be done this way. Ah, don't forget to delete the files after.
You may use PHPexcel (can be found here: phpexcel) to parse your excel and assert on data collected by phpexcel
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With