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
2 changes: 1 addition & 1 deletion src/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private function createRequest(
$request = $this->requestFactory->createRequest($method, $url);

if ([] !== $body) {
$jsonData = json_encode($body, JSON_THROW_ON_ERROR);
$jsonData = json_encode($body, JSON_THROW_ON_ERROR | JSON_PRESERVE_ZERO_FRACTION);

$dataLength = strlen($jsonData);

Expand Down
5 changes: 4 additions & 1 deletion src/KafkaSchemaRegistryApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,9 @@ public function setImportMode(string $mode): bool
*/
private function createRequestBodyFromSchema(string $schema): array
{
return ['schema' => json_encode(json_decode($schema, true, 512, JSON_THROW_ON_ERROR), JSON_THROW_ON_ERROR)];
return ['schema' => json_encode(
json_decode($schema, true, 512, JSON_THROW_ON_ERROR),
JSON_THROW_ON_ERROR | JSON_PRESERVE_ZERO_FRACTION
)];
}
}
20 changes: 14 additions & 6 deletions tests/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ public function testCreateRequest(): void
$this->assertSame('', $response->getUri()->getQuery());
}

public function testCreateRequestWithBody(): void
/**
* @dataProvider requestBodyDataProvider
**/
public function testCreateRequestWithBody(array $body, string $expectedEncodedBody): void
{
$httpClient = new HttpClient(
new Curl(new Psr17Factory()),
Expand All @@ -48,15 +51,11 @@ public function testCreateRequestWithBody(): void
'http://some-url/'
);

$body = ['a' => 'b'];
$jsonEncodedBody = json_encode($body);

/** @var RequestInterface $response */
$response = $this->invokeMethod($httpClient, 'createRequest', ['GET', 'uri', $body]);
$response->getBody()->rewind();

$this->assertSame('9', $response->getHeader('Content-Length')[0]);
$this->assertSame($jsonEncodedBody, $response->getBody()->read(9));
$this->assertSame($expectedEncodedBody, $response->getBody()->getContents());
}

public function testCreateRequestWithQueryString(): void
Expand Down Expand Up @@ -164,4 +163,13 @@ public function testCallMethodWithThrownException(): void
$this->expectException(Exception::class);
$httpClient->call('GET', 'uri');
}

public function requestBodyDataProvider(): array
{
return [
[['a' => 'b'], '{"a":"b"}'],
[['a' => 0.0], '{"a":0.0}'],
[['a' => '{"b":0.0}'], '{"a":"{\"b\":0.0}"}'],
];
}
}
36 changes: 27 additions & 9 deletions tests/KafkaSchemaRegistryApiClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,21 @@ public function testGetSchemaById(): void
$api->getSchemaById(1);
}

public function testRegisterNewSchemaVersion(): void
/**
* @dataProvider schemaDataProvider
**/
public function testRegisterNewSchemaVersion(string $testSchema, string $expectedSchema): void
{
$httpClientMock = $this->getHttpClientMock();

$httpClientMock
->expects(self::once())
->method('call')
->with('POST', sprintf('subjects/%s/versions', self::TEST_SUBJECT_NAME), ['schema' => '[]'])
->with('POST', sprintf('subjects/%s/versions', self::TEST_SUBJECT_NAME), ['schema' => $expectedSchema])
->willReturn([]);

$api = new KafkaSchemaRegistryApiClient($httpClientMock);
$api->registerNewSchemaVersion(self::TEST_SUBJECT_NAME, self::TEST_SCHEMA);
$api->registerNewSchemaVersion(self::TEST_SUBJECT_NAME, $testSchema);
}

public function testCheckSchemaCompatibilityForVersionFalseOnEmptyResponse(): void
Expand All @@ -161,7 +164,10 @@ public function testCheckSchemaCompatibilityForVersionFalseOnEmptyResponse(): vo
self::assertFalse($result);
}

public function testCheckSchemaCompatibilityForVersionTrue(): void
/**
* @dataProvider schemaDataProvider
**/
public function testCheckSchemaCompatibilityForVersionTrue(string $testSchema, string $expectedSchema): void
{
$httpClientMock = $this->getHttpClientMock();

Expand All @@ -171,14 +177,14 @@ public function testCheckSchemaCompatibilityForVersionTrue(): void
->with(
'POST',
sprintf('compatibility/subjects/%s/versions/%s', self::TEST_SUBJECT_NAME, self::TEST_VERSION),
['schema' => '[]']
['schema' => $expectedSchema]
)
->willReturn(['is_compatible' => true]);

$api = new KafkaSchemaRegistryApiClient($httpClientMock);
$result = $api->checkSchemaCompatibilityForVersion(
self::TEST_SUBJECT_NAME,
self::TEST_SCHEMA,
$testSchema,
self::TEST_VERSION
);

Expand Down Expand Up @@ -378,7 +384,10 @@ public function testSetDefaultCompatibilityLeve(): void
self::assertTrue($result);
}

public function testGetVersionForSchema(): void
/**
* @dataProvider schemaDataProvider
**/
public function testGetVersionForSchema(string $testSchema, string $expectedSchema): void
{
$httpClientMock = $this->getHttpClientMock();

Expand All @@ -388,12 +397,12 @@ public function testGetVersionForSchema(): void
->with(
'POST',
sprintf('subjects/%s', self::TEST_SUBJECT_NAME),
['schema' => '[]']
['schema' => $expectedSchema]
)
->willReturn(['version' => self::TEST_VERSION]);

$api = new KafkaSchemaRegistryApiClient($httpClientMock);
$result = $api->getVersionForSchema(self::TEST_SUBJECT_NAME, self::TEST_SCHEMA);
$result = $api->getVersionForSchema(self::TEST_SUBJECT_NAME, $testSchema);

self::assertSame((string) self::TEST_VERSION, $result);
}
Expand Down Expand Up @@ -560,4 +569,13 @@ private function getHttpClientMock(): MockObject
->onlyMethods(['call'])
->getMock();
}

public function schemaDataProvider(): array
{
return [
'empty schema' => ['{}', '[]'],
'schema' => ['{"id": "string"}', '{"id":"string"}'],
'preserves zero fraction' => ['{"double":0.0}', '{"double":0.0}'],
];
}
}