Skip to content

Commit 0bbc6d7

Browse files
committed
test(mssql): add support for running tests against ms sql server
1 parent 2b1be88 commit 0bbc6d7

16 files changed

+277
-6
lines changed

composer.json

+11-2
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,26 @@
4444
"test": [
4545
"phpunit --configuration phpunit.mysql5.7.xml",
4646
"phpunit --configuration phpunit.mysql8.0.xml",
47-
"phpunit --configuration phpunit.pgsql.xml"
47+
"phpunit --configuration phpunit.pgsql.xml",
48+
"phpunit --configuration phpunit.mssql2017.xml",
49+
"phpunit --configuration phpunit.mssql2019.xml",
50+
"phpunit --configuration phpunit.mssql2022.xml"
4851
],
4952
"test-coverage": [
5053
"phpunit --configuration phpunit.mysql5.7.xml --coverage-php .phpunit.cache/code-coverage/mysql5.7.cov",
5154
"phpunit --configuration phpunit.mysql8.0.xml --coverage-php .phpunit.cache/code-coverage/mysql8.0.cov",
5255
"phpunit --configuration phpunit.pgsql.xml --coverage-php .phpunit.cache/code-coverage/pgsql.cov",
56+
"phpunit --configuration phpunit.mssql2017.xml --coverage-php .phpunit.cache/code-coverage/mssql2017.cov",
57+
"phpunit --configuration phpunit.mssql2019.xml --coverage-php .phpunit.cache/code-coverage/mssql2019.cov",
58+
"phpunit --configuration phpunit.mssql2022.xml --coverage-php .phpunit.cache/code-coverage/mssql2022.cov",
5359
"phpcov merge --clover .phpunit.cache/code-coverage/clover.xml .phpunit.cache/code-coverage/"
5460
],
5561
"test-mysql5.7": "phpunit --configuration phpunit.mysql5.7.xml",
5662
"test-mysql8.0": "phpunit --configuration phpunit.mysql8.0.xml",
57-
"test-pgsql": "phpunit --configuration phpunit.pgsql.xml"
63+
"test-pgsql": "phpunit --configuration phpunit.pgsql.xml",
64+
"test-mssql2017": "phpunit --configuration phpunit.mssql2017.xml",
65+
"test-mssql2019": "phpunit --configuration phpunit.mssql2019.xml",
66+
"test-mssql2022": "phpunit --configuration phpunit.mssql2022.xml"
5867
},
5968
"autoload": {
6069
"psr-0": {

docker/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ docker exec spatial-php8 cp docker/phpunit*.xml .
1919
docker exec spatial-php8 composer test-mysql5
2020
docker exec spatial-php8 composer test-mysql8
2121
docker exec spatial-php8 composer test-pgsql
22+
docker exec spatial-php8 composer test-mssql2017
23+
docker exec spatial-php8 composer test-mssql2019
24+
docker exec spatial-php8 composer test-mssql2022

docker/docker-compose.yaml

+37
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,43 @@ services:
4040
MYSQL_PORT: 3306
4141
ports:
4242
- "3380:3306"
43+
database-mssql2017:
44+
build:
45+
context: ./mssql
46+
args:
47+
IMAGE_TAG: "2017-latest"
48+
USER_NAME: "root"
49+
container_name: "spatial-mssql2017"
50+
restart: always
51+
environment:
52+
ACCEPT_EULA: Y
53+
MSSQL_SA_PASSWORD: mainExtendedToMoreThan8Chars
54+
ports:
55+
- "1433:1433"
56+
database-mssql2019:
57+
build:
58+
context: ./mssql
59+
args:
60+
IMAGE_TAG: "2019-latest"
61+
container_name: "spatial-mssql2019"
62+
restart: always
63+
environment:
64+
ACCEPT_EULA: Y
65+
MSSQL_SA_PASSWORD: mainExtendedToMoreThan8Chars
66+
ports:
67+
- "1434:1433"
68+
database-mssql2022:
69+
build:
70+
context: ./mssql
71+
args:
72+
IMAGE_TAG: "2022-latest"
73+
container_name: "spatial-mssql2022"
74+
restart: always
75+
environment:
76+
ACCEPT_EULA: Y
77+
MSSQL_SA_PASSWORD: mainExtendedToMoreThan8Chars
78+
ports:
79+
- "1435:1433"
4380
service_doc:
4481
container_name: "spatial-doc"
4582
build:

docker/mssql/Dockerfile

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
ARG IMAGE_TAG
2+
ARG USER_NAME=mssql
3+
FROM mcr.microsoft.com/mssql/server:${IMAGE_TAG}
4+
USER root
5+
RUN mkdir -p /usr/src/app
6+
WORKDIR /usr/src/app
7+
COPY usr/src/app .
8+
RUN chmod +x run-initialization.sh entrypoint.sh
9+
USER ${USER_NAME}
10+
EXPOSE 1433
11+
12+
CMD /bin/bash ./entrypoint.sh
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CREATE DATABASE main;
2+
GO
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/usr/src/app/run-initialization.sh & /opt/mssql/bin/sqlservr
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Wait to be sure that SQL Server came up
2+
sleep 90s
3+
4+
# Run the setup script to create the DB
5+
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P "${MSSQL_SA_PASSWORD:-notProvided}" -d master -i create-database.sql

docker/php8/Dockerfile

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
FROM php:8.1-fpm-alpine
22

3-
RUN apk --update --no-cache add bash git postgresql-dev mysql-dev autoconf gcc make g++ \
4-
&& docker-php-ext-install pdo_pgsql pdo_mysql \
5-
&& pecl install pcov \
6-
&& docker-php-ext-enable pcov pdo_pgsql pdo_mysql
3+
#Install the package(s)
4+
#for Microsoft ODBC driver for SQL Server see https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-linux-ver15#alpine-linux
5+
RUN apk --update --no-cache add bash git postgresql-dev mysql-dev unixodbc-dev autoconf gcc make g++ &&\
6+
cd /tmp && \
7+
curl -O https://download.microsoft.com/download/3/5/5/355d7943-a338-41a7-858d-53b259ea33f5/msodbcsql18_18.3.2.1-1_amd64.apk && \
8+
curl -O https://download.microsoft.com/download/3/5/5/355d7943-a338-41a7-858d-53b259ea33f5/mssql-tools18_18.3.1.1-1_amd64.apk && \
9+
apk add --allow-untrusted msodbcsql18_18.3.2.1-1_amd64.apk && \
10+
apk add --allow-untrusted mssql-tools18_18.3.1.1-1_amd64.apk && \
11+
docker-php-ext-install pdo_pgsql pdo_mysql && \
12+
pecl install pcov pdo_sqlsrv-5.11.1 sqlsrv-5.11.1 && \
13+
docker-php-ext-enable pcov pdo_pgsql pdo_mysql pdo_sqlsrv sqlsrv
714

815
#Install composer
916
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer

docker/phpunit.mssql2017.xml

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
4+
bootstrap="tests/LongitudeOne/Spatial/Tests/TestInit.php" executionOrder="depends,defects" stopOnFailure="true"
5+
displayDetailsOnTestsThatTriggerDeprecations="true"
6+
displayDetailsOnTestsThatTriggerWarnings="true"
7+
displayDetailsOnTestsThatTriggerErrors="true"
8+
stopOnError="true" stopOnWarning="true" stopOnDefect="true" cacheDirectory=".phpunit.cache">
9+
<testsuites>
10+
<testsuite name="default">
11+
<directory>tests/LongitudeOne/Spatial/Tests</directory>
12+
</testsuite>
13+
</testsuites>
14+
<groups>
15+
<include>
16+
<group>php</group>
17+
<group>dql</group>
18+
<group>geometry</group>
19+
<group>geography</group>
20+
<group>srid</group>
21+
<group>pgsql-only</group>
22+
<group>issue</group>
23+
<group>mssql-only</group>
24+
</include>
25+
<exclude>
26+
<group>mysql-only</group>
27+
<group>pgsql-only</group>
28+
</exclude>
29+
</groups>
30+
<php>
31+
<var name="db_type" value="pdo_sqlsrv"/>
32+
<var name="db_host" value="spatial-mssql2017"/>
33+
<var name="db_username" value="sa"/>
34+
<var name="db_password" value="mainExtendedToMoreThan8Chars"/>
35+
<var name="db_name" value="main"/>
36+
<var name="db_port" value="1433"/>
37+
<var name="db_driver_options" value="TrustServerCertificate=1"/>
38+
<!-- mssql cannot drop current database, -->
39+
<!-- Also, we connect on the alternate database, then we drop the main database -->
40+
<var name="db_alternate" value="master"/>
41+
<!-- Select timezone for log -->
42+
<var name="opt_log_timezone" value="Europe/Paris"/>
43+
<!-- Select the log level : debug to get each request, info to get each starting test and disconnection -->
44+
<var name="opt_log_level" value="debug"/>
45+
<!-- Select the directory to store the log files -->
46+
<var name="opt_log_dir" value=".phpunit.cache/logs"/>
47+
<!-- Select the log file name -->
48+
<var name="opt_log_file" value="mssql2017.log"/>
49+
</php>
50+
<source>
51+
<include>
52+
<directory suffix=".php">lib/</directory>
53+
</include>
54+
</source>
55+
</phpunit>

docker/phpunit.mssql2019.xml

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
4+
bootstrap="tests/LongitudeOne/Spatial/Tests/TestInit.php" executionOrder="depends,defects" stopOnFailure="true"
5+
displayDetailsOnTestsThatTriggerDeprecations="true"
6+
displayDetailsOnTestsThatTriggerWarnings="true"
7+
displayDetailsOnTestsThatTriggerErrors="true"
8+
stopOnError="true" stopOnWarning="true" stopOnDefect="true" cacheDirectory=".phpunit.cache">
9+
<testsuites>
10+
<testsuite name="default">
11+
<directory>tests/LongitudeOne/Spatial/Tests</directory>
12+
</testsuite>
13+
</testsuites>
14+
<groups>
15+
<include>
16+
<group>php</group>
17+
<group>dql</group>
18+
<group>geometry</group>
19+
<group>geography</group>
20+
<group>srid</group>
21+
<group>pgsql-only</group>
22+
<group>issue</group>
23+
<group>mssql-only</group>
24+
</include>
25+
<exclude>
26+
<group>mysql-only</group>
27+
<group>pgsql-only</group>
28+
</exclude>
29+
</groups>
30+
<php>
31+
<var name="db_type" value="pdo_sqlsrv"/>
32+
<var name="db_host" value="spatial-mssql2019"/>
33+
<var name="db_username" value="sa"/>
34+
<var name="db_password" value="mainExtendedToMoreThan8Chars"/>
35+
<var name="db_name" value="main"/>
36+
<var name="db_port" value="1433"/>
37+
<var name="db_driver_options" value="TrustServerCertificate=1"/>
38+
<!-- mssql cannot drop current database, -->
39+
<!-- Also, we connect on the alternate database, then we drop the main database -->
40+
<var name="db_alternate" value="master"/>
41+
<!-- Select timezone for log -->
42+
<var name="opt_log_timezone" value="Europe/Paris"/>
43+
<!-- Select the log level : debug to get each request, info to get each starting test and disconnection -->
44+
<var name="opt_log_level" value="debug"/>
45+
<!-- Select the directory to store the log files -->
46+
<var name="opt_log_dir" value=".phpunit.cache/logs"/>
47+
<!-- Select the log file name -->
48+
<var name="opt_log_file" value="mssql2019.log"/>
49+
</php>
50+
<source>
51+
<include>
52+
<directory suffix=".php">lib/</directory>
53+
</include>
54+
</source>
55+
</phpunit>

docker/phpunit.mssql2022.xml

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
4+
bootstrap="tests/LongitudeOne/Spatial/Tests/TestInit.php" executionOrder="depends,defects" stopOnFailure="true"
5+
displayDetailsOnTestsThatTriggerDeprecations="true"
6+
displayDetailsOnTestsThatTriggerWarnings="true"
7+
displayDetailsOnTestsThatTriggerErrors="true"
8+
stopOnError="true" stopOnWarning="true" stopOnDefect="true" cacheDirectory=".phpunit.cache">
9+
<testsuites>
10+
<testsuite name="default">
11+
<directory>tests/LongitudeOne/Spatial/Tests</directory>
12+
</testsuite>
13+
</testsuites>
14+
<groups>
15+
<include>
16+
<group>php</group>
17+
<group>dql</group>
18+
<group>geometry</group>
19+
<group>geography</group>
20+
<group>srid</group>
21+
<group>pgsql-only</group>
22+
<group>issue</group>
23+
<group>mssql-only</group>
24+
</include>
25+
<exclude>
26+
<group>mysql-only</group>
27+
<group>pgsql-only</group>
28+
</exclude>
29+
</groups>
30+
<php>
31+
<var name="db_type" value="pdo_sqlsrv"/>
32+
<var name="db_host" value="spatial-mssql2022"/>
33+
<var name="db_username" value="sa"/>
34+
<var name="db_password" value="mainExtendedToMoreThan8Chars"/>
35+
<var name="db_name" value="main"/>
36+
<var name="db_port" value="1433"/>
37+
<var name="db_driver_options" value="TrustServerCertificate=1"/>
38+
<!-- mssql cannot drop current database, -->
39+
<!-- Also, we connect on the alternate database, then we drop the main database -->
40+
<var name="db_alternate" value="master"/>
41+
<!-- Select timezone for log -->
42+
<var name="opt_log_timezone" value="Europe/Paris"/>
43+
<!-- Select the log level : debug to get each request, info to get each starting test and disconnection -->
44+
<var name="opt_log_level" value="debug"/>
45+
<!-- Select the directory to store the log files -->
46+
<var name="opt_log_dir" value=".phpunit.cache/logs"/>
47+
<!-- Select the log file name -->
48+
<var name="opt_log_file" value="mssql2022.log"/>
49+
</php>
50+
<source>
51+
<include>
52+
<directory suffix=".php">lib/</directory>
53+
</include>
54+
</source>
55+
</phpunit>

docker/phpunit.mysql5.7.xml

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
</include>
2323
<exclude>
2424
<group>pgsql-only</group>
25+
<group>mssql-only</group>
2526
<group>srid</group>
2627
<group>deprecation</group>
2728
</exclude>

docker/phpunit.mysql8.0.xml

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
</include>
2323
<exclude>
2424
<group>pgsql-only</group>
25+
<group>mssql-only</group>
2526
<group>srid</group>
2627
<group>deprecation</group>
2728
</exclude>

docker/phpunit.pgsql.xml

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
</include>
2424
<exclude>
2525
<group>mysql-only</group>
26+
<group>mssql-only</group>
2627
</exclude>
2728
</groups>
2829
<php>

tests/LongitudeOne/Spatial/Tests/Doctrine/ConnectionParameters.php

+7
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ public static function getCommonConnectionParameters(): array
5959
if (isset($GLOBALS['db_version'])) {
6060
$connectionParams['driverOptions']['server_version'] = $GLOBALS['db_version'];
6161
}
62+
if (isset($GLOBALS['db_driver_options'])) {
63+
$options = explode(',', $GLOBALS['db_driver_options']);
64+
foreach ($options as $option) {
65+
[$key, $val] = explode('=', $option);
66+
$connectionParams['driverOptions'][$key] = $val;
67+
}
68+
}
6269

6370
return $connectionParams;
6471
}

tests/LongitudeOne/Spatial/Tests/OrmTestCase.php

+20
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Doctrine\DBAL\Platforms\AbstractPlatform;
2727
use Doctrine\DBAL\Platforms\MySQLPlatform;
2828
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
29+
use Doctrine\DBAL\Platforms\SQLServerPlatform;
2930
use Doctrine\DBAL\Types\Exception\UnknownColumnType;
3031
use Doctrine\DBAL\Types\Type;
3132
use Doctrine\ORM\Configuration;
@@ -368,6 +369,10 @@ protected static function getConnection(): Connection
368369
return $connection;
369370
}
370371

