Merge pull request #4745 from oleibman/sharedxls

Improve Coverage in Shared
This commit is contained in:
oleibman
2025-12-14 04:43:08 +00:00
committed by GitHub
14 changed files with 118 additions and 40 deletions
@@ -165,11 +165,7 @@ class SpContainer
*/
public function getOPT(int $property): mixed
{
if (isset($this->OPT[$property])) {
return $this->OPT[$property];
}
return null;
return $this->OPT[$property] ?? null;
}
/**
+4 -16
View File
@@ -130,12 +130,7 @@ class File
public static function temporaryFilename(): string
{
$filename = tempnam(self::sysGetTempDir(), 'phpspreadsheet');
if ($filename === false) {
throw new Exception('Could not create temporary file');
}
return $filename;
return tempnam(self::sysGetTempDir(), 'phpspreadsheet') ?: throw new Exception('Could not create temporary file');
}
/**
@@ -143,12 +138,8 @@ class File
*/
public static function assertFile(string $filename, string $zipMember = ''): void
{
if (!is_file($filename)) {
throw new ReaderException('File "' . $filename . '" does not exist.');
}
if (!is_readable($filename)) {
throw new ReaderException('Could not open "' . $filename . '" for reading.');
if (!is_file($filename) || !is_readable($filename)) {
throw new ReaderException('File "' . $filename . '" does not exist or is not readable.');
}
if ($zipMember !== '') {
@@ -168,10 +159,7 @@ class File
*/
public static function testFileNoThrow(string $filename, ?string $zipMember = null): bool
{
if (!is_file($filename)) {
return false;
}
if (!is_readable($filename)) {
if (!is_file($filename) || !is_readable($filename)) {
return false;
}
if ($zipMember === null) {
+1 -4
View File
@@ -233,10 +233,7 @@ class OLE
$path .= '&blockId=' . $blockIdOrPps;
}
$resource = fopen($path, 'rb');
if ($resource === false) {
throw new Exception("Unable to open stream $path");
}
$resource = fopen($path, 'rb') ?: throw new Exception("Unable to open stream $path");
return $resource;
}
+11 -1
View File
@@ -111,6 +111,16 @@ abstract class BestFit
return $this->xValues;
}
/**
* Return the original set of Y-Values.
*
* @return float[] Y-Values
*/
public function getYValues(): array
{
return $this->yValues;
}
/**
* Return the Equation of the best-fit line.
*
@@ -416,7 +426,7 @@ abstract class BestFit
// Define X Values if necessary
if ($xValueCount === 0) {
$xValues = range(1, $yValueCount);
$xValues = range(1.0, $yValueCount);
} elseif ($yValueCount !== $xValueCount) {
// Ensure both arrays of points are the same size
$this->error = true;
@@ -69,7 +69,7 @@ class LinearBestFit extends BestFit
parent::__construct($yValues, $xValues);
if (!$this->error) {
$this->linearRegression($yValues, $xValues, (bool) $const);
$this->linearRegression($this->yValues, $this->xValues, (bool) $const);
}
}
}
+7 -11
View File
@@ -202,7 +202,7 @@ class Xls
* @param int $width Width in pixels
* @param int $height Height in pixels
*
* @return array{startCoordinates: string, startOffsetX: int, startOffsetY: int, endCoordinates: string, endOffsetX: int, endOffsetY: int}
* @return ?array{startCoordinates: string, startOffsetX: float|int, startOffsetY: float|int, endCoordinates: string, endOffsetX: float|int, endOffsetY: float|int}
*/
public static function oneAnchor2twoAnchor(Worksheet $worksheet, string $coordinates, int $offsetX, int $offsetY, int $width, int $height): ?array
{
@@ -241,16 +241,12 @@ class Xls
// Bitmap isn't allowed to start or finish in a hidden cell, i.e. a cell
// with zero height or width.
if (self::sizeCol($worksheet, Coordinate::stringFromColumnIndex($col_start)) == 0) {
return null;
}
if (self::sizeCol($worksheet, Coordinate::stringFromColumnIndex($col_end)) == 0) {
return null;
}
if (self::sizeRow($worksheet, $row_start + 1) == 0) {
return null;
}
if (self::sizeRow($worksheet, $row_end + 1) == 0) {
if (
self::sizeCol($worksheet, Coordinate::stringFromColumnIndex($col_start)) == 0
|| self::sizeCol($worksheet, Coordinate::stringFromColumnIndex($col_end)) == 0
|| self::sizeRow($worksheet, $row_start + 1) == 0
|| self::sizeRow($worksheet, $row_end + 1) == 0
) {
return null;
}
@@ -16,4 +16,12 @@ class DggContainerTest extends TestCase
$bse->setParent($container);
self::assertSame($container, $bse->getParent());
}
public function testGetOpt(): void
{
$dgg = new DggContainer();
self::assertNull($dgg->getOPT(99));
$dgg->setOPT(98, 'whatever');
self::assertSame('whatever', $dgg->getOPT(98));
}
}
@@ -97,7 +97,7 @@ class FileTest extends TestCase
}
self::assertFalse(File::testFileNoThrow($temp));
$this->expectException(ReaderException::class);
$this->expectExceptionMessage('for reading');
$this->expectExceptionMessage('is not readable');
File::assertFile($temp);
}
@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Shared;
use PhpOffice\PhpSpreadsheet\Shared\Escher\DgContainer\SpgrContainer;
use PHPUnit\Framework\TestCase;
class SpgrContainerTest extends TestCase
{
public function testParent(): void
{
$container = new SpgrContainer();
$contained = new SpgrContainer();
$contained->setParent($container);
self::assertSame($container, $contained->getParent());
}
}
@@ -45,6 +45,10 @@ class BestFitTest extends TestCase
$maxType = $type;
}
self::assertEqualsWithDelta(-0.0724, $goodness, self::LBF_PRECISION);
self::assertEqualsWithDelta(0.3116, $result->getValueOfXForY(2.1), self::LBF_PRECISION);
$equation = $result->getEquation();
$match = preg_match('/^Y = 0[.]01765\d+ [*] log[(]2.1205\d+ [*] X[)]$/', $equation);
self::assertSame(1, $match, $equation);
$type = Trend::TREND_POWER;
$result = Trend::calculate($type, $yValues, $xValues);
@@ -54,6 +58,11 @@ class BestFitTest extends TestCase
$maxType = $type;
}
self::assertEqualsWithDelta(0.9946, $goodness, self::LBF_PRECISION);
self::assertEqualsWithDelta(28.0886, $result->getValueOfXForY(10.0), self::LBF_PRECISION);
$equation = $result->getEquation();
$match = preg_match('/^Y = 0[.]1705\d+ [*] X\^1[.]2207\d+$/', $equation);
self::assertSame(1, $match, $equation);
self::assertEqualsWithDelta(0.1705, $result->getIntersect(4), self::LBF_PRECISION);
$type = Trend::TREND_BEST_FIT_NO_POLY;
$result = Trend::calculate($type, $yValues, $xValues);
@@ -9,6 +9,10 @@ use PHPUnit\Framework\TestCase;
class ExponentialBestFitTest extends TestCase
{
private const EBF_PRECISION6 = 1.0E-6;
private const EBF_PRECISION5 = 1.0E-5;
private const DP = 4;
/**
* @param array<mixed> $expectedSlope
* @param array<mixed> $expectedIntersect
@@ -28,6 +32,32 @@ class ExponentialBestFitTest extends TestCase
$bestFit = new ExponentialBestFit($yValues, $xValues);
$slope = $bestFit->getSlope(1);
self::assertEquals($expectedSlope[0], $slope);
self::assertFalse($bestFit->getError());
self::assertEqualsWithDelta(0.2117, $bestFit->getSlopeSE(self::DP), self::EBF_PRECISION5);
self::assertEqualsWithDelta(1.5380, $bestFit->getIntersectSE(self::DP), self::EBF_PRECISION5);
self::assertEqualsWithDelta(
90.486819,
$bestFit->getGoodnessOfFitPercent(),
self::EBF_PRECISION6
);
self::assertEqualsWithDelta(
90.4868,
$bestFit->getGoodnessOfFitPercent(self::DP),
self::EBF_PRECISION5
);
self::assertEqualsWithDelta(2.3031, $bestFit->getStdevOfResiduals(self::DP), self::EBF_PRECISION5);
self::assertEqualsWithDelta(403.6333, $bestFit->getSSRegression(self::DP), self::EBF_PRECISION5);
self::assertEqualsWithDelta(42.4353, $bestFit->getSSResiduals(self::DP), self::EBF_PRECISION5);
self::assertEqualsWithDelta(8, $bestFit->getDFResiduals(self::DP), self::EBF_PRECISION5);
self::assertEqualsWithDelta(76.0938, $bestFit->getF(self::DP), self::EBF_PRECISION5);
self::assertEqualsWithDelta(-13.1, $bestFit->getCovariance(self::DP), self::EBF_PRECISION5);
self::assertEqualsWithDelta(-0.919, $bestFit->getCorrelation(self::DP), self::EBF_PRECISION5);
self::assertEqualsWithDelta(3.51845, $bestFit->getValueOfXForY(10.0), self::EBF_PRECISION5);
$values = $bestFit->getYBestFitValues();
self::assertCount(10, $values);
self::assertEqualsWithDelta(3.965445, $values[0], self::EBF_PRECISION6);
$slope = $bestFit->getSlope();
self::assertEquals($expectedSlope[1], $slope);
$intersect = $bestFit->getIntersect(1);
@@ -26,7 +26,8 @@ class LinearBestFitTest extends TestCase
array $expectedGoodnessOfFit,
mixed $expectedEquation,
array $yValues,
array $xValues
array $xValues,
float $xForY,
): void {
$bestFit = new LinearBestFit($yValues, $xValues);
$slope = $bestFit->getSlope(1);
@@ -43,10 +44,21 @@ class LinearBestFitTest extends TestCase
self::assertSame($expectedGoodnessOfFit[0], $bestFit->getGoodnessOfFit(6));
self::assertSame($expectedGoodnessOfFit[1], $bestFit->getGoodnessOfFit());
self::assertEqualsWithDelta($xForY, $bestFit->getValueOfXForY(0.0), self::LBF_PRECISION);
}
public static function providerLinearBestFit(): array
{
return require 'tests/data/Shared/Trend/LinearBestFit.php';
}
public function testConstructor(): void
{
$bestFit = new LinearBestFit([1, 2, 3], [4, 5]);
self::assertTrue($bestFit->getError());
$bestFit = new LinearBestFit([6.0, 8.0, 10.0]);
self::assertFalse($bestFit->getError());
self::assertSame([6.0, 8.0, 10.0], $bestFit->getYValues());
self::assertSame([1.0, 2.0, 3.0], $bestFit->getXValues());
}
}
@@ -21,6 +21,17 @@ class XlsTest extends TestCase
self::assertSame(0, SharedXls::sizeCol($sheet, 'B'));
self::assertSame(20, SharedXls::sizeRow($sheet, 1));
self::assertSame(0, SharedXls::sizeRow($sheet, 2));
self::assertNull(SharedXls::oneAnchor2twoAnchor($sheet, 'B1', 0, 0, 100, 100));
self::assertNull(SharedXls::oneAnchor2twoAnchor($sheet, 'A2', 0, 0, 100, 100));
$expected = [
'startCoordinates' => 'D9',
'startOffsetX' => 0,
'startOffsetY' => 0,
'endCoordinates' => 'E13',
'endOffsetX' => 576.0,
'endOffsetY' => 256,
];
self::assertSame($expected, SharedXls::oneAnchor2twoAnchor($sheet, 'D9', 0, 0, 100, 100));
$spreadsheet->disconnectWorksheets();
}
}
@@ -10,6 +10,7 @@ return [
'expectedEquation' => 'Y = 14.08 + -1.11 * X',
'yValues' => [3, 10, 3, 6, 8, 12, 1, 4, 9, 14],
'xValues' => [8, 2, 11, 6, 5, 4, 12, 9, 6, 1],
'xForY' => 12.72671756,
],
[
'expectedSlope' => [1.0, 1.0],
@@ -18,5 +19,6 @@ return [
'expectedEquation' => 'Y = -2 + 1 * X',
'yValues' => [1, 2, 3, 4, 5],
'xValues' => [3, 4, 5, 6, 7],
'xForY' => 2.0,
],
];