Skip to content

Commit

Permalink
Replace Logger mock with TestHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
romantmb committed May 10, 2024
1 parent b282c8b commit 322afc2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
8 changes: 3 additions & 5 deletions src/Migrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private function restoreDestinationProject(array $restoreCredentials): void

private function migrateSecrets(): void
{
$this->logger->info('Migrating secrets in configurations');
$this->logger->info('Migrating secrets in configurations', ['secrets']);

$sourceClient = $this->createSourceClient();

Expand All @@ -171,7 +171,7 @@ private function migrateSecrets(): void
$sourceComponentsApi = new Components($sourceClient);
$components = $sourceComponentsApi->listComponents();
if (!$components) {
$this->logger->info('There are no components to migrate.');
$this->logger->info('There are no components to migrate.', ['secrets']);
return;
}

Expand All @@ -194,11 +194,9 @@ private function migrateSecrets(): void
(string) $defaultSourceBranch['id'],
);

$this->logger->debug($response['message']);
$this->logger->info($response['message'], ['secrets']);
}
}

$this->logger->info('Secrets in configurations have been migrated.');
}

private function createSourceClient(): StorageClient
Expand Down
42 changes: 31 additions & 11 deletions tests/phpunit/MigrateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
use Keboola\EncryptionApiClient\Migrations;
use Keboola\StorageApi\Client as StorageClient;
use Keboola\Syrup\ClientException;
use Monolog\Handler\TestHandler;
use Monolog\Logger;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

class MigrateTest extends TestCase
Expand Down Expand Up @@ -129,7 +130,8 @@ public function testMigrateSuccess(
new ConfigDefinition()
);

$loggerMock = $this->createMock(LoggerInterface::class);
$logsHandler = new TestHandler();
$logger = new Logger('tests', [$logsHandler]);

/** @var JobRunner $sourceJobRunnerMock */
/** @var JobRunner $destJobRunnerMock */
Expand All @@ -139,7 +141,7 @@ public function testMigrateSuccess(
$destJobRunnerMock,
'https://dest-stack/',
'dest-token',
$loggerMock,
$logger,
);

$sourceClientMock = $this->createMock(StorageClient::class);
Expand Down Expand Up @@ -205,14 +207,6 @@ public function testMigrateSuccess(
'data' => [],
];
});

$loggerMock->expects(self::exactly(3))
->method('debug')
->withConsecutive(
[self::equalTo('Configuration with ID \'101\' successfully migrated to stack \'dest-stack\'.')],
[self::equalTo('Configuration with ID \'102\' successfully migrated to stack \'dest-stack\'.')],
[self::equalTo('Configuration with ID \'201\' successfully migrated to stack \'dest-stack\'.')]
);
} else {
$migrationsClientMock->expects(self::never())->method('migrateConfiguration');
}
Expand All @@ -221,6 +215,32 @@ public function testMigrateSuccess(
$migrate->setMigrationsClientFactory(fn() => $migrationsClientMock);

$migrate->run();

if ($migrateSecrets) {
$records = array_filter(
$logsHandler->getRecords(),
fn(array $record) => in_array('secrets', $record['context'] ?? [], true)
);
self::assertCount(4, $records);

$record = array_shift($records);
self::assertSame('Migrating secrets in configurations', $record['message']);
$record = array_shift($records);
self::assertSame(
'Configuration with ID \'101\' successfully migrated to stack \'dest-stack\'.',
$record['message']
);
$record = array_shift($records);
self::assertSame(
'Configuration with ID \'102\' successfully migrated to stack \'dest-stack\'.',
$record['message']
);
$record = array_shift($records);
self::assertSame(
'Configuration with ID \'201\' successfully migrated to stack \'dest-stack\'.',
$record['message']
);
}
}

public function testShouldFailOnSnapshotError(): void
Expand Down

0 comments on commit 322afc2

Please sign in to comment.