Skip to content

Commit

Permalink
chore: Add protobuf sync message example
Browse files Browse the repository at this point in the history
  • Loading branch information
tienvx committed Jan 12, 2024
1 parent 3de5491 commit 181d6ea
Show file tree
Hide file tree
Showing 19 changed files with 533 additions and 6 deletions.
10 changes: 8 additions & 2 deletions .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ BUILD_TEST_TASK_TEMPLATE: &BUILD_TEST_TASK_TEMPLATE
- composer run lint
static_analysis_script:
- composer run static-code-analysis
generate_library_script:
- composer gen-lib
test_script:
- composer test

Expand All @@ -26,12 +28,15 @@ linux_arm64_task:
- VERSION: 8.0
arm_container:
image: php:$VERSION
cpu: 4
memory: 12G
pre_req_script:
- apt update --yes && apt install --yes zip unzip git libffi-dev
- apt update --yes && apt install --yes zip unzip git libffi-dev protobuf-compiler
- curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php
- php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer
- docker-php-ext-install sockets
- docker-php-ext-install ffi
- MAKEFLAGS=" -j4" pecl install grpc
version_check_script:
- php --version
<< : *BUILD_TEST_TASK_TEMPLATE
Expand All @@ -46,7 +51,8 @@ macos_arm64_task:
macos_instance:
image: ghcr.io/cirruslabs/macos-ventura-base:latest
pre_req_script:
- brew install php@$VERSION composer
- brew install php@$VERSION composer protobuf
- MAKEFLAGS=" -j4" pecl install grpc
version_check_script:
- php --version
<< : *BUILD_TEST_TASK_TEMPLATE
20 changes: 19 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,33 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
extensions: sockets, curl, zip, ffi
extensions: ${{ matrix.operating-system == 'windows-latest' && matrix.php == '8.2' && 'sockets, curl, zip, ffi' || 'sockets, curl, zip, ffi, grpc' }}
php-version: ${{ matrix.php }}
coverage: none
ini-values: ${{ matrix.operating-system == 'windows-latest' && 'opcache.enable=0 opcache.enable_cli=0' || '' }}

- name: Install Protoc
uses: arduino/setup-protoc@v2
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}

- name: Install gRPC Extension (for PHP 8.2 on Windows)
run: |
cd C:\tools\php
Invoke-WebRequest -Uri https://phpdev.toolsforresearch.com/php-8.2.7-nts-Win32-vs16-x64-grpc-protobuf.zip -OutFile php8.2.zip
unzip php8.2.zip ext/php_grpc.dll
rm php8.2.zip
echo "extension=php_grpc.dll" >> php.ini
if: ${{ matrix.operating-system == 'windows-latest' && matrix.php == '8.2' }}
shell: pwsh

- name: Composer install
uses: ramsey/composer-install@v2
with:
dependency-versions: ${{ matrix.dependencies }}

- name: Generate Library
run: composer gen-lib

- name: Composer test
run: composer test
1 change: 1 addition & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
->in(__DIR__ . '/tests')
->in(__DIR__ . '/example')
->in(__DIR__ . '/compatibility-suite/tests')
->exclude('library/src')
->name('*.php');

$config = new PhpCsFixer\Config();
Expand Down
23 changes: 20 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"guzzlehttp/guzzle": "^7.8",
"behat/behat": "^3.13",
"galbar/jsonpath": "^3.0",
"ramsey/uuid": "^4.7"
"ramsey/uuid": "^4.7",
"pact-foundation/example-protobuf-sync-message-provider": "@dev"
},
"autoload": {
"psr-4": {
Expand Down Expand Up @@ -72,7 +73,14 @@
"MatchersProvider\\Tests\\": "example/matchers/provider/tests",
"GeneratorsConsumer\\": "example/generators/consumer/src",
"GeneratorsConsumer\\Tests\\": "example/generators/consumer/tests",
"GeneratorsProvider\\Tests\\": "example/generators/provider/tests"
"GeneratorsProvider\\Tests\\": "example/generators/provider/tests",
"": [
"example/protobuf-sync-message/library/src"
],
"ProtobufSyncMessageConsumer\\": "example/protobuf-sync-message/consumer/src",
"ProtobufSyncMessageConsumer\\Tests\\": "example/protobuf-sync-message/consumer/tests",
"ProtobufSyncMessageProvider\\": "example/protobuf-sync-message/provider/src",
"ProtobufSyncMessageProvider\\Tests\\": "example/protobuf-sync-message/provider/tests"
}
},
"scripts": {
Expand All @@ -83,6 +91,9 @@
"php -r \"array_map('unlink', glob('./example/*/pacts/*.json'));\"",
"phpunit --debug"
],
"gen-lib": [
"protoc --php_out=example/protobuf-sync-message/library/src example/protobuf-sync-message/library/proto/area_calculator.proto"
],
"check-compatibility": "behat"
},
"extra": {
Expand Down Expand Up @@ -120,5 +131,11 @@
"allow-plugins": {
"tienvx/composer-downloads-plugin": true
}
}
},
"repositories": [
{
"type": "path",
"url": "example/protobuf-sync-message/provider"
}
]
}
11 changes: 11 additions & 0 deletions example/protobuf-sync-message/consumer/phpunit.xml
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 example/protobuf-sync-message/consumer/src/CalculatorClient.php
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 example/protobuf-sync-message/consumer/src/ProtobufClient.php
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);
}
}
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 example/protobuf-sync-message/library/proto/area_calculator.proto
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;
}
1 change: 1 addition & 0 deletions example/protobuf-sync-message/library/src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.php
Loading

0 comments on commit 181d6ea

Please sign in to comment.