Skip to content

Commit 86d3d3f

Browse files
committed
Enable SQLite for Unit Testing and Improve ODBC Compatibility
- Added support for running unit tests using SQLite by introducing `mockDb2UsingSqlite` config option. - Refactored DSN handling with a `buildDsn` method for better maintainability. - Fixed issues with ODBC connection handling, improving compatibility with PHP 8.4. - Updated tests to dynamically determine the DSN based on the configuration. This makes it easier to run tests without an IBM i system and allows for automated testing across multiple PHP versions.
1 parent 15fb93d commit 86d3d3f

File tree

4 files changed

+47
-6
lines changed

4 files changed

+47
-6
lines changed

ToolkitApi/Toolkit.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public function __construct($databaseNameOrResource, $userOrI5NamingFlag = '0',
158158
}
159159

160160
// stop any types that are not valid for first parameter. Invalid values may cause toolkit to try to create another database connection.
161-
if (!is_string($databaseNameOrResource) && !is_resource($databaseNameOrResource) && ((!is_object($databaseNameOrResource) || (is_object($databaseNameOrResource) && get_class($databaseNameOrResource) !== PDO::class)))) {
161+
if (!is_string($databaseNameOrResource) && !is_resource($databaseNameOrResource) && ((!is_object($databaseNameOrResource) || (is_object($databaseNameOrResource) && !in_array(get_class($databaseNameOrResource), [PDO::class, \Odbc\Connection::class], true))))) {
162162

163163
// initialize generic message
164164
$this->error = "\nFailed to connect. databaseNameOrResource " . var_export($databaseNameOrResource, true) . " not valid.";
@@ -224,7 +224,7 @@ public function __construct($databaseNameOrResource, $userOrI5NamingFlag = '0',
224224
if ($this->isDebug()) {
225225
$this->debugLog("Re-using existing db connection with schema separator: $schemaSep");
226226
}
227-
} elseif ($transportType === 'odbc' && $isResource) {
227+
} elseif ($transportType === 'odbc' && ($isResource || $databaseNameOrResource instanceof \Odbc\Connection)) {
228228
$conn = $databaseNameOrResource;
229229
$this->_i5NamingFlag = $userOrI5NamingFlag;
230230
$schemaSep = ($this->_i5NamingFlag) ? '/' : '.';

UNITTEST.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
### Running Unit Tests with SQLite
2+
3+
1. **Install dependencies**:
4+
```sh
5+
composer install
6+
```
7+
8+
2. **Install required packages on Debian**:
9+
```sh
10+
apt install php-pdo php-pdo-sqlite php-odbc libsqliteodbc sqlite3
11+
```
12+
13+
3. **Enable SQLite mocking**:
14+
- Open `tests/config/db.config.php`
15+
- Set:
16+
```php
17+
'mockDb2UsingSqlite' => true,
18+
```
19+
20+
4. **Run the tests**:
21+
```sh
22+
vendor/bin/phpunit
23+
```

tests/config/db.config.php.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
return [
33
'db' => [
4+
'mockDb2UsingSqlite' => false,
45
'odbc' => [
56
'dsn' => 'DSN=*LOCAL;',
67
'username' => 'MYUSER',

tests/functional/ToolkitTest.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,16 @@ final class ToolkitTest extends TestCase
2121
*/
2222
private $toolkitOptions;
2323

24+
/**
25+
* @var bool
26+
*/
27+
private $mockDb2UsingSqlite;
28+
2429
public function setUp(): void
2530
{
2631
$config = getConfig();
2732

33+
$this->mockDb2UsingSqlite = $config['db']['mockDb2UsingSqlite'] ?? false;
2834
$this->connectionOptions = $config['db']['odbc'];
2935
$this->toolkitOptions = $config['toolkit'];
3036
}
@@ -35,7 +41,7 @@ public function setUp(): void
3541
public function testCanPassPdoOdbcObjectToToolkit()
3642
{
3743
$pdo = new \PDO(
38-
'odbc:' . $this->connectionOptions['dsn'],
44+
$this->buildDsn('pdo'),
3945
$this->connectionOptions['username'],
4046
$this->connectionOptions['password'],
4147
[
@@ -44,7 +50,7 @@ public function testCanPassPdoOdbcObjectToToolkit()
4450
'quote_identifiers' => $this->connectionOptions['platform_options']['quote_identifiers'],
4551
]
4652
]
47-
);
53+
);
4854

4955
$toolkit = new Toolkit($pdo, null, null, 'pdo');
5056
$toolkit->setOptions($this->toolkitOptions);
@@ -57,7 +63,7 @@ public function testCanPassPdoOdbcObjectToToolkit()
5763
*/
5864
public function testCanPassOdbcResourceToToolkit()
5965
{
60-
$connection = odbc_connect($this->connectionOptions['dsn'], $this->connectionOptions['username'], $this->connectionOptions['password']);
66+
$connection = odbc_connect($this->buildDsn('odbc'), $this->connectionOptions['username'], $this->connectionOptions['password']);
6167

6268
if (!$connection) {
6369
throw new \Exception('Connection failed');
@@ -74,7 +80,7 @@ public function testCanPassOdbcResourceToToolkit()
7480
public function testCanPassOdbcConnectionParametersToToolkit()
7581
{
7682
$toolkit = new Toolkit(
77-
$this->connectionOptions['dsn'],
83+
$this->buildDsn('odbc'),
7884
$this->connectionOptions['username'],
7985
$this->connectionOptions['password'],
8086
'odbc'
@@ -83,4 +89,15 @@ public function testCanPassOdbcConnectionParametersToToolkit()
8389

8490
$this->assertInstanceOf(Toolkit::class, $toolkit);
8591
}
92+
93+
/**
94+
* Builds the appropriate DSN based on configuration.
95+
*/
96+
private function buildDsn(string $type = 'pdo'): string
97+
{
98+
if ($this->mockDb2UsingSqlite) {
99+
return ($type === 'pdo') ? 'sqlite::memory:' : 'Driver=SQLite3;Database=:memory:';
100+
}
101+
return ($type === 'pdo') ? 'odbc:' . $this->connectionOptions['dsn'] : $this->connectionOptions['dsn'];
102+
}
86103
}

0 commit comments

Comments
 (0)