Skip to content

Commit

Permalink
mariadb
Browse files Browse the repository at this point in the history
  • Loading branch information
inmanturbo committed Feb 3, 2024
1 parent 7d85731 commit cd675fd
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 30 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
},
"require-dev": {
"laravel/pint": "^1.13.7",
"nunomaduro/collision": "^7.10",
"orchestra/testbench": "^8.17",
"nunomaduro/collision": "^8.0",
"orchestra/testbench": "^9.0",
"pestphp/pest": "^2.28",
"pestphp/pest-plugin-arch": "^2.5",
"pestphp/pest-plugin-laravel": "^2.2",
Expand Down
129 changes: 129 additions & 0 deletions tests/MariadbTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

use Envor\DatabaseManager\MySQLDatabaseManager;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Process;
use Spatie\Docker\DockerContainer;

beforeEach(function () {
if (! `which mysql`) {
$this->fail('MySQL client is not installed');
}

if (! `which docker`) {
$this->fail('Docker is not installed');
}

$this->containerInstance = DockerContainer::create('mariadb:latest')
->setEnvironmentVariable('MARIADB_ROOT_PASSWORD', 'root')
->setEnvironmentVariable('MARIADB_DATABASE', 'database_manager_mariadb')
->name('database_manager_mariadb')
->mapPort(10001, 3306)
->start();

$i = 0;

while ($i < 50) {
$process = Process::run('mysql -u root -proot -P 10001 -h 127.0.0.1 database_manager_mariadb -e "show tables;"');
if ($process->successful()) {
break;
}
sleep(.5);
}

config(['database.connections.database_manager_mariadb' => array_merge(config('database.connections.mariadb'), [
'database' => 'database_manager_mariadb',
'host' => '127.0.0.1',
'port' => '10001',
'username' => 'root',
'password' => 'root',
])]);

$this->databaseManager = getDatabaseMysqlManager('database_manager_mariadb');
$this->databaseManager->createDatabase('test_database');
$this->time = Carbon::now();
});

afterEach(function () {
$this->containerInstance->stop();
});

it('can create a database', function () {
expect($this->databaseManager->databaseExists('test_database'))->toBeTrue();
});

it('can delete a database without erasing it from disk', function () {
expect($this->databaseManager->databaseExists('test_database'))->toBeTrue();

$this->databaseManager->deleteDatabase('test_database', $this->time);

expect($this->databaseManager->databaseExists('test_database'))->toBeFalse();
expect($this->databaseManager->databaseExists('deleted_'.$this->time->format('Y_m_d_H_i_s_').'test_database'))->toBeTrue();
});

it('can check if a database exists', function () {
expect($this->databaseManager->databaseExists('test_database_two'))->toBeFalse();

expect($this->databaseManager->databaseExists('test_database'))->toBeTrue();
});

it('can make a connection config', function () {
$connectionConfig = $this->databaseManager->makeConnectionConfig(
baseConfig: config('database.connections.database_manager_mariadb'),
databaseName: 'test_database',
);

expect($connectionConfig)->toBe(array_merge(config('database.connections.database_manager_mariadb'), [
'database' => 'test_database',
]));
});

it('can set a connection', function () {
$connection = getProperty($this->databaseManager, 'connection');
expect($connection)->toBe('database_manager_mariadb');
});

it('can get a list of table names', function () {
DB::connection('database_manager_mariadb')
->statement('CREATE TABLE test_table (id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (id))');

expect($this->databaseManager->listTableNames())->toBe(['test_table']);
});

it('can erase a database from disk', function () {
expect($this->databaseManager->databaseExists('test_database'))->toBeTrue();

$this->databaseManager->eraseDatabase('test_database');

expect($this->databaseManager->databaseExists('test_database'))->toBeFalse();
expect($this->databaseManager->databaseExists('deleted_'.$this->time->format('Y_m_d_H_i_s_').'test_database'))->toBeFalse();
});

it('can cleanup old databases', function () {
$threeDaysOld = $this->time->subDays(3);

$this->databaseManager->deleteDatabase('test_database', $threeDaysOld);

expect($this->databaseManager->databaseExists('deleted_'.$threeDaysOld->format('Y_m_d_H_i_s_').'test_database'))->toBeTrue();

$this->databaseManager->cleanupOldDatabases(2);

expect($this->databaseManager->databaseExists('deleted_'.$threeDaysOld->format('Y_m_d_H_i_s_').'test_database'))->toBeFalse();

$this->databaseManager->createDatabase('test_database');

$this->databaseManager->deleteDatabase('test_database', $this->time);

expect($this->databaseManager->databaseExists('deleted_'.$this->time->format('Y_m_d_H_i_s_').'test_database'))->toBeTrue();

$count = $this->databaseManager->cleanupOldDatabases(0);

expect($count)->toBe(1);

expect($this->databaseManager->databaseExists('deleted_'.$this->time->format('Y_m_d_H_i_s_').'test_database'))->toBeFalse();
});

it('can get a database name', function () {
expect($this->databaseManager->getDatabaseName('test_database'))->toBe('test_database');
});
42 changes: 14 additions & 28 deletions tests/MySQLDatabaseManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,32 @@
$this->fail('Docker is not installed');
}

