Skip to content

Commit

Permalink
Fix warnings bubbling up in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus committed Jan 22, 2024
1 parent 918db1a commit 43cff1c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 27 deletions.
53 changes: 33 additions & 20 deletions tests/Functional/Driver/OCI8/ResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
use Doctrine\DBAL\Tests\TestUtil;
use Generator;

use function func_get_args;
use function ini_get;
use function restore_error_handler;
use function set_error_handler;
use function sprintf;

use const E_ALL;
use const E_WARNING;
use function str_contains;

/** @requires extension oci8 */
class ResultTest extends FunctionalTestCase
Expand Down Expand Up @@ -56,9 +57,6 @@ public function testTruncatedFetch(
bool $invalidateDataMidFetch,
): void {
if ($invalidateDataMidFetch) {
// prevent the PHPUnit error handler from handling the warnings that oci_*() functions may trigger
$this->iniSet('error_reporting', (string) (E_ALL & ~E_WARNING));

$this->expectException(DriverException::class);
$this->expectExceptionCode(4068);
}
Expand Down Expand Up @@ -88,22 +86,37 @@ public function testTruncatedFetch(
// Invalidate the original dataset by changing the pipelined function
// after the initial prefetch that caches locally the first X results
$this->createOrReplacePipelinedFunction($expectedTotalRowCount + 10);

$previous = null;
$previous = set_error_handler(static function (int $errno, string $errstr) use (&$previous): bool {
if (str_contains($errstr, 'ORA-04061')) {
return true;
}

return $previous !== null && $previous(...func_get_args());
});
}

while ($result->fetchOne()) {
// Attempt to access all remaining rows from the original fetch
// The rows locally cached from the default prefetch will first be used
// but when the result attempts to get the remaining 10 rows beyond
// the first prefetch, nothing will be returned
//
// PHP oci8 oci_fetch_array will issue a PHP E_WARNING when the 2nd prefetch occurs
// oci_fetch_array(): ORA-04068: existing state of packages has been discarded
// ORA-04061: existing state of function "ROOT.TEST_ORACLE_FETCH_FAILURE" has been invalidated
// ORA-04065: not executed, altered or dropped function "ROOT.TEST_ORACLE_FETCH_FAILURE"
//
// If there was no issue, this should have returned rows totalling 10
// higher than the oci8 default prefetch
continue;
try {
while ($result->fetchOne()) {
// Attempt to access all remaining rows from the original fetch
// The rows locally cached from the default prefetch will first be used
// but when the result attempts to get the remaining 10 rows beyond
// the first prefetch, nothing will be returned
//
// PHP oci8 oci_fetch_array will issue a PHP E_WARNING when the 2nd prefetch occurs
// oci_fetch_array(): ORA-04068: existing state of packages has been discarded
// ORA-04061: existing state of function "ROOT.TEST_ORACLE_FETCH_FAILURE" has been invalidated
// ORA-04065: not executed, altered or dropped function "ROOT.TEST_ORACLE_FETCH_FAILURE"
//
// If there was no issue, this should have returned rows totalling 10
// higher than the oci8 default prefetch
continue;
}
} finally {
if ($invalidateDataMidFetch) {
restore_error_handler();
}
}

self::assertEquals(
Expand Down
21 changes: 18 additions & 3 deletions tests/Functional/ExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
use function exec;
use function extension_loaded;
use function file_exists;
use function func_get_args;
use function posix_geteuid;
use function restore_error_handler;
use function set_error_handler;
use function sprintf;
use function sys_get_temp_dir;
use function touch;
Expand Down Expand Up @@ -82,11 +85,23 @@ public function testInvalidFieldNameException(): void
$table->addColumn('id', Types::INTEGER, []);
$this->dropAndCreateTable($table);

$this->expectException(Exception\InvalidFieldNameException::class);

// prevent the PHPUnit error handler from handling the warning that db2_bind_param() may trigger
$this->iniSet('error_reporting', (string) (E_ALL & ~E_WARNING));
$previous = null;
$previous = set_error_handler(static function (int $errno) use (&$previous): bool {
if (($errno & ~E_WARNING) === 0) {
return true;
}

$this->expectException(Exception\InvalidFieldNameException::class);
$this->connection->insert('bad_columnname_table', ['name' => 5]);
return $previous !== null && $previous(...func_get_args());
});

try {
$this->connection->insert('bad_columnname_table', ['name' => 5]);
} finally {
restore_error_handler();
}
}

public function testNonUniqueFieldNameException(): void
Expand Down
21 changes: 17 additions & 4 deletions tests/Functional/TransactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
use Doctrine\DBAL\Platforms\AbstractMySQLPlatform;
use Doctrine\DBAL\Tests\FunctionalTestCase;

use function func_get_args;
use function restore_error_handler;
use function set_error_handler;
use function sleep;

use const E_ALL;
use const E_WARNING;
use const PHP_VERSION_ID;

Expand Down Expand Up @@ -53,10 +55,21 @@ private function expectConnectionLoss(callable $scenario): void
// during the sleep MySQL will close the connection
sleep(2);

$this->expectException(ConnectionLost::class);

// prevent the PHPUnit error handler from handling the "MySQL server has gone away" warning
$this->iniSet('error_reporting', (string) (E_ALL & ~E_WARNING));
$previous = null;
$previous = set_error_handler(static function (int $errno) use (&$previous): bool {
if (($errno & ~E_WARNING) === 0) {
return true;
}

$this->expectException(ConnectionLost::class);
$scenario($this->connection);
return $previous !== null && $previous(...func_get_args());
});
try {
$scenario($this->connection);
} finally {
restore_error_handler();
}
}
}

0 comments on commit 43cff1c

Please sign in to comment.