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

[9.x] Fix MySqlSchemaState does not add --ssl-ca to mysql cli #41315

Merged
merged 2 commits into from
Mar 3, 2022
Merged
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
10 changes: 8 additions & 2 deletions src/Illuminate/Database/Schema/MySqlSchemaState.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,15 @@ protected function connectionString()
{
$value = ' --user="${:LARAVEL_LOAD_USER}" --password="${:LARAVEL_LOAD_PASSWORD}"';

$value .= $this->connection->getConfig()['unix_socket'] ?? false
$config = $this->connection->getConfig();

$value .= $config['unix_socket'] ?? false
? ' --socket="${:LARAVEL_LOAD_SOCKET}"'
: ' --host="${:LARAVEL_LOAD_HOST}" --port="${:LARAVEL_LOAD_PORT}" --ssl-ca="${:LARAVEL_LOAD_SSL_CA}"';
: ' --host="${:LARAVEL_LOAD_HOST}" --port="${:LARAVEL_LOAD_PORT}"';

if (isset($config['options'][\PDO::MYSQL_ATTR_SSL_CA])) {
$value .= ' --ssl-ca="${:LARAVEL_LOAD_SSL_CA}"';
}

return $value;
}
Expand Down
86 changes: 86 additions & 0 deletions tests/Database/DatabaseMySqlSchemaStateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace Illuminate\Tests\Database;

use Illuminate\Database\MySqlConnection;
use Illuminate\Database\Schema\MySqlSchemaState;
use PHPUnit\Framework\TestCase;

class DatabaseMySqlSchemaStateTest extends TestCase
{
/**
* @dataProvider provider
*/
public function testConnectionString(string $expectedConnectionString, array $expectedVariables, array $dbConfig): void
{
$connection = $this->createMock(MySqlConnection::class);
$connection->method('getConfig')->willReturn($dbConfig);

$schemaState = new MySqlSchemaState($connection);

// test connectionString
$method = new \ReflectionMethod(get_class($schemaState), 'connectionString');
$connString = tap($method)->setAccessible(true)->invoke($schemaState);

self::assertEquals($expectedConnectionString, $connString);

// test baseVariables
$method = new \ReflectionMethod(get_class($schemaState), 'baseVariables');
$variables = tap($method)->setAccessible(true)->invoke($schemaState, $dbConfig);

self::assertEquals($expectedVariables, $variables);
}

public function provider(): \Generator
{
yield 'default' => [
' --user="${:LARAVEL_LOAD_USER}" --password="${:LARAVEL_LOAD_PASSWORD}" --host="${:LARAVEL_LOAD_HOST}" --port="${:LARAVEL_LOAD_PORT}"', [
'LARAVEL_LOAD_SOCKET' => '',
'LARAVEL_LOAD_HOST' => '127.0.0.1',
'LARAVEL_LOAD_PORT' => '',
'LARAVEL_LOAD_USER' => 'root',
'LARAVEL_LOAD_PASSWORD' => '',
'LARAVEL_LOAD_DATABASE' => 'forge',
'LARAVEL_LOAD_SSL_CA' => '',
], [
'username' => 'root',
'host' => '127.0.0.1',
'database' => 'forge',
],
];

yield 'ssl_ca' => [
' --user="${:LARAVEL_LOAD_USER}" --password="${:LARAVEL_LOAD_PASSWORD}" --host="${:LARAVEL_LOAD_HOST}" --port="${:LARAVEL_LOAD_PORT}" --ssl-ca="${:LARAVEL_LOAD_SSL_CA}"', [
'LARAVEL_LOAD_SOCKET' => '',
'LARAVEL_LOAD_HOST' => '',
'LARAVEL_LOAD_PORT' => '',
'LARAVEL_LOAD_USER' => 'root',
'LARAVEL_LOAD_PASSWORD' => '',
'LARAVEL_LOAD_DATABASE' => 'forge',
'LARAVEL_LOAD_SSL_CA' => 'ssl.ca',
], [
'username' => 'root',
'database' => 'forge',
'options' => [
\PDO::MYSQL_ATTR_SSL_CA => 'ssl.ca',
],
],
];

yield 'unix socket' => [
' --user="${:LARAVEL_LOAD_USER}" --password="${:LARAVEL_LOAD_PASSWORD}" --socket="${:LARAVEL_LOAD_SOCKET}"', [
'LARAVEL_LOAD_SOCKET' => '/tmp/mysql.sock',
'LARAVEL_LOAD_HOST' => '',
'LARAVEL_LOAD_PORT' => '',
'LARAVEL_LOAD_USER' => 'root',
'LARAVEL_LOAD_PASSWORD' => '',
'LARAVEL_LOAD_DATABASE' => 'forge',
'LARAVEL_LOAD_SSL_CA' => '',
], [
'username' => 'root',
'database' => 'forge',
'unix_socket' => '/tmp/mysql.sock',
],
];
}
}