Skip to content

Commit

Permalink
Do not reuse connections inside integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Nov 19, 2023
1 parent c69dcf3 commit d9e61e2
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ protected function driverName() { return 'mssql'; }
*
* @param string name
*/
protected function createAutoIncrementTable($name) {
$this->removeTable($name);
$this->db()->query('create table %c (pk int identity, username varchar(30))', $name);
protected function createAutoIncrementTable($conn, $name) {
$this->removeTable($conn, $name);
$conn->query('create table %c (pk int identity, username varchar(30))', $name);
}

/**
* Create transactions table
*
* @param string name
*/
protected function createTransactionsTable($name) {
$this->removeTable($name);
$this->db()->query('create table %c (pk int, username varchar(30))', $name);
protected function createTransactionsTable($conn, $name) {
$this->removeTable($conn, $name);
$conn->query('create table %c (pk int, username varchar(30))', $name);
}

#[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,28 @@ class MySQLIntegrationTest extends RdbmsIntegrationTest {
/** @return string */
protected function driverName() { return 'mysql'; }

/** @return void */
#[After]
public function tearDown() {
parent::tearDown();

// Suppress "mysql_connect(): The mysql extension is deprecated [...]"
foreach (\xp::$errors as $file => $errors) {
if (strstr($file, 'MySQLConnection')) {
unset(\xp::$errors[$file]);
}
}
}

/**
* Create autoincrement table
*
* @param rdbms.DBConnection $conn
* @param string $name
* @return void
*/
protected function createAutoIncrementTable($name) {
$this->removeTable($name);
$this->db()->query('create table %c (pk int primary key auto_increment, username varchar(30))', $name);
protected function createAutoIncrementTable($conn, $name) {
$this->removeTable($conn, $name);
$conn->query('create table %c (pk int primary key auto_increment, username varchar(30))', $name);
}

/**
* Create transactions table
*
* @param rdbms.DBConnection $conn
* @param string $name
* @return void
*/
protected function createTransactionsTable($name) {
$this->removeTable($name);
$this->db()->query('create table %c (pk int, username varchar(30)) Engine=InnoDB', $name);
protected function createTransactionsTable($conn, $name) {
$this->removeTable($conn, $name);
$conn->query('create table %c (pk int, username varchar(30)) Engine=InnoDB', $name);
}

#[Test, Ignore('Numeric not supported by MySQL')]
Expand Down Expand Up @@ -236,13 +225,20 @@ public function selectSmallintOne() { }
#[Test, Ignore('Cast to smallint not supported by MySQL')]
public function selectSmallintZero() { }

#[Test, Ignore('Does not cause an exception in MySQL')]
public function arithmeticOverflowWithQuery() { }

#[Test, Ignore('Does not cause an exception in MySQL')]
public function arithmeticOverflowWithOpen() { }

#[Test]
public function selectUtf8mb4() {
$conn= $this->db();

// Sending characters outside the BMP while the encoding isn't utf8mb4
// produces a warning.
Assert::equals('💩', $this->db()->query("select '💩' as poop")->next('poop'));
Assert::null($this->db()->query('show warnings')->next());
Assert::equals('💩', $conn->query("select '💩' as poop")->next('poop'));
Assert::null($conn->query('show warnings')->next());
}

#[Test]
Expand All @@ -259,24 +255,4 @@ public function reconnects_when_server_disconnects() {
$after= $conn->query('select connection_id() as id')->next('id');
Assert::notEquals($before, $after, 'Connection IDs must be different');
}

#[Test]
public function does_not_reconnect_if_disconnected_inside_transaction() {
$conn= $this->db();
$before= $conn->query('select connection_id() as id')->next('id');

$tran= $conn->begin(new Transaction('test'));
try {
$conn->query('kill %d', $before);
} catch (SQLException $expected) {
// errorcode 1927: Connection was killed (sqlstate 70100)
}

try {
$conn->query('select connection_id() as id');
$this->fail('No exception raised', null, SQLConnectionClosedException::class);
} catch (SQLConnectionClosedException $expected) {
// errorcode 2006: Server disconnected (sqlstate 00000)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?php namespace rdbms\unittest\integration;

use rdbms\SQLException;
use unittest\Assert;
use unittest\{Ignore, Test};
use unittest\{Assert, Ignore, Test};

class PostgreSQLIntegrationTest extends RdbmsIntegrationTest {

Expand All @@ -12,23 +11,25 @@ protected function driverName() { return 'pgsql'; }
/**
* Create autoincrement table
*
* @param rdbms.DBConnection $conn
* @param string $name
* @return void
*/
protected function createAutoIncrementTable($name) {
$this->removeTable($name);
$this->db()->query('create table %c (pk serial primary key, username varchar(30))', $name);
protected function createAutoIncrementTable($conn, $name) {
$this->removeTable($conn, $name);
$conn->query('create table %c (pk serial primary key, username varchar(30))', $name);
}

/**
* Create transactions table
*
* @param rdbms.DBConnection $conn
* @param string $name
* @return void
*/
protected function createTransactionsTable($name) {
$this->removeTable($name);
$this->db()->query('create table %c (pk serial primary key, username varchar(30))', $name);
protected function createTransactionsTable($conn, $name) {
$this->removeTable($conn, $name);
$conn->query('create table %c (pk serial primary key, username varchar(30))', $name);
}

#[Test, Ignore('Numeric not supported by PostgreSQL')]
Expand Down
Loading

0 comments on commit d9e61e2

Please sign in to comment.