Skip to content

Commit a317715

Browse files
committed
allow future extending, add basic ezSchema phpunit tests
1 parent c869721 commit a317715

File tree

10 files changed

+125
-17
lines changed

10 files changed

+125
-17
lines changed

lib/Constants.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
// String SQL data types
126126
\define('CHAR', 'CHAR');
127127
\define('VARCHAR', 'VARCHAR');
128+
\define('CHARACTER', 'CHARACTER');
128129
\define('TEXT', 'TEXT');
129130
\define('TINY', 'TINYTEXT');
130131
\define('TINYTEXT', 'TINYTEXT');

lib/Database/ez_mysqli.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use ezsql\ConfigInterface;
99
use ezsql\DatabaseInterface;
1010

11-
final class ez_mysqli extends ezsqlModel implements DatabaseInterface
11+
class ez_mysqli extends ezsqlModel implements DatabaseInterface
1212
{
1313
/*
1414
* ezSQL error strings - mySQLi

lib/Database/ez_pdo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use ezsql\ConfigInterface;
1010
use ezsql\DatabaseInterface;
1111

12-
final class ez_pdo extends ezsqlModel implements DatabaseInterface
12+
class ez_pdo extends ezsqlModel implements DatabaseInterface
1313
{
1414
/**
1515
* ezSQL error strings - PDO

lib/Database/ez_pgsql.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use ezsql\ConfigInterface;
99
use ezsql\DatabaseInterface;
1010

11-
final class ez_pgsql extends ezsqlModel implements DatabaseInterface
11+
class ez_pgsql extends ezsqlModel implements DatabaseInterface
1212
{
1313
/**
1414
* ezSQL error strings - PostgreSQL

lib/Database/ez_sqlite3.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use ezsql\ConfigInterface;
99
use ezsql\DatabaseInterface;
1010

11-
final class ez_sqlite3 extends ezsqlModel implements DatabaseInterface
11+
class ez_sqlite3 extends ezsqlModel implements DatabaseInterface
1212
{
1313
/**
1414
* ezSQL error strings - SQLite

lib/Database/ez_sqlsrv.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use ezsql\ConfigInterface;
99
use ezsql\DatabaseInterface;
1010

11-
final class ez_sqlsrv extends ezsqlModel implements DatabaseInterface
11+
class ez_sqlsrv extends ezsqlModel implements DatabaseInterface
1212
{
1313
/**
1414
* ezSQL error strings - sqlsrv

lib/DatabaseInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ interface DatabaseInterface
1818
* - getIsFile();
1919
* - getToMssql();
2020
* - getPath();
21-
*---------------
21+
*---
2222
* - setDriver($args);
2323
* - setDsn($args);
2424
* - setUser($args);
2525
* - setPassword($args);
26-
* - setName($args);
26+
* - setName($args);
2727
* - setHost($args);
2828
* - setPort($args);
2929
* - setCharset($args);

lib/ezFunctions.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ function sqliteInstance($databaseSetting = null, $instanceTag = null)
4545
return \database(\SQLITE3, $databaseSetting, $instanceTag);
4646
}
4747

48+
function getVendor()
49+
{
50+
return ezSchema::vendor();
51+
}
52+
4853
function to_string($arrays, $separation = ',')
4954
{
5055
return ezQuery::to_string($arrays, $separation);

lib/ezSchema.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class ezSchema
88
const STRINGS = [
99
'common' => ['CHAR', 'VARCHAR', 'TEXT'],
1010
'mysqli' => ['TINYTEXT', 'MEDIUMTEXT', 'LONGTEXT', 'BINARY', 'VARBINARY'],
11-
'pgsql' => ['character', 'character varying'],
11+
'pgsql' => ['CHARACTER', 'CHARACTER VARYING'],
1212
'sqlsrv' => ['NCHAR', 'NVARCHAR', 'NTEXT', 'BINARY', 'VARBINARY', 'IMAGE'],
1313
'sqlite3' => ['TINYTEXT', 'MEDIUMTEXT', 'LONGTEXT', 'NCHAR', 'NVARCHAR', 'CLOB']
1414
];
@@ -17,9 +17,9 @@ class ezSchema
1717
'common' => ['INT'],
1818
'mysqli' => ['BIT', 'INTEGER', 'TINYINT', 'SMALLINT', 'MEDIUMINT', 'BIGINT', 'FLOAT',
1919
'BOOL', 'BOOLEAN'],
20-
'pgsql' => ['bit', 'varbit', 'bit varying', 'smallint', 'int', 'integer',
21-
'bigint', 'smallserial', 'serial', 'bigserial', 'double precision', 'real',
22-
'money', 'bool', 'boolean'],
20+
'pgsql' => ['BIT', 'VARBIT', 'BIT VARYING', 'SMALLINT', 'INT', 'INTEGER',
21+
'BIGINT', 'SMALLSERIAL', 'SERIAL', 'BIGSERIAL', 'DOUBLE PRECISION', 'REAL',
22+
'MONEY', 'BOOL', 'BOOLEAN'],
2323
'sqlsrv' => ['BIT', 'TINYINT', 'SMALLINT', 'BIGINT', 'SMALLMONEY', 'MONEY',
2424
'FLOAT', 'REAL'],
2525
'sqlite3' => ['TINYINT', 'SMALLINT', 'MEDIUMINT', 'BIGINT', 'INTEGER', 'INT2',
@@ -38,8 +38,8 @@ class ezSchema
3838
'common' => ['DATE', 'TIMESTAMP', 'TIME'],
3939
'mysqli' => ['DATETIME', 'YEAR'],
4040
'pgsql' => [
41-
'timestamp without time zone', 'timestamp with time zone',
42-
'time without time zone', 'time with time zone'
41+
'TIMESTAMP WITHOUT TIME ZONE', 'TIMESTAMP WITH TIME ZONE',
42+
'TIME WITHOUT TIME ZONE', 'TIME WITH TIME ZONE'
4343
],
4444
'sqlsrv' => ['DATETIME', 'DATETIME2', 'SMALLDATETIME', 'DATETIMEOFFSET'],
4545
'sqlite3' => ['DATETIME']
@@ -113,7 +113,7 @@ public function __call($type, $args)
113113
// check for string data type
114114
$numberOrString = $args[0];
115115
$store = \is_int($numberOrString) ? '('.$numberOrString.')' : '';
116-
$store = empty($store) && !empty($numberOrString) ? $numberOrString : $store;
116+
$store = empty($store) && !empty($numberOrString) ? ' '.$numberOrString : $store;
117117
$value = !empty($args[1]) ? ' '.$args[1] : '';
118118
$options = !empty($args[2]) ? ' '.$args[2] : '';
119119
$extra = !empty($args[3]) ? ' '.$args[3] : '';
@@ -130,7 +130,7 @@ public function __call($type, $args)
130130
// check for whole number data type
131131
$numberOrString = $args[0];
132132
$store = \is_int($numberOrString) ? '('.$numberOrString.')' : '';
133-
$store = empty($store) && !empty($numberOrString) ? $numberOrString : $store;
133+
$store = empty($store) && !empty($numberOrString) ? ' '.$numberOrString : $store;
134134
$value = !empty($args[1]) ? ' '.$args[1] : '';
135135
$options = !empty($args[2]) ? ' '.$args[2] : '';
136136
$extra = !empty($args[3]) ? ' '.$args[3] : '';
@@ -139,13 +139,13 @@ public function __call($type, $args)
139139
// check for date time data type
140140
$numberOrString = $args[0];
141141
$store = \is_int($numberOrString) ? '('.$numberOrString.')' : '';
142-
$fraction = empty($store) && !empty($numberOrString) ? $numberOrString : $store;
142+
$fraction = empty($store) && !empty($numberOrString) ? ' '.$numberOrString : $store;
143143
$value = !empty($args[1]) ? ' '.$args[1] : '';
144144
$options = !empty($args[2]) ? ' '.$args[2] : '';
145145
$data = $type.$fraction.$value.$options;
146146
} elseif (\preg_match($objectPattern, $type)) {
147147
// check for large object data type
148-
$value = !empty($args[0]) ? ' '.$args[0] : '';
148+
$value = !empty($args[0]) ? ' '.$args[0] : ' ';
149149
$data = $type.$value;
150150
} else {
151151
throw new \Exception("$type does not exist");

tests/ezSchemaTest.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
namespace ezsql\Tests;
4+
5+
use ezsql\ezSchema;
6+
use ezsql\Tests\EZTestCase;
7+
8+
class ezSchemaTest extends EZTestCase
9+
{
10+
/**
11+
* @covers ezsql\ezSchema::vendor
12+
*/
13+
public function testVendor()
14+
{
15+
clearInstance();
16+
$this->assertEquals(null, getVendor());
17+
$this->assertEquals(false, ezSchema::datatype(BLOB, NULLS));
18+
$this->assertFalse(column('id', INTR, 32, AUTO, PRIMARY));
19+
}
20+
21+
/**
22+
* @covers ezsql\ezSchema::vendor
23+
*/
24+
public function testVendor_mysqli()
25+
{
26+
if (!extension_loaded('mysqli')) {
27+
$this->markTestSkipped(
28+
'The MySQLi extension is not available.'
29+
);
30+
}
31+
32+
mysqlInstance([self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME]);
33+
$this->assertEquals(MYSQLI, getVendor());
34+
$this->assertEquals('BLOB NULL', ezSchema::datatype(BLOB, NULLS));
35+
$this->assertEquals('VARCHAR(256) NOT NULL', ezSchema::datatype(VARCHAR, 256, notNULL));
36+
$this->assertEquals('id INT(32) AUTO_INCREMENT PRIMARY KEY, ', column('id', INTR, 32, AUTO, PRIMARY));
37+
}
38+
39+
/**
40+
* @covers ezsql\ezSchema::vendor
41+
*/
42+
public function testVendor_Pgsql()
43+
{
44+
if (!extension_loaded('pgsql')) {
45+
$this->markTestSkipped(
46+
'The PostgreSQL Lib is not available.'
47+
);
48+
}
49+
50+
pgsqlInstance([self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME, self::TEST_DB_HOST, self::TEST_DB_PORT]);
51+
$this->assertEquals(PGSQL, getVendor());
52+
$this->assertEquals('TIMESTAMP NOT NULL', ezSchema::datatype(TIMESTAMP, notNULL));
53+
$this->assertEquals('price NUMERIC(6,2) NULL, ', column('price', NUMERIC, 6, 2, NULLS));
54+
$this->assertEquals('id SERIAL PRIMARY KEY, ', column('id', AUTO, PRIMARY));
55+
}
56+
57+
/**
58+
* @covers ezsql\ezSchema::vendor
59+
*/
60+
public function testVendor_Sqlite3()
61+
{
62+
if (!extension_loaded('sqlite3')) {
63+
$this->markTestSkipped(
64+
'The sqlite3 Lib is not available.'
65+
);
66+
}
67+
68+
sqliteInstance([self::TEST_SQLITE_DB_DIR, self::TEST_SQLITE_DB]);
69+
$this->assertEquals(SQLITE3, getVendor());
70+
}
71+
72+
/**
73+
* @covers ezsql\ezSchema::vendor
74+
*/
75+
public function testVendor_Sqlsrv()
76+
{
77+
if (!extension_loaded('sqlsrv')) {
78+
$this->markTestSkipped(
79+
'The sqlsrv Lib is not available.'
80+
);
81+
}
82+
83+
mssqlInstance([self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME]);
84+
$this->assertEquals(MSSQL, getVendor());
85+
}
86+
87+
/**
88+
* @covers ezsql\ezSchema::vendor
89+
*/
90+
public function testVendor_Pdo()
91+
{
92+
if ( ! \class_exists ('PDO') ) {
93+
$this->markTestSkipped(
94+
'The PDO Lib is not available.'
95+
);
96+
}
97+
98+
$pdo_mysql = pdoInstance(['mysql:host='.self::TEST_DB_HOST.';dbname='.self::TEST_DB_NAME.';port=3306', self::TEST_DB_USER,self::TEST_DB_PASSWORD]);
99+
$pdo_mysql->connect();
100+
$this->assertEquals(MYSQLI, getVendor());
101+
}
102+
}

0 commit comments

Comments
 (0)