public function test_changeParent()
 {
     $obm = \Magento\Framework\App\ObjectManager::getInstance();
     /** @var  $call \Praxigento\Downline\Service\ICustomer */
     $call = $obm->get(\Praxigento\Downline\Service\ICustomer::class);
     $request = new Request\ChangeParent();
     $request->setCustomerId(6);
     $request->setNewParentId(1);
     $response = $call->changeParent($request);
     $this->assertTrue($response->isSucceed());
 }
Exemplo n.º 2
0
 public function changeParent(Request\ChangeParent $request)
 {
     $result = new Response\ChangeParent();
     $customerId = $request->getCustomerId();
     $newParentId = $request->getNewParentId();
     $formatted = $request->getDate();
     $this->_logger->info("Set up new parent #{$newParentId} for customer #{$customerId}.");
     $def = $this->_manTrans->begin();
     try {
         /* get customer's downline  data */
         $data = $this->_repoCustomer->getById($customerId);
         $currParentId = $data->getParentId();
         $currDepth = $data->getDepth();
         $currPath = $data->getPath();
         if ($currParentId == $newParentId) {
             /* nothing to change */
             $result->markSucceed();
             $this->_manTrans->commit($def);
             $this->_logger->notice("Current parent is the same as new one. Nothing to do.");
         } else {
             if ($customerId == $newParentId) {
                 /* change to root node */
                 $newCustomerDepth = Cfg::INIT_DEPTH;
                 $newCustomerPath = Cfg::DTPS;
             } else {
                 /* get new parent data */
                 $newParentData = $this->_repoCustomer->getById($newParentId);
                 $newParentDepth = $newParentData->getDepth();
                 $newParentPath = $newParentData->getPath();
                 $newCustomerDepth = $newParentDepth + 1;
                 $newCustomerPath = $newParentPath . $newParentId . Cfg::DTPS;
             }
             /* update customer with new data */
             $bind = [Customer::ATTR_PARENT_ID => $newParentId, Customer::ATTR_DEPTH => $newCustomerDepth, Customer::ATTR_PATH => $newCustomerPath];
             $updateRows = $this->_repoCustomer->updateById($customerId, $bind);
             if ($updateRows == 1) {
                 /* update depths and paths in downline */
                 $deltaDepth = $newCustomerDepth - $currDepth;
                 $pathKey = $currPath . $customerId . Cfg::DTPS;
                 $pathReplace = $newCustomerPath . $customerId . Cfg::DTPS;
                 $rowsUpdated = $this->_repoCustomer->updateChildrenPath($pathKey, $pathReplace, $deltaDepth);
                 $this->_logger->info("Total '{$rowsUpdated}' customers in downline were updated.");
                 /* save new record into change log */
                 $bind = [Change::ATTR_CUSTOMER_ID => $customerId, Change::ATTR_PARENT_ID => $newParentId, Change::ATTR_DATE_CHANGED => $formatted];
                 $insertedId = $this->_repoChange->create($bind);
                 if ($insertedId) {
                     $this->_logger->info("New change log record #{$insertedId} is inserted (customer: {$customerId}, parent: {$newParentId}, date: {$formatted}).");
                     $this->_manTrans->commit($def);
                     $result->markSucceed();
                     $this->_logger->info("New parent #{$newParentId} for customer #{$customerId} is set.");
                 }
             }
         }
     } finally {
         $this->_manTrans->end($def);
     }
     return $result;
 }
 public function test_changeParent_rootNode()
 {
     /** === Test Data === */
     $DATE = '2015-12-05 12:34:56';
     $CUSTOMER_ID = 21;
     $PARENT_ID_CUR = 43;
     $PATH_CUR = '/1/2/3/43/';
     $DEPTH_CUR = 4;
     $PARENT_ID_NEW = $CUSTOMER_ID;
     $PATH_NEW = '/1/2/55/';
     $DEPTH_NEW = 3;
     $DO_CUST = new Customer();
     $DO_CUST->setParentId($PARENT_ID_CUR);
     $DO_CUST->setPath($PATH_CUR);
     $DO_CUST->setDepth($DEPTH_CUR);
     $DO_PARENT = new Customer();
     $DO_PARENT->setPath($PATH_NEW);
     $DO_PARENT->setDepth($DEPTH_NEW);
     /** === Setup Mocks === */
     // $def = $this->_manTrans->begin();
     $mDef = $this->_mockTransactionDefinition();
     $this->mManTrans->shouldReceive('begin')->once()->andReturn($mDef);
     // $data = $this->_repoCustomer->getById($customerId);
     $this->mRepoCustomer->shouldReceive('getById')->once()->andReturn($DO_CUST);
     // $updateRows = $this->_repoCustomer->updateById($customerId, $bind);
     $this->mRepoCustomer->shouldReceive('updateById')->once()->andReturn(1);
     // $rowsUpdated = $this->_repoCustomer->updateChildrenPath($pathKey, $pathReplace, $deltaDepth);
     $this->mRepoCustomer->shouldReceive('updateChildrenPath')->once()->andReturn(2);
     // $insertedId = $this->_repoChange->create($bind);
     $this->mRepoChange->shouldReceive('create')->once()->andReturn(433);
     // $this->_manTrans->commit($def);
     $this->mManTrans->shouldReceive('commit')->once();
     // $this->_manTrans->end($def);
     $this->mManTrans->shouldReceive('end')->once();
     /** === Call and asserts  === */
     $req = new Request\ChangeParent();
     $req->setCustomerId($CUSTOMER_ID);
     $req->setNewParentId($PARENT_ID_NEW);
     $req->setDate($DATE);
     $resp = $this->obj->changeParent($req);
     $this->assertTrue($resp->isSucceed());
 }