• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google Inc.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 #include <memory>
9 
10 #include "google/protobuf/descriptor.pb.h"
11 #include <gtest/gtest.h>
12 #include "google/protobuf/compiler/command_line_interface_tester.h"
13 #include "google/protobuf/compiler/php/php_generator.h"
14 
15 namespace google {
16 namespace protobuf {
17 namespace compiler {
18 namespace php {
19 namespace {
20 
21 class PhpGeneratorTest : public CommandLineInterfaceTester {
22  protected:
PhpGeneratorTest()23   PhpGeneratorTest() {
24     RegisterGenerator("--php_out", "--php_opt", std::make_unique<Generator>(),
25                       "PHP test generator");
26 
27     // Generate built-in protos.
28     CreateTempFile(
29         "google/protobuf/descriptor.proto",
30         google::protobuf::DescriptorProto::descriptor()->file()->DebugString());
31   }
32 };
33 
TEST_F(PhpGeneratorTest,Basic)34 TEST_F(PhpGeneratorTest, Basic) {
35   CreateTempFile("foo.proto",
36                  R"schema(
37     syntax = "proto3";
38     message Foo {
39       optional int32 bar = 1;
40       int32 baz = 2;
41     })schema");
42 
43   RunProtoc(
44       "protocol_compiler --proto_path=$tmpdir --php_out=$tmpdir foo.proto");
45 
46   ExpectNoErrors();
47 }
48 
TEST_F(PhpGeneratorTest,Proto2File)49 TEST_F(PhpGeneratorTest, Proto2File) {
50   CreateTempFile("foo.proto",
51                  R"schema(
52     syntax = "proto2";
53     message Foo {
54       optional int32 bar = 1;
55     })schema");
56 
57   RunProtoc(
58       "protocol_compiler --proto_path=$tmpdir --php_out=$tmpdir foo.proto");
59 
60   ExpectNoErrors();
61 }
62 
TEST_F(PhpGeneratorTest,RequiredFieldError)63 TEST_F(PhpGeneratorTest, RequiredFieldError) {
64   CreateTempFile("foo.proto",
65                  R"schema(
66     syntax = "proto2";
67     message FooBar {
68       required int32 foo_message = 1;
69     })schema");
70 
71   RunProtoc(
72       "protocol_compiler --proto_path=$tmpdir --php_out=$tmpdir foo.proto");
73 
74   ExpectErrorSubstring(
75       "Can't generate PHP code for required field FooBar.foo_message");
76 }
77 
TEST_F(PhpGeneratorTest,GroupFieldError)78 TEST_F(PhpGeneratorTest, GroupFieldError) {
79   CreateTempFile("foo.proto",
80                  R"schema(
81     syntax = "proto2";
82     message Foo {
83       optional group Bar = 1 {
84         optional int32 baz = 1;
85       };
86     })schema");
87 
88   RunProtoc(
89       "protocol_compiler --proto_path=$tmpdir --php_out=$tmpdir foo.proto");
90 
91   ExpectErrorSubstring("Can't generate PHP code for group field Foo.bar");
92 }
93 
TEST_F(PhpGeneratorTest,ClosedEnumError)94 TEST_F(PhpGeneratorTest, ClosedEnumError) {
95   CreateTempFile("foo.proto",
96                  R"schema(
97     syntax = "proto2";
98     enum Foo {
99       BAR = 0;
100     })schema");
101 
102   RunProtoc(
103       "protocol_compiler --proto_path=$tmpdir --php_out=$tmpdir foo.proto");
104 
105   ExpectErrorSubstring("Can't generate PHP code for closed enum Foo");
106 }
107 
108 }  // namespace
109 }  // namespace php
110 }  // namespace compiler
111 }  // namespace protobuf
112 }  // namespace google
113