createGeneratedFolderIfNeeded($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName); /** @var Writer $writer */ $writer = WriterEntityFactory::createWriter(Type::ODS); $writer->setShouldCreateNewSheetsAutomatically(true); $writer->openToFile($resourcePath); for ($i = 1; $i <= $numRows; $i++) { $writer->addRow(WriterEntityFactory::createRowFromArray(["ods--{$i}-1", "ods--{$i}-2", "ods--{$i}-3"])); } $writer->close(); $this->assertEquals($numRows, $this->getNumWrittenRows($resourcePath), "The created ODS ($fileName) should contain $numRows rows"); $executionTime = time() - $startTime; $this->assertTrue($executionTime < $expectedMaxExecutionTime, "Writing 1 million rows should take less than $expectedMaxExecutionTime seconds (took $executionTime seconds)"); $memoryPeakUsage = memory_get_peak_usage(true) - $beforeMemoryPeakUsage; $this->assertTrue($memoryPeakUsage < $expectedMaxMemoryPeakUsage, 'Writing 1 million rows should require less than ' . ($expectedMaxMemoryPeakUsage / 1024 / 1024) . ' MB of memory (required ' . ($memoryPeakUsage / 1024 / 1024) . ' MB)'); } /** * @param string $resourcePath * @return int */ private function getNumWrittenRows($resourcePath) { $numWrittenRows = 0; // to avoid executing the regex of the entire file to get the last row number, we only retrieve the last 10 lines $endingContentXmlContents = $this->getLastCharactersOfContentXmlFile($resourcePath); if (preg_match_all('/ods--(\d+)-\d<\/text:p>/', $endingContentXmlContents, $matches)) { $lastMatch = array_pop($matches); $numWrittenRows = (int) (array_pop($lastMatch)); } return $numWrittenRows; } /** * @param string $resourcePath * @return string */ private function getLastCharactersOfContentXmlFile($resourcePath) { $pathToContentXmlFile = 'zip://' . $resourcePath . '#content.xml'; // since we cannot execute "tail" on a file inside a zip, we need to copy it outside first $tmpFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'get_last_characters.xml'; copy($pathToContentXmlFile, $tmpFile); // Get the last 200 characters $lastCharacters = `tail -c 200 $tmpFile`; // remove the temporary file unlink($tmpFile); return $lastCharacters; } }