From cc117aec43a5f354299bff334c650053af9a8a07 Mon Sep 17 00:00:00 2001 From: zajca Date: Fri, 21 Apr 2023 11:48:42 +0200 Subject: [PATCH] add functional tests --- libs/azure-api-client/README.md | 10 ++ .../EventGridApiClientFunctionalTest.php | 91 +++++++++++++++++++ .../ServiceBusApiClientFunctionalTest.php | 59 ++++++++++++ 3 files changed, 160 insertions(+) create mode 100644 libs/azure-api-client/tests/EventGrid/EventGridApiClientFunctionalTest.php create mode 100644 libs/azure-api-client/tests/ServiceBus/ServiceBusApiClientFunctionalTest.php diff --git a/libs/azure-api-client/README.md b/libs/azure-api-client/README.md index 8a6b2e782..a2c0fc6ce 100644 --- a/libs/azure-api-client/README.md +++ b/libs/azure-api-client/README.md @@ -135,6 +135,7 @@ If even this is not enough for your use-case, you can implement your own ## Development +### Resources To run functional tests (PHPUnit group "functional") you have to setup resources in Azure. Requirements: @@ -147,6 +148,15 @@ terraform -chdir="provisioning" init terraform -chdir="provisioning" apply -var name_prefix= -var az_subscription_id= -var az_tenant_id= terraform -chdir="provisioning" output -json | jq -r 'keys[] as $k | "\($k)=\(.[$k] | .value)"' >| .env ``` +### Tests + +```bash +# Run only unit tests +docker-compose run --rm dev-azure-api-client composer tests -- --exclude=functional + +# Run only functional tests +docker-compose run --rm dev-azure-api-client composer tests -- --group=functional +``` ## License diff --git a/libs/azure-api-client/tests/EventGrid/EventGridApiClientFunctionalTest.php b/libs/azure-api-client/tests/EventGrid/EventGridApiClientFunctionalTest.php new file mode 100644 index 000000000..86566b62c --- /dev/null +++ b/libs/azure-api-client/tests/EventGrid/EventGridApiClientFunctionalTest.php @@ -0,0 +1,91 @@ +getClient(); + $client->publishEvents([ + new EventGridEvent( + '3e9825ed-b7db-44ea-a5c9-a1601fa43e23', + 'TestSubject', + [ + 'Property1' => 'Value1', + 'Property2' => 'Value2', + ], + 'Keboola.EventGridClient.TestEvent' + ), + ]); + + $serviceBusClient = $this->getServiceBusClient(); + $message = $serviceBusClient->peakMessage('queue-tests'); + $this->assertNotNull($message); + // returned message has id assigned by service bus + self::assertNotSame('3e9825ed-b7db-44ea-a5c9-a1601fa43e23', $message->id); + $messageBody = Json::decodeArray($message->body); + self::assertSame('3e9825ed-b7db-44ea-a5c9-a1601fa43e23', $messageBody['id']); + self::assertSame('TestSubject', $messageBody['subject']); + self::assertSame([ + 'Property1' => 'Value1', + 'Property2' => 'Value2', + ], $messageBody['data']); + self::assertSame('Keboola.EventGridClient.TestEvent', $messageBody['eventType']); + self::assertSame('1.0', $messageBody['dataVersion']); + self::assertSame('1', $messageBody['metadataVersion']); + self::assertEqualsWithDelta( + (new DateTimeImmutable('now'))->getTimestamp(), + (new DateTimeImmutable($messageBody['eventTime']))->getTimestamp(), + 10 // peak has timeout 10s + ); + self::assertStringEndsWith('Microsoft.EventGrid/topics/' . getenv('AZURE_API_CLIENT_CI__EVENT_GRID__TOPIC_NAME'), $messageBody['topic']); + $serviceBusClient->deleteMessage($message->lockLocation); + } +} diff --git a/libs/azure-api-client/tests/ServiceBus/ServiceBusApiClientFunctionalTest.php b/libs/azure-api-client/tests/ServiceBus/ServiceBusApiClientFunctionalTest.php new file mode 100644 index 000000000..226ba6e29 --- /dev/null +++ b/libs/azure-api-client/tests/ServiceBus/ServiceBusApiClientFunctionalTest.php @@ -0,0 +1,59 @@ +getClient(); + $messageSend = ServiceBusBrokerMessageRequest::createJson( + '123', + ['testdata' => 'value'] + ); + $client->sendMessage( + 'queue-tests', + $messageSend + ); + $messageReceived = $client->peakMessage( + 'queue-tests' + ); + $this->assertNotNull($messageReceived); + self::assertSame($messageSend->id, $messageReceived->id); + $messageBody = Json::decodeArray($messageReceived->body); + self::assertSame([ + 'testdata' => 'value', + ], $messageBody); + $client->deleteMessage($messageReceived->lockLocation); + } +}