Skip to content

Commit

Permalink
Temporary workaround for protobuf extension issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Takashi Matsuo committed Jul 13, 2018
1 parent a5d099c commit affac46
Showing 1 changed file with 58 additions and 1 deletion.
59 changes: 58 additions & 1 deletion src/ApiCore/Testing/GeneratedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,66 @@
use Google\Protobuf\DescriptorPool;
use Google\Protobuf\Internal\Message;
use Google\Protobuf\Internal\RepeatedField;
use PHPUnit\Framework\Exception;
use PHPUnit\Framework\TestCase;

abstract class GeneratedTest extends TestCase
{
// Workaround for https://github.com/google/protobuf/issues/4761
public static function assertSame($expected, $actual, $message = '')
{
if ($expected instanceof \Google\Protobuf\Internal\Message) {
if (!$actual instanceof \Google\Protobuf\Internal\Message) {
throw new Exception("not \\Google\\Protobuf\\Internal\\Message object");
return;
}
parent::assertSame(spl_object_hash($expected), spl_object_hash($actual), $message);
}
parent::assertSame($expected, $actual, $message);
}

// Workaround for https://github.com/google/protobuf/issues/4761
public static function assertEquals($expected, $actual, $message = '', $delta = 0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false)
{
if (is_array($expected)){
if (count($expected) === 0) {
parent::assertEquals($expected, $actual, $message);
return;
}
if ($expected[0] instanceof \Google\Protobuf\Internal\Message) {
foreach($expected as $key => $value) {
if (! array_key_exists($key, $actual)) {
throw new Exception("Key: $key does not exist");
return;
}
self::assertEquals($value, $actual[$key]);
return;
}
} else {
parent::assertEquals($expected, $actual, $message);
return;
}
}
if ($expected instanceof \Google\Protobuf\Internal\Message) {
if (!$actual instanceof \Google\Protobuf\Internal\Message) {
throw new Exception("not \\Google\\Protobuf\\Internal\\Message object");
return;
}
parent::assertEquals(
$expected->serializeToString(),
$actual->serializeToString(),
$message
);
} else if ($expected instanceof \Google\Protobuf\GPBEmpty) {
if (!$actual instanceof \Google\Protobuf\GPBEmpty) {
throw new Exception("not a \\Google\\Protobuf\\GPBEmpty object");
return;
}
} else {
parent::assertEquals($expected, $actual, $message);
}
}

public function assertProtobufEquals(&$expected, &$actual)
{
if ($expected === $actual) {
Expand All @@ -61,7 +117,8 @@ public function assertProtobufEquals(&$expected, &$actual)
$this->assertProtobufEquals($expectedElement, $actualElement);
}
} else {
$this->assertEquals($expected, $actual);
// Call the workaround function above
self::assertEquals($expected, $actual);
if ($expected instanceof Message) {
$pool = DescriptorPool::getGeneratedPool();
$descriptor = $pool->getDescriptorByClassName(get_class($expected));
Expand Down

0 comments on commit affac46

Please sign in to comment.