Skip to content

Commit 29cbbc4

Browse files
committed
Merge remote-tracking branch 'origin/php-ext-convert' into release-31.0-patch
2 parents 69cde17 + 4629255 commit 29cbbc4

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

php/ext/google/protobuf/convert.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,9 @@ static bool to_double(zval* php_val, double* dbl) {
281281

282282
static bool to_bool(zval* from, bool* to) {
283283
switch (Z_TYPE_P(from)) {
284+
case IS_NULL:
285+
*to = false;
286+
return true;
284287
case IS_TRUE:
285288
*to = true;
286289
return true;
@@ -315,6 +318,9 @@ static bool to_string(zval* from) {
315318
switch (Z_TYPE_P(from)) {
316319
case IS_STRING:
317320
return true;
321+
case IS_NULL:
322+
ZVAL_EMPTY_STRING(from);
323+
return true;
318324
case IS_TRUE:
319325
case IS_FALSE:
320326
case IS_LONG:

php/tests/GeneratedClassTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,63 @@ public function testOptionalInt32Field()
249249
$this->assertSame(0, $m->getTrueOptionalInt32());
250250
}
251251

252+
#########################################################
253+
# Test values that are converted by setter
254+
#########################################################
255+
256+
public function testConvertValueSetter()
257+
{
258+
// convert null
259+
$m_null = new TestMessage();
260+
261+
$m_null->setOptionalBool(null);
262+
$m_null->setOptionalBytes(null);
263+
$m_null->setOptionalString(null);
264+
$m_null->setTrueOptionalBool(null);
265+
$m_null->setTrueOptionalBytes(null);
266+
$m_null->setTrueOptionalString(null);
267+
// allow null message
268+
$m_null->setOptionalMessage(null);
269+
$m_null->setTrueOptionalMessage(null);
270+
271+
$this->assertSame(false, $m_null->getOptionalBool());
272+
$this->assertSame('', $m_null->getOptionalString());
273+
$this->assertSame('', $m_null->getOptionalBytes());
274+
$this->assertNull($m_null->getOptionalMessage());
275+
276+
$this->assertSame(false, $m_null->getTrueOptionalBool());
277+
$this->assertSame('', $m_null->getTrueOptionalString());
278+
$this->assertSame('', $m_null->getTrueOptionalBytes());
279+
$this->assertNull($m_null->getTrueOptionalMessage());
280+
281+
// Convert int
282+
$m_number = new TestMessage();
283+
284+
$m_number->setOptionalBool(0);
285+
$m_number->setOptionalBytes(0);
286+
$m_number->setOptionalString(0);
287+
$m_number->setTrueOptionalBool(1);
288+
$m_number->setTrueOptionalBytes(1);
289+
$m_number->setTrueOptionalString(1);
290+
291+
$this->assertSame(false, $m_number->getOptionalBool());
292+
$this->assertSame('0', $m_number->getOptionalString());
293+
$this->assertSame('0', $m_number->getOptionalBytes());
294+
295+
$this->assertSame(true, $m_number->getTrueOptionalBool());
296+
$this->assertSame('1', $m_number->getTrueOptionalString());
297+
$this->assertSame('1', $m_number->getTrueOptionalBytes());
298+
299+
// Convert str
300+
$m_number_str = new TestMessage();
301+
302+
$m_number_str->setOptionalBool('');
303+
$m_number_str->setTrueOptionalBool('STR');
304+
305+
$this->assertSame(false, $m_number_str->getOptionalBool());
306+
$this->assertSame(true, $m_number_str->getTrueOptionalBool());
307+
}
308+
252309
#########################################################
253310
# Test uint32 field.
254311
#########################################################

0 commit comments

Comments
 (0)