-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests for each supported serviceHandler type EndpointHandler | Callable | String
- Loading branch information
1 parent
177c37f
commit bac7349
Showing
2 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
]; | ||
} | ||
} |