Skip to content

Commit 044836b

Browse files
author
MarkBaker
committed
Unit test for the Calculation Engine debug log
1 parent ac33bd9 commit 044836b

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Calculation;
4+
5+
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
6+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class CalculationLoggingTest extends TestCase
10+
{
11+
public function testFormulaWithLogging(): void
12+
{
13+
$spreadsheet = new Spreadsheet();
14+
$sheet = $spreadsheet->getActiveSheet();
15+
16+
$sheet->fromArray(
17+
[
18+
[1, 2, 3],
19+
[4, 5, 6],
20+
[7, 8, 9],
21+
]
22+
);
23+
24+
$debugLog = Calculation::getInstance($spreadsheet)->getDebugLog();
25+
$debugLog->setWriteDebugLog(true);
26+
27+
$cell = $sheet->getCell('E5');
28+
$cell->setValue('=ROUND(SQRT(SUM(A1:C3)), 1)');
29+
self::assertEquals(6.7, $cell->getCalculatedValue());
30+
31+
$log = $debugLog->getLog();
32+
self::assertIsArray($log);
33+
$entries = count($log);
34+
self::assertGreaterThan(0, $entries);
35+
36+
$finalEntry = array_pop($log);
37+
self::assertStringContainsString('Evaluation Result', $finalEntry);
38+
}
39+
40+
public function testFormulaWithMultipleCellLogging(): void
41+
{
42+
$spreadsheet = new Spreadsheet();
43+
$sheet = $spreadsheet->getActiveSheet();
44+
45+
$sheet->fromArray(
46+
[
47+
[1, 2, 3],
48+
[4, 5, 6],
49+
[7, 8, 9],
50+
]
51+
);
52+
53+
$debugLog = Calculation::getInstance($spreadsheet)->getDebugLog();
54+
$debugLog->setWriteDebugLog(true);
55+
56+
$cell = $sheet->getCell('E1');
57+
$cell->setValue('=SUM(A1:C3)');
58+
59+
$cell = $sheet->getCell('E3');
60+
$cell->setValue('=SQRT(E1)');
61+
62+
$cell = $sheet->getCell('E5');
63+
$cell->setValue('=ROUND(E3, 1)');
64+
self::assertEquals(6.7, $cell->getCalculatedValue());
65+
66+
$log = $debugLog->getLog();
67+
68+
self::assertIsArray($log);
69+
$entries = count($log);
70+
self::assertGreaterThan(0, $entries);
71+
72+
$finalEntry = array_pop($log);
73+
self::assertStringContainsString('Evaluation Result', $finalEntry);
74+
75+
$e1Log = array_filter($log, function ($entry) {
76+
return strpos($entry, 'E1') !== false;
77+
});
78+
$e1Entries = count($e1Log);
79+
self::assertGreaterThan(0, $e1Entries);
80+
81+
$e1FinalEntry = array_pop($e1Log);
82+
self::assertStringContainsString('Evaluation Result', $e1FinalEntry);
83+
84+
$e3Log = array_filter($log, function ($entry) {
85+
return strpos($entry, 'E1') !== false;
86+
});
87+
$e3Entries = count($e3Log);
88+
self::assertGreaterThan(0, $e3Entries);
89+
90+
$e3FinalEntry = array_pop($e3Log);
91+
self::assertStringContainsString('Evaluation Result', $e3FinalEntry);
92+
}
93+
94+
public function testFlushLog(): void
95+
{
96+
$spreadsheet = new Spreadsheet();
97+
$sheet = $spreadsheet->getActiveSheet();
98+
99+
$sheet->fromArray(
100+
[
101+
[1, 2, 3],
102+
[4, 5, 6],
103+
[7, 8, 9],
104+
]
105+
);
106+
107+
$debugLog = Calculation::getInstance($spreadsheet)->getDebugLog();
108+
$debugLog->setWriteDebugLog(true);
109+
110+
$cell = $sheet->getCell('E5');
111+
$cell->setValue('=(1+-2)*3/4');
112+
self::assertEquals(-0.75, $cell->getCalculatedValue());
113+
114+
$log = $debugLog->getLog();
115+
self::assertIsArray($log);
116+
$entries = count($log);
117+
self::assertGreaterThan(0, $entries);
118+
119+
$debugLog->clearLog();
120+
121+
$log = $debugLog->getLog();
122+
self::assertIsArray($log);
123+
self::assertEmpty($log);
124+
}
125+
}

0 commit comments

Comments
 (0)