diff --git a/composer.json b/composer.json index d9e0ca646..4b84d8816 100644 --- a/composer.json +++ b/composer.json @@ -43,7 +43,8 @@ }, "require-dev": { "phpunit/phpunit": "^9.5", - "google/gax": "^1.37.0" + "google/gax": "^1.37.0", + "phpdocumentor/reflection-docblock": "^6.0" }, "scripts": { "update-all-tests": [ diff --git a/googleapis b/googleapis index 9841522f9..d931df43c 160000 --- a/googleapis +++ b/googleapis @@ -1 +1 @@ -Subproject commit 9841522f92c6542aad0049d98721cba04f541f29 +Subproject commit d931df43c4686be708ddfee74ee3be64571af0a2 diff --git a/src/PostProcessor/PostProcessor.php b/src/PostProcessor/PostProcessor.php index 9a96cd9ba..ec3b31e6e 100644 --- a/src/PostProcessor/PostProcessor.php +++ b/src/PostProcessor/PostProcessor.php @@ -45,6 +45,9 @@ private function execute(): void private static function loadProcessors(array $opts): Vector { - return Vector::new([FragmentInjectionProcessor::class]); + return Vector::new([ + FragmentInjectionProcessor::class, + ProtobufDeprecationsProcessor::class, + ]); } } diff --git a/src/PostProcessor/ProtobufDeprecationsProcessor.php b/src/PostProcessor/ProtobufDeprecationsProcessor.php new file mode 100644 index 000000000..9a83066d3 --- /dev/null +++ b/src/PostProcessor/ProtobufDeprecationsProcessor.php @@ -0,0 +1,72 @@ +fixRepeatedFieldDeprecations(); + + // Write the new contents to the class file. + file_put_contents($classFile, $processor->getContents()); + print("Deprecations fixed in $classFile\n"); + } + + public function __construct(private string $contents) + { + } + + public function fixRepeatedFieldDeprecations(): void + { + $this->contents = str_replace( + [ + 'use Google\Protobuf\Internal\RepeatedField;', + '\Google\Protobuf\Internal\RepeatedField', + ], + [ + 'use Google\Protobuf\RepeatedField;', + '\Google\Protobuf\RepeatedField', + ], + $this->contents, + ); + } + + public function getContents(): string + { + return $this->contents; + } +} diff --git a/tests/Integration/goldens/asset/samples/V1/AssetServiceClient/analyze_iam_policy.php b/tests/Integration/goldens/asset/samples/V1/AssetServiceClient/analyze_iam_policy.php index 7f3523874..8ab8eea07 100644 --- a/tests/Integration/goldens/asset/samples/V1/AssetServiceClient/analyze_iam_policy.php +++ b/tests/Integration/goldens/asset/samples/V1/AssetServiceClient/analyze_iam_policy.php @@ -1,6 +1,6 @@ expectException(get_class($expectedException)); diff --git a/tests/Unit/PostProcessor/ProtobufDeprecationsProcessorTest.php b/tests/Unit/PostProcessor/ProtobufDeprecationsProcessorTest.php new file mode 100644 index 000000000..08ecde82a --- /dev/null +++ b/tests/Unit/PostProcessor/ProtobufDeprecationsProcessorTest.php @@ -0,0 +1,134 @@ +classContents, 'getConstructor()->getParameters()[0]; + $this->assertNotEquals(RepeatedField::class, $param->getType()); + } + + /** + * @runInSeparateProcess + */ + public function testFixRepeatedFieldDeprecations() + { + $addFragmentUtil = new ProtobufDeprecationsProcessor($this->classContents); + + // Fix the deprecations + $addFragmentUtil->fixRepeatedFieldDeprecations(); + $newClassContents = $addFragmentUtil->getContents(); + + eval(ltrim($newClassContents, 'getConstructor()->getParameters()[0]; + $this->assertEquals(RepeatedField::class, $param->getType()); + } + + /** + * @runInSeparateProcess + */ + public function testFixRepeatedFieldDeprecationsWithProtobufMessage() + { + $protobufMessage = __DIR__ . '/../../../generated/Google/Api/RoutingRule.php'; + + $addFragmentUtil = new ProtobufDeprecationsProcessor(file_get_contents($protobufMessage)); + + // Fix the deprecations + $addFragmentUtil->fixRepeatedFieldDeprecations(); + $newClassContents = $addFragmentUtil->getContents(); + + eval(ltrim($newClassContents, 'create($reflection->getDocComment()); + $returnTags = $docblock->getTagsByName('return'); + + $this->assertEquals('\\' . RepeatedField::class, (string) $returnTags[0]->getType()); + + $reflection = new ReflectionMethod(RoutingRule::class, 'setRoutingParameters'); + $docblock = $factory->create($reflection->getDocComment()); + $paramTags = $docblock->getTagsByName('param'); + + $this->assertStringContainsString(RepeatedField::class, (string) $paramTags[0]->getType()); + } + + /** + * @runInSeparateProcess + */ + public function testFragmentInjectionProcessor() + { + $tmpDir = sys_get_temp_dir() . '/test-fragment-injection-processor-' . rand(); + mkdir($tmpDir . '/proto/src', 0777, true); + file_put_contents($toFile = $tmpDir . '/proto/src/Bar.php', $this->classContents); + + ProtobufDeprecationsProcessor::run($tmpDir); + + // Verify the deprecations were fixed + require_once($toFile); + $param = (new ReflectionClass(\Bar::class))->getConstructor()->getParameters()[0]; + $this->assertEquals(RepeatedField::class, $param->getType()); + + // Verify post-processor output + $this->expectOutputString('Deprecations fixed in ' . $toFile . PHP_EOL); + } +} diff --git a/tests/Unit/ProtoTests/Basic/out/src/resources/basic_descriptor_config.php b/tests/Unit/ProtoTests/Basic/out/src/resources/basic_descriptor_config.php index bed8c0a43..356ebf9e4 100644 --- a/tests/Unit/ProtoTests/Basic/out/src/resources/basic_descriptor_config.php +++ b/tests/Unit/ProtoTests/Basic/out/src/resources/basic_descriptor_config.php @@ -1,6 +1,6 @@ assertTrue(file_exists($filename), "Expected code file missing: '{$filename}'"); $expectedCode = file_get_contents($filename); if (trim($expectedCode) !== 'IGNORE' && trim($expectedCode) !== 'assertEquals($expectedCode, $code); + $this->assertEquals($expectedCode, $code, str_replace(__DIR__ . '/', '', $filename)); } unset($files[$filename]); } diff --git a/tests/Unit/ProtoTests/ResourceNames/out/src/resources/resource_names_descriptor_config.php b/tests/Unit/ProtoTests/ResourceNames/out/src/resources/resource_names_descriptor_config.php index 1a9f2abad..66adca8aa 100644 --- a/tests/Unit/ProtoTests/ResourceNames/out/src/resources/resource_names_descriptor_config.php +++ b/tests/Unit/ProtoTests/ResourceNames/out/src/resources/resource_names_descriptor_config.php @@ -1,6 +1,6 @@