From 2e48072bcafc1d4bedc3263d366438ddb2bf5a6b Mon Sep 17 00:00:00 2001 From: Marco Szulik Date: Tue, 10 Dec 2024 10:50:46 +0100 Subject: [PATCH] add a test for the protector configuration --- src/Classes/AbstractMySqlSchemaStateProxy.php | 5 + src/Classes/MySqlSchemaStateProxy.php | 23 +++-- src/Classes/PostgresSchemaStateProxy.php | 15 +-- src/Traits/HasConfiguration.php | 5 + tests/feature/ExportDumpTest.php | 91 +++++++++++++++++-- 5 files changed, 116 insertions(+), 23 deletions(-) diff --git a/src/Classes/AbstractMySqlSchemaStateProxy.php b/src/Classes/AbstractMySqlSchemaStateProxy.php index a74f53c..064c62b 100644 --- a/src/Classes/AbstractMySqlSchemaStateProxy.php +++ b/src/Classes/AbstractMySqlSchemaStateProxy.php @@ -58,4 +58,9 @@ protected function appendMigrationData(string $path): void { $this->schemaState->appendMigrationData(...func_get_args()); } + + public function getConditionalParameters(): array + { + return []; + } } diff --git a/src/Classes/MySqlSchemaStateProxy.php b/src/Classes/MySqlSchemaStateProxy.php index 27bce0e..a336aa9 100644 --- a/src/Classes/MySqlSchemaStateProxy.php +++ b/src/Classes/MySqlSchemaStateProxy.php @@ -39,15 +39,6 @@ protected function getCommandString(): string { $command = 'mysqldump '.$this->schemaState->connectionString().' '; - $conditionalParameters = [ - '--set-gtid-purged=OFF' => !$this->schemaState->connection->isMaria(), - '--no-create-db' => !$this->protector->shouldCreateDb(), - '--skip-comments' => !$this->protector->shouldDumpComments(), - '--skip-set-charset' => !$this->protector->shouldDumpCharsets(), - '--no-data' => !$this->protector->shouldDumpData(), - '--no-tablespaces' => !$this->protector->shouldUseTablespaces(), - ]; - $parameters = [ '--add-locks', '--routines', @@ -55,10 +46,22 @@ protected function getCommandString(): string '--column-statistics=0', '--result-file="${:LARAVEL_LOAD_PATH}"', '--max-allowed-packet='.$this->protector->getMaxPacketLength(), - ...array_keys(array_filter($conditionalParameters)), + ...array_keys(array_filter($this->getConditionalParameters())), '"${:LARAVEL_LOAD_DATABASE}"', ]; return $command.implode(' ', $parameters); } + + public function getConditionalParameters(): array + { + return [ + '--set-gtid-purged=OFF' => !$this->schemaState->connection->isMaria(), + '--no-create-db' => !$this->protector->shouldCreateDb(), + '--skip-comments' => !$this->protector->shouldDumpComments(), + '--skip-set-charset' => !$this->protector->shouldDumpCharsets(), + '--no-data' => !$this->protector->shouldDumpData(), + '--no-tablespaces' => !$this->protector->shouldUseTablespaces(), + ]; + } } diff --git a/src/Classes/PostgresSchemaStateProxy.php b/src/Classes/PostgresSchemaStateProxy.php index 2bc7d8e..24e746f 100644 --- a/src/Classes/PostgresSchemaStateProxy.php +++ b/src/Classes/PostgresSchemaStateProxy.php @@ -27,17 +27,20 @@ public function dump(Connection $connection, $path) protected function getBaseDumpArguments(): array { - $conditionalArguments = [ + return [ + ...parent::getBaseDumpArguments(), + ...array_keys(array_filter($this->getConditionalParameters())), + ]; + } + + public function getConditionalParameters(): array + { + return [ '--create' => $this->protector->shouldCreateDb(), '--clean' => $this->protector->shouldCreateDb() && $this->protector->shouldDropDb(), '--verbose' => $this->protector->shouldDumpComments(), '--schema-only' => !$this->protector->shouldDumpData(), '--no-tablespaces' => !$this->protector->shouldUseTablespaces(), ]; - - return [ - ...parent::getBaseDumpArguments(), - ...array_keys(array_filter($conditionalArguments)), - ]; } } diff --git a/src/Traits/HasConfiguration.php b/src/Traits/HasConfiguration.php index 601718b..b1989c2 100644 --- a/src/Traits/HasConfiguration.php +++ b/src/Traits/HasConfiguration.php @@ -304,6 +304,11 @@ protected function getPrivateKey(): string return env($this->privateKeyName, ''); } + public function getConnectionName(): string + { + return $this->connectionName; + } + /** * Retrieves the server url of the dump endpoint. */ diff --git a/tests/feature/ExportDumpTest.php b/tests/feature/ExportDumpTest.php index 53c9608..f23cab9 100644 --- a/tests/feature/ExportDumpTest.php +++ b/tests/feature/ExportDumpTest.php @@ -2,11 +2,13 @@ namespace Cybex\Protector\Tests\feature; +use Cybex\Protector\Protector; use Cybex\Protector\Tests\TestCase; use Illuminate\Contracts\Filesystem\Filesystem; use Illuminate\Http\Request; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Config; +use Illuminate\Support\Facades\DB; use PDOException; use Symfony\Component\HttpFoundation\StreamedResponse; @@ -25,7 +27,7 @@ protected function setUp(): void $this->disk = $this->getFakeDumpDisk(); $this->baseDirectory = Config::get('protector.baseDirectory'); - $this->filePath = sprintf('%s/dump.sql', $this->baseDirectory); + $this->filePath = sprintf('%s/dump.sql', $this->baseDirectory); $this->emptyDumpPath = 'testDumps/dump.sql'; } @@ -36,7 +38,7 @@ public function createDestinationFilePath() { $this->disk->deleteDirectory(Config::get('protector.baseDirectory')); - $filePath = $this->protector->createDestinationFilePath(__FUNCTION__); + $filePath = $this->protector->createDestinationFilePath(__FUNCTION__); $destinationFilePath = $this->disk->path($filePath); $this->runProtectedMethod('createDirectory', [$filePath, $this->disk]); @@ -50,7 +52,7 @@ public function createDestinationFilePathWithSubFolder() { $this->disk->deleteDirectory(Config::get('protector.baseDirectory')); - $filePath = $this->protector->createDestinationFilePath(__FUNCTION__, __FUNCTION__); + $filePath = $this->protector->createDestinationFilePath(__FUNCTION__, __FUNCTION__); $destinationFilePath = $this->disk->path($filePath); $this->runProtectedMethod('createDirectory', [$filePath, $this->disk]); @@ -99,10 +101,10 @@ public function failGeneratingDumpWhenTryingToConnectToDatabase() { // Provide an database connection to a non-existing database. Config::set('database.connections.invalid', [ - 'driver' => 'mysql', - 'url' => env('DATABASE_URL'), - 'host' => env('DB_HOST', '127.0.0.1'), - 'port' => env('DB_PORT', '3306'), + 'driver' => 'mysql', + 'url' => env('DATABASE_URL'), + 'host' => env('DB_HOST', '127.0.0.1'), + 'port' => env('DB_PORT', '3306'), 'database' => 'invalid_database_name', 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), @@ -128,4 +130,79 @@ public function createsStreamedFileDownloadResponse() $this->assertInstanceOf(StreamedResponse::class, $response); $this->assertEquals(200, $response->getStatusCode()); } + + /** + * @test + * @dataProvider provideForHasCorrectConfiguration + */ + public function hasCorrectConfiguration(array $conditionalParams, callable $createConfiguredProtector): void + { + $protector = $createConfiguredProtector(); + + $connection = DB::connection($protector->getConnectionName()); + $schemaState = $connection->getSchemaState(); + $schemaStateProxy = $this->runProtectedMethod('getProxyForSchemaState', [$schemaState]); + + $conditionalParameters = $schemaStateProxy->getConditionalParameters(); + + $this->assertEquals($conditionalParams[$connection->getDriverName()], $conditionalParameters); + } + + public static function provideForHasCorrectConfiguration(): array + { + return [ + 'fully enabled' => [ + 'conditionalParams' => [ + 'pgsql' => [ + '--create' => true, + '--clean' => true, + '--verbose' => true, + '--schema-only' => false, + '--no-tablespaces' => false, + ], + 'mysql' => [ + '--set-gtid-purged=OFF' => false, + '--no-create-db' => false, + '--skip-comments' => false, + '--skip-set-charset' => false, + '--no-data' => false, + '--no-tablespaces' => false, + ] + ], + 'createConfiguredProtector' => fn() => app('protector') + ->withCreateDb() + ->withDropDb() + ->withComments() + ->withCharsets() + ->withData() + ->withTablespaces() + ], + 'fully disabled' => [ + 'conditionalParams' => [ + 'pgsql' => [ + '--create' => false, + '--clean' => false, + '--verbose' => false, + '--schema-only' => true, + '--no-tablespaces' => true, + ], + 'mysql' => [ + '--set-gtid-purged=OFF' => false, + '--no-create-db' => true, + '--skip-comments' => true, + '--skip-set-charset' => true, + '--no-data' => true, + '--no-tablespaces' => true, + ] + ], + 'createConfiguredProtector' => fn() => app('protector') + ->withoutCreateDb() + ->withoutDropDb() + ->withoutComments() + ->withoutCharsets() + ->withoutData() + ->withoutTablespaces() + ], + ]; + } }