diff --git a/composer.json b/composer.json index b3fc3cd..c8507af 100644 --- a/composer.json +++ b/composer.json @@ -15,23 +15,24 @@ "homepage": "https://careers.jobcloud.ch/en/our-team/?term=133" } ], - "config": { - "sort-packages": true - }, "require": { "php": ">=7.4", "ext-json": "*", - "pimple/pimple": "^3.2", - "symfony/cache": "^5.0", - "nyholm/psr7": "^1.2", - "kriswallsmith/buzz": "^1.0", - "psr/http-message": "^1.0" + "psr/http-message": "^1.0", + "psr/http-client": "^1.0.1" }, "require-dev": { - "squizlabs/php_codesniffer": "^3.4.2", - "phpunit/phpunit": "^9.5", + "infection/infection": "^0.21", + "kriswallsmith/buzz": "^1.0", + "nyholm/psr7": "^1.2", + "php-mock/php-mock-phpunit": "^2.6", "phpstan/phpstan": "^0.12", + "phpunit/phpunit": "^9.5", + "pimple/pimple": "^3.2", "rregeer/phpunit-coverage-check": "^0.3.1", - "infection/infection": "^0.21" + "squizlabs/php_codesniffer": "^3.4.2" + }, + "config": { + "sort-packages": true } } diff --git a/phpunit.xml b/phpunit.xml index 478d6bf..3dfbd31 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,5 +1,17 @@ - + src diff --git a/src/ServiceProvider/KafkaSchemaRegistryApiClientProvider.php b/src/ServiceProvider/KafkaSchemaRegistryApiClientProvider.php index 26281ca..d8eef03 100644 --- a/src/ServiceProvider/KafkaSchemaRegistryApiClientProvider.php +++ b/src/ServiceProvider/KafkaSchemaRegistryApiClientProvider.php @@ -4,8 +4,11 @@ use Buzz\Client\Curl; use Jobcloud\Kafka\SchemaRegistryClient\ErrorHandler; +use Jobcloud\Kafka\SchemaRegistryClient\ErrorHandlerInterface; use Jobcloud\Kafka\SchemaRegistryClient\HttpClient; +use Jobcloud\Kafka\SchemaRegistryClient\HttpClientInterface; use Jobcloud\Kafka\SchemaRegistryClient\KafkaSchemaRegistryApiClient; +use Jobcloud\Kafka\SchemaRegistryClient\KafkaSchemaRegistryApiClientInterface; use LogicException; use Nyholm\Psr7\Factory\Psr17Factory; use Pimple\Container; @@ -35,26 +38,26 @@ public function register(Container $container): void { $this->checkRequiredOffsets($container); - if (false === isset($container[self::REQUEST_FACTORY])) { - $container[self::REQUEST_FACTORY] = static function (Container $container) { + if (false === isset($container[self::REQUEST_FACTORY]) && class_exists('Nyholm\Psr7\Factory\Psr17Factory')) { + $container[self::REQUEST_FACTORY] = static function (): RequestFactoryInterface { return new Psr17Factory(); }; } - if (false === isset($container[self::CLIENT])) { - $container[self::CLIENT] = static function (Container $container) { + if (false === isset($container[self::CLIENT]) && class_exists('Buzz\Client\Curl')) { + $container[self::CLIENT] = static function (Container $container): ClientInterface { return new Curl($container[self::REQUEST_FACTORY]); }; } if (false === isset($container[self::ERROR_HANDLER])) { - $container[self::ERROR_HANDLER] = static function () { + $container[self::ERROR_HANDLER] = static function (): ErrorHandlerInterface { return new ErrorHandler(); }; } if (false === isset($container[self::HTTP_CLIENT])) { - $container[self::HTTP_CLIENT] = static function (Container $container) { + $container[self::HTTP_CLIENT] = static function (Container $container): HttpClientInterface { /** @var ClientInterface $client */ $client = $container[self::CLIENT]; @@ -73,7 +76,9 @@ public function register(Container $container): void } if (false === isset($container[self::API_CLIENT])) { - $container[self::API_CLIENT] = static function (Container $container) { + $container[self::API_CLIENT] = static function ( + Container $container + ): KafkaSchemaRegistryApiClientInterface { /** @var HttpClient $client */ $client = $container[self::HTTP_CLIENT]; @@ -84,7 +89,6 @@ public function register(Container $container): void private function checkRequiredOffsets(Container $container): void { - if (false === isset($container[self::CONTAINER_KEY][self::SETTING_KEY_BASE_URL])) { throw new LogicException( sprintf( diff --git a/tests/KafkaSchemaRegistryApiClientProviderTest.php b/tests/KafkaSchemaRegistryApiClientProviderTest.php index 61f0c82..32067ab 100644 --- a/tests/KafkaSchemaRegistryApiClientProviderTest.php +++ b/tests/KafkaSchemaRegistryApiClientProviderTest.php @@ -7,6 +7,9 @@ use Jobcloud\Kafka\SchemaRegistryClient\KafkaSchemaRegistryApiClientInterface; use Jobcloud\Kafka\SchemaRegistryClient\ServiceProvider\KafkaSchemaRegistryApiClientProvider; use LogicException; +use phpmock\phpunit\MockObjectProxy; +use phpmock\phpunit\PHPMock; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Pimple\Container; use Psr\Http\Message\RequestFactoryInterface; @@ -16,10 +19,32 @@ */ class KafkaSchemaRegistryApiClientProviderTest extends TestCase { + use PHPMock; use ReflectionAccessTrait; + /** + * @var MockObject|MockObjectProxy + */ + private $classExistsMock; + + protected function setUp(): void + { + parent::setUp(); // TODO: Change the autogenerated stub + + $this->classExistsMock = $this->getFunctionMock( + 'Jobcloud\Kafka\SchemaRegistryClient\ServiceProvider', + 'class_exists' + ); + } + + public function testDefaultContainersAndServicesSetWithMinimalConfig(): void { + $this->classExistsMock->expects(self::exactly(2))->withConsecutive( + ['Nyholm\Psr7\Factory\Psr17Factory'], + ['Buzz\Client\Curl'] + )->willReturn(true); + $container = new Container(); self::assertArrayNotHasKey('kafka.schema.registry', $container); @@ -62,6 +87,11 @@ public function testDefaultContainersAndServicesSetWithMinimalConfig(): void public function testSuccessWithMissingAuth(): void { + $this->classExistsMock->expects(self::exactly(2))->withConsecutive( + ['Nyholm\Psr7\Factory\Psr17Factory'], + ['Buzz\Client\Curl'] + )->willReturn(true); + $container = new Container(); $container['kafka.schema.registry'] = [ @@ -83,6 +113,8 @@ public function testSuccessWithMissingAuth(): void public function testFailOnMissingBaseUrlInContainer(): void { + $this->classExistsMock->expects(self::never()); + $container = new Container(); $container['kafka.schema.registry'] = [ @@ -98,6 +130,11 @@ public function testFailOnMissingBaseUrlInContainer(): void public function testUserNameAndPasswordFromSettingsArePassedToHttpClient(): void { + $this->classExistsMock->expects(self::exactly(2))->withConsecutive( + ['Nyholm\Psr7\Factory\Psr17Factory'], + ['Buzz\Client\Curl'] + )->willReturn(true); + $container = new Container(); $container['kafka.schema.registry'] = [