summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2021-09-07 18:12:26 +0300
committerValery Piashchynski <[email protected]>2021-09-07 18:12:26 +0300
commit2f41909aeb6a01ae1a0b5ebabe9a9afc1360744a (patch)
tree9609019ad2ba4b6ae3a241aa5e6f68872b89feaf /tests
parent6749db4a2d39fa70b426bcf50edf66a176c07f57 (diff)
Initial commit for the GRPC plugin
Signed-off-by: Valery Piashchynski <[email protected]>
Diffstat (limited to 'tests')
-rw-r--r--tests/plugins/grpc/grpc_plugin_test.go7
-rw-r--r--tests/plugins/grpc/plugin_test.go178
-rw-r--r--tests/plugins/grpc/testdata/import/Import/ServiceInterface.php32
-rw-r--r--tests/plugins/grpc/testdata/import/service.proto17
-rw-r--r--tests/plugins/grpc/testdata/import/sub/message.proto7
-rw-r--r--tests/plugins/grpc/testdata/import_custom/Test/CustomImport/ServiceInterface.php32
-rw-r--r--tests/plugins/grpc/testdata/import_custom/service.proto19
-rw-r--r--tests/plugins/grpc/testdata/import_custom/sub/message.proto14
-rw-r--r--tests/plugins/grpc/testdata/php_namespace/Test/CustomNamespace/ServiceInterface.php22
-rw-r--r--tests/plugins/grpc/testdata/php_namespace/service.proto15
-rw-r--r--tests/plugins/grpc/testdata/simple/TestSimple/SimpleServiceInterface.php22
-rw-r--r--tests/plugins/grpc/testdata/simple/simple.proto13
-rw-r--r--tests/plugins/grpc/testdata/use_empty/Test/ServiceInterface.php23
-rw-r--r--tests/plugins/grpc/testdata/use_empty/service.proto10
-rwxr-xr-xtests/plugins/grpcplugbin0 -> 6024075 bytes
-rwxr-xr-xtests/plugins/grpcpluginbin0 -> 6024075 bytes
16 files changed, 411 insertions, 0 deletions
diff --git a/tests/plugins/grpc/grpc_plugin_test.go b/tests/plugins/grpc/grpc_plugin_test.go
new file mode 100644
index 00000000..85dd5723
--- /dev/null
+++ b/tests/plugins/grpc/grpc_plugin_test.go
@@ -0,0 +1,7 @@
+package grpc_test
+
+import "testing"
+
+func GrpcInit(t *testing.T) {
+
+}
diff --git a/tests/plugins/grpc/plugin_test.go b/tests/plugins/grpc/plugin_test.go
new file mode 100644
index 00000000..74a71c62
--- /dev/null
+++ b/tests/plugins/grpc/plugin_test.go
@@ -0,0 +1,178 @@
+package grpc
+
+import (
+ "io/ioutil"
+ "os"
+ "os/exec"
+ "strings"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+func init() {
+ err := build()
+ if err != nil {
+ panic(err)
+ }
+}
+
+func build() error {
+ cmd := exec.Command("go", "build", "-o", "plugin", "../../../plugins/grpc/protoc-gen-php-grpc")
+ return cmd.Run()
+}
+
+func protoc(t *testing.T, args []string) {
+ cmd := exec.Command("protoc", "--plugin=protoc-gen-php-grpc=./plugin")
+ cmd.Args = append(cmd.Args, args...)
+ out, err := cmd.CombinedOutput()
+
+ if len(out) > 0 || err != nil {
+ t.Log("RUNNING: ", strings.Join(cmd.Args, " "))
+ }
+
+ if len(out) > 0 {
+ t.Log(string(out))
+ }
+
+ if err != nil {
+ t.Fatalf("protoc: %v", err)
+ }
+}
+
+func Test_Simple(t *testing.T) {
+ workdir, _ := os.Getwd()
+ tmpdir, err := ioutil.TempDir("", "proto-test")
+ require.NoError(t, err)
+
+ defer func() {
+ assert.NoError(t, os.RemoveAll(tmpdir))
+ }()
+
+ args := []string{
+ "-Itestdata",
+ "--php-grpc_out=" + tmpdir,
+ "simple/simple.proto",
+ }
+
+ protoc(t, args)
+
+ assertEqualFiles(
+ t,
+ workdir+"/testdata/simple/TestSimple/SimpleServiceInterface.php",
+ tmpdir+"/TestSimple/SimpleServiceInterface.php",
+ )
+}
+
+func Test_PhpNamespaceOption(t *testing.T) {
+ workdir, _ := os.Getwd()
+ tmpdir, err := ioutil.TempDir("", "proto-test")
+ require.NoError(t, err)
+
+ defer func() {
+ assert.NoError(t, os.RemoveAll(tmpdir))
+ }()
+
+ args := []string{
+ "-Itestdata",
+ "--php-grpc_out=" + tmpdir,
+ "php_namespace/service.proto",
+ }
+ protoc(t, args)
+
+ assertEqualFiles(
+ t,
+ workdir+"/testdata/php_namespace/Test/CustomNamespace/ServiceInterface.php",
+ tmpdir+"/Test/CustomNamespace/ServiceInterface.php",
+ )
+}
+
+func Test_UseImportedMessage(t *testing.T) {
+ workdir, _ := os.Getwd()
+ tmpdir, err := ioutil.TempDir("", "proto-test")
+ require.NoError(t, err)
+
+ defer func() {
+ assert.NoError(t, os.RemoveAll(tmpdir))
+ }()
+
+ args := []string{
+ "-Itestdata",
+ "--php-grpc_out=" + tmpdir,
+ "import/service.proto",
+ }
+ protoc(t, args)
+
+ assertEqualFiles(
+ t,
+ workdir+"/testdata/import/Import/ServiceInterface.php",
+ tmpdir+"/Import/ServiceInterface.php",
+ )
+}
+
+func Test_PhpNamespaceOptionInUse(t *testing.T) {
+ workdir, _ := os.Getwd()
+ tmpdir, err := ioutil.TempDir("", "proto-test")
+ require.NoError(t, err)
+
+ defer func() {
+ assert.NoError(t, os.RemoveAll(tmpdir))
+ }()
+
+ args := []string{
+ "-Itestdata",
+ "--php-grpc_out=" + tmpdir,
+ "import_custom/service.proto",
+ }
+ protoc(t, args)
+
+ assertEqualFiles(
+ t,
+ workdir+"/testdata/import_custom/Test/CustomImport/ServiceInterface.php",
+ tmpdir+"/Test/CustomImport/ServiceInterface.php",
+ )
+}
+
+func Test_UseOfGoogleEmptyMessage(t *testing.T) {
+ workdir, _ := os.Getwd()
+ tmpdir, err := ioutil.TempDir("", "proto-test")
+ require.NoError(t, err)
+
+ defer func() {
+ assert.NoError(t, os.RemoveAll(tmpdir))
+ }()
+
+ args := []string{
+ "-Itestdata",
+ "--php-grpc_out=" + tmpdir,
+ "use_empty/service.proto",
+ }
+ protoc(t, args)
+
+ assertEqualFiles(
+ t,
+ workdir+"/testdata/use_empty/Test/ServiceInterface.php",
+ tmpdir+"/Test/ServiceInterface.php",
+ )
+
+ assert.NoError(t, os.RemoveAll("plugin"))
+}
+
+func assertEqualFiles(t *testing.T, original, generated string) {
+ assert.FileExists(t, generated)
+
+ originalData, err := ioutil.ReadFile(original)
+ if err != nil {
+ t.Fatal("Can't find original file for comparison")
+ }
+
+ generatedData, err := ioutil.ReadFile(generated)
+ if err != nil {
+ t.Fatal("Can't find generated file for comparison")
+ }
+
+ // every OS has a special boy
+ r := strings.NewReplacer("\r\n", "", "\n", "")
+ assert.Equal(t, r.Replace(string(originalData)), r.Replace(string(generatedData)))
+}
diff --git a/tests/plugins/grpc/testdata/import/Import/ServiceInterface.php b/tests/plugins/grpc/testdata/import/Import/ServiceInterface.php
new file mode 100644
index 00000000..13e58daf
--- /dev/null
+++ b/tests/plugins/grpc/testdata/import/Import/ServiceInterface.php
@@ -0,0 +1,32 @@
+<?php
+# Generated by the protocol buffer compiler (spiral/php-grpc). DO NOT EDIT!
+# source: import/service.proto
+
+namespace Import;
+
+use Spiral\GRPC;
+use Import\Sub;
+
+interface ServiceInterface extends GRPC\ServiceInterface
+{
+ // GRPC specific service name.
+ public const NAME = "import.Service";
+
+ /**
+ * @param GRPC\ContextInterface $ctx
+ * @param Message $in
+ * @return Message
+ *
+ * @throws GRPC\Exception\InvokeException
+ */
+ public function SimpleMethod(GRPC\ContextInterface $ctx, Message $in): Message;
+
+ /**
+ * @param GRPC\ContextInterface $ctx
+ * @param Sub\Message $in
+ * @return Sub\Message
+ *
+ * @throws GRPC\Exception\InvokeException
+ */
+ public function ImportMethod(GRPC\ContextInterface $ctx, Sub\Message $in): Sub\Message;
+}
diff --git a/tests/plugins/grpc/testdata/import/service.proto b/tests/plugins/grpc/testdata/import/service.proto
new file mode 100644
index 00000000..5d888f09
--- /dev/null
+++ b/tests/plugins/grpc/testdata/import/service.proto
@@ -0,0 +1,17 @@
+syntax = "proto3";
+
+package import;
+
+import "import/sub/message.proto";
+
+service Service {
+ rpc SimpleMethod (Message) returns (Message) {
+ }
+
+ rpc ImportMethod (import.sub.Message) returns (import.sub.Message) {
+ }
+}
+
+message Message {
+ int64 id = 1;
+} \ No newline at end of file
diff --git a/tests/plugins/grpc/testdata/import/sub/message.proto b/tests/plugins/grpc/testdata/import/sub/message.proto
new file mode 100644
index 00000000..1db0313b
--- /dev/null
+++ b/tests/plugins/grpc/testdata/import/sub/message.proto
@@ -0,0 +1,7 @@
+syntax = "proto3";
+
+package import.sub;
+
+message Message {
+ int64 id = 1;
+} \ No newline at end of file
diff --git a/tests/plugins/grpc/testdata/import_custom/Test/CustomImport/ServiceInterface.php b/tests/plugins/grpc/testdata/import_custom/Test/CustomImport/ServiceInterface.php
new file mode 100644
index 00000000..b010ce4f
--- /dev/null
+++ b/tests/plugins/grpc/testdata/import_custom/Test/CustomImport/ServiceInterface.php
@@ -0,0 +1,32 @@
+<?php
+# Generated by the protocol buffer compiler (spiral/php-grpc). DO NOT EDIT!
+# source: import_custom/service.proto
+
+namespace Test\CustomImport;
+
+use Spiral\GRPC;
+use Test\CustomImport\Message;
+
+interface ServiceInterface extends GRPC\ServiceInterface
+{
+ // GRPC specific service name.
+ public const NAME = "import.Service";
+
+ /**
+ * @param GRPC\ContextInterface $ctx
+ * @param Message $in
+ * @return Message
+ *
+ * @throws GRPC\Exception\InvokeException
+ */
+ public function SimpleMethod(GRPC\ContextInterface $ctx, Message $in): Message;
+
+ /**
+ * @param GRPC\ContextInterface $ctx
+ * @param Message\Message $in
+ * @return Message\Message
+ *
+ * @throws GRPC\Exception\InvokeException
+ */
+ public function ImportMethod(GRPC\ContextInterface $ctx, Message\Message $in): Message\Message;
+}
diff --git a/tests/plugins/grpc/testdata/import_custom/service.proto b/tests/plugins/grpc/testdata/import_custom/service.proto
new file mode 100644
index 00000000..872aaae3
--- /dev/null
+++ b/tests/plugins/grpc/testdata/import_custom/service.proto
@@ -0,0 +1,19 @@
+syntax = "proto3";
+
+package import;
+
+option php_namespace = "Test\\CustomImport";
+
+import "import_custom/sub/message.proto";
+
+service Service {
+ rpc SimpleMethod (Message) returns (Message) {
+ }
+
+ rpc ImportMethod (import.sub.Message) returns (import.sub.Message) {
+ }
+}
+
+message Message {
+ int64 id = 1;
+} \ No newline at end of file
diff --git a/tests/plugins/grpc/testdata/import_custom/sub/message.proto b/tests/plugins/grpc/testdata/import_custom/sub/message.proto
new file mode 100644
index 00000000..5d722ca3
--- /dev/null
+++ b/tests/plugins/grpc/testdata/import_custom/sub/message.proto
@@ -0,0 +1,14 @@
+syntax = "proto3";
+
+package import.sub;
+option php_namespace = "Test\\CustomImport\\Message";
+
+
+service Service {
+ rpc AnotherMethod (Message) returns (Message) {
+ }
+}
+
+message Message {
+ int64 id = 1;
+} \ No newline at end of file
diff --git a/tests/plugins/grpc/testdata/php_namespace/Test/CustomNamespace/ServiceInterface.php b/tests/plugins/grpc/testdata/php_namespace/Test/CustomNamespace/ServiceInterface.php
new file mode 100644
index 00000000..2090ba97
--- /dev/null
+++ b/tests/plugins/grpc/testdata/php_namespace/Test/CustomNamespace/ServiceInterface.php
@@ -0,0 +1,22 @@
+<?php
+# Generated by the protocol buffer compiler (spiral/php-grpc). DO NOT EDIT!
+# source: php_namespace/service.proto
+
+namespace Test\CustomNamespace;
+
+use Spiral\GRPC;
+
+interface ServiceInterface extends GRPC\ServiceInterface
+{
+ // GRPC specific service name.
+ public const NAME = "testPhpNamespace.Service";
+
+ /**
+ * @param GRPC\ContextInterface $ctx
+ * @param SimpleMessage $in
+ * @return SimpleMessage
+ *
+ * @throws GRPC\Exception\InvokeException
+ */
+ public function SimpleMethod(GRPC\ContextInterface $ctx, SimpleMessage $in): SimpleMessage;
+}
diff --git a/tests/plugins/grpc/testdata/php_namespace/service.proto b/tests/plugins/grpc/testdata/php_namespace/service.proto
new file mode 100644
index 00000000..a3bfa3c0
--- /dev/null
+++ b/tests/plugins/grpc/testdata/php_namespace/service.proto
@@ -0,0 +1,15 @@
+syntax = "proto3";
+
+package testPhpNamespace;
+
+option php_namespace = "Test\\CustomNamespace";
+
+service Service {
+ rpc SimpleMethod (SimpleMessage) returns (SimpleMessage) {
+ }
+}
+
+message SimpleMessage {
+ int32 id = 1;
+ string name = 2;
+} \ No newline at end of file
diff --git a/tests/plugins/grpc/testdata/simple/TestSimple/SimpleServiceInterface.php b/tests/plugins/grpc/testdata/simple/TestSimple/SimpleServiceInterface.php
new file mode 100644
index 00000000..f9e84bf7
--- /dev/null
+++ b/tests/plugins/grpc/testdata/simple/TestSimple/SimpleServiceInterface.php
@@ -0,0 +1,22 @@
+<?php
+# Generated by the protocol buffer compiler (spiral/php-grpc). DO NOT EDIT!
+# source: simple/simple.proto
+
+namespace TestSimple;
+
+use Spiral\GRPC;
+
+interface SimpleServiceInterface extends GRPC\ServiceInterface
+{
+ // GRPC specific service name.
+ public const NAME = "testSimple.SimpleService";
+
+ /**
+ * @param GRPC\ContextInterface $ctx
+ * @param SimpleMessage $in
+ * @return SimpleMessage
+ *
+ * @throws GRPC\Exception\InvokeException
+ */
+ public function SimpleMethod(GRPC\ContextInterface $ctx, SimpleMessage $in): SimpleMessage;
+}
diff --git a/tests/plugins/grpc/testdata/simple/simple.proto b/tests/plugins/grpc/testdata/simple/simple.proto
new file mode 100644
index 00000000..aca3c1d9
--- /dev/null
+++ b/tests/plugins/grpc/testdata/simple/simple.proto
@@ -0,0 +1,13 @@
+syntax = "proto3";
+
+package testSimple;
+
+service SimpleService {
+ rpc SimpleMethod (SimpleMessage) returns (SimpleMessage) {
+ }
+}
+
+message SimpleMessage {
+ int32 id = 1;
+ string name = 2;
+} \ No newline at end of file
diff --git a/tests/plugins/grpc/testdata/use_empty/Test/ServiceInterface.php b/tests/plugins/grpc/testdata/use_empty/Test/ServiceInterface.php
new file mode 100644
index 00000000..fe6d345a
--- /dev/null
+++ b/tests/plugins/grpc/testdata/use_empty/Test/ServiceInterface.php
@@ -0,0 +1,23 @@
+<?php
+# Generated by the protocol buffer compiler (spiral/php-grpc). DO NOT EDIT!
+# source: use_empty/service.proto
+
+namespace Test;
+
+use Spiral\GRPC;
+use Google\Protobuf;
+
+interface ServiceInterface extends GRPC\ServiceInterface
+{
+ // GRPC specific service name.
+ public const NAME = "test.Service";
+
+ /**
+ * @param GRPC\ContextInterface $ctx
+ * @param Protobuf\GPBEmpty $in
+ * @return Protobuf\GPBEmpty
+ *
+ * @throws GRPC\Exception\InvokeException
+ */
+ public function Test(GRPC\ContextInterface $ctx, Protobuf\GPBEmpty $in): Protobuf\GPBEmpty;
+}
diff --git a/tests/plugins/grpc/testdata/use_empty/service.proto b/tests/plugins/grpc/testdata/use_empty/service.proto
new file mode 100644
index 00000000..8c68d8d3
--- /dev/null
+++ b/tests/plugins/grpc/testdata/use_empty/service.proto
@@ -0,0 +1,10 @@
+syntax = "proto3";
+
+package test;
+
+import "google/protobuf/empty.proto";
+
+service Service {
+ rpc Test (google.protobuf.Empty) returns (google.protobuf.Empty) {
+ }
+} \ No newline at end of file
diff --git a/tests/plugins/grpcplug b/tests/plugins/grpcplug
new file mode 100755
index 00000000..21785aeb
--- /dev/null
+++ b/tests/plugins/grpcplug
Binary files differ
diff --git a/tests/plugins/grpcplugin b/tests/plugins/grpcplugin
new file mode 100755
index 00000000..21785aeb
--- /dev/null
+++ b/tests/plugins/grpcplugin
Binary files differ