1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
<?php
namespace Illuminate\Tests\Database;
use Exception;
use Generator;
use Illuminate\Database\MySqlConnection;
use Illuminate\Database\Schema\MySqlSchemaState;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use ReflectionMethod;
use Symfony\Component\Process\Process;
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 = $method->invoke($schemaState);
self::assertEquals($expectedConnectionString, $connString);
// test baseVariables
$method = new ReflectionMethod(get_class($schemaState), 'baseVariables');
$variables = $method->invoke($schemaState, $dbConfig);
self::assertEquals($expectedVariables, $variables);
}
public static 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',
],
];
}
public function testExecuteDumpProcessForDepth()
{
$mockProcess = $this->createMock(Process::class);
$mockProcess->method('setTimeout')->willReturnSelf();
$mockProcess->method('mustRun')->will(
$this->throwException(new Exception('column-statistics'))
);
$mockOutput = $this->createMock(\stdClass::class);
$mockVariables = [];
$schemaState = $this->getMockBuilder(MySqlSchemaState::class)
->disableOriginalConstructor()
->onlyMethods(['makeProcess'])
->getMock();
$schemaState->method('makeProcess')->willReturn($mockProcess);
$this->expectException(Exception::class);
$this->expectExceptionMessage('Dump execution exceeded maximum depth of 30.');
// test executeDumpProcess
$method = new ReflectionMethod(get_class($schemaState), 'executeDumpProcess');
$method->invoke($schemaState, $mockProcess, $mockOutput, $mockVariables, 31);
}
}
|