-
Notifications
You must be signed in to change notification settings - Fork 15.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expand PHP generator to support overlapping subset of proto2.
This will allow us to support editions without adding support for proto2 concepts such as closed enums, required fields, and groups. The generator will now only ban unsupported features, meaning that some types of proto2 files will be allowed. The PHP runtime does not yet support editions. PiperOrigin-RevId: 588091790
- Loading branch information
1 parent
b64c6e1
commit b603fb6
Showing
4 changed files
with
186 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Protocol Buffers - Google's data interchange format | ||
// Copyright 2023 Google Inc. All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file or at | ||
// https://developers.google.com/open-source/licenses/bsd | ||
|
||
#include <memory> | ||
|
||
#include "google/protobuf/descriptor.pb.h" | ||
#include <gtest/gtest.h> | ||
#include "google/protobuf/compiler/command_line_interface_tester.h" | ||
#include "google/protobuf/compiler/php/php_generator.h" | ||
|
||
namespace google { | ||
namespace protobuf { | ||
namespace compiler { | ||
namespace php { | ||
namespace { | ||
|
||
class PhpGeneratorTest : public CommandLineInterfaceTester { | ||
protected: | ||
PhpGeneratorTest() { | ||
RegisterGenerator("--php_out", "--php_opt", std::make_unique<Generator>(), | ||
"PHP test generator"); | ||
|
||
// Generate built-in protos. | ||
CreateTempFile( | ||
"google/protobuf/descriptor.proto", | ||
google::protobuf::DescriptorProto::descriptor()->file()->DebugString()); | ||
} | ||
}; | ||
|
||
TEST_F(PhpGeneratorTest, Basic) { | ||
CreateTempFile("foo.proto", | ||
R"schema( | ||
syntax = "proto3"; | ||
message Foo { | ||
optional int32 bar = 1; | ||
int32 baz = 2; | ||
})schema"); | ||
|
||
RunProtoc( | ||
"protocol_compiler --proto_path=$tmpdir --php_out=$tmpdir foo.proto"); | ||
|
||
ExpectNoErrors(); | ||
} | ||
|
||
TEST_F(PhpGeneratorTest, Proto2File) { | ||
CreateTempFile("foo.proto", | ||
R"schema( | ||
syntax = "proto2"; | ||
message Foo { | ||
optional int32 bar = 1; | ||
})schema"); | ||
|
||
RunProtoc( | ||
"protocol_compiler --proto_path=$tmpdir --php_out=$tmpdir foo.proto"); | ||
|
||
ExpectNoErrors(); | ||
} | ||
|
||
TEST_F(PhpGeneratorTest, RequiredFieldError) { | ||
CreateTempFile("foo.proto", | ||
R"schema( | ||
syntax = "proto2"; | ||
message FooBar { | ||
required int32 foo_message = 1; | ||
})schema"); | ||
|
||
RunProtoc( | ||
"protocol_compiler --proto_path=$tmpdir --php_out=$tmpdir foo.proto"); | ||
|
||
ExpectErrorSubstring( | ||
"Can't generate PHP code for required field FooBar.foo_message"); | ||
} | ||
|
||
TEST_F(PhpGeneratorTest, GroupFieldError) { | ||
CreateTempFile("foo.proto", | ||
R"schema( | ||
syntax = "proto2"; | ||
message Foo { | ||
optional group Bar = 1 { | ||
optional int32 baz = 1; | ||
}; | ||
})schema"); | ||
|
||
RunProtoc( | ||
"protocol_compiler --proto_path=$tmpdir --php_out=$tmpdir foo.proto"); | ||
|
||
ExpectErrorSubstring("Can't generate PHP code for group field Foo.bar"); | ||
} | ||
|
||
TEST_F(PhpGeneratorTest, ClosedEnumError) { | ||
CreateTempFile("foo.proto", | ||
R"schema( | ||
syntax = "proto2"; | ||
enum Foo { | ||
BAR = 0; | ||
})schema"); | ||
|
||
RunProtoc( | ||
"protocol_compiler --proto_path=$tmpdir --php_out=$tmpdir foo.proto"); | ||
|
||
ExpectErrorSubstring("Can't generate PHP code for closed enum Foo"); | ||
} | ||
|
||
} // namespace | ||
} // namespace php | ||
} // namespace compiler | ||
} // namespace protobuf | ||
} // namespace google |
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