Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions src/Command/RegisterChangedSchemasCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,20 +124,23 @@ protected function isLocalSchemaCompatible(
/**
* @param string $schemaName
* @param string $localSchema
* @param string $latestVersion
* @return boolean
*/
protected function isLocalSchemaEqualToLatestSchema(
protected function isAlreadyRegistered(
string $schemaName,
string $localSchema,
string $latestVersion
string $localSchema
): bool {
$schema = $this->schemaRegistryApi->getSchemaByVersion(
$schemaName,
$latestVersion
);
$version = null;

try {
$version = $this->schemaRegistryApi->getVersionForSchema(
$schemaName,
$localSchema
);
} catch (\Throwable $e) {
}

return $schema === $localSchema;
return null !== $version;
}

/**
Expand All @@ -161,7 +164,7 @@ private function registerFiles(array $avroFiles, OutputInterface $output, array
$latestVersion = $this->schemaRegistryApi->getLatestSchemaVersion($schemaName);

if (null !== $latestVersion) {
if (true === $this->isLocalSchemaEqualToLatestSchema($schemaName, $localSchema, $latestVersion)) {
if (true === $this->isAlreadyRegistered($schemaName, $localSchema)) {
$output->writeln(sprintf('Schema %s has been skipped (no change)', $schemaName));
continue;
}
Expand Down
41 changes: 37 additions & 4 deletions tests/Command/RegisterChangedSchemasCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class RegisterChangedSchemasCommandTest extends AbstractSchemaRegistryTestCase
protected const DUMMY_SCHEMA = <<<EOF
{
"type": "record",
"name": "evolution",
"namespace": "com.landoop",
"name": "test",
"namespace": "ch.jobcloud",
"doc": "This is a sample Avro schema to get you started. Please edit",
"fields": [
{
Expand Down Expand Up @@ -72,6 +72,11 @@ protected function generateFiles(int $numberOfFiles, string $contents = self::DU
$contents
);
});

file_put_contents(
sprintf('%s/test.txt', self::SCHEMA_DIRECTORY),
'bla'
);
}

public function testOutputWhenCommandRegisterWithSuccess():void
Expand Down Expand Up @@ -113,7 +118,7 @@ public function testOutputWhenCommandSuccessWithSkipping():void
/** @var MockObject|SchemaRegistryApi $schemaRegistryApi */
$schemaRegistryApi = $this->makeMock(SchemaRegistryApi::class, [
'checkSchemaCompatibilityForVersion' => TRUE,
'getSchemaByVersion' => json_encode(json_decode(self::DUMMY_SCHEMA)),
'getVersionForSchema' => 1,
'createNewSchemaVersion',
'getLatestSchemaVersion' => '1'
]);
Expand All @@ -138,6 +143,34 @@ public function testOutputWhenCommandSuccessWithSkipping():void
self::assertEquals(0, $commandTester->getStatusCode());
}

public function testOutputWhenCommandFailsRegisteringASchema():void
{
$this->generateFiles(1, 'asdf');

/** @var MockObject|SchemaRegistryApi $schemaRegistryApi */
$schemaRegistryApi = $this->makeMock(SchemaRegistryApi::class, [
'checkSchemaCompatibilityForVersion' => TRUE,
'getVersionForSchema' => null,
'createNewSchemaVersion',
'getLatestSchemaVersion' => '1'
]);

$application = new Application();
$application->add(new RegisterChangedSchemasCommand($schemaRegistryApi));
$command = $application->find('kafka-schema-registry:register:changed');
$commandTester = new CommandTester($command);

$commandTester->execute([
'schemaDirectory' => self::SCHEMA_DIRECTORY
]);

$commandOutput = trim($commandTester->getDisplay());

self::assertStringContainsString('Skipping test.schema.1 for now because is not a schema we know about.', $commandOutput);

self::assertEquals(1, $commandTester->getStatusCode());
}

public function testOutputTotalFailDueToIncompatibility():void
{
$this->generateFiles(5);
Expand Down Expand Up @@ -165,4 +198,4 @@ public function testOutputTotalFailDueToIncompatibility():void

self::assertEquals(1, $commandTester->getStatusCode());
}
}
}