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
|
<?php
namespace Illuminate\Tests\Testing\Concerns;
use Illuminate\Config\Repository as Config;
use Illuminate\Container\Container;
use Illuminate\Support\Facades\DB;
use Illuminate\Testing\Concerns\TestDatabases;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use ReflectionMethod;
class TestDatabasesTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
Container::setInstance($container = new Container);
$container->singleton('config', function () {
return m::mock(Config::class)
->shouldReceive('get')
->once()
->with('database.default', null)
->andReturn('mysql')
->getMock();
});
$_SERVER['LARAVEL_PARALLEL_TESTING'] = 1;
}
public function testSwitchToDatabaseWithoutUrl()
{
DB::shouldReceive('purge')->once();
config()->shouldReceive('get')
->once()
->with('database.connections.mysql.url', false)
->andReturn(false);
config()->shouldReceive('set')
->once()
->with('database.connections.mysql.database', 'my_database_test_1');
$this->switchToDatabase('my_database_test_1');
}
/**
* @dataProvider databaseUrls
*/
public function testSwitchToDatabaseWithUrl($testDatabase, $url, $testUrl)
{
DB::shouldReceive('purge')->once();
config()->shouldReceive('get')
->once()
->with('database.connections.mysql.url', false)
->andReturn($url);
config()->shouldReceive('set')
->once()
->with('database.connections.mysql.url', $testUrl);
$this->switchToDatabase($testDatabase);
}
public function switchToDatabase($database)
{
$instance = new class
{
use TestDatabases;
};
$method = new ReflectionMethod($instance, 'switchToDatabase');
$method->invoke($instance, $database);
}
public static function databaseUrls()
{
return [
[
'my_database_test_1',
'mysql://root:@127.0.0.1/my_database?charset=utf8mb4',
'mysql://root:@127.0.0.1/my_database_test_1?charset=utf8mb4',
],
[
'my_database_test_1',
'mysql://my-user:@localhost/my_database',
'mysql://my-user:@localhost/my_database_test_1',
],
[
'my-database_test_1',
'postgresql://my_database_user:@127.0.0.1/my-database?charset=utf8',
'postgresql://my_database_user:@127.0.0.1/my-database_test_1?charset=utf8',
],
];
}
protected function tearDown(): void
{
parent::tearDown();
Container::setInstance(null);
DB::clearResolvedInstances();
DB::setFacadeApplication(null);
unset($_SERVER['LARAVEL_PARALLEL_TESTING']);
m::close();
}
}
|