Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable SQLite for Unit Testing & Improve ODBC Compatibility in PHP 8.4 #215

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ToolkitApi/Toolkit.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public function __construct($databaseNameOrResource, $userOrI5NamingFlag = '0',
}

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

// initialize generic message
$this->error = "\nFailed to connect. databaseNameOrResource " . var_export($databaseNameOrResource, true) . " not valid.";
Expand Down Expand Up @@ -224,7 +224,7 @@ public function __construct($databaseNameOrResource, $userOrI5NamingFlag = '0',
if ($this->isDebug()) {
$this->debugLog("Re-using existing db connection with schema separator: $schemaSep");
}
} elseif ($transportType === 'odbc' && $isResource) {
} elseif ($transportType === 'odbc' && ($isResource || $databaseNameOrResource instanceof \Odbc\Connection)) {
$conn = $databaseNameOrResource;
$this->_i5NamingFlag = $userOrI5NamingFlag;
$schemaSep = ($this->_i5NamingFlag) ? '/' : '.';
Expand Down
23 changes: 23 additions & 0 deletions UNITTEST.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
### Running Unit Tests with SQLite

1. **Install dependencies**:
```sh
composer install
```

2. **Install required packages on Debian**:
```sh
apt install php-pdo php-pdo-sqlite php-odbc libsqliteodbc sqlite3
```

3. **Enable SQLite mocking**:
- Open `tests/config/db.config.php`
- Set:
```php
'mockDb2UsingSqlite' => true,
```

4. **Run the tests**:
```sh
vendor/bin/phpunit
```
1 change: 1 addition & 0 deletions tests/config/db.config.php.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
return [
'db' => [
'mockDb2UsingSqlite' => false,
'odbc' => [
'dsn' => 'DSN=*LOCAL;',
'username' => 'MYUSER',
Expand Down
25 changes: 21 additions & 4 deletions tests/functional/ToolkitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@ final class ToolkitTest extends TestCase
*/
private $toolkitOptions;

/**
* @var bool
*/
private $mockDb2UsingSqlite;

public function setUp(): void
{
$config = getConfig();

$this->mockDb2UsingSqlite = $config['db']['mockDb2UsingSqlite'] ?? false;
$this->connectionOptions = $config['db']['odbc'];
$this->toolkitOptions = $config['toolkit'];
}
Expand All @@ -35,7 +41,7 @@ public function setUp(): void
public function testCanPassPdoOdbcObjectToToolkit()
{
$pdo = new \PDO(
'odbc:' . $this->connectionOptions['dsn'],
$this->buildDsn('pdo'),
$this->connectionOptions['username'],
$this->connectionOptions['password'],
[
Expand All @@ -44,7 +50,7 @@ public function testCanPassPdoOdbcObjectToToolkit()
'quote_identifiers' => $this->connectionOptions['platform_options']['quote_identifiers'],
]
]
);
);

$toolkit = new Toolkit($pdo, null, null, 'pdo');
$toolkit->setOptions($this->toolkitOptions);
Expand All @@ -57,7 +63,7 @@ public function testCanPassPdoOdbcObjectToToolkit()
*/
public function testCanPassOdbcResourceToToolkit()
{
$connection = odbc_connect($this->connectionOptions['dsn'], $this->connectionOptions['username'], $this->connectionOptions['password']);
$connection = odbc_connect($this->buildDsn('odbc'), $this->connectionOptions['username'], $this->connectionOptions['password']);

if (!$connection) {
throw new \Exception('Connection failed');
Expand All @@ -74,7 +80,7 @@ public function testCanPassOdbcResourceToToolkit()
public function testCanPassOdbcConnectionParametersToToolkit()
{
$toolkit = new Toolkit(
$this->connectionOptions['dsn'],
$this->buildDsn('odbc'),
$this->connectionOptions['username'],
$this->connectionOptions['password'],
'odbc'
Expand All @@ -83,4 +89,15 @@ public function testCanPassOdbcConnectionParametersToToolkit()

$this->assertInstanceOf(Toolkit::class, $toolkit);
}

/**
* Builds the appropriate DSN based on configuration.
*/
private function buildDsn(string $type = 'pdo'): string
{
if ($this->mockDb2UsingSqlite) {
return ($type === 'pdo') ? 'sqlite::memory:' : 'Driver=SQLite3;Database=:memory:';
}
return ($type === 'pdo') ? 'odbc:' . $this->connectionOptions['dsn'] : $this->connectionOptions['dsn'];
}
}