Skip to content

Commit

Permalink
Passes ShapeType instead of int and adds a test case for ShapeType::U…
Browse files Browse the repository at this point in the history
…nknown
  • Loading branch information
JoolsMcFly committed Oct 10, 2024
1 parent ebfc8fd commit 5bbba9a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
3 changes: 1 addition & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ parameters:

-
message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertEquals\\(\\)\\.$#"
count: 17
count: 18
path: tests/ShapeFileTest.php

-
Expand Down Expand Up @@ -159,4 +159,3 @@ parameters:
message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertEquals\\(\\)\\.$#"
count: 1
path: tests/UtilTest.php

6 changes: 5 additions & 1 deletion src/ShapeFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,11 @@ private function loadHeaders(): bool
$this->readSHP(4);

$shapeType = Util::loadData('V', $this->readSHP(4));
$this->shapeType = ShapeType::tryFrom((int) $shapeType) ?? ShapeType::Unknown;
if ($shapeType === false) {
$this->shapeType = ShapeType::Unknown;
} else {
$this->shapeType = ShapeType::tryFrom((int) $shapeType) ?? ShapeType::Unknown;
}

$this->boundingBox = [];
$this->boundingBox['xmin'] = Util::loadData('d', $this->readSHP(8));
Expand Down
39 changes: 20 additions & 19 deletions tests/ShapeFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ public function testShapeName(): void
$this->assertEquals('Point', $obj->getShapeName());
$obj = new ShapeRecord(ShapeType::Null);
$this->assertEquals('Null Shape', $obj->getShapeName());
$obj = new ShapeRecord(ShapeType::Unknown);
$this->assertEquals('Unknown Shape', $obj->getShapeName());
}

/**
Expand All @@ -314,14 +316,13 @@ public function testShapeName(): void
*
* @dataProvider shapesProvider
*/
public function testShapeSaveLoad(int $shapeType, array $points): void
public function testShapeSaveLoad(ShapeType $shapeType, array $points): void
{
$shapeEnum = ShapeType::tryFrom($shapeType) ?? ShapeType::Unknown;
$filename = './data/test_shape-' . $shapeType . '.*';
$shp = new ShapeFile($shapeEnum);
$filename = './data/test_shape-' . $shapeType->value . '.*';
$shp = new ShapeFile($shapeType);
$shp->setDBFHeader([['ID', 'N', 19, 0], ['DESC', 'C', 14, 0]]);

$record0 = new ShapeRecord($shapeEnum);
$record0 = new ShapeRecord($shapeType);

foreach ($points as $point) {
$record0->addPoint($point[0], $point[1]);
Expand All @@ -331,7 +332,7 @@ public function testShapeSaveLoad(int $shapeType, array $points): void

$shp->saveToFile($filename);

$shp2 = new ShapeFile($shapeEnum);
$shp2 = new ShapeFile($shapeType);
$shp2->loadFromFile($filename);

$this->assertEquals(count($shp->records), count($shp2->records));
Expand All @@ -355,7 +356,7 @@ public function testShapeSaveLoad(int $shapeType, array $points): void
/**
* Data provider for save/load testing.
*
* @psalm-return list<array{int, list<array{mixed[], int}>}>
* @psalm-return list<array{ShapeType, list<array{mixed[], int}>}>
*/
public static function shapesProvider(): array
{
Expand Down Expand Up @@ -384,18 +385,18 @@ public static function shapesProvider(): array
];

return [
[ShapeType::Point->value, $pointsForPointType],
[ShapeType::PolyLine->value, $pointsForPolyLineType],
[ShapeType::Polygon->value, $pointsForPolygonType],
[ShapeType::MultiPoint->value, $pointsForMultiPointType],
[ShapeType::PointZ->value, $pointsForPointType],
[ShapeType::PolyLineZ->value, $pointsForPolyLineType],
[ShapeType::PolygonZ->value, $pointsForPolygonType],
[ShapeType::MultiPointZ->value, $pointsForMultiPointType],
[ShapeType::PointM->value, $pointsForPointType],
[ShapeType::PolyLineM->value, $pointsForPolyLineType],
[ShapeType::PolygonM->value, $pointsForPolygonType],
[ShapeType::MultiPointM->value, $pointsForMultiPointType],
[ShapeType::Point, $pointsForPointType],
[ShapeType::PolyLine, $pointsForPolyLineType],
[ShapeType::Polygon, $pointsForPolygonType],
[ShapeType::MultiPoint, $pointsForMultiPointType],
[ShapeType::PointZ, $pointsForPointType],
[ShapeType::PolyLineZ, $pointsForPolyLineType],
[ShapeType::PolygonZ, $pointsForPolygonType],
[ShapeType::MultiPointZ, $pointsForMultiPointType],
[ShapeType::PointM, $pointsForPointType],
[ShapeType::PolyLineM, $pointsForPolyLineType],
[ShapeType::PolygonM, $pointsForPolygonType],
[ShapeType::MultiPointM, $pointsForMultiPointType],
];
}

Expand Down

0 comments on commit 5bbba9a

Please sign in to comment.