diff --git a/.circleci/config.yml b/.circleci/config.yml index ff23468..8fb3237 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -5,32 +5,27 @@ orbs: ci-php: jobcloud/ci-php@2.4 workflows: - php-kafka-schema-registry-client: + version: 2 + test-php-kafka-schema-registry-client: jobs: - - ci-caching/build-docker-images: - dockerComposeFile: "./docker/docker-compose.yml" + - ci-caching/build-docker-images - ci-php/install-dependencies: - dockerComposeFile: "./docker/docker-compose.yml" dependencyCheckSumFile: "./composer.json" requires: - ci-caching/build-docker-images - ci-php/coverage: - dockerComposeFile: "./docker/docker-compose.yml" dependencyCheckSumFile: "./composer.json" requires: - ci-php/install-dependencies - ci-php/code-style: - dockerComposeFile: "./docker/docker-compose.yml" dependencyCheckSumFile: "./composer.json" requires: - ci-php/install-dependencies - ci-php/static-analysis: - dockerComposeFile: "./docker/docker-compose.yml" dependencyCheckSumFile: "./composer.json" requires: - ci-php/install-dependencies - ci-php/infection-testing: - dockerComposeFile: "./docker/docker-compose.yml" dependencyCheckSumFile: "./composer.json" requires: - ci-php/install-dependencies diff --git a/.gitignore b/.gitignore index 161b849..d14e45b 100644 --- a/.gitignore +++ b/.gitignore @@ -4,9 +4,7 @@ # Packages vendor/ composer.lock -composer.symlink # Builds & caches /build/ .phpunit.result.cache - diff --git a/README.md b/README.md index fe6abea..1621089 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ composer require jobcloud/php-kafka-schema-registry-client ``` ## Requirements -- php: >= 7.4 +- php: ^7.4|^8.0 ## Supported API calls Currently it supports: diff --git a/composer.json b/composer.json index 588582f..00b2a56 100644 --- a/composer.json +++ b/composer.json @@ -5,35 +5,42 @@ "keywords": ["kafka", "schema", "registry", "php"], "autoload": { "psr-4": { - "Jobcloud\\Kafka\\SchemaRegistryClient\\": "src/", + "Jobcloud\\Kafka\\SchemaRegistryClient\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { "Jobcloud\\Kafka\\SchemaRegistryClient\\Tests\\": "tests/" } }, "authors": [ { - "name": "Jobcloud AG", + "name": "JobCloud AG", "homepage": "https://careers.jobcloud.ch/en/our-team/?term=133" } ], "require": { - "php": ">=7.4", + "php": "^7.4|^8.0", "ext-json": "*", - "psr/http-message": "^1.0", + "psr/http-message": "^1.0.1", "psr/http-client": "^1.0.1" }, "require-dev": { - "infection/infection": "^0.25", - "kriswallsmith/buzz": "^1.0", - "nyholm/psr7": "^1.2", - "php-mock/php-mock-phpunit": "^2.6", - "phpstan/phpstan": "^1.2", - "phpunit/phpunit": "^9.5", - "pimple/pimple": "^3.2", + "infection/infection": "^0.26.6", + "kriswallsmith/buzz": "^1.2.1", + "nyholm/psr7": "^1.5.1", + "php-mock/php-mock-phpunit": "^2.6.0", + "phpstan/phpstan": "^1.7.15", + "phpunit/phpunit": "^9.5.21", + "pimple/pimple": "^3.5.0", "rregeer/phpunit-coverage-check": "^0.3.1", - "squizlabs/php_codesniffer": "^3.4.2" + "squizlabs/php_codesniffer": "^3.7.1" }, "config": { "sort-packages": true, + "platform": { + "php": "7.4.7" + }, "allow-plugins": { "infection/extension-installer": true } diff --git a/docker/dev/php/Dockerfile b/docker/dev/php/Dockerfile index baa4ad3..d87c59f 100644 --- a/docker/dev/php/Dockerfile +++ b/docker/dev/php/Dockerfile @@ -1,21 +1,25 @@ -FROM php:7.4-cli-alpine3.15 +FROM php:8.1-cli-alpine3.16 -ARG HOST_USER_ID +ARG USER_ID + +COPY files/bin /usr/local/bin/ +COPY files/php /phpIni # SYS: Install required packages RUN apk --no-cache upgrade && \ apk --no-cache add bash git sudo autoconf gcc g++ make make shadow -COPY dev/php/files/bin/ /usr/local/bin/ - # Set host user id if given, to avoid permission conflicts in dev -RUN /bin/bash -c 'if [ -n "$HOST_USER_ID" ] && [ "$HOST_USER_ID" -lt 60000 ]; then \ - usermod -u ${HOST_USER_ID} www-data; \ - fi' +RUN if [ -n "$USER_ID" ] && [ "$USER_ID" -lt 60001 ]; then \ + usermod -u ${USER_ID} -o www-data; \ + fi RUN usermod -s /bin/bash www-data && \ echo 'www-data ALL=(ALL) NOPASSWD: ALL' > '/etc/sudoers.d/www-data' +# USER: copy home +COPY --chown=www-data:www-data files/user-home /home/www-data + # PHP: Install php extensions RUN pecl channel-update pecl.php.net && \ pecl install pcov && \ diff --git a/docker/dev/php/files/php/20-pcov.ini b/docker/dev/php/files/php/20-pcov.ini new file mode 100644 index 0000000..3a52c5f --- /dev/null +++ b/docker/dev/php/files/php/20-pcov.ini @@ -0,0 +1 @@ +extension=pcov.so diff --git a/docker/dev/php/files/user-home/.bashrc b/docker/dev/php/files/user-home/.bashrc new file mode 100644 index 0000000..8c1d66f --- /dev/null +++ b/docker/dev/php/files/user-home/.bashrc @@ -0,0 +1,7 @@ +alias cls='printf "\033c"' + +export PS1='\[\e[1;32m\]\h\[\e[0m\] \[\e[1;37m\]\w\[\e[0m\] \[\e[1;32m\]\u\[\e[0m\] \[\e[1;37m\]\$\[\e[0m\] ' + +if [ -f ~/.bash_aliases ]; then + . ~/.bash_aliases +fi diff --git a/docker/docker-compose.ci.yml b/docker/docker-compose.ci.yml new file mode 100644 index 0000000..2a35f96 --- /dev/null +++ b/docker/docker-compose.ci.yml @@ -0,0 +1,13 @@ +version: '3.2' + +services: + php: + hostname: kafka-schema-registry-client-php + container_name: kafka-schema-registry-client-php + build: + context: dev/php + args: + USER_ID: ${USER_ID} + volumes: + - ../:/var/www/html + tty: true diff --git a/docker/docker-compose.dev.yml b/docker/docker-compose.dev.yml new file mode 100644 index 0000000..2a35f96 --- /dev/null +++ b/docker/docker-compose.dev.yml @@ -0,0 +1,13 @@ +version: '3.2' + +services: + php: + hostname: kafka-schema-registry-client-php + container_name: kafka-schema-registry-client-php + build: + context: dev/php + args: + USER_ID: ${USER_ID} + volumes: + - ../:/var/www/html + tty: true diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml deleted file mode 100644 index 5f46b8a..0000000 --- a/docker/docker-compose.yml +++ /dev/null @@ -1,17 +0,0 @@ -version: '3.2' -services: - php: - build: - context: ./ - dockerfile: dev/php/Dockerfile - args: - HOST_USER: ${USER} - HOST_USER_ID: ${USER_ID} - tty: true - hostname: kafka-schema-registry-client-php - container_name: kafka-schema-registry-client-php - volumes: - - ../:/var/www/html - - ~/.bash_aliases:/home/${USER}/.bash_aliases:rw - - ~/.bash_history:/home/${USER}/.bash_history:rw - diff --git a/phpcs.xml b/phpcs.xml index 72cc772..d44e266 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -1,6 +1,7 @@ src + tests diff --git a/src/ErrorHandler.php b/src/ErrorHandler.php index 97e5332..4ba8f20 100644 --- a/src/ErrorHandler.php +++ b/src/ErrorHandler.php @@ -26,10 +26,6 @@ class ErrorHandler implements ErrorHandlerInterface { /** - * @param ResponseInterface $response - * @param string|null $uri - * @param RequestInterface|null $request - * @return void * @throws BackendDatastoreException * @throws ClientException * @throws CompatibilityException diff --git a/src/ErrorHandlerInterface.php b/src/ErrorHandlerInterface.php index aca8580..fa22e2b 100644 --- a/src/ErrorHandlerInterface.php +++ b/src/ErrorHandlerInterface.php @@ -11,8 +11,6 @@ interface ErrorHandlerInterface { /** - * @param ResponseInterface $response - * @param string|null $uri * @throws ClientExceptionInterface * @throws SchemaRegistryExceptionInterface * @throws JsonException diff --git a/src/HttpClient.php b/src/HttpClient.php index 6c9bf5e..5d18caf 100644 --- a/src/HttpClient.php +++ b/src/HttpClient.php @@ -41,14 +41,6 @@ class HttpClient implements HttpClientInterface */ private $errorHandler; - /** - * @param ClientInterface $client - * @param RequestFactoryInterface $requestFactory - * @param ErrorHandlerInterface $errorHandler - * @param string $baseUrl - * @param string|null $username - * @param string|null $password - */ public function __construct( ClientInterface $client, RequestFactoryInterface $requestFactory, @@ -66,11 +58,8 @@ public function __construct( } /** - * @param string $method - * @param string $uri * @param array $body * @param array $queryParams - * @return RequestInterface * @throws JsonException */ private function createRequest( @@ -108,8 +97,6 @@ private function createRequest( } /** - * @param string $method - * @param string $uri * @param array $body * @param array $queryParams * @return mixed diff --git a/src/HttpClientInterface.php b/src/HttpClientInterface.php index 8aa0db0..69b336b 100644 --- a/src/HttpClientInterface.php +++ b/src/HttpClientInterface.php @@ -9,8 +9,6 @@ interface HttpClientInterface { /** - * @param string $method - * @param string $uri * @param array $body * @param array $queryParams * @return mixed diff --git a/src/KafkaSchemaRegistryApiClient.php b/src/KafkaSchemaRegistryApiClient.php index 3a9e1e5..7e5d836 100644 --- a/src/KafkaSchemaRegistryApiClient.php +++ b/src/KafkaSchemaRegistryApiClient.php @@ -15,10 +15,6 @@ class KafkaSchemaRegistryApiClient implements KafkaSchemaRegistryApiClientInterf */ private $httpClient; - /** - * KafkaSchemaRegistryApi constructor. - * @param HttpClientInterface $httpClient - */ public function __construct(HttpClientInterface $httpClient) { $this->httpClient = $httpClient; @@ -36,7 +32,6 @@ public function getSubjects(): array } /** - * @param string $subjectName * @return array * @throws ClientExceptionInterface * @throws SchemaRegistryExceptionInterface @@ -48,8 +43,6 @@ public function getAllSubjectVersions(string $subjectName): array } /** - * @param string $subjectName - * @param string $version * @return array * @throws ClientExceptionInterface * @throws SchemaRegistryExceptionInterface @@ -63,8 +56,6 @@ public function getSchemaByVersion(string $subjectName, string $version = self:: } /** - * @param string $subjectName - * @param string $version * @return array|string * @throws ClientExceptionInterface * @throws SchemaRegistryExceptionInterface @@ -81,9 +72,6 @@ public function getSchemaDefinitionByVersion(string $subjectName, string $versio } /** - * @param string $subjectName - * @param string $version - * @return int|null * @throws ClientExceptionInterface * @throws SchemaRegistryExceptionInterface * @throws JsonException @@ -94,8 +82,6 @@ public function deleteSchemaVersion(string $subjectName, string $version = self: } /** - * @param int $id - * @return string * @throws ClientExceptionInterface * @throws SchemaRegistryExceptionInterface * @throws JsonException @@ -106,8 +92,6 @@ public function getSchemaById(int $id): string } /** - * @param string $schema - * @param string $subjectName * @return array * @throws ClientExceptionInterface * @throws SchemaRegistryExceptionInterface @@ -125,10 +109,6 @@ public function registerNewSchemaVersion(string $subjectName, string $schema): a } /** - * @param string $schema - * @param string $subjectName - * @param string $version - * @return bool * @throws ClientExceptionInterface * @throws SchemaRegistryExceptionInterface * @throws JsonException @@ -145,17 +125,15 @@ public function checkSchemaCompatibilityForVersion( 'POST', sprintf('compatibility/subjects/%s/versions/%s', $subjectName, $version), $this->createRequestBodyFromSchema($schema) - ) ?? []; + ); } catch (SubjectNotFoundException $e) { return true; } - return $results['is_compatible'] === true; + return ($results['is_compatible'] ?? false) === true; } /** - * @param string $subjectName - * @return string|null * @throws ClientExceptionInterface * @throws SchemaRegistryExceptionInterface * @throws JsonException @@ -171,9 +149,6 @@ public function getSubjectCompatibilityLevel(string $subjectName): ?string } /** - * @param string $subjectName - * @param string $level - * @return bool * @throws ClientExceptionInterface * @throws SchemaRegistryExceptionInterface * @throws JsonException @@ -185,7 +160,6 @@ public function setSubjectCompatibilityLevel(string $subjectName, string $level } /** - * @return string * @throws ClientExceptionInterface * @throws SchemaRegistryExceptionInterface * @throws JsonException @@ -197,8 +171,6 @@ public function getDefaultCompatibilityLevel(): string } /** - * @param string $level - * @return bool * @throws ClientExceptionInterface * @throws SchemaRegistryExceptionInterface * @throws JsonException @@ -210,9 +182,6 @@ public function setDefaultCompatibilityLevel(string $level = self::LEVEL_FULL): } /** - * @param string $subjectName - * @param string $schema - * @return string|null * @throws ClientExceptionInterface * @throws SchemaRegistryExceptionInterface * @throws JsonException @@ -226,9 +195,9 @@ public function getVersionForSchema(string $subjectName, string $schema): ?strin 'POST', sprintf('subjects/%s', $subjectName), $this->createRequestBodyFromSchema($schema) - ) ?? []; + ); - return $results['version']; + return $results['version'] ?? null; } catch (SubjectNotFoundException $e) { return null; } catch (SchemaNotFoundException $e) { @@ -237,9 +206,6 @@ public function getVersionForSchema(string $subjectName, string $schema): ?strin } /** - * @param string $subjectName - * @param string $schema - * @return bool * @throws ClientExceptionInterface * @throws SchemaRegistryExceptionInterface * @throws JsonException @@ -250,7 +216,6 @@ public function isSchemaAlreadyRegistered(string $subjectName, string $schema): } /** - * @param string $subjectName * @return array * @throws ClientExceptionInterface * @throws SchemaRegistryExceptionInterface @@ -262,8 +227,6 @@ public function deleteSubject(string $subjectName): array } /** - * @param string $subjectName - * @return string|null * @throws ClientExceptionInterface * @throws SchemaRegistryExceptionInterface * @throws JsonException @@ -276,8 +239,6 @@ public function getLatestSubjectVersion(string $subjectName): ?string } /** - * @param string $mode - * @return bool * @throws ClientExceptionInterface * @throws SchemaRegistryExceptionInterface * @throws JsonException @@ -289,7 +250,6 @@ public function setImportMode(string $mode): bool } /** - * @param string $schema * @return array * @throws JsonException */ diff --git a/src/KafkaSchemaRegistryApiClientInterface.php b/src/KafkaSchemaRegistryApiClientInterface.php index 81d7966..b582b81 100644 --- a/src/KafkaSchemaRegistryApiClientInterface.php +++ b/src/KafkaSchemaRegistryApiClientInterface.php @@ -31,7 +31,6 @@ interface KafkaSchemaRegistryApiClientInterface public function getSubjects(): array; /** - * @param string $subjectName * @return array * @throws ClientExceptionInterface * @throws SchemaRegistryExceptionInterface @@ -40,8 +39,6 @@ public function getSubjects(): array; public function getAllSubjectVersions(string $subjectName): array; /** - * @param string $subjectName - * @param string $version * @return array * @throws ClientExceptionInterface * @throws SchemaRegistryExceptionInterface @@ -50,8 +47,6 @@ public function getAllSubjectVersions(string $subjectName): array; public function getSchemaByVersion(string $subjectName, string $version = 'latest'): array; /** - * @param string $subjectName - * @param string $version * @return array|string * @throws ClientExceptionInterface * @throws SchemaRegistryExceptionInterface @@ -60,9 +55,6 @@ public function getSchemaByVersion(string $subjectName, string $version = 'lates public function getSchemaDefinitionByVersion(string $subjectName, string $version = self::VERSION_LATEST); /** - * @param string $subjectName - * @param string $version - * @return int|null * @throws ClientExceptionInterface * @throws SchemaRegistryExceptionInterface * @throws JsonException @@ -70,8 +62,6 @@ public function getSchemaDefinitionByVersion(string $subjectName, string $versio public function deleteSchemaVersion(string $subjectName, string $version = self::VERSION_LATEST): ?int; /** - * @param int $id - * @return string * @throws ClientExceptionInterface * @throws SchemaRegistryExceptionInterface * @throws JsonException @@ -79,8 +69,6 @@ public function deleteSchemaVersion(string $subjectName, string $version = self: public function getSchemaById(int $id): string; /** - * @param string $subjectName - * @param string $schema * @return array * @throws ClientExceptionInterface * @throws SchemaRegistryExceptionInterface @@ -89,10 +77,6 @@ public function getSchemaById(int $id): string; public function registerNewSchemaVersion(string $subjectName, string $schema): array; /** - * @param string $subjectName - * @param string $schema - * @param string $version - * @return bool * @throws ClientExceptionInterface * @throws SchemaRegistryExceptionInterface * @throws JsonException @@ -104,8 +88,6 @@ public function checkSchemaCompatibilityForVersion( ): bool; /** - * @param string $subjectName - * @return string * @throws ClientExceptionInterface * @throws SchemaRegistryExceptionInterface * @throws JsonException @@ -113,9 +95,6 @@ public function checkSchemaCompatibilityForVersion( public function getSubjectCompatibilityLevel(string $subjectName): ?string; /** - * @param string $subjectName - * @param string $level - * @return bool * @throws ClientExceptionInterface * @throws SchemaRegistryExceptionInterface * @throws JsonException @@ -123,7 +102,6 @@ public function getSubjectCompatibilityLevel(string $subjectName): ?string; public function setSubjectCompatibilityLevel(string $subjectName, string $level = self::LEVEL_FULL): bool; /** - * @return string * @throws ClientExceptionInterface * @throws SchemaRegistryExceptionInterface * @throws JsonException @@ -131,8 +109,6 @@ public function setSubjectCompatibilityLevel(string $subjectName, string $level public function getDefaultCompatibilityLevel(): string; /** - * @param string $level - * @return bool * @throws ClientExceptionInterface * @throws SchemaRegistryExceptionInterface * @throws JsonException @@ -140,9 +116,6 @@ public function getDefaultCompatibilityLevel(): string; public function setDefaultCompatibilityLevel(string $level = self::LEVEL_FULL): bool; /** - * @param string $subjectName - * @param string $schema - * @return string|null * @throws ClientExceptionInterface * @throws SchemaRegistryExceptionInterface * @throws JsonException @@ -150,9 +123,6 @@ public function setDefaultCompatibilityLevel(string $level = self::LEVEL_FULL): public function getVersionForSchema(string $subjectName, string $schema): ?string; /** - * @param string $subjectName - * @param string $schema - * @return bool * @throws ClientExceptionInterface * @throws SchemaRegistryExceptionInterface * @throws JsonException @@ -160,7 +130,6 @@ public function getVersionForSchema(string $subjectName, string $schema): ?strin public function isSchemaAlreadyRegistered(string $subjectName, string $schema): bool; /** - * @param string $subjectName * @return array * @throws ClientExceptionInterface * @throws SchemaRegistryExceptionInterface @@ -169,8 +138,6 @@ public function isSchemaAlreadyRegistered(string $subjectName, string $schema): public function deleteSubject(string $subjectName): array; /** - * @param string $mode - * @return bool * @throws ClientExceptionInterface * @throws SchemaRegistryExceptionInterface * @throws JsonException @@ -178,8 +145,6 @@ public function deleteSubject(string $subjectName): array; public function setImportMode(string $mode): bool; /** - * @param string $subjectName - * @return string|null * @throws JsonException */ public function getLatestSubjectVersion(string $subjectName): ?string; diff --git a/src/ServiceProvider/KafkaSchemaRegistryApiClientProvider.php b/src/ServiceProvider/KafkaSchemaRegistryApiClientProvider.php index 8769bfd..4a6d624 100644 --- a/src/ServiceProvider/KafkaSchemaRegistryApiClientProvider.php +++ b/src/ServiceProvider/KafkaSchemaRegistryApiClientProvider.php @@ -30,9 +30,6 @@ class KafkaSchemaRegistryApiClientProvider implements ServiceProviderInterface public const API_CLIENT = 'kafka.schema.registry.client.api'; public const ERROR_HANDLER = 'kafka.schema.registry.error.handler'; - /** - * @param Container $container - */ public function register(Container $container): void { $this->checkRequiredOffsets($container); @@ -68,8 +65,8 @@ public function register(Container $container): void $requestFactory, $container[self::ERROR_HANDLER], $container[self::CONTAINER_KEY][self::SETTING_KEY_BASE_URL], - $container[self::CONTAINER_KEY][self::SETTING_KEY_USERNAME], - $container[self::CONTAINER_KEY][self::SETTING_KEY_PASSWORD] + $container[self::CONTAINER_KEY][self::SETTING_KEY_USERNAME] ?? null, + $container[self::CONTAINER_KEY][self::SETTING_KEY_PASSWORD] ?? null ); }; } diff --git a/tests/ErrorHandlerTest.php b/tests/ErrorHandlerTest.php index 2a59bc7..4c9ed13 100644 --- a/tests/ErrorHandlerTest.php +++ b/tests/ErrorHandlerTest.php @@ -55,7 +55,8 @@ private function makeResponseInterfaceMock(?string $code = null, ?string $messag return $responseMock; } - public function exceptionTestDataProvider(): array{ + public function exceptionTestDataProvider(): array + { return [ [50001, BackendDatastoreException::class], [50002, OperationTimeoutException::class], @@ -83,8 +84,6 @@ public function exceptionTestDataProvider(): array{ /** * @dataProvider exceptionTestDataProvider * - * @param int $code - * @param string $expectedException * @throws BackendDatastoreException * @throws ClientException * @throws CompatibilityException @@ -139,7 +138,9 @@ public function testExceptionThrowWithRequest(): void $errorHandler = new ErrorHandler(); $this->expectException(BackendDatastoreException::class); - $this->expectExceptionMessage(self::TEST_MESSAGE . sprintf(' (%s) with request body: %s', 'http://test.com', 'test body')); + $this->expectExceptionMessage( + self::TEST_MESSAGE . sprintf(' (%s) with request body: %s', 'http://test.com', 'test body') + ); $errorHandler->handleError($responseMock, 'http://test.com', $requestMock); } diff --git a/tests/KafkaSchemaRegistryApiClientProviderTest.php b/tests/KafkaSchemaRegistryApiClientProviderTest.php index 32067ab..eda33b8 100644 --- a/tests/KafkaSchemaRegistryApiClientProviderTest.php +++ b/tests/KafkaSchemaRegistryApiClientProviderTest.php @@ -37,7 +37,6 @@ protected function setUp(): void ); } - public function testDefaultContainersAndServicesSetWithMinimalConfig(): void { $this->classExistsMock->expects(self::exactly(2))->withConsecutive( @@ -87,10 +86,13 @@ public function testDefaultContainersAndServicesSetWithMinimalConfig(): void public function testSuccessWithMissingAuth(): void { - $this->classExistsMock->expects(self::exactly(2))->withConsecutive( - ['Nyholm\Psr7\Factory\Psr17Factory'], - ['Buzz\Client\Curl'] - )->willReturn(true); + $this->classExistsMock + ->expects(self::exactly(2)) + ->withConsecutive( + ['Nyholm\Psr7\Factory\Psr17Factory'], + ['Buzz\Client\Curl'] + ) + ->willReturn(true); $container = new Container(); @@ -103,10 +105,6 @@ public function testSuccessWithMissingAuth(): void $client = $container['kafka.schema.registry.client.http']; self::assertInstanceOf(HttpClientInterface::class, $client); - self::assertEquals( - $container['kafka.schema.registry']['password'], - self::getPropertyValue($client, 'password') - ); self::assertNull(self::getPropertyValue($client, 'username')); self::assertNull(self::getPropertyValue($client, 'password')); } @@ -153,6 +151,5 @@ public function testUserNameAndPasswordFromSettingsArePassedToHttpClient(): void $container['kafka.schema.registry']['password'], self::getPropertyValue($container['kafka.schema.registry.client.http'], 'password') ); - } } diff --git a/tests/KafkaSchemaRegistryApiClientTest.php b/tests/KafkaSchemaRegistryApiClientTest.php index 80135ec..61efce3 100644 --- a/tests/KafkaSchemaRegistryApiClientTest.php +++ b/tests/KafkaSchemaRegistryApiClientTest.php @@ -21,23 +21,11 @@ class KafkaSchemaRegistryApiClientTest extends TestCase private const TEST_SCHEMA = '{}'; private const TEST_VERSION = 3; - /** - * @return MockObject|HttpClientInterface - */ - private function getHttpClientMock(): MockObject - { - return $this - ->getMockBuilder(HttpClient::class) - ->disableOriginalConstructor() - ->onlyMethods(['call']) - ->getMock(); - } - public function testGetSubjects(): void { $httpClientMock = $this->getHttpClientMock(); - $httpClientMock->expects($this->once())->method('call')->with('GET', 'subjects'); + $httpClientMock->expects(self::once())->method('call')->with('GET', 'subjects'); $api = new KafkaSchemaRegistryApiClient($httpClientMock); $api->getSubjects(); @@ -48,7 +36,7 @@ public function testGetAllSubjectVersions(): void $httpClientMock = $this->getHttpClientMock(); $httpClientMock - ->expects($this->once()) + ->expects(self::once()) ->method('call') ->with('GET', sprintf('subjects/%s/versions', self::TEST_SUBJECT_NAME)); @@ -61,7 +49,7 @@ public function testGetSchemaByVersion(): void $httpClientMock = $this->getHttpClientMock(); $httpClientMock - ->expects($this->once()) + ->expects(self::once()) ->method('call') ->with('GET', sprintf('subjects/%s/versions/%s', self::TEST_SUBJECT_NAME, self::TEST_VERSION)) ->willReturn(['schema' => '{}']); @@ -69,7 +57,7 @@ public function testGetSchemaByVersion(): void $api = new KafkaSchemaRegistryApiClient($httpClientMock); $result = $api->getSchemaByVersion(self::TEST_SUBJECT_NAME, self::TEST_VERSION); - $this->assertSame(['schema' => '{}'], $result); + self::assertSame(['schema' => '{}'], $result); } public function testGetSchemaDefinitionByVersionForComplexSchema(): void @@ -77,7 +65,7 @@ public function testGetSchemaDefinitionByVersionForComplexSchema(): void $httpClientMock = $this->getHttpClientMock(); $httpClientMock - ->expects($this->once()) + ->expects(self::once()) ->method('call') ->with('GET', sprintf('subjects/%s/versions/%s/schema', self::TEST_SUBJECT_NAME, self::TEST_VERSION)) ->willReturn(['a' => 'b']); @@ -85,7 +73,7 @@ public function testGetSchemaDefinitionByVersionForComplexSchema(): void $api = new KafkaSchemaRegistryApiClient($httpClientMock); $result = $api->getSchemaDefinitionByVersion(self::TEST_SUBJECT_NAME, self::TEST_VERSION); - $this->assertSame(['a' => 'b'], $result); + self::assertSame(['a' => 'b'], $result); } public function testGetSchemaDefinitionByVersionForOptimizedPrimitiveSchema(): void @@ -93,7 +81,7 @@ public function testGetSchemaDefinitionByVersionForOptimizedPrimitiveSchema(): v $httpClientMock = $this->getHttpClientMock(); $httpClientMock - ->expects($this->once()) + ->expects(self::once()) ->method('call') ->with('GET', sprintf('subjects/%s/versions/%s/schema', self::TEST_SUBJECT_NAME, self::TEST_VERSION)) ->willReturn("string"); @@ -101,7 +89,7 @@ public function testGetSchemaDefinitionByVersionForOptimizedPrimitiveSchema(): v $api = new KafkaSchemaRegistryApiClient($httpClientMock); $result = $api->getSchemaDefinitionByVersion(self::TEST_SUBJECT_NAME, self::TEST_VERSION); - $this->assertSame("string", $result); + self::assertSame("string", $result); } public function testDeleteSchemaVersion(): void @@ -109,7 +97,7 @@ public function testDeleteSchemaVersion(): void $httpClientMock = $this->getHttpClientMock(); $httpClientMock - ->expects($this->once()) + ->expects(self::once()) ->method('call') ->with('DELETE', sprintf('subjects/%s/versions/%s', self::TEST_SUBJECT_NAME, self::TEST_VERSION)) ->willReturn(1); @@ -117,7 +105,7 @@ public function testDeleteSchemaVersion(): void $api = new KafkaSchemaRegistryApiClient($httpClientMock); $result = $api->deleteSchemaVersion(self::TEST_SUBJECT_NAME, self::TEST_VERSION); - $this->assertSame(1, $result); + self::assertSame(1, $result); } public function testGetSchemaById(): void @@ -125,7 +113,7 @@ public function testGetSchemaById(): void $httpClientMock = $this->getHttpClientMock(); $httpClientMock - ->expects($this->once()) + ->expects(self::once()) ->method('call') ->with('GET', sprintf('schemas/ids/%s', 1)) ->willReturn(['schema' => '{}']); @@ -139,7 +127,7 @@ public function testRegisterNewSchemaVersion(): void $httpClientMock = $this->getHttpClientMock(); $httpClientMock - ->expects($this->once()) + ->expects(self::once()) ->method('call') ->with('POST', sprintf('subjects/%s/versions', self::TEST_SUBJECT_NAME), ['schema' => '[]']) ->willReturn([]); @@ -148,12 +136,36 @@ public function testRegisterNewSchemaVersion(): void $api->registerNewSchemaVersion(self::TEST_SUBJECT_NAME, self::TEST_SCHEMA); } + public function testCheckSchemaCompatibilityForVersionFalseOnEmptyResponse(): void + { + $httpClientMock = $this->getHttpClientMock(); + + $httpClientMock + ->expects(self::once()) + ->method('call') + ->with( + 'POST', + sprintf('compatibility/subjects/%s/versions/%s', self::TEST_SUBJECT_NAME, self::TEST_VERSION), + ['schema' => '[]'] + ) + ->willReturn([]); + + $api = new KafkaSchemaRegistryApiClient($httpClientMock); + $result = $api->checkSchemaCompatibilityForVersion( + self::TEST_SUBJECT_NAME, + self::TEST_SCHEMA, + self::TEST_VERSION + ); + + self::assertFalse($result); + } + public function testCheckSchemaCompatibilityForVersionTrue(): void { $httpClientMock = $this->getHttpClientMock(); $httpClientMock - ->expects($this->once()) + ->expects(self::once()) ->method('call') ->with( 'POST', @@ -163,8 +175,13 @@ public function testCheckSchemaCompatibilityForVersionTrue(): void ->willReturn(['is_compatible' => true]); $api = new KafkaSchemaRegistryApiClient($httpClientMock); - $result = $api->checkSchemaCompatibilityForVersion(self::TEST_SUBJECT_NAME, self::TEST_SCHEMA, self::TEST_VERSION); - $this->assertTrue($result); + $result = $api->checkSchemaCompatibilityForVersion( + self::TEST_SUBJECT_NAME, + self::TEST_SCHEMA, + self::TEST_VERSION + ); + + self::assertTrue($result); } public function testCheckSchemaCompatibilityForVersionFalse(): void @@ -172,7 +189,7 @@ public function testCheckSchemaCompatibilityForVersionFalse(): void $httpClientMock = $this->getHttpClientMock(); $httpClientMock - ->expects($this->once()) + ->expects(self::once()) ->method('call') ->with( 'POST', @@ -182,8 +199,13 @@ public function testCheckSchemaCompatibilityForVersionFalse(): void ->willReturn(['is_compatible' => false]); $api = new KafkaSchemaRegistryApiClient($httpClientMock); - $result = $api->checkSchemaCompatibilityForVersion(self::TEST_SUBJECT_NAME, self::TEST_SCHEMA, self::TEST_VERSION); - $this->assertFalse($result); + $result = $api->checkSchemaCompatibilityForVersion( + self::TEST_SUBJECT_NAME, + self::TEST_SCHEMA, + self::TEST_VERSION + ); + + self::assertFalse($result); } public function testCheckSchemaCompatibilityForVersionNotFound(): void @@ -191,7 +213,7 @@ public function testCheckSchemaCompatibilityForVersionNotFound(): void $httpClientMock = $this->getHttpClientMock(); $httpClientMock - ->expects($this->once()) + ->expects(self::once()) ->method('call') ->with( 'POST', @@ -201,8 +223,12 @@ public function testCheckSchemaCompatibilityForVersionNotFound(): void ->willThrowException(new SubjectNotFoundException()); $api = new KafkaSchemaRegistryApiClient($httpClientMock); - $result = $api->checkSchemaCompatibilityForVersion(self::TEST_SUBJECT_NAME, self::TEST_SCHEMA, self::TEST_VERSION); - $this->assertTrue($result); + $result = $api->checkSchemaCompatibilityForVersion( + self::TEST_SUBJECT_NAME, + self::TEST_SCHEMA, + self::TEST_VERSION + ); + self::assertTrue($result); } public function testGetSubjectCompatibilityLevel(): void @@ -210,7 +236,7 @@ public function testGetSubjectCompatibilityLevel(): void $httpClientMock = $this->getHttpClientMock(); $httpClientMock - ->expects($this->once()) + ->expects(self::once()) ->method('call') ->with( 'GET', @@ -220,7 +246,8 @@ public function testGetSubjectCompatibilityLevel(): void $api = new KafkaSchemaRegistryApiClient($httpClientMock); $result = $api->getSubjectCompatibilityLevel(self::TEST_SUBJECT_NAME); - $this->assertSame(KafkaSchemaRegistryApiClientInterface::LEVEL_FULL, $result); + + self::assertSame(KafkaSchemaRegistryApiClientInterface::LEVEL_FULL, $result); } public function testGetDefaultCompatibiltyLeveWhenGetSubjectCompatibilityLevelThrowsException(): void @@ -243,7 +270,8 @@ public function testGetDefaultCompatibiltyLeveWhenGetSubjectCompatibilityLevelTh $api = new KafkaSchemaRegistryApiClient($httpClientMock); $result = $api->getSubjectCompatibilityLevel(self::TEST_SUBJECT_NAME); - $this->assertSame(KafkaSchemaRegistryApiClientInterface::LEVEL_FULL, $result); + + self::assertSame(KafkaSchemaRegistryApiClientInterface::LEVEL_FULL, $result); } public function testSetSubjectCompatibilityLevel(): void @@ -251,7 +279,7 @@ public function testSetSubjectCompatibilityLevel(): void $httpClientMock = $this->getHttpClientMock(); $httpClientMock - ->expects($this->once()) + ->expects(self::once()) ->method('call') ->with( 'PUT', @@ -265,7 +293,7 @@ public function testSetSubjectCompatibilityLevel(): void KafkaSchemaRegistryApiClientInterface::LEVEL_FULL ); - $this->assertTrue($result); + self::assertTrue($result); } public function testGetDefaultCompatibilityLeve(): void @@ -273,14 +301,15 @@ public function testGetDefaultCompatibilityLeve(): void $httpClientMock = $this->getHttpClientMock(); $httpClientMock - ->expects($this->once()) + ->expects(self::once()) ->method('call') ->with('GET', sprintf('config')) ->willReturn(['compatibilityLevel' => KafkaSchemaRegistryApiClientInterface::LEVEL_FULL]); $api = new KafkaSchemaRegistryApiClient($httpClientMock); $result = $api->getDefaultCompatibilityLevel(); - $this->assertSame(KafkaSchemaRegistryApiClientInterface::LEVEL_FULL, $result); + + self::assertSame(KafkaSchemaRegistryApiClientInterface::LEVEL_FULL, $result); } public function testSetDefaultCompatibilityLeve(): void @@ -288,13 +317,14 @@ public function testSetDefaultCompatibilityLeve(): void $httpClientMock = $this->getHttpClientMock(); $httpClientMock - ->expects($this->once()) + ->expects(self::once()) ->method('call') ->with('PUT', 'config', ['compatibility' => KafkaSchemaRegistryApiClientInterface::LEVEL_FULL]); $api = new KafkaSchemaRegistryApiClient($httpClientMock); $result = $api->setDefaultCompatibilityLevel(); - $this->assertTrue($result); + + self::assertTrue($result); } public function testGetVersionForSchema(): void @@ -302,7 +332,7 @@ public function testGetVersionForSchema(): void $httpClientMock = $this->getHttpClientMock(); $httpClientMock - ->expects($this->once()) + ->expects(self::once()) ->method('call') ->with( 'POST', @@ -313,7 +343,8 @@ public function testGetVersionForSchema(): void $api = new KafkaSchemaRegistryApiClient($httpClientMock); $result = $api->getVersionForSchema(self::TEST_SUBJECT_NAME, self::TEST_SCHEMA); - $this->assertSame((string) self::TEST_VERSION, $result); + + self::assertSame((string) self::TEST_VERSION, $result); } public function testGetVersionForSchemaThrowsSubjectNotFoundExceptionResultsAsNull(): void @@ -321,7 +352,7 @@ public function testGetVersionForSchemaThrowsSubjectNotFoundExceptionResultsAsNu $httpClientMock = $this->getHttpClientMock(); $httpClientMock - ->expects($this->once()) + ->expects(self::once()) ->method('call') ->with( 'POST', @@ -332,7 +363,8 @@ public function testGetVersionForSchemaThrowsSubjectNotFoundExceptionResultsAsNu $api = new KafkaSchemaRegistryApiClient($httpClientMock); $result = $api->getVersionForSchema(self::TEST_SUBJECT_NAME, self::TEST_SCHEMA); - $this->assertNull($result); + + self::assertNull($result); } public function testGetVersionForSchemaThrowsSchematNotFoundExceptionResultsAsNull(): void @@ -340,7 +372,7 @@ public function testGetVersionForSchemaThrowsSchematNotFoundExceptionResultsAsNu $httpClientMock = $this->getHttpClientMock(); $httpClientMock - ->expects($this->once()) + ->expects(self::once()) ->method('call') ->with( 'POST', @@ -351,7 +383,8 @@ public function testGetVersionForSchemaThrowsSchematNotFoundExceptionResultsAsNu $api = new KafkaSchemaRegistryApiClient($httpClientMock); $result = $api->getVersionForSchema(self::TEST_SUBJECT_NAME, self::TEST_SCHEMA); - $this->assertNull($result); + + self::assertNull($result); } public function testSchemaExistsTrue(): void @@ -359,7 +392,7 @@ public function testSchemaExistsTrue(): void $httpClientMock = $this->getHttpClientMock(); $httpClientMock - ->expects($this->once()) + ->expects(self::once()) ->method('call') ->with( 'POST', @@ -370,7 +403,8 @@ public function testSchemaExistsTrue(): void $api = new KafkaSchemaRegistryApiClient($httpClientMock); $result = $api->isSchemaAlreadyRegistered(self::TEST_SUBJECT_NAME, self::TEST_SCHEMA); - $this->assertTrue($result); + + self::assertTrue($result); } public function testSchemaExistsFalse(): void @@ -378,7 +412,7 @@ public function testSchemaExistsFalse(): void $httpClientMock = $this->getHttpClientMock(); $httpClientMock - ->expects($this->once()) + ->expects(self::once()) ->method('call') ->with( 'POST', @@ -389,7 +423,8 @@ public function testSchemaExistsFalse(): void $api = new KafkaSchemaRegistryApiClient($httpClientMock); $result = $api->isSchemaAlreadyRegistered(self::TEST_SUBJECT_NAME, self::TEST_SCHEMA); - $this->assertFalse($result); + + self::assertFalse($result); } public function testDeleteSubject(): void @@ -397,7 +432,7 @@ public function testDeleteSubject(): void $httpClientMock = $this->getHttpClientMock(); $httpClientMock - ->expects($this->once()) + ->expects(self::once()) ->method('call') ->with('DELETE', sprintf('subjects/%s', self::TEST_SUBJECT_NAME)) ->willReturn([1,2,3,4]); @@ -405,7 +440,7 @@ public function testDeleteSubject(): void $api = new KafkaSchemaRegistryApiClient($httpClientMock); $result = $api->deleteSubject(self::TEST_SUBJECT_NAME); - $this->assertSame([1,2,3,4], $result); + self::assertSame([1,2,3,4], $result); } public function testGetLatestSubjectVersion(): void @@ -413,14 +448,15 @@ public function testGetLatestSubjectVersion(): void $httpClientMock = $this->getHttpClientMock(); $httpClientMock - ->expects($this->once()) + ->expects(self::once()) ->method('call') ->with('GET', sprintf('subjects/%s/versions', self::TEST_SUBJECT_NAME)) ->willReturn([1,2,3,4,5,6]); $api = new KafkaSchemaRegistryApiClient($httpClientMock); $result = $api->getLatestSubjectVersion(self::TEST_SUBJECT_NAME); - $this->assertSame('6', $result); + + self::assertSame('6', $result); } public function testImportModeFail(): void @@ -428,7 +464,7 @@ public function testImportModeFail(): void $httpClientMock = $this->getHttpClientMock(); $httpClientMock - ->expects($this->once()) + ->expects(self::once()) ->method('call') ->with( 'PUT', @@ -448,7 +484,7 @@ public function testImportModeSuccess(): void $httpClientMock = $this->getHttpClientMock(); $httpClientMock - ->expects($this->once()) + ->expects(self::once()) ->method('call') ->with( 'PUT', @@ -458,6 +494,19 @@ public function testImportModeSuccess(): void $api = new KafkaSchemaRegistryApiClient($httpClientMock); $result = $api->setImportMode('ABC'); + self::assertTrue($result); } + + /** + * @return MockObject|HttpClientInterface + */ + private function getHttpClientMock(): MockObject + { + return $this + ->getMockBuilder(HttpClient::class) + ->disableOriginalConstructor() + ->onlyMethods(['call']) + ->getMock(); + } } diff --git a/tests/ReflectionAccessTrait.php b/tests/ReflectionAccessTrait.php index 19b25dd..788c624 100644 --- a/tests/ReflectionAccessTrait.php +++ b/tests/ReflectionAccessTrait.php @@ -5,10 +5,6 @@ use ReflectionClass; use ReflectionException; -/** - * Trait ReflectionAccessTrait - * @package Jobcloud\MarketplaceAdapterMessage\Tests - */ trait ReflectionAccessTrait { /** @@ -63,4 +59,4 @@ final public function invokeMethod(object $object, string $methodName, array $pa return $method->invokeArgs($object, $parameters); } -} \ No newline at end of file +}