Skip to content

Commit 3fc24cf

Browse files
authored
Merge pull request #49 from moufmouf/mariadb
Adding tests for MariaDB
2 parents f29122c + 1f0df3a commit 3fc24cf

File tree

4 files changed

+89
-4
lines changed

4 files changed

+89
-4
lines changed

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ matrix:
1010
env: PREFER_LOWEST="" DB=mysql
1111
- php: 7.1
1212
env: PREFER_LOWEST="--prefer-lowest" DB=mysql
13+
- php: 7.1
14+
env: PREFER_LOWEST="" DB=mariadb
15+
addons:
16+
mariadb: '10.0'
17+
- php: 7.1
18+
env: PREFER_LOWEST="--prefer-lowest" DB=mariadb
19+
addons:
20+
mariadb: '10.0'
1321
- php: 7.1
1422
env: PREFER_LOWEST="--prefer-lowest" DB=oracle PHPUNITFILE="-c phpunit.oracle.xml"
1523
sudo: required

phpunit.mariadb.xml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
bootstrap="vendor/autoload.php"
13+
>
14+
<testsuites>
15+
<testsuite name="TDBM Test Suite">
16+
<directory>./tests/</directory>
17+
</testsuite>
18+
</testsuites>
19+
20+
<php>
21+
<!-- "Real" test database -->
22+
<var name="db_host" value="127.0.0.1" />
23+
<var name="db_username" value="root" />
24+
<var name="db_password" value="root" />
25+
<var name="db_name" value="tdbm_testcase" />
26+
<var name="db_port" value="3306"/>
27+
<var name="db_driver" value="pdo_mysql"/>
28+
</php>
29+
30+
<filter>
31+
<whitelist processUncoveredFilesFromWhitelist="true">
32+
<directory suffix=".php">src/</directory>
33+
<exclude>
34+
<directory suffix=".php">src/Test</directory>
35+
</exclude>
36+
</whitelist>
37+
</filter>
38+
<logging>
39+
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
40+
<log type="coverage-clover" target="build/logs/clover.xml"/>
41+
</logging>
42+
</phpunit>

src/Utils/ScalarBeanPropertyDescriptor.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Doctrine\DBAL\Schema\Column;
66
use Doctrine\DBAL\Schema\Table;
77
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
8+
use Doctrine\DBAL\Types\DateTimeImmutableType;
9+
use Doctrine\DBAL\Types\DateTimeType;
810
use Ramsey\Uuid\Uuid;
911
use TheCodingMachine\TDBM\TDBMException;
1012
use TheCodingMachine\TDBM\Utils\Annotation\Annotation;
@@ -134,7 +136,8 @@ private function getAnnotations(): Annotations
134136
*/
135137
public function hasDefault()
136138
{
137-
return $this->column->getDefault() !== null || $this->hasUuidAnnotation();
139+
// MariaDB 10.3 issue: it returns "NULL" (the string) instead of *null*
140+
return ($this->column->getDefault() !== null && $this->column->getDefault() !== 'NULL') || $this->hasUuidAnnotation();
138141
}
139142

140143
/**
@@ -162,12 +165,27 @@ public function assignToDefaultCode()
162165
}
163166
} else {
164167
$default = $this->column->getDefault();
165-
166-
if (in_array(strtoupper($default), ['CURRENT_TIMESTAMP' /* MySQL */, 'NOW()' /* PostgreSQL */, 'SYSDATE' /* Oracle */ ], true)) {
167-
$defaultCode = 'new \DateTimeImmutable()';
168+
$type = $this->column->getType();
169+
170+
if (in_array($type->getName(), [
171+
'datetime',
172+
'datetime_immutable',
173+
'datetimetz',
174+
'datetimetz_immutable',
175+
'date',
176+
'date_immutable',
177+
'time',
178+
'time_immutable',
179+
], true)) {
180+
if (in_array(strtoupper($default), ['CURRENT_TIMESTAMP' /* MySQL */, 'NOW()' /* PostgreSQL */, 'SYSDATE' /* Oracle */ , 'CURRENT_TIMESTAMP()' /* MariaDB 10.3 */], true)) {
181+
$defaultCode = 'new \DateTimeImmutable()';
182+
} else {
183+
throw new TDBMException('Unable to set default value for date. Database passed this default value: "'.$default.'"');
184+
}
168185
} else {
169186
$defaultCode = var_export($this->column->getDefault(), true);
170187
}
188+
171189
}
172190

173191
return sprintf($str, $this->getSetterName(), $defaultCode);

tests/phpunit-mariadb.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env bash
2+
3+
# Use this file to start a PostgreSQL database using Docker and then run the test suite on the PostgreSQL database.
4+
5+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
6+
cd $DIR
7+
cd ..
8+
9+
docker run --name mariadb-tdbm-test -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d mariadb:10.3
10+
11+
# Let's wait for MariaDB to start
12+
sleep 20
13+
14+
vendor/bin/phpunit -c phpunit.mariadb.xml
15+
16+
docker stop mariadb-tdbm-test
17+
docker rm mariadb-tdbm-test

0 commit comments

Comments
 (0)