From cd675fde746e25f6cc2b275df94c2cbbb224e5b9 Mon Sep 17 00:00:00 2001 From: = Date: Sat, 3 Feb 2024 03:00:05 -0500 Subject: [PATCH] mariadb --- composer.json | 4 +- tests/MariadbTest.php | 129 +++++++++++++++++++++++++++++ tests/MySQLDatabaseManagerTest.php | 42 ++++------ tests/Pest.php | 15 ++++ 4 files changed, 160 insertions(+), 30 deletions(-) create mode 100644 tests/MariadbTest.php diff --git a/composer.json b/composer.json index 8b3a218..6ad6114 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/tests/MariadbTest.php b/tests/MariadbTest.php new file mode 100644 index 0000000..8af1ab3 --- /dev/null +++ b/tests/MariadbTest.php @@ -0,0 +1,129 @@ +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'); +}); \ No newline at end of file diff --git a/tests/MySQLDatabaseManagerTest.php b/tests/MySQLDatabaseManagerTest.php index 13c545b..808b5a5 100644 --- a/tests/MySQLDatabaseManagerTest.php +++ b/tests/MySQLDatabaseManagerTest.php @@ -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(); }); @@ -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(); }); @@ -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']); diff --git a/tests/Pest.php b/tests/Pest.php index eb68e1f..8912ea4 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -1,5 +1,20 @@ 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); +} \ No newline at end of file