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

MySQL: Modernise connection for PHP 8.4 #5

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
1 change: 0 additions & 1 deletion src/Lunr/Gravity/MySQL/MySQLConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,6 @@ public function disconnect(): void
return;
}

$this->mysqli->kill($this->mysqli->thread_id);
$this->mysqli->close();
$this->connected = FALSE;
}
Expand Down
77 changes: 21 additions & 56 deletions src/Lunr/Gravity/MySQL/Tests/MySQLConnectionConnectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,8 @@ public function testSuccessfulConnectReadonly(): void

$this->class->connect();

$property = $this->get_accessible_reflection_property('connected');

$this->assertTrue($property->getValue($this->class));

$property->setValue($this->class, FALSE);
$this->assertPropertyEquals('connected', TRUE);
$this->set_reflection_property_value('connected', FALSE); //avoid deconstructor rollback
}

/**
Expand Down Expand Up @@ -87,11 +84,8 @@ public function testSuccessfulConnectReadwrite(): void

$this->class->connect();

$property = $this->get_accessible_reflection_property('connected');

$this->assertTrue($property->getValue($this->class));

$property->setValue($this->class, FALSE);
$this->assertPropertyEquals('connected', TRUE);
$this->set_reflection_property_value('connected', FALSE); //avoid deconstructor rollback
}

/**
Expand Down Expand Up @@ -130,11 +124,8 @@ public function testSuccessfulConnectReadwriteWithSSL(): void

$this->class->connect();

$property = $this->get_accessible_reflection_property('connected');

$this->assertTrue($property->getValue($this->class));

$property->setValue($this->class, FALSE);
$this->assertPropertyEquals('connected', TRUE);
$this->set_reflection_property_value('connected', FALSE); //avoid deconstructor rollback
}

/**
Expand All @@ -146,7 +137,7 @@ public function testSuccessfulConnectReadwriteWithSSL(): void
public function testSuccessfulReconnect(): void
{
$mysqli = $this->getMockBuilder('Lunr\Gravity\MySQL\Tests\MockMySQLi')
->setMethods([ 'connect', 'ssl_set', 'set_charset', 'kill', 'close' ])
->setMethods([ 'connect', 'ssl_set', 'set_charset', 'close' ])
->getMock();

$this->set_reflection_property_value('mysqli', $mysqli);
Expand All @@ -168,9 +159,6 @@ public function testSuccessfulReconnect(): void
->method('ssl_set')
->with('ssl_key', 'ssl_cert', 'ca_cert', 'ca_path', 'cipher');

$mysqli->expects($this->exactly(4))
->method('kill');

$mysqli->expects($this->exactly(4))
->method('close');

Expand All @@ -180,11 +168,8 @@ public function testSuccessfulReconnect(): void

$this->class->connect();

$property = $this->get_accessible_reflection_property('connected');

$this->assertTrue($property->getValue($this->class));

$property->setValue($this->class, FALSE);
$this->assertPropertyEquals('connected', TRUE);
$this->set_reflection_property_value('connected', FALSE); //avoid deconstructor rollback
}

/**
Expand Down Expand Up @@ -217,10 +202,7 @@ public function testFailedConnect(): void
}
finally
{
$property = $this->reflection->getProperty('connected');
$property->setAccessible(TRUE);

$this->assertFalse($this->get_reflection_property_value('connected'));
$this->assertPropertyEquals('connected', FALSE); //avoid deconstructor rollback
}
}

Expand All @@ -233,7 +215,7 @@ public function testFailedConnect(): void
public function testUnsuccessfulReconnect(): void
{
$mysqli = $this->getMockBuilder('Lunr\Gravity\MySQL\Tests\MockMySQLi')
->setMethods([ 'connect', 'ssl_set', 'set_charset', 'kill', 'close' ])
->setMethods([ 'connect', 'ssl_set', 'set_charset', 'close' ])
->getMock();

$this->set_reflection_property_value('mysqli', $mysqli);
Expand All @@ -255,9 +237,6 @@ public function testUnsuccessfulReconnect(): void
->method('ssl_set')
->with('ssl_key', 'ssl_cert', 'ca_cert', 'ca_path', 'cipher');

$mysqli->expects($this->exactly(5))
->method('kill');

$mysqli->expects($this->exactly(5))
->method('close');

Expand All @@ -278,10 +257,7 @@ public function testUnsuccessfulReconnect(): void
}
finally
{
$property = $this->reflection->getProperty('connected');
$property->setAccessible(TRUE);

$this->assertFalse($this->get_reflection_property_value('connected'));
$this->assertPropertyEquals('connected', FALSE);
}
}

Expand All @@ -292,8 +268,7 @@ public function testUnsuccessfulReconnect(): void
*/
public function testConnectDoesNotReconnectWhenAlreadyConnected(): void
{
$property = $this->get_accessible_reflection_property('connected');
$property->setValue($this->class, TRUE);
$this->set_reflection_property_value('connected', TRUE);

$this->mysqli->expects($this->never())
->method('connect');
Expand All @@ -306,11 +281,7 @@ public function testConnectDoesNotReconnectWhenAlreadyConnected(): void

$this->class->connect();

$property = $this->get_accessible_reflection_property('connected');

$this->assertTrue($property->getValue($this->class));

$property->setValue($this->class, FALSE);
$this->assertPropertyEquals('connected', TRUE);
}

/**
Expand Down Expand Up @@ -363,14 +334,12 @@ public function testConnectFailsWhenDriverIsNotMysql(): void
*/
public function testDisconnectDoesNotTryToDisconnectWhenNotConnected(): void
{
$this->mysqli->expects($this->never())
->method('kill');
$this->mysqli->expects($this->never())
->method('close');

$this->class->disconnect();

$this->assertFalse($this->get_reflection_property_value('connected'));
$this->assertPropertyEquals('connected', FALSE);
}

/**
Expand All @@ -390,13 +359,11 @@ public function testDisconnect(): void

$this->class->connect();

$property = $this->get_accessible_reflection_property('connected');

$this->assertTrue($property->getValue($this->class));
$this->assertPropertyEquals('connected', TRUE);

$this->class->disconnect();

$this->assertFalse($property->getValue($this->class));
$this->assertPropertyEquals('connected', FALSE);
}

/**
Expand Down Expand Up @@ -424,16 +391,15 @@ public function testChangeDatabaseThrowsExceptionWhenNotConnected(): void
*/
public function testChangeDatabaseReturnsFalseWhenSelectDBFailed(): void
{
$property = $this->get_accessible_reflection_property('connected');
$property->setValue($this->class, TRUE);
$this->set_reflection_property_value('connected', TRUE);

$this->mysqli->expects($this->once())
->method('select_db')
->will($this->returnValue(FALSE));

$this->assertFalse($this->class->change_database('new_db'));

$property->setValue($this->class, FALSE);
$this->set_reflection_property_value('connected', FALSE); //avoid deconstructor rollback
}

/**
Expand All @@ -444,16 +410,15 @@ public function testChangeDatabaseReturnsFalseWhenSelectDBFailed(): void
*/
public function testChangeDatabaseReturnsTrueWhenSelectDBWorked(): void
{
$property = $this->get_accessible_reflection_property('connected');
$property->setValue($this->class, TRUE);
$this->set_reflection_property_value('connected', TRUE);

$this->mysqli->expects($this->once())
->method('select_db')
->will($this->returnValue(TRUE));

$this->assertTrue($this->class->change_database('new_db'));

$property->setValue($this->class, FALSE);
$this->set_reflection_property_value('connected', FALSE); //avoid deconstructor rollback
}

/**
Expand Down
Loading