. namespace dataformat_ods; use core\dataformat; /** * Tests for the dataformat_ods writer * * @package dataformat_ods * @copyright 2022 Marina Glancy * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class writer_test extends \advanced_testcase { /** * Test writing data whose content contains an image with pluginfile.php source */ public function test_write_data(): void { $columns = ['fruit', 'colour', 'animal']; $rows = [ ['banana', 'yellow', 'monkey'], ['apple', 'red', 'wolf'], ['melon', 'green', 'aardvark'], ]; // Export to file. $exportfile = dataformat::write_data('My export', 'ods', $columns, $rows); // Read the file. $odscells = $this->get_ods_rows_content(file_get_contents($exportfile)); $this->assertEquals(array_merge([$columns], $rows), $odscells); } /** * Get ods rows from binary content * @param string $content * @return array * @throws \OpenSpout\Common\Exception\IOException * @throws \OpenSpout\Reader\Exception\ReaderNotOpenedException */ private function get_ods_rows_content($content) { $file = tempnam(sys_get_temp_dir(), 'ods_'); $handle = fopen($file, "w"); fwrite($handle, $content); /** @var \OpenSpout\Reader\ODS\Reader $reader */ $reader = \OpenSpout\Reader\Common\Creator\ReaderFactory::createFromFileByMimeType($file); $reader->open($file); /** @var \OpenSpout\Reader\ODS\Sheet[] $sheets */ $sheets = $reader->getSheetIterator(); $rowscellsvalues = []; foreach ($sheets as $sheet) { /** @var \OpenSpout\Common\Entity\Row[] $rows */ $rows = $sheet->getRowIterator(); foreach ($rows as $row) { $thisvalues = []; foreach ($row->getCells() as $cell) { $thisvalues[] = $cell->getValue(); } $rowscellsvalues[] = $thisvalues; } } return $rowscellsvalues; } }