372+
if ($connection->getDatabasePlatform() instanceof SQLServerPlatform) {
373+
return $connection;
374+
}
375+
371376
throw new UnsupportedPlatformException(sprintf(
372377
'DBAL platform "%s" is not currently supported.',
373378
$connection->getDatabasePlatform()::class
@@ -478,6 +483,11 @@ protected function setUpFunctions(): void
478483
// Specific functions of MySQL 5.7 and 8.0 database engines
479484
$this->addSpecificMySqlFunctions($configuration);
480485
}
486+
487+
if ($this->getPlatform() instanceof SQLServerPlatform) {
488+
// Specific functions of Microsoft SQL Server 2017, 2019, 2022
489+
$this->addSpecificMsSqlFunctions($configuration);
490+
}
481491
}
482492

483493
/**
@@ -609,6 +619,16 @@ private function addSpecificPostgreSqlFunctions(Configuration $configuration): v
609619
$configuration->addCustomNumericFunction('PgSql_Translate', SpTranslate::class);
610620
}
611621

622+
/**
623+
* Complete configuration with MS SQL Server spatial functions.
624+
*
625+
* @param Configuration $configuration the current configuration
626+
*/
627+
private function addSpecificMsSqlFunctions(Configuration $configuration): void
628+
{
629+
// ready to add related functions for Microsoft SQL Server
630+
}
631+
612632
/**
613633
* Add all standard functions.
614634
*

0 commit comments

Comments
 (0)