diff --git a/src/Command/RegisterChangedSchemasCommand.php b/src/Command/RegisterChangedSchemasCommand.php index bc1af08..18c71bb 100644 --- a/src/Command/RegisterChangedSchemasCommand.php +++ b/src/Command/RegisterChangedSchemasCommand.php @@ -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; } /** @@ -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; } diff --git a/tests/Command/RegisterChangedSchemasCommandTest.php b/tests/Command/RegisterChangedSchemasCommandTest.php index e2c53a6..1256e66 100644 --- a/tests/Command/RegisterChangedSchemasCommandTest.php +++ b/tests/Command/RegisterChangedSchemasCommandTest.php @@ -16,8 +16,8 @@ class RegisterChangedSchemasCommandTest extends AbstractSchemaRegistryTestCase protected const DUMMY_SCHEMA = <<makeMock(SchemaRegistryApi::class, [ 'checkSchemaCompatibilityForVersion' => TRUE, - 'getSchemaByVersion' => json_encode(json_decode(self::DUMMY_SCHEMA)), + 'getVersionForSchema' => 1, 'createNewSchemaVersion', 'getLatestSchemaVersion' => '1' ]); @@ -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); @@ -165,4 +198,4 @@ public function testOutputTotalFailDueToIncompatibility():void self::assertEquals(1, $commandTester->getStatusCode()); } -} \ No newline at end of file +}