PhpSpreadsheet/tests/PhpSpreadsheetTests/Calculation/Functions/Engineering/Hex2OctTest.php

117 lines
3.7 KiB
PHP

<?php
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Engineering;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PHPUnit\Framework\TestCase;
class Hex2OctTest extends TestCase
{
/**
* @var string
*/
private $compatibilityMode;
protected function setUp(): void
{
$this->compatibilityMode = Functions::getCompatibilityMode();
}
protected function tearDown(): void
{
Functions::setCompatibilityMode($this->compatibilityMode);
}
/**
* @dataProvider providerHEX2OCT
*
* @param mixed $expectedResult
* @param mixed $formula
*/
public function testHEX2OCT($expectedResult, $formula): void
{
if ($expectedResult === 'exception') {
$this->expectException(CalcExp::class);
}
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A2', 'B');
$sheet->getCell('A1')->setValue("=HEX2OCT($formula)");
$result = $sheet->getCell('A1')->getCalculatedValue();
self::assertEquals($expectedResult, $result);
}
public function providerHEX2OCT(): array
{
return require 'tests/data/Calculation/Engineering/HEX2OCT.php';
}
/**
* @dataProvider providerHEX2OCT
*
* @param mixed $expectedResult
* @param mixed $formula
*/
public function testHEX2OCTOds($expectedResult, $formula): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE);
if ($formula === 'true') {
$expectedResult = 1;
} elseif ($formula === 'false') {
$expectedResult = 0;
}
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A2', 'B');
$sheet->getCell('A1')->setValue("=HEX2OCT($formula)");
$result = $sheet->getCell('A1')->getCalculatedValue();
self::assertEquals($expectedResult, $result);
}
public function testHEX2OCTFrac(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
Functions::setCompatibilityMode(Functions::COMPATIBILITY_GNUMERIC);
$cell = 'G1';
$sheet->setCellValue($cell, '=HEX2OCT(10.1)');
self::assertEquals(20, $sheet->getCell($cell)->getCalculatedValue());
$cell = 'F21';
$sheet->setCellValue($cell, '=HEX2OCT("A.1")');
self::assertEquals('#NUM!', $sheet->getCell($cell)->getCalculatedValue());
Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE);
$cell = 'O1';
$sheet->setCellValue($cell, '=HEX2OCT(10.1)');
self::assertEquals('#NUM!', $sheet->getCell($cell)->getCalculatedValue());
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
$cell = 'E1';
$sheet->setCellValue($cell, '=HEX2OCT(10.1)');
self::assertEquals('#NUM!', $sheet->getCell($cell)->getCalculatedValue(), 'Excel');
}
/**
* @dataProvider providerHex2OctArray
*/
public function testHex2OctArray(array $expectedResult, string $value): void
{
$calculation = Calculation::getInstance();
$formula = "=HEX2OCT({$value})";
$result = $calculation->_calculateFormulaValue($formula);
self::assertEquals($expectedResult, $result);
}
public function providerHex2OctArray(): array
{
return [
'row/column vector' => [
[['4', '7', '77', '231', '314', '525']],
'{"4", "7", "3F", "99", "CC", "155"}',
],
];
}
}