Skip to content

Commit

Permalink
Add integration test for My & Maria
Browse files Browse the repository at this point in the history
  • Loading branch information
tontonsb committed May 26, 2024
1 parent 4425f23 commit 9b5662b
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 1 deletion.
55 changes: 54 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,59 @@ jobs:
- name: Run tests
run: composer test

mariadb-test:
runs-on: ubuntu-latest

name: Test with MariaDB

services:
postgis:
image: mariadb:10
env:
MARIADB_ALLOW_EMPTY_ROOT_PASSWORD: yes
MARIADB_DATABASE: sfa
MARIADB_USER: sfa
MARIADB_PASSWORD: sfa
ports:
- 3306:3306

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Install dependencies
uses: ramsey/composer-install@v2

- name: Run tests
run: composer test-maria


mysql-test:
runs-on: ubuntu-latest

name: Test with Mysql

services:
postgis:
image: mysql:8
env:
MYSQL_DATABASE: sfa
MYSQL_USER: sfa
MYSQL_PASSWORD: sfa
ports:
- 3306:3306

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Install dependencies
uses: ramsey/composer-install@v2

- name: Run tests
run: composer test-my


postgis-test:
runs-on: ubuntu-latest

Expand All @@ -49,7 +102,7 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Install dependencies
uses: ramsey/composer-install@v2
Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
},
"scripts": {
"test": "XDEBUG_MODE=coverage phpunit --testsuite=DatabaseLess --coverage-text --colors --testdox",
"test-maria": "phpunit --testsuite=MariaIntegration --colors --testdox",
"test-my": "phpunit --testsuite=MyIntegration --colors --testdox",
"test-pg": "phpunit --testsuite=PGIntegration --colors --testdox",
"fix": "php-cs-fixer fix",
"cs": "@fix --dry-run --diff"
Expand Down
6 changes: 6 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
<testsuite name="SQL">
<directory suffix="Test.php">./tests/SQL</directory>
</testsuite>
<testsuite name="MariaIntegration">
<directory suffix="Test.php">./tests/Integration/MariaDB</directory>
</testsuite>
<testsuite name="MyIntegration">
<directory suffix="Test.php">./tests/Integration/MySQL</directory>
</testsuite>
<testsuite name="PGIntegration">
<directory suffix="Test.php">./tests/Integration/PostGIS</directory>
</testsuite>
Expand Down
92 changes: 92 additions & 0 deletions tests/Integration/MariaDB/PDOTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace GlaivePro\SF\Tests\Integration\MariaDB;

use GlaivePro\SF\Expression;
use GlaivePro\SF\MariaDB\Geometry;
use GlaivePro\SF\MariaDB\Point;
use GlaivePro\SF\MariaDB\Sfc;
use PDO;
use PHPUnit\Framework\TestCase;

class PDOTest extends TestCase
{
protected \PDO $pdo;

protected function setUp(): void
{
parent::setUp();

$dsn = "pgsql:host=localhost;port=5432;dbname=sfa;";

$this->pdo = new PDO($dsn, 'sfa', 'sfa', [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]);
}

public function testConstructors(): void
{
$point = Sfc::point(23, 56, 4326);

$this->assertEquals(23, $this->selectVal($point->x()));
$this->assertEquals(56, $this->selectVal($point->y()));
$this->assertEquals(4326, $this->selectVal($point->srid()));
}

/**
* Let's produce a triangular hull around some points and check whether
* other geometries are inside the hull.
*/
public function testComplexActions(): void
{
// Let's try various APIs
$points = [
Sfc::makePoint(0, 0),
new Point('ST_MakePoint(?, ?)', [0, 10]),
new Geometry("'POINT(5 5)'::geometry"),
];

$hull = $points[0]->union($points[1])->union($points[2])->convexHull();

$this->assertSame('POLYGON((0 0,0 10,5 5,0 0))', $this->selectVal($hull->asText()));

$pointInside = Sfc::point(3, 5);
$pointOutside = Sfc::point(10, 10);
$this->assertTrue($this->selectVal(
$hull->contains($pointInside)
));
$this->assertTrue($this->selectVal(
$pointInside->within($hull)
));
$this->assertFalse($this->selectVal(
$hull->contains($pointOutside)
));
$this->assertFalse($this->selectVal(
$pointOutside->within($hull)
));

$line = Sfc::lineFromText('LINESTRING(3 3,10 10)');
$this->assertFalse($this->selectVal(
$hull->contains($line)
));
$this->assertFalse($this->selectVal(
$line->within($hull)
));
$this->assertTrue($this->selectVal(
$hull->intersects($line)
));
$this->assertTrue($this->selectVal(
$line->intersects($hull)
));
}


protected function selectVal(Expression $expression)
{
$query = $this->pdo->prepare("SELECT $expression as value");
$query->execute($expression->bindings);

return $query->fetch()['value'];
}
}
92 changes: 92 additions & 0 deletions tests/Integration/MySQL/PDOTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace GlaivePro\SF\Tests\Integration\MySQL;

use GlaivePro\SF\Expression;
use GlaivePro\SF\MySQL\Geometry;
use GlaivePro\SF\MySQL\Point;
use GlaivePro\SF\MySQL\Sfc;
use PDO;
use PHPUnit\Framework\TestCase;

class PDOTest extends TestCase
{
protected \PDO $pdo;

protected function setUp(): void
{
parent::setUp();

$dsn = "mysql:host=localhost;port=3306;dbname=sfa;";

$this->pdo = new PDO($dsn, 'sfa', 'sfa', [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]);
}

public function testConstructors(): void
{
$point = Sfc::point(23, 56, 4326);

$this->assertEquals(23, $this->selectVal($point->x()));
$this->assertEquals(56, $this->selectVal($point->y()));
$this->assertEquals(4326, $this->selectVal($point->srid()));
}

/**
* Let's produce a triangular hull around some points and check whether
* other geometries are inside the hull.
*/
public function testComplexActions(): void
{
// Let's try various APIs
$points = [
Sfc::makePoint(0, 0),
new Point('ST_MakePoint(?, ?)', [0, 10]),
new Geometry("'POINT(5 5)'::geometry"),
];

$hull = $points[0]->union($points[1])->union($points[2])->convexHull();

$this->assertSame('POLYGON((0 0,0 10,5 5,0 0))', $this->selectVal($hull->asText()));

$pointInside = Sfc::point(3, 5);
$pointOutside = Sfc::point(10, 10);
$this->assertTrue($this->selectVal(
$hull->contains($pointInside)
));
$this->assertTrue($this->selectVal(
$pointInside->within($hull)
));
$this->assertFalse($this->selectVal(
$hull->contains($pointOutside)
));
$this->assertFalse($this->selectVal(
$pointOutside->within($hull)
));

$line = Sfc::lineFromText('LINESTRING(3 3,10 10)');
$this->assertFalse($this->selectVal(
$hull->contains($line)
));
$this->assertFalse($this->selectVal(
$line->within($hull)
));
$this->assertTrue($this->selectVal(
$hull->intersects($line)
));
$this->assertTrue($this->selectVal(
$line->intersects($hull)
));
}


protected function selectVal(Expression $expression)
{
$query = $this->pdo->prepare("SELECT $expression as value");
$query->execute($expression->bindings);

return $query->fetch()['value'];
}
}

0 comments on commit 9b5662b

Please sign in to comment.