Skip to content

Commit

Permalink
Implemented tests for each type
Browse files Browse the repository at this point in the history
Added tests for each supported serviceHandler type

EndpointHandler | Callable | String
  • Loading branch information
EmilJimenez21 authored and nekufa committed Oct 1, 2024
1 parent 177c37f commit bac7349
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
88 changes: 88 additions & 0 deletions tests/Functional/ServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace Tests\Functional;

use Basis\Nats\Client;
use Basis\Nats\Message\Payload;
use Basis\Nats\Service\Service;
use Tests\FunctionalTestCase;
use Tests\Utils\TestEndpoint;

class ServiceTest extends FunctionalTestCase
{
private bool $tested = false;

private function createTestService(): Service
{
/** @var Client $client */
$client = $this->createClient([
'host' => 'hermes.internal'
]);

/** @var Service $service */
$service = $client->service(
name: 'TestService',
description: 'Service description',
version: '1.0'
);

return $service;
}
public function testServiceRequestReplyString()
{
$service = $this->createTestService();

$service
->addGroup('v1')
->addEndpoint(
'test',
TestEndpoint::class
);

$service->client->publish('v1.test', '');

$response = $service->client->process(1);

$this->assertTrue($response['success']);
}

public function testServiceRequestReplyCallable()
{
$service = $this->createTestService();

$service
->addGroup('v1')
->addEndpoint(
'test',
function (Payload $payload) {
return [
'success' => true
];
}
);

$service->client->publish('v1.test', '');

$response = $service->client->process(1);

$this->assertTrue($response['success']);
}

public function testServiceRequestReplyClass()
{
$service = $this->createTestService();

$service
->addGroup('v1')
->addEndpoint(
'test',
new TestEndpoint()
);

$service->client->publish('v1.test', '');

$response = $service->client->process(1);

$this->assertTrue($response['success']);
}
}
17 changes: 17 additions & 0 deletions tests/Utils/TestEndpoint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Tests\Utils;

use Basis\Nats\Message\Payload;
use Basis\Nats\Service\EndpointHandler;

class TestEndpoint implements EndpointHandler
{

public function handle(Payload $payload): array
{
return [
'success' => true
];
}
}

0 comments on commit bac7349

Please sign in to comment.