From 1b2c99b1907308f72e5a5935f2875ed536159e12 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Sat, 4 Jan 2020 18:09:46 +0100 Subject: [PATCH] Initial unit test for locale floats (#1304) * Initial unit test for locale floats This will require potential modification of the TravisCI environment to support other locales * var_dump to check output on TravisCI * Fix assertions for double/float and with/without line reference * Style in unit test --- .../Writer/Xlsx/LocaleFloatsTest.php | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 tests/PhpSpreadsheetTests/Writer/Xlsx/LocaleFloatsTest.php diff --git a/tests/PhpSpreadsheetTests/Writer/Xlsx/LocaleFloatsTest.php b/tests/PhpSpreadsheetTests/Writer/Xlsx/LocaleFloatsTest.php new file mode 100644 index 00000000..2a87ffea --- /dev/null +++ b/tests/PhpSpreadsheetTests/Writer/Xlsx/LocaleFloatsTest.php @@ -0,0 +1,58 @@ +currentLocale = setlocale(LC_ALL, '0'); + + if (!setlocale(LC_ALL, 'fr_FR.UTF-8')) { + $this->localeAdjusted = false; + + return; + } + + $this->localeAdjusted = true; + } + + public function tearDown() + { + if ($this->localeAdjusted) { + setlocale(LC_ALL, $this->currentLocale); + } + } + + public function testLocaleFloatsCorrectlyConvertedByWriter() + { + if (!$this->localeAdjusted) { + $this->markTestSkipped('Unable to set locale for testing.'); + } + + $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet(); + $spreadsheet->getActiveSheet()->setCellValue('A1', 1.1); + + $filename = 'decimalcomma.xlsx'; + $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet); + $writer->save($filename); + + $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx(); + $spreadsheet = $reader->load($filename); + + $result = $spreadsheet->getActiveSheet()->getCell('A1')->getValue(); + + ob_start(); + var_dump($result); + preg_match('/(?:double|float)\(([^\)]+)\)/mui', ob_get_clean(), $matches); + $this->assertArrayHasKey(1, $matches); + $actual = $matches[1]; + $this->assertEquals('1,1', $actual); + } +}