Skip to content

Commit

Permalink
Execute MySQLi functional tests with SSL too
Browse files Browse the repository at this point in the history
  • Loading branch information
lcobucci committed Nov 19, 2017
1 parent c9f50b9 commit 6793dc7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 15 deletions.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ jobs:
sudo: required
before_script:
- bash ./tests/travis/install-mysql-5.7.sh
- stage: Test
php: 7.1
env: DB=mysqli MYSQL_VERSION=5.7 MYSQL_SSL=1
sudo: required
before_script:
- bash ./tests/travis/install-mysql-5.7.sh
- stage: Test
php: 7.2
env: DB=mysqli MYSQL_VERSION=5.7
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
<?php
namespace Doctrine\Tests\DBAL\Functional\Driver\Mysqli;

use Doctrine\DBAL\Driver\Mysqli\Driver;
use Doctrine\DBAL\Driver\Mysqli\MysqliConnection;

class ConnectionTest extends \Doctrine\Tests\DbalFunctionalTestCase
{
protected function setUp()
{
if (!extension_loaded('mysqli')) {
if ( ! extension_loaded('mysqli')) {
$this->markTestSkipped('mysqli is not installed.');
}

parent::setUp();

if ( !($this->_conn->getDriver() instanceof \Doctrine\DBAL\Driver\Mysqli\Driver)) {
if ( ! $this->_conn->getDriver() instanceof Driver) {
$this->markTestSkipped('MySQLi only test.');
}
}
Expand All @@ -23,35 +26,69 @@ protected function tearDown()

public function testDriverOptions()
{
$driverOptions = array(
\MYSQLI_OPT_CONNECT_TIMEOUT => 1,
);
$connection = $this->getConnection([\MYSQLI_OPT_CONNECT_TIMEOUT => 1]);

$connection = $this->getConnection($driverOptions);
self::assertInstanceOf("\Doctrine\DBAL\Driver\Mysqli\MysqliConnection", $connection);
self::assertInstanceOf(MysqliConnection::class, $connection);
}

/**
* @expectedException \Doctrine\DBAL\Driver\Mysqli\MysqliException
*/
public function testUnsupportedDriverOption()
{
$this->getConnection(array('hello' => 'world')); // use local infile
$this->getConnection(['hello' => 'world']); // use local infile
}

public function testPing()
{
$conn = $this->getConnection(array());
$conn = $this->getConnection([]);

self::assertTrue($conn->ping());
}

/**
* @group 2815
*
* Verifies that connection using SSL works (we can't verify server certificate since it's self signed, though)
*/
public function testSecureConnection() : void
{
if (getenv('MYSQL_SSL') !== '1') {
$this->markTestSkipped('Should only be executed when server is using SSL');
}

$conn = $this->getConnection(
['flags' => \MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT],
[
'host' => '127.0.0.1',
'ssl_ca' => '/var/lib/mysql/ca.pem',
'ssl_cert' => '/var/lib/mysql/client-cert.pem',
'ssl_key' => '/var/lib/mysql/client-key.pem',
]
);

self::assertTrue($conn->ping());

$sslCipher = $conn->query('SHOW STATUS LIKE "Ssl_cipher"')->fetchAll();

self::assertCount(1, $sslCipher);
self::assertArrayHasKey('Value', $sslCipher[0]);
self::assertNotEmpty($sslCipher[0]['Value']);
}

private function getConnection(array $driverOptions)
private function getConnection(array $driverOptions, ?array $extraParams = null)
{
return new \Doctrine\DBAL\Driver\Mysqli\MysqliConnection(
array(
'host' => $GLOBALS['db_host'],
'dbname' => $GLOBALS['db_name'],
),
$params = [
'host' => $GLOBALS['db_host'],
'dbname' => $GLOBALS['db_name'],
];

if ($extraParams !== null) {
$params = $extraParams + $params;
}

return new MysqliConnection(
$params,
$GLOBALS['db_username'],
$GLOBALS['db_password'],
$driverOptions
Expand Down
1 change: 1 addition & 0 deletions tests/travis/install-mysql-5.7.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ sudo mysql_upgrade
echo "Restart mysql..."
sudo mysql -e "use mysql; update user set authentication_string=PASSWORD('') where User='root'; update user set plugin='mysql_native_password';FLUSH PRIVILEGES;"

if [[ "$MYSQL_SSL" == 1 ]]; then sudo mysql_ssl_rsa_setup; fi;

0 comments on commit 6793dc7

Please sign in to comment.