Skip to content

Commit

Permalink
FactoryPoint tested
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandre-T committed Aug 2, 2024
1 parent 24b2bc3 commit 429da27
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/LongitudeOne/SpatialTypes/Factory/FactoryPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class FactoryPoint
*/
public static function fromCoordinates(float|int|string $x, float|int|string $y, null|float|int $z = null, null|\DateTimeInterface|float|int $m = null, ?int $srid = null, FamilyEnum $family = FamilyEnum::GEOMETRY, DimensionEnum $dimension = DimensionEnum::X_Y): PointInterface
{
if (DimensionEnum::X_Y === $dimension && !(empty($m) && empty($z))) {
throw new InvalidDimensionException('The third and fourth dimensions are not supported for two-dimensions points. Did you miss the 7th parameter DimensionEnum?');
}

if (DimensionEnum::X_Y === $dimension) {
if (FamilyEnum::GEOGRAPHY === $family) {
return new GeographicPoint($x, $y, $srid);
Expand Down Expand Up @@ -80,6 +84,14 @@ public static function fromIndexedArray(
): PointInterface {
$dimensionHelper = new DimensionHelper($dimension);

if (count($point) < 2) {
throw new MissingValueException('The array must contain at least two coordinates to create a point.');
}

if (count($point) > 4) {
throw new InvalidDimensionException('The array must contain at most four coordinates.');
}

if (!isset($point[0])) {
throw new MissingValueException('The first coordinate of array is missing.');
}
Expand Down
15 changes: 15 additions & 0 deletions quality/php-stan/phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
parameters:
ignoreErrors:
-
message: "#^Comparison operation \"\\<\" between int\\<2, 4\\> and 2 is always false\\.$#"
count: 1
path: ../../lib/LongitudeOne/SpatialTypes/Factory/FactoryPoint.php

-
message: "#^Comparison operation \"\\>\" between int\\<2, 4\\> and 4 is always false\\.$#"
count: 1
path: ../../lib/LongitudeOne/SpatialTypes/Factory/FactoryPoint.php

-
message: "#^Offset 0 on array\\{0\\: float\\|int\\|string, 1\\: float\\|int\\|string, 2\\?\\: float\\|int\\|null, 3\\?\\: DateTimeInterface\\|float\\|int\\|null\\} in isset\\(\\) always exists and is not nullable\\.$#"
count: 1
Expand All @@ -24,3 +34,8 @@ parameters:
message: "#^Parameter \\#1 \\$points of class LongitudeOne\\\\SpatialTypes\\\\Types\\\\Geometry\\\\MultiPoint constructor expects array\\<array\\{0\\: float\\|int\\|string, 1\\: float\\|int\\|string, 2\\?\\: float\\|int\\|null, 3\\?\\: DateTimeInterface\\|float\\|int\\|null\\}\\|LongitudeOne\\\\SpatialTypes\\\\Interfaces\\\\PointInterface\\>, array\\{1, 2, 3, 4\\} given\\.$#"
count: 1
path: ../../tests/LongitudeOne/SpatialTypes/Tests/Old/Types/Geometry/MultiPointTest.php

-
message: "#^Parameter \\#1 \\$point of static method LongitudeOne\\\\SpatialTypes\\\\Factory\\\\FactoryPoint\\:\\:fromIndexedArray\\(\\) expects array\\{0\\: float\\|int\\|string, 1\\: float\\|int\\|string, 2\\?\\: float\\|int\\|null, 3\\?\\: DateTimeInterface\\|float\\|int\\|null\\}, array\\<int\\> given\\.$#"
count: 1
path: ../../tests/LongitudeOne/SpatialTypes/Tests/Unit/Factory/FactoryPointTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php
/**
* This file is part of the spatial project.
*
* PHP 8.1 | 8.2 | 8.3
*
* Copyright Alexandre Tranchant <[email protected]> 2024
* Copyright Longitude One 2024
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/

declare(strict_types=1);

namespace LongitudeOne\SpatialTypes\Tests\Unit\Factory;

use LongitudeOne\SpatialTypes\Enum\DimensionEnum;
use LongitudeOne\SpatialTypes\Enum\FamilyEnum;
use LongitudeOne\SpatialTypes\Enum\TypeEnum;
use LongitudeOne\SpatialTypes\Exception\InvalidDimensionException;
use LongitudeOne\SpatialTypes\Exception\MissingValueException;
use LongitudeOne\SpatialTypes\Exception\SpatialTypeExceptionInterface;
use LongitudeOne\SpatialTypes\Factory\FactoryPoint;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

/**
* @internal
*
* @coversNothing
*/
class FactoryPointTest extends TestCase
{
/**
* Provide bad values to test the factory.
*
* @return \Generator<string, array{0: int[], 1: class-string<SpatialTypeExceptionInterface>, 2: string}, null, void>
*/
public static function provideBadValues(): \Generator
{
yield 'Empty array' => [
[],
MissingValueException::class,
'The array must contain at least two coordinates to create a point.',
];

yield 'One value' => [
[1],
MissingValueException::class,
'The array must contain at least two coordinates to create a point.',
];

yield 'Three values' => [
[1, 2, 3],
InvalidDimensionException::class,
'The third and fourth dimensions are not supported for two-dimensions points. Did you miss the 7th parameter DimensionEnum?',
];

yield 'Four values' => [
[1, 2, 3, 4],
InvalidDimensionException::class,
'The third and fourth dimensions are not supported for two-dimensions points. Did you miss the 7th parameter DimensionEnum?',
];

yield 'Five values' => [
[1, 2, 3, 4, 5],
InvalidDimensionException::class,
'The array must contain at most four coordinates.',
];
}

/**
* Test the factory with some good coordinates.
*/
public function testFromCoordinates(): void
{
$point = FactoryPoint::fromCoordinates(1, 2);
static::assertSame(1, $point->getX());
static::assertSame(2, $point->getY());
static::assertFalse($point->hasM());
static::assertFalse($point->hasZ());
static::assertSame(FamilyEnum::GEOMETRY, $point->getFamily());
static::assertSame(TypeEnum::POINT->value, $point->getType());

$point = FactoryPoint::fromCoordinates(42.1, 42.2, null, null, 4326, FamilyEnum::GEOGRAPHY, DimensionEnum::X_Y);
static::assertSame(42.1, $point->getX());
static::assertSame(42.2, $point->getY());
static::assertFalse($point->hasM());
static::assertFalse($point->hasZ());
static::assertSame(FamilyEnum::GEOGRAPHY, $point->getFamily());
static::assertSame(TypeEnum::POINT->value, $point->getType());
}

/**
* Test the factory with some bad dimension.
*/
public function testFromCoordinatesWithBadDimension(): void
{
self::expectException(InvalidDimensionException::class);
self::expectExceptionMessage('The third and fourth dimensions are not supported for two-dimensions points. Did you miss the 7th parameter DimensionEnum?');
FactoryPoint::fromCoordinates(1, 2, 3, 4);
}

/**
* Test the factory with some good coordinates in an array.
*/
public function testFromIndexedArray(): void
{
$point = FactoryPoint::fromIndexedArray([1, 2]);
static::assertSame(1, $point->getX());
static::assertSame(2, $point->getY());
static::assertFalse($point->hasM());
static::assertFalse($point->hasZ());
static::assertSame(FamilyEnum::GEOMETRY, $point->getFamily());
static::assertSame(TypeEnum::POINT->value, $point->getType());

$point = FactoryPoint::fromIndexedArray([42.1, 42.2, null, null], 4326, FamilyEnum::GEOGRAPHY, DimensionEnum::X_Y);
static::assertSame(42.1, $point->getX());
static::assertSame(42.2, $point->getY());
static::assertFalse($point->hasM());
static::assertFalse($point->hasZ());
static::assertSame(FamilyEnum::GEOGRAPHY, $point->getFamily());
static::assertSame(TypeEnum::POINT->value, $point->getType());
}

// phpcs:disable Squiz.Commenting.FunctionComment.IncorrectTypeHint

/**
* Test the factory with some bad values in an array.
*
* @param int[] $values The values to test
* @param class-string<SpatialTypeExceptionInterface> $exceptedException The expected exception
* @param string $expectedMessage The expected message
*/
#[DataProvider('provideBadValues')]
public function testFromIndexedArrayWithBadValues(array $values, string $exceptedException, string $expectedMessage): void
{
self::expectException($exceptedException);
self::expectExceptionMessage($expectedMessage);
FactoryPoint::fromIndexedArray($values);
}
}

0 comments on commit 429da27

Please sign in to comment.