Skip to content

Commit 24da7f4

Browse files
committed
some more complex nested tests
1 parent c654963 commit 24da7f4

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

src/ObjectMerge.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,17 @@ private static function mergeValues($recurse, $opts, $cb, $key, $leftValue, $rig
233233
$key
234234
)
235235
);
236-
} elseif ($leftUndefined) {
236+
}
237+
238+
// if the right value was undefined, return left value and move on.
239+
if ($rightUndefined) {
240+
return $leftValue;
241+
}
242+
243+
// if left side undefined, create new empty representation of the right type to allow processing to continue
244+
// todo: revisit this, bit wasteful.
245+
if ($leftUndefined) {
237246
$leftValue = self::newEmptyValue($rightValue);
238-
} elseif ($rightUndefined) {
239-
$rightValue = self::newEmptyValue($leftValue);
240247
}
241248

242249
list($leftType, $rightType, $equal) = self::compareTypes($leftValue, $rightValue);
@@ -252,10 +259,6 @@ private static function mergeValues($recurse, $opts, $cb, $key, $leftValue, $rig
252259
);
253260
}
254261

255-
if ($rightUndefined) {
256-
return $leftValue;
257-
}
258-
259262
if (!$recurse || in_array($leftType, self::$_SIMPLE_TYPES, true)) {
260263
return $rightValue;
261264
}

tests/ObjectMergeTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,23 @@ class ObjectMergeTest extends TestCase
107107
],
108108
'expected' => '{"key":{"sub":{"sub2":{"sub3":"value"},"sub22":{"sub223":"value2"}}}}',
109109
'recurse' => true
110+
],
111+
[
112+
'objects' => [
113+
'{"glossary":{"GlossDiv":{"GlossList":{"GlossEntry":{"Abbrev":"ISO 8879:1986","Acronym":"SGML","GlossDef":{"GlossSeeAlso":["GML","XML"],"para":"A meta-markup language, used to create markup languages such as DocBook.","leftOnly":["things"]},"GlossSee":"markup","leftOnly":{"leftKey":"leftValue"},"GlossTerm":"Standard Generalized Markup Language","ID":"SGML","SortAs":"SGML","bothSides":{"leftKey":"leftValue"}}},"title":"S","leftOnly":"hello"},"title":"example glossary"}}',
114+
'{"glossary":{"GlossDiv":{"GlossList":{"GlossEntry":{"Abbrev":"ISO 8879:1986","Acronym":"SGML","GlossDef":{"GlossSeeAlso":["GML","XML"],"para":"A meta-markup language, used to create markup languages such as DocBook."},"GlossSee":"markup","GlossTerm":"Standard Generalized Markup Language","ID":"SGML","SortAs":"SGML","rightOnly":{"rightKey":"rightKey"},"bothSides":{"rightKey":"rightValue"}}},"title":"S"},"title":"example glossary","rightOnly":"hello"}}',
115+
],
116+
'expected' => '{"glossary":{"GlossDiv":{"GlossList":{"GlossEntry":{"Abbrev":"ISO 8879:1986","Acronym":"SGML","GlossDef":{"GlossSeeAlso":["GML","XML","GML","XML"],"leftOnly":["things"],"para":"A meta-markup language, used to create markup languages such as DocBook."},"GlossSee":"markup","GlossTerm":"Standard Generalized Markup Language","ID":"SGML","SortAs":"SGML","bothSides":{"leftKey":"leftValue","rightKey":"rightValue"},"leftOnly":{"leftKey":"leftValue"},"rightOnly":{"rightKey":"rightKey"}}},"leftOnly":"hello","title":"S"},"rightOnly":"hello","title":"example glossary"}}',
117+
'recurse' => true,
118+
],
119+
[
120+
'objects' => [
121+
'{"glossary":{"GlossDiv":{"GlossList":{"GlossEntry":{"Abbrev":"ISO 8879:1986","Acronym":"SGML","GlossDef":{"GlossSeeAlso":["GML","XML"],"para":"A meta-markup language, used to create markup languages such as DocBook.","leftOnly":["things"]},"GlossSee":"markup","leftOnly":{"leftKey":"leftValue"},"GlossTerm":"Standard Generalized Markup Language","ID":"SGML","SortAs":"SGML","bothSides":{"leftKey":"leftValue"}}},"title":"S","leftOnly":"hello"},"title":"example glossary"}}',
122+
'{"glossary":{"GlossDiv":{"GlossList":{"GlossEntry":{"Abbrev":"ISO 8879:1986","Acronym":"SGML","GlossDef":{"GlossSeeAlso":["GML","XML"],"para":"A meta-markup language, used to create markup languages such as DocBook."},"GlossSee":"markup","GlossTerm":"Standard Generalized Markup Language","ID":"SGML","SortAs":"SGML","rightOnly":{"rightKey":"rightKey"},"bothSides":{"rightKey":"rightValue"}}},"title":"S"},"title":"example glossary","rightOnly":"hello"}}',
123+
],
124+
'expected' => '{"glossary":{"GlossDiv":{"GlossList":{"GlossEntry":{"Abbrev":"ISO 8879:1986","Acronym":"SGML","GlossDef":{"GlossSeeAlso":["GML","XML"],"leftOnly":["things"],"para":"A meta-markup language, used to create markup languages such as DocBook."},"GlossSee":"markup","GlossTerm":"Standard Generalized Markup Language","ID":"SGML","SortAs":"SGML","bothSides":{"leftKey":"leftValue","rightKey":"rightValue"},"leftOnly":{"leftKey":"leftValue"},"rightOnly":{"rightKey":"rightKey"}}},"leftOnly":"hello","title":"S"},"rightOnly":"hello","title":"example glossary"}}',
125+
'recurse' => true,
126+
'opts' => OBJECT_MERGE_OPT_UNIQUE_ARRAYS,
110127
]
111128
);
112129

@@ -120,7 +137,12 @@ private function doDecode($test, $json)
120137
$out = json_decode($json);
121138
if (JSON_ERROR_NONE !== json_last_error()) {
122139
throw new RuntimeException(
123-
sprintf('json_decode returned error while processing test "%s": %s', $test, $json)
140+
sprintf(
141+
'json_decode returned error while processing test "%s": %s; json =%s',
142+
$test,
143+
json_last_error_msg(),
144+
$json
145+
)
124146
);
125147
}
126148
return $out;

0 commit comments

Comments
 (0)