Skip to content

Commit

Permalink
add a test for the protector configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
mszulik committed Dec 10, 2024
1 parent 0c3f8b8 commit 2e48072
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 23 deletions.
5 changes: 5 additions & 0 deletions src/Classes/AbstractMySqlSchemaStateProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,9 @@ protected function appendMigrationData(string $path): void
{
$this->schemaState->appendMigrationData(...func_get_args());
}

public function getConditionalParameters(): array
{
return [];
}
}
23 changes: 13 additions & 10 deletions src/Classes/MySqlSchemaStateProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,29 @@ 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',
'--tz-utc',
'--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(),
];
}
}
15 changes: 9 additions & 6 deletions src/Classes/PostgresSchemaStateProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
];
}
}
5 changes: 5 additions & 0 deletions src/Traits/HasConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
91 changes: 84 additions & 7 deletions tests/feature/ExportDumpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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';
}

Expand All @@ -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]);
Expand All @@ -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]);
Expand Down Expand Up @@ -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', ''),
Expand All @@ -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()
],
];
}
}

0 comments on commit 2e48072

Please sign in to comment.