-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add protobuf sync message example
- Loading branch information
Showing
19 changed files
with
535 additions
and
6 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
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
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
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
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,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit bootstrap="../../../vendor/autoload.php" colors="true"> | ||
<testsuites> | ||
<testsuite name="PhpPact Example Tests"> | ||
<directory>./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<php> | ||
<env name="PACT_LOGLEVEL" value="DEBUG"/> | ||
</php> | ||
</phpunit> |
23 changes: 23 additions & 0 deletions
23
example/protobuf-sync-message/consumer/src/CalculatorClient.php
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,23 @@ | ||
<?php | ||
|
||
namespace ProtobufSyncMessageConsumer; | ||
|
||
use Grpc\BaseStub; | ||
use Plugins\ShapeMessage; | ||
use Plugins\AreaResponse; | ||
|
||
class CalculatorClient extends BaseStub | ||
{ | ||
public function calculate(ShapeMessage $request, array $metadata = []): AreaResponse | ||
{ | ||
[$response, $status] = $this->_simpleRequest( | ||
'/plugins.Calculator/calculate', | ||
$request, | ||
[AreaResponse::class, 'decode'], | ||
$metadata, | ||
[] | ||
)->wait(); | ||
|
||
return $response; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
example/protobuf-sync-message/consumer/src/ProtobufClient.php
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,23 @@ | ||
<?php | ||
|
||
namespace ProtobufSyncMessageConsumer; | ||
|
||
use Plugins\AreaResponse; | ||
use Plugins\ShapeMessage; | ||
use Grpc\ChannelCredentials; | ||
|
||
class ProtobufClient | ||
{ | ||
public function __construct(private string $baseUrl) | ||
{ | ||
} | ||
|
||
public function calculate(ShapeMessage $shapeMessage): AreaResponse | ||
{ | ||
$client = new CalculatorClient($this->baseUrl, [ | ||
'credentials' => ChannelCredentials::createInsecure(), | ||
]); | ||
|
||
return $client->calculate($shapeMessage); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
example/protobuf-sync-message/consumer/tests/ProtobufClientTest.php
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,61 @@ | ||
<?php | ||
|
||
namespace ProtobufSyncMessageConsumer\Tests; | ||
|
||
use PhpPact\Consumer\Model\Body\Text; | ||
use PhpPact\Plugins\Protobuf\Factory\ProtobufSyncMessageDriverFactory; | ||
use PhpPact\Standalone\MockService\MockServerConfig; | ||
use PhpPact\SyncMessage\SyncMessageBuilder; | ||
use PHPUnit\Framework\TestCase; | ||
use Plugins\Rectangle; | ||
use Plugins\ShapeMessage; | ||
use ProtobufSyncMessageConsumer\ProtobufClient; | ||
|
||
class ProtobufClientTest extends TestCase | ||
{ | ||
public function testCalculateArea(): void | ||
{ | ||
$protoPath = __DIR__ . '/../../library/proto/area_calculator.proto'; | ||
|
||
$config = new MockServerConfig(); | ||
$config->setConsumer('protobufSyncMessageConsumer'); | ||
$config->setProvider('protobufSyncMessageProvider'); | ||
$config->setPactSpecificationVersion('4.0.0'); | ||
$config->setPactDir(__DIR__.'/../../pacts'); | ||
if ($logLevel = \getenv('PACT_LOGLEVEL')) { | ||
$config->setLogLevel($logLevel); | ||
} | ||
$config->setHost('127.0.0.1'); | ||
$builder = new SyncMessageBuilder($config, new ProtobufSyncMessageDriverFactory()); | ||
$builder | ||
->expectsToReceive('request for calculate shape area') | ||
->withMetadata([]) | ||
->withContent(new Text( | ||
json_encode([ | ||
'pact:proto' => $protoPath, | ||
'pact:content-type' => 'application/grpc', | ||
'pact:proto-service' => 'Calculator/calculate', | ||
|
||
'request' => [ | ||
'rectangle' => [ | ||
'length' => 'matching(number, 3)', | ||
'width' => 'matching(number, 4)', | ||
], | ||
], | ||
'response' => [ | ||
'value' => 'matching(number, 12)', | ||
] | ||
]), | ||
'application/grpc' | ||
)); | ||
$builder->registerMessage(); | ||
|
||
$service = new ProtobufClient("{$config->getHost()}:{$config->getPort()}"); | ||
$rectangle = (new Rectangle())->setLength(3)->setWidth(4); | ||
$message = (new ShapeMessage())->setRectangle($rectangle); | ||
$response = $service->calculate($message); | ||
|
||
$this->assertTrue($builder->verify()); | ||
$this->assertEquals(3 * 4, $response->getValue()); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
example/protobuf-sync-message/library/proto/area_calculator.proto
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,47 @@ | ||
syntax = "proto3"; | ||
|
||
package plugins; | ||
|
||
option php_generic_services = true; | ||
|
||
service Calculator { | ||
rpc calculate (ShapeMessage) returns (AreaResponse) {} | ||
} | ||
|
||
message ShapeMessage { | ||
oneof shape { | ||
Square square = 1; | ||
Rectangle rectangle = 2; | ||
Circle circle = 3; | ||
Triangle triangle = 4; | ||
Parallelogram parallelogram = 5; | ||
} | ||
} | ||
|
||
message Square { | ||
float edge_length = 1; | ||
} | ||
|
||
message Rectangle { | ||
float length = 1; | ||
float width = 2; | ||
} | ||
|
||
message Circle { | ||
float radius = 1; | ||
} | ||
|
||
message Triangle { | ||
float edge_a = 1; | ||
float edge_b = 2; | ||
float edge_c = 3; | ||
} | ||
|
||
message Parallelogram { | ||
float base_length = 1; | ||
float height = 2; | ||
} | ||
|
||
message AreaResponse { | ||
float value = 1; | ||
} |
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 @@ | ||
*.php |
Oops, something went wrong.