feat: support ops/closure v4 by bshaffer · Pull Request #8559 · googleapis/google-cloud-php · GitHub
Skip to content
Merged
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
2 changes: 1 addition & 1 deletion Core/composer.json
1 change: 1 addition & 0 deletions Core/src/Batch/OpisClosureSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* A closure serializer utilizing
* [Opis Closure Library](https://github.com/opis/closure).
*
* @deprecated use OpisClosureSerializerV4
* @experimental The experimental flag means that while we believe this method
* or class is ready for use, it may change before release in backwards-
* incompatible ways. Please use with caution, and test thoroughly when
Expand Down
52 changes: 52 additions & 0 deletions Core/src/Batch/OpisClosureSerializerV4.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Copyright 2018 Google Inc. All Rights Reserved.
*
* 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
*
* http://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.
*/

namespace Google\Cloud\Core\Batch;

use Opis\Closure;

/**
* A closure serializer utilizing
* [Opis Closure Library](https://github.com/opis/closure).
*
* @experimental The experimental flag means that while we believe this method
* or class is ready for use, it may change before release in backwards-
* incompatible ways. Please use with caution, and test thoroughly when
* upgrading.
*/
class OpisClosureSerializerV4 implements ClosureSerializerInterface
{
/**
* Recursively serializes closures.
*
* @param mixed $data
*/
public function wrapClosures(&$data)
{
$data = Closure\serialize($data);
}

/**
* Recursively unserializes closures.
*
* @param mixed $data
*/
public function unwrapClosures(&$data)
{
$data = Closure\unserialize($data);
}
}
3 changes: 3 additions & 0 deletions Core/src/Batch/SerializableClientTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ private function getUnwrappedClientConfig()
*/
private function getDefaultClosureSerializer()
{
if (function_exists('Opis\Closure\serialize')) {
return new OpisClosureSerializerV4();
}
if (class_exists(SerializableClosure::class)) {
return new OpisClosureSerializer();
}
Expand Down
36 changes: 26 additions & 10 deletions Core/tests/Unit/Batch/OpisClosureSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
namespace Google\Cloud\Core\Tests\Unit\Batch;

use Google\Cloud\Core\Batch\OpisClosureSerializer;
use Google\Cloud\Core\Batch\OpisClosureSerializerV4;
use Opis\Closure\SerializableClosure;
use PHPUnit\Framework\TestCase;

Expand All @@ -27,25 +28,40 @@
*/
class OpisClosureSerializerTest extends TestCase
{
private $serialzer;

public function setUp(): void
public function testWrapAndUnwrapClosures()
{
$this->serializer = new OpisClosureSerializer();
if (!method_exists(SerializableClosure::class, 'enterContext')) {
$this->markTestSkipped('Requires ops/serializer:v3');
}

$data['closure'] = function () {
return true;
};

$serializer = new OpisClosureSerializer();

$serializer->wrapClosures($data);
$this->assertInstanceOf(SerializableClosure::class, $data['closure']);

$serializer->unwrapClosures($data);
$this->assertTrue($data['closure']());
}

public function testWrapAndUnwrapClosures()
public function testWrapAndUnwrapClosuresV4()
{
if (!function_exists('Opis\Closure\serialize')) {
$this->markTestSkipped('Requires ops/serializer:v3');
}

$data['closure'] = function () {
return true;
};

$this->serializer
->wrapClosures($data);
$serializer = new OpisClosureSerializerV4();
$serializer->wrapClosures($data);
$this->assertIsString($data);

$this->assertInstanceOf(SerializableClosure::class, $data['closure']);
$this->serializer
->unwrapClosures($data);
$serializer->unwrapClosures($data);
$this->assertTrue($data['closure']());
}
}
1 change: 1 addition & 0 deletions ErrorReporting/tests/Unit/BootstrapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ public function testExceptionHandler($exception)
/**
* @dataProvider exceptionProvider
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testExceptionHandlerWithHttpContext($exception)
{
Expand Down
7 changes: 4 additions & 3 deletions Logging/tests/Unit/LoggingClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use Google\Cloud\Core\Batch\BatchRunner;
use Google\Cloud\Core\Batch\OpisClosureSerializer;
use Google\Cloud\Core\Batch\OpisClosureSerializerV4;
use Google\Cloud\Core\Report\EmptyMetadataProvider;
use Google\Cloud\Core\Testing\GrpcTestTrait;
use Google\Cloud\Core\Testing\TestHelpers;
Expand Down Expand Up @@ -313,6 +314,7 @@ public function testGetsPsrLogger()

public function testOptionsArePassedToPsrLogger()
{
$opisV4 = class_exists(OpisClosureSerializerV4::class);
$options = [
'metadataProvider' => new EmptyMetadataProvider,
'batchEnabled' => true,
Expand All @@ -326,7 +328,7 @@ public function testOptionsArePassedToPsrLogger()
'projectId' => 'test'
],
'batchRunner' => new BatchRunner,
'closureSerializer' => new OpisClosureSerializer,
'closureSerializer' => $opisV4 ? new OpisClosureSerializerV4 : new OpisClosureSerializer,
'debugOutputResource' => fopen('php://temp', 'wb')
];

Expand All @@ -335,9 +337,8 @@ public function testOptionsArePassedToPsrLogger()
$reflection = new \ReflectionClass($psrLogger);
foreach ($options as $name => $value) {
$attr = $reflection->getProperty($name);
$attr->setAccessible(true);
$this->assertEquals(
$value,
$name === 'clientConfig' && $opisV4 ? serialize($value) : $value,
$attr->getValue($psrLogger),
"$name assertion failed."
);
Expand Down
5 changes: 4 additions & 1 deletion Logging/tests/Unit/PsrLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace Google\Cloud\Logging\Tests\Unit;

use Google\Cloud\Core\Batch\OpisClosureSerializerV4;
use Google\Cloud\Core\Report\EmptyMetadataProvider;
use Google\Cloud\Logging\Logger;
use Google\Cloud\Logging\PsrLogger;
Expand Down Expand Up @@ -267,7 +268,9 @@ public function testSerializesCorrectly()
$attr->setAccessible(true);
$this->assertEquals(
$attr->getValue($psrLogger),
$options[$attributeName]
$attributeName === 'clientConfig'&& class_exists(OpisClosureSerializerV4::class)
? serialize($options[$attributeName])
: $options[$attributeName]
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"erusev/parsedown": "^1.6",
"phpseclib/phpseclib": "^3.0",
"google/cloud-tools": "^0.16.0",
"opis/closure": "^3.0",
"opis/closure": "^3.7||^4.0",
"flix-tech/avro-php": "^5.0.0",
"phpspec/prophecy-phpunit": "^2.1",
"kreait/firebase-php": "^6.9",
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon.dist
Loading