$this->containerInstance = DockerContainer::create('mariadb:latest')
->setEnvironmentVariable('MARIADB_ROOT_PASSWORD', 'root')
->setEnvironmentVariable('MARIADB_DATABASE', 'database_manager_tests')
->name('database_manager_tests')
->mapPort(10001, 3306)
$this->containerInstance = DockerContainer::create('mysql:latest')
->setEnvironmentVariable('MYSQL_ROOT_PASSWORD', 'root')
->setEnvironmentVariable('MYSQL_DATABASE', 'database_manager_mysql')
->name('database_manager_mysql')
->mapPort(10002, 3306)
->start();

$i = 0;

while ($i < 50) {
$process = Process::run('mysql -u root -proot -P 10001 -h 127.0.0.1 database_manager_tests -e "show tables;"');
$process = Process::run('mysql -u root -proot -P 10002 -h 127.0.0.1 database_manager_mysql -e "show tables;"');
if ($process->successful()) {
break;
}
sleep(.5);
}

config(['database.connections.database_manager_tests' => array_merge(config('database.connections.mysql'), [
'database' => 'database_manager_tests',
config(['database.connections.database_manager_mysql' => array_merge(config('database.connections.mysql'), [
'database' => 'database_manager_mysql',
'host' => '127.0.0.1',
'port' => '10001',
'port' => '10002',
'username' => 'root',
'password' => 'root',
])]);

$this->databaseManager = getDatabaseManager();
$this->databaseManager = getDatabaseMysqlManager('database_manager_mysql');
$this->databaseManager->createDatabase('test_database');
$this->time = Carbon::now();
});
Expand All @@ -49,20 +49,6 @@
$this->containerInstance->stop();
});

function getDatabaseManager(): MySQLDatabaseManager
{
return (new MySQLDatabaseManager)
->setConnection('database_manager_tests');
}

function getProperty($object, $property)
{
$reflection = (new \ReflectionClass($object))->getProperty($property);
$reflection->setAccessible(true);

return $reflection->getValue($object);
}

it('can create a database', function () {
expect($this->databaseManager->databaseExists('test_database'))->toBeTrue();
});
Expand All @@ -84,22 +70,22 @@ function getProperty($object, $property)

it('can make a connection config', function () {
$connectionConfig = $this->databaseManager->makeConnectionConfig(
baseConfig: config('database.connections.database_manager_tests'),
baseConfig: config('database.connections.database_manager_mysql'),
databaseName: 'test_database',
);

expect($connectionConfig)->toBe(array_merge(config('database.connections.database_manager_tests'), [
expect($connectionConfig)->toBe(array_merge(config('database.connections.database_manager_mysql'), [
'database' => 'test_database',
]));
});

it('can set a connection', function () {
$connection = getProperty($this->databaseManager, 'connection');
expect($connection)->toBe('database_manager_tests');
expect($connection)->toBe('database_manager_mysql');
});

it('can get a list of table names', function () {
DB::connection('database_manager_tests')
DB::connection('database_manager_mysql')
->statement('CREATE TABLE test_table (id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (id))');

expect($this->databaseManager->listTableNames())->toBe(['test_table']);
Expand Down
15 changes: 15 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
<?php

use Envor\DatabaseManager\MySQLDatabaseManager;
use Envor\DatabaseManager\Tests\TestCase;

uses(TestCase::class)->in(__DIR__);

function getDatabaseMysqlManager(string $conection): MySQLDatabaseManager
{
return (new MySQLDatabaseManager)
->setConnection($conection);
}

function getProperty($object, $property)
{
$reflection = (new \ReflectionClass($object))->getProperty($property);
$reflection->setAccessible(true);

return $reflection->getValue($object);
}

0 comments on commit cd675fd

Please sign in to comment.