From def985eac074c92494cf469657671c1722dea670 Mon Sep 17 00:00:00 2001 From: Nick Date: Mon, 7 Oct 2019 12:39:27 +0200 Subject: [PATCH 1/3] improve register of schemas --- src/Command/RegisterChangedSchemasCommand.php | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/Command/RegisterChangedSchemasCommand.php b/src/Command/RegisterChangedSchemasCommand.php index bc1af08..35d0e81 100644 --- a/src/Command/RegisterChangedSchemasCommand.php +++ b/src/Command/RegisterChangedSchemasCommand.php @@ -127,17 +127,22 @@ protected function isLocalSchemaCompatible( * @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 +166,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; } From 362266618a05de71b3f59d1365508de8154fe63c Mon Sep 17 00:00:00 2001 From: Nick Date: Mon, 7 Oct 2019 12:43:19 +0200 Subject: [PATCH 2/3] fix cs and stan --- src/Command/RegisterChangedSchemasCommand.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Command/RegisterChangedSchemasCommand.php b/src/Command/RegisterChangedSchemasCommand.php index 35d0e81..18c71bb 100644 --- a/src/Command/RegisterChangedSchemasCommand.php +++ b/src/Command/RegisterChangedSchemasCommand.php @@ -124,7 +124,6 @@ protected function isLocalSchemaCompatible( /** * @param string $schemaName * @param string $localSchema - * @param string $latestVersion * @return boolean */ protected function isAlreadyRegistered( @@ -139,7 +138,6 @@ protected function isAlreadyRegistered( $localSchema ); } catch (\Throwable $e) { - } return null !== $version; From 8f2e9902462faa0d4ebd278f31d4fd5eeadf71d4 Mon Sep 17 00:00:00 2001 From: Nick Date: Mon, 7 Oct 2019 13:12:10 +0200 Subject: [PATCH 3/3] up coverage --- .../RegisterChangedSchemasCommandTest.php | 41 +++++++++++++++++-- 1 file changed, 37 insertions(+), 4 deletions(-) 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 +}