Skip to content

Commit

Permalink
Allow database collation to be configurable via environment variables.
Browse files Browse the repository at this point in the history
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
crynobone committed Nov 24, 2023
1 parent 8e94aa8 commit 854d74f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Concerns/Database/HandlesConnections.php
Expand Up @@ -25,6 +25,7 @@ final protected function usesDatabaseConnectionsEnvironmentVariables(Repository
'database' => ['DB', 'DATABASE'],
'username' => ['USER', 'USERNAME'],
'password' => 'PASSWORD',
'collation' => 'COLLATION',
];

foreach ($options as $key => $value) {
Expand Down
49 changes: 49 additions & 0 deletions tests/Concerns/Database/HandlesConnectionsTest.php
@@ -0,0 +1,49 @@
<?php

namespace Orchestra\Testbench\Tests\Concerns\Database;

use Illuminate\Contracts\Config\Repository;
use Mockery as m;
use Orchestra\Testbench\Concerns\Database\HandlesConnections;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

class HandlesConnectionsTest extends TestCase
{
use HandlesConnections;

/**
* Teardown the test environment.
*/
protected function tearDown(): void
{
m::close();
}

/** @test */
public function it_can_build_mysql_connection()
{
$config = m::mock(Repository::class);

$_ENV['MYSQL_URL'] = 'mysql://127.0.0.1:3306';

$config->shouldReceive('get')->never()->with('database.connections.mysql.url')->andReturnNull()
->shouldReceive('get')->once()->with('database.connections.mysql.host')->andReturn('127.0.0.1')
->shouldReceive('get')->once()->with('database.connections.mysql.port')->andReturn('3306')
->shouldReceive('get')->once()->with('database.connections.mysql.database')->andReturn('laravel')
->shouldReceive('get')->once()->with('database.connections.mysql.username')->andReturn('root')
->shouldReceive('get')->once()->with('database.connections.mysql.password')->andReturn('secret')
->shouldReceive('get')->once()->with('database.connections.mysql.collation')->andReturn('utf8mb4_0900_ai_ci')
->shouldReceive('set')->once()->with([
'database.connections.mysql.url' => 'mysql://127.0.0.1:3306',
'database.connections.mysql.host' => '127.0.0.1',
'database.connections.mysql.port' => '3306',
'database.connections.mysql.database' => 'laravel',
'database.connections.mysql.username' => 'root',
'database.connections.mysql.password' => 'secret',
'database.connections.mysql.collation' => 'utf8mb4_0900_ai_ci',
]);

$this->usesDatabaseConnectionsEnvironmentVariables($config, 'mysql', 'MYSQL');
}
}

0 comments on commit 854d74f

Please sign in to comment.