diff --git a/src/CodeGenerator.php b/src/CodeGenerator.php index 7cfa990d5..6c0013e3e 100644 --- a/src/CodeGenerator.php +++ b/src/CodeGenerator.php @@ -152,12 +152,20 @@ public static function generate( $mixinRpcNames = []; $serviceYamlConfig = new ServiceYamlConfig($serviceYaml); $transportType = Transport::parseTransport($transport); - foreach ($byPackage as [$_, $singlePackageFileDescs]) { + foreach ($byPackage as [$package, $singlePackageFileDescs]) { + $fileNamespaces = $singlePackageFileDescs + ->map(fn ($x) => sprintf('%s (%s)', $x->getName(), ProtoHelpers::getNamespace($x))); $namespaces = $singlePackageFileDescs ->map(fn ($x) => ProtoHelpers::getNamespace($x)) ->distinct(); if (count($namespaces) > 1) { - throw new Exception('All files in the same package must have the same PHP namespace'); + throw new Exception(sprintf( + 'All files in the same package must have the same PHP namespace; ' . + 'package "%s" has mismatched namespaces [%s] from files: %s', + $package, + $namespaces->join(', '), + $fileNamespaces->join(', ') + )); } // Full protobuf names, e.g. google.cloud.foo.Bar. diff --git a/tests/Unit/CodeGeneratorNamespaceTest.php b/tests/Unit/CodeGeneratorNamespaceTest.php new file mode 100644 index 000000000..3c74a7293 --- /dev/null +++ b/tests/Unit/CodeGeneratorNamespaceTest.php @@ -0,0 +1,73 @@ +fileDesc('tests/Unit/a.proto', 'testing.ns_mismatch', 'Testing\\NsMismatchA'); + $fileB = $this->fileDesc('tests/Unit/b.proto', 'testing.ns_mismatch', 'Testing\\NsMismatchB'); + + try { + iterator_to_array(CodeGenerator::generate( + Vector::new([$fileA, $fileB]), + Vector::new([$fileA->getName(), $fileB->getName()]), + null, + false, + null, + null, + null, + )); + $this->fail('Expected Exception was not thrown'); + } catch (Exception $e) { + $this->assertStringContainsString( + 'All files in the same package must have the same PHP namespace', + $e->getMessage() + ); + $this->assertStringContainsString('package "testing.ns_mismatch"', $e->getMessage()); + $this->assertStringContainsString('Testing\\NsMismatchA', $e->getMessage()); + $this->assertStringContainsString('Testing\\NsMismatchB', $e->getMessage()); + $this->assertStringContainsString('tests/Unit/a.proto', $e->getMessage()); + $this->assertStringContainsString('tests/Unit/b.proto', $e->getMessage()); + } + } + + private function fileDesc(string $name, string $package, string $phpNamespace): FileDescriptorProto + { + $opts = new FileOptions(); + $opts->setPhpNamespace($phpNamespace); + + $file = new FileDescriptorProto(); + $file->setName($name); + $file->setPackage($package); + $file->setSyntax('proto3'); + $file->setOptions($opts); + $file->setSourceCodeInfo(new SourceCodeInfo()); + return $file; + } +} diff --git a/tests/Unit/autoload.php b/tests/Unit/autoload.php index c2cc451ca..c017f6926 100644 --- a/tests/Unit/autoload.php +++ b/tests/Unit/autoload.php @@ -18,6 +18,7 @@ use Google\Protobuf\Internal\CodedInputStream; use function Opis\Closure\{serialize, unserialize}; +use Google\Protobuf\Internal\CodedInputStream; if (!isset($GLOBALS['gapic_php_test_temp_files'])) { $GLOBALS['gapic_php_test_temp_files'] = []; @@ -76,15 +77,15 @@ public function serializeToString($recursion_limit = CodedInputStream::DEFAULT_R return $result; } - public function mergeFromString($s, $recursion_limit = CodedInputStream::DEFAULT_RECURSION_LIMIT) + public function mergeFromString($data, $recursion_limit = CodedInputStream::DEFAULT_RECURSION_LIMIT) { $pos = 0; - while ($pos < strlen($s)) { - $colon1 = strpos($s, ':', $pos); - $colon2 = strpos($s, ':', $colon1 + 1); - $name = substr($s, $pos, $colon1 - $pos); - $valueLen = (int)substr($s, $colon1 + 1, $colon2 - $colon1 - 1); - $value = unserialize(substr($s, $colon2 + 1, $valueLen)); + while ($pos < strlen($data)) { + $colon1 = strpos($data, ':', $pos); + $colon2 = strpos($data, ':', $colon1 + 1); + $name = substr($data, $pos, $colon1 - $pos); + $valueLen = (int)substr($data, $colon1 + 1, $colon2 - $colon1 - 1); + $value = unserialize(substr($data, $colon2 + 1, $valueLen)); $this->$name = $value; $pos = $colon2 + 1 + $valueLen; }