From 044836b42672d42de7e7449a376d87ab571af250 Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Sat, 5 Mar 2022 22:31:52 +0100 Subject: [PATCH] Unit test for the Calculation Engine debug log --- .../Calculation/CalculationLoggingTest.php | 125 ++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 tests/PhpSpreadsheetTests/Calculation/CalculationLoggingTest.php diff --git a/tests/PhpSpreadsheetTests/Calculation/CalculationLoggingTest.php b/tests/PhpSpreadsheetTests/Calculation/CalculationLoggingTest.php new file mode 100644 index 00000000..cb110574 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Calculation/CalculationLoggingTest.php @@ -0,0 +1,125 @@ +getActiveSheet(); + + $sheet->fromArray( + [ + [1, 2, 3], + [4, 5, 6], + [7, 8, 9], + ] + ); + + $debugLog = Calculation::getInstance($spreadsheet)->getDebugLog(); + $debugLog->setWriteDebugLog(true); + + $cell = $sheet->getCell('E5'); + $cell->setValue('=ROUND(SQRT(SUM(A1:C3)), 1)'); + self::assertEquals(6.7, $cell->getCalculatedValue()); + + $log = $debugLog->getLog(); + self::assertIsArray($log); + $entries = count($log); + self::assertGreaterThan(0, $entries); + + $finalEntry = array_pop($log); + self::assertStringContainsString('Evaluation Result', $finalEntry); + } + + public function testFormulaWithMultipleCellLogging(): void + { + $spreadsheet = new Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + + $sheet->fromArray( + [ + [1, 2, 3], + [4, 5, 6], + [7, 8, 9], + ] + ); + + $debugLog = Calculation::getInstance($spreadsheet)->getDebugLog(); + $debugLog->setWriteDebugLog(true); + + $cell = $sheet->getCell('E1'); + $cell->setValue('=SUM(A1:C3)'); + + $cell = $sheet->getCell('E3'); + $cell->setValue('=SQRT(E1)'); + + $cell = $sheet->getCell('E5'); + $cell->setValue('=ROUND(E3, 1)'); + self::assertEquals(6.7, $cell->getCalculatedValue()); + + $log = $debugLog->getLog(); + + self::assertIsArray($log); + $entries = count($log); + self::assertGreaterThan(0, $entries); + + $finalEntry = array_pop($log); + self::assertStringContainsString('Evaluation Result', $finalEntry); + + $e1Log = array_filter($log, function ($entry) { + return strpos($entry, 'E1') !== false; + }); + $e1Entries = count($e1Log); + self::assertGreaterThan(0, $e1Entries); + + $e1FinalEntry = array_pop($e1Log); + self::assertStringContainsString('Evaluation Result', $e1FinalEntry); + + $e3Log = array_filter($log, function ($entry) { + return strpos($entry, 'E1') !== false; + }); + $e3Entries = count($e3Log); + self::assertGreaterThan(0, $e3Entries); + + $e3FinalEntry = array_pop($e3Log); + self::assertStringContainsString('Evaluation Result', $e3FinalEntry); + } + + public function testFlushLog(): void + { + $spreadsheet = new Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + + $sheet->fromArray( + [ + [1, 2, 3], + [4, 5, 6], + [7, 8, 9], + ] + ); + + $debugLog = Calculation::getInstance($spreadsheet)->getDebugLog(); + $debugLog->setWriteDebugLog(true); + + $cell = $sheet->getCell('E5'); + $cell->setValue('=(1+-2)*3/4'); + self::assertEquals(-0.75, $cell->getCalculatedValue()); + + $log = $debugLog->getLog(); + self::assertIsArray($log); + $entries = count($log); + self::assertGreaterThan(0, $entries); + + $debugLog->clearLog(); + + $log = $debugLog->getLog(); + self::assertIsArray($log); + self::assertEmpty($log); + } +}