diff --git a/.gitignore b/.gitignore index f040fe2..4d4666f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ composer.lock +.phpunit.result.cache /vendor .idea diff --git a/composer.json b/composer.json index 45e1627..a98969c 100755 --- a/composer.json +++ b/composer.json @@ -28,5 +28,12 @@ } }, "minimum-stability": "dev", - "prefer-stable": true + "prefer-stable": true, + "scripts": { + "tests": "./vendor/bin/phpunit tests/", + "coverage": "./vendor/bin/phpunit --whitelist tests/ --coverage-html tests/coverage/", + "check": [ + "@tests" + ] + } } diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..1a1e6e1 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,22 @@ + + + + + ./src/ + + + + + + + + + + ./tests/ + + + + + + + diff --git a/src/Exceptions/PusherException.php b/src/Exceptions/PusherException.php new file mode 100644 index 0000000..84088b8 --- /dev/null +++ b/src/Exceptions/PusherException.php @@ -0,0 +1,17 @@ +exporter = $exporter; + } + + /** + * @param BaseSchema $schema + * @param string $stream + * @param string|null $key + * @throws EmptyExportersException + * @throws PusherException + */ + public function push(BaseSchema $schema, string $stream, string $key = null): void + { + if (empty($this->exporter)) { + throw new EmptyExportersException(); + } + + try { + $this->exporter->push($schema, $stream, $key); + } catch (Throwable $exception) { + throw new PusherException($exception); + } + } +} diff --git a/tests/PusherTest.php b/tests/PusherTest.php new file mode 100644 index 0000000..bb527d7 --- /dev/null +++ b/tests/PusherTest.php @@ -0,0 +1,44 @@ + 'test']; + $this->schema = $latestSchemaFactory->createFromParameters('source', 'type', 1, $data); + $this->exporter = $this->createMock(ExporterInterface::class); + } + + public function testPushEvent(): void + { + $this->exporter->expects(self::once())->method('push'); + + $pusher = new Pusher($this->exporter); + $pusher->push($this->schema,'topic'); + } + + public function testExporterThrowException(): void + { + $this->exporter->expects(self::once()) + ->method('push') + ->willThrowException(new FailedSenderToKafkaException()); + + $this->expectException(PusherException::class); + + $pusher = new Pusher($this->exporter); + $pusher->push($this->schema,'topic'); + } +}