Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/CodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
73 changes: 73 additions & 0 deletions tests/Unit/CodeGeneratorNamespaceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/*
* Copyright 2026 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
declare(strict_types=1);

namespace Google\Generator\Tests\Unit;

use Exception;
use Google\Generator\CodeGenerator;
use Google\Generator\Collections\Vector;
use Google\Protobuf\Internal\FileDescriptorProto;
use Google\Protobuf\Internal\FileOptions;
use Google\Protobuf\Internal\SourceCodeInfo;
use PHPUnit\Framework\TestCase;

final class CodeGeneratorNamespaceTest extends TestCase
{
public function testMismatchedPhpNamespacesIncludeDetailsInError(): void
{
$fileA = $this->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;
}
}
15 changes: 8 additions & 7 deletions tests/Unit/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] = [];
Expand Down Expand Up @@ -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;
}
Expand Down
Loading