Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make asserts on the content from an Excel file?

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.

like image 677
M. Marc Avatar asked Nov 10 '22 15:11

M. Marc


1 Answers

You may use PHPexcel (can be found here: phpexcel) to parse your excel and assert on data collected by phpexcel

like image 76
lordyoum Avatar answered Nov 14 '22 21:11

lordyoum