Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial Formula Translation tests #1886

Merged
merged 5 commits into from Feb 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions samples/Basic/43_Merge_workbooks.php
Expand Up @@ -18,6 +18,10 @@

foreach ($spreadsheet2->getSheetNames() as $sheetName) {
$sheet = $spreadsheet2->getSheetByName($sheetName);
if ($sheet === null) {
continue;
}

$sheet->setTitle($sheet->getTitle() . ' copied');
$spreadsheet1->addExternalSheet($sheet);
}
Expand Down
51 changes: 51 additions & 0 deletions tests/PhpSpreadsheetTests/Calculation/TranslationTest.php
@@ -0,0 +1,51 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Calculation;

use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Settings;
use PHPUnit\Framework\TestCase;

class TranslationTest extends TestCase
{
private $compatibilityMode;

private $returnDate;

protected function setUp(): void
{
$this->compatibilityMode = Functions::getCompatibilityMode();
$this->returnDate = Functions::getReturnDateType();
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
Functions::setReturnDateType(Functions::RETURNDATE_EXCEL);
}

protected function tearDown(): void
{
Functions::setCompatibilityMode($this->compatibilityMode);
Functions::setReturnDateType($this->returnDate);
}

/**
* @dataProvider providerTranslations
*/
public function testTranslation(string $expectedResult, string $locale, string $formula): void
{
$validLocale = Settings::setLocale($locale);
if (!$validLocale) {
self::markTestSkipped("Unable to set locale to {$locale}");
}

$translatedFormula = Calculation::getInstance()->_translateFormulaToLocale($formula);
self::assertSame($expectedResult, $translatedFormula);

$restoredFormula = Calculation::getInstance()->_translateFormulaToEnglish($translatedFormula);
self::assertSame($formula, $restoredFormula);
}

public function providerTranslations()
{
return require 'tests/data/Calculation/Translations.php';
}
}
48 changes: 48 additions & 0 deletions tests/data/Calculation/Translations.php
@@ -0,0 +1,48 @@
<?php

return [
[
'=DAGEN360(DATUM(2010;2;5);DATUM(2010;12;31);WAAR)',
'nl',
'=DAYS360(DATE(2010,2,5),DATE(2010,12,31),TRUE)',
],
[
'=DIAS360(DATA(2010;2;5);DATA(2010;12;31);VERDADEIRO)',
'pt_br',
'=DAYS360(DATE(2010,2,5),DATE(2010,12,31),TRUE)',
],
[
'=ДНЕЙ360(ДАТА(2010;2;5);ДАТА(2010;12;31);ИСТИНА)',
'ru',
'=DAYS360(DATE(2010,2,5),DATE(2010,12,31),TRUE)',
],
[
'=TEKST.SAMENVOEGEN(A1; " "; B1)',
'nl',
'=CONCATENATE(A1, " ", B1)',
],
[
'=TEKST.SAMENVOEGEN("""Hello "; B1; ""","; " I said.")',
'nl',
'=CONCATENATE("""Hello ", B1, """,", " I said.")',
],
[
'=TEKST.SAMENVOEGEN(JAAR(VANDAAG());
" is ";
ALS(
DAYS(DATUM(JAAR(VANDAAG())+1; 1; 1); DATUM(JAAR(VANDAAG()); 1; 1)) = 365;
"NOT a Leap Year";
"a Leap Year"
)
)',
'nl',
'=CONCATENATE(YEAR(TODAY()),
" is ",
IF(
DAYS(DATE(YEAR(TODAY())+1, 1, 1), DATE(YEAR(TODAY()), 1, 1)) = 365,
"NOT a Leap Year",
"a Leap Year"
)
)',
],
];