Skip to content

Commit

Permalink
Merge pull request #8003 from anight/php_extension_add_has_oneof_fiel…
Browse files Browse the repository at this point in the history
…d_method

add missing hasOneof method to check presence of oneof fields
  • Loading branch information
haberman committed Nov 4, 2020
2 parents 1dd4834 + 52d3be3 commit 957a0bf
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
34 changes: 34 additions & 0 deletions php/ext/google/protobuf/message.c
Expand Up @@ -904,6 +904,39 @@ PHP_METHOD(Message, whichOneof) {
RETURN_STRING(field ? upb_fielddef_name(field) : "");
}

/**
* Message::hasOneof()
*
* Returns the presence of the given oneof field, given a field number. Called
* from generated code methods such as:
*
* public function hasDoubleValueOneof()
* {
* return $this->hasOneof(10);
* }
*
* @return boolean
*/
PHP_METHOD(Message, hasOneof) {
Message* intern = (Message*)Z_OBJ_P(getThis());
zend_long field_num;
const upb_fielddef* f;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &field_num) == FAILURE) {
return;
}

f = upb_msgdef_itof(intern->desc->msgdef, field_num);

if (!f || !upb_fielddef_realcontainingoneof(f)) {
php_error_docref(NULL, E_USER_ERROR,
"Internal error, no such oneof field %d\n",
(int)field_num);
}

RETVAL_BOOL(upb_msg_has(intern->msg, f));
}

/**
* Message::readOneof()
*
Expand Down Expand Up @@ -1014,6 +1047,7 @@ static zend_function_entry Message_methods[] = {
PHP_ME(Message, mergeFrom, arginfo_mergeFrom, ZEND_ACC_PUBLIC)
PHP_ME(Message, readWrapperValue, arginfo_read, ZEND_ACC_PROTECTED)
PHP_ME(Message, writeWrapperValue, arginfo_write, ZEND_ACC_PROTECTED)
PHP_ME(Message, hasOneof, arginfo_read, ZEND_ACC_PROTECTED)
PHP_ME(Message, readOneof, arginfo_read, ZEND_ACC_PROTECTED)
PHP_ME(Message, writeOneof, arginfo_write, ZEND_ACC_PROTECTED)
PHP_ME(Message, whichOneof, arginfo_read, ZEND_ACC_PROTECTED)
Expand Down
1 change: 1 addition & 0 deletions php/phpunit.xml
Expand Up @@ -13,6 +13,7 @@
<file>tests/DescriptorsTest.php</file>
<file>tests/GeneratedServiceTest.php</file>
<file>tests/WrapperTypeSettersTest.php</file>
<file>tests/HasOneofTest.php</file>
</testsuite>
</testsuites>
</phpunit>
26 changes: 26 additions & 0 deletions php/tests/HasOneofTest.php
@@ -0,0 +1,26 @@
<?php

require_once('test_util.php');

use Foo\TestMessage;

class HasOneofTest extends \PHPUnit\Framework\TestCase {

#########################################################
# Test hasOneof<Field> methods exists and working
#########################################################

public function testHasOneof() {
$m = new TestMessage();
$this->assertFalse($m->hasOneofInt32());
$m->setOneofInt32(42);
$this->assertTrue($m->hasOneofInt32());
$m->setOneofString("bar");
$this->assertFalse($m->hasOneofInt32());
$this->assertTrue($m->hasOneofString());
$m->clear();
$this->assertFalse($m->hasOneofInt32());
$this->assertFalse($m->hasOneofString());
}

}
2 changes: 1 addition & 1 deletion php/tests/test.sh
Expand Up @@ -29,7 +29,7 @@ esac

[ -f $PHPUNIT ] || wget https://phar.phpunit.de/$PHPUNIT

tests=( ArrayTest.php EncodeDecodeTest.php GeneratedClassTest.php MapFieldTest.php WellKnownTest.php DescriptorsTest.php WrapperTypeSettersTest.php)
tests=( ArrayTest.php EncodeDecodeTest.php GeneratedClassTest.php MapFieldTest.php WellKnownTest.php DescriptorsTest.php WrapperTypeSettersTest.php HasOneofTest.php)

for t in "${tests[@]}"
do
Expand Down

0 comments on commit 957a0bf

Please sign in to comment.