You are here

public function ElementTreeTest::testApplyRecursivelyPostOrder in Little helpers 7.2

Test post-order traversal.

File

tests/ElementTreeTest.php, line 46

Class

ElementTreeTest
Test the element tree utility functions.

Namespace

Drupal\little_helpers

Code

public function testApplyRecursivelyPostOrder() {
  $test_array = [
    'a' => [
      'a1' => [
        '#add' => [
          1,
          2,
        ],
      ],
      '#add' => [
        3,
      ],
    ],
    'b' => [
      '#add' => [
        4,
      ],
    ],
  ];
  ElementTree::applyRecursively($test_array, function (&$element, $key, &$parent) {
    $element += [
      '#sum' => 0,
      '#add' => [],
    ];
  });
  ElementTree::applyRecursively($test_array, function (&$element, $key, &$parent) {
    $element['#sum'] += array_sum($element['#add']);
    if ($parent) {
      $parent['#sum'] += $element['#sum'];
    }
  }, TRUE);
  $this
    ->assertEqual(3, $test_array['a']['a1']['#sum']);
  $this
    ->assertEqual(6, $test_array['a']['#sum']);
  $this
    ->assertEqual(10, $test_array['#sum']);
}