Add test for CRM_Utils_Array::flatten()
authorAidan Saunders <aidan.saunders@squiffle.uk>
Wed, 31 Oct 2018 19:07:09 +0000 (19:07 +0000)
committerAidan Saunders <aidan.saunders@squiffle.uk>
Thu, 29 Nov 2018 13:27:53 +0000 (13:27 +0000)
tests/phpunit/CRM/Utils/ArrayTest.php

index 135b0f05c5a8202036ac3bf62fb64b72594656d4..212b523ceb49d8b86bf5bf36bec942d785557805 100644 (file)
@@ -328,4 +328,43 @@ class CRM_Utils_ArrayTest extends CiviUnitTestCase {
     $this->assertEquals($expected, $source);
   }
 
+  /**
+   * Test the flatten function
+   */
+  public function testFlatten() {
+    $data = [
+      'my_array' => [
+        '0' => 'bar',
+        '1' => 'baz',
+        '2' => 'boz',
+      ],
+      'my_complex' => [
+         'dog' => 'woof',
+         'asdf' => [
+           'my_zero' => 0,
+           'my_int' => 1,
+           'my_null' => NULL,
+           'my_empty' => '',
+         ],
+      ],
+      'my_simple' => 999,
+    ];
+
+    $expected = [
+      'my_array.0' => 'bar',
+      'my_array.1' => 'baz',
+      'my_array.2' => 'boz',
+      'my_complex.dog' => 'woof',
+      'my_complex.asdf.my_zero' => 0,
+      'my_complex.asdf.my_int' => 1,
+      'my_complex.asdf.my_null' => NULL,
+      'my_complex.asdf.my_empty' => '',
+      'my_simple' => 999,
+    ];
+
+    $flat = [];
+    CRM_Utils_Array::flatten($data, $flat);
+    $this->assertEquals($flat, $expected);
+  }
+
 }