public function test_extendMinimal()
 {
     $obm = \Magento\Framework\App\ObjectManager::getInstance();
     /** @var  $call \Praxigento\Downline\Service\Snap\Call */
     $call = $obm->get('Praxigento\\Downline\\Service\\Snap\\Call');
     $req = new Request\ExpandMinimal();
     $req->setTree([2 => 1, 3 => 1, 4 => 2, 5 => 2, 6 => 3, 7 => 3, 20 => 20, 10 => 7, 11 => 7, 1 => 1, 12 => 10]);
     /** @var  $resp Response\Calc */
     $resp = $call->expandMinimal($req);
     $this->assertTrue($resp->isSucceed());
 }
 public function test_expandMinimal()
 {
     /** === Test Data === */
     $tree = [2 => 1, 3 => 1, 4 => 2, 5 => 2, 6 => 3, 7 => 3, 20 => 20, 10 => 7, 11 => 7, 1 => 1, 12 => 10, 12 => 121];
     /** === Setup Mocks === */
     /** === Call and asserts  === */
     $req = new Request\ExpandMinimal();
     $req->setTree($tree);
     $resp = $this->obj->expandMinimal($req);
     $this->assertTrue($resp->isSucceed());
     $snapData = $resp->getSnapData();
     $this->assertTrue(is_array($snapData));
 }
Exemplo n.º 3
0
 /**
  * Extend minimal Downline Tree Data (customer & parent) with depth and path.
  *
  * @param Request\ExpandMinimal $request
  *
  * @return Response\ExpandMinimal
  *
  * @deprecated use \Praxigento\Downline\Tool\ITree::expandMinimal instead
  */
 public function expandMinimal(Request\ExpandMinimal $request)
 {
     $result = new Response\ExpandMinimal();
     $keyCustomerId = $request->getKeyCustomerId();
     $keyParentId = $request->getKeyParentId();
     $treeIn = $request->getTree();
     /**
      * Validate tree consistency: all parents should be customers too, create map for customers with invalid
      * parents to set this entries as orphans.
      */
     $mapCusts = [];
     // registry for all customers.
     foreach ($treeIn as $ndx => $item) {
         $custId = is_null($keyCustomerId) ? $ndx : $item[$keyCustomerId];
         $mapCusts[] = $custId;
     }
     $mapOrphans = [];
     // registry for orphan customers.
     foreach ($treeIn as $ndx => $item) {
         $custId = is_null($keyCustomerId) ? $ndx : $item[$keyCustomerId];
         $parentId = !is_array($item) ? $item : $item[$keyParentId];
         if (!in_array($parentId, $mapCusts)) {
             $msg = "Parent #{$parentId} for customer #{$custId} is not present in the minimal tree.";
             $msg .= " Customer #{$custId} is set as orphan.";
             $this->_logger->warning($msg);
             $mapOrphans[] = $custId;
         }
     }
     /* create tree (see http://stackoverflow.com/questions/2915748/how-can-i-convert-a-series-of-parent-child-relationships-into-a-hierarchical-tre) */
     $flat = [];
     $tree = [];
     foreach ($treeIn as $ndx => $item) {
         $custId = is_null($keyCustomerId) ? $ndx : $item[$keyCustomerId];
         $parentId = !is_array($item) ? $item : $item[$keyParentId];
         /* filter orphans */
         if (in_array($custId, $mapOrphans)) {
             $parentId = $custId;
         }
         /* map customers into tree */
         if (!isset($flat[$custId])) {
             $flat[$custId] = [];
         }
         if ($custId != $parentId) {
             $flat[$parentId][$custId] =& $flat[$custId];
         } else {
             /* root node */
             $tree[$custId] =& $flat[$custId];
         }
     }
     /* populate tree with depth/path/... and compose array to insert into DB  */
     $snapData = [];
     $this->_composeSnapData($snapData, $tree);
     $result->setSnapData($snapData);
     $result->markSucceed();
     return $result;
 }