public function test_getRepresentative()
 {
     $obm = \Magento\Framework\App\ObjectManager::getInstance();
     /** @var  $call \Praxigento\Accounting\Service\Account\Call */
     $call = $obm->get('Praxigento\\Accounting\\Service\\Account\\Call');
     $request = new Request\GetRepresentative();
     $request->setData(GetRepresentativeRequest::ASSET_TYPE_ID, 1);
     $response = $call->getRepresentative($request);
     $this->assertTrue($response->isSucceed());
 }
 public function test_getRepresentative_byAssetId_accountsFound()
 {
     /** === Test Data === */
     $custId = 32;
     $accId1 = 34;
     $accId2 = 43;
     $assetTypeId1 = 45;
     $assetTypeId2 = 65;
     $data = [new Account([Account::ATTR_ID => $accId1, Account::ATTR_ASSET_TYPE_ID => $assetTypeId1]), new Account([Account::ATTR_ID => $accId2, Account::ATTR_ASSET_TYPE_ID => $assetTypeId2])];
     /** === Setup Mocks === */
     // $customerId = $this->_repoMod->getRepresentativeCustomerId();
     $this->mRepoMod->shouldReceive('getRepresentativeCustomerId')->once()->andReturn($custId);
     // accounts = $this->_repoAccount->getAllByCustomerId($customerId);
     $this->mRepoAccount->shouldReceive('getAllByCustomerId')->once()->andReturn($data);
     /** === Call and asserts  === */
     $req = new Request\GetRepresentative();
     $req->setAssetTypeId($assetTypeId1);
     $resp = $this->obj->getRepresentative($req);
     $this->assertTrue($resp->isSucceed());
     $this->assertEquals($accId1, $resp->getData(Account::ATTR_ID));
     /* secondary request to use cache */
     $resp = $this->obj->getRepresentative($req);
     $this->assertTrue($resp->isSucceed());
     $this->assertEquals($accId1, $resp->getData(Account::ATTR_ID));
 }
 /**
  * @param Request\GetRepresentative $request
  *
  * @return Response\GetRepresentative
  */
 public function getRepresentative(Request\GetRepresentative $request)
 {
     $result = new Response\GetRepresentative();
     $typeId = $request->getAssetTypeId();
     $typeCode = $request->getAssetTypeCode();
     $this->_logger->info("'Get representative account' operation is called.");
     if (is_null($typeId)) {
         $typeId = $this->_repoTypeAsset->getIdByCode($typeCode);
     }
     if (!is_null($typeId)) {
         if (isset($this->_cachedRepresentAccs[$typeId])) {
             $result->setData($this->_cachedRepresentAccs[$typeId]);
             $result->markSucceed();
         } else {
             /* there is no cached data yet */
             /* get representative customer ID */
             $customerId = $this->_repoAccount->getRepresentativeCustomerId();
             /* get all accounts for the representative customer */
             $accounts = $this->_repoAccount->getAllByCustomerId($customerId);
             if ($accounts) {
                 $mapped = [];
                 foreach ($accounts as $one) {
                     $mapped[$one->getAssetTypeId()] = $one;
                 }
                 $this->_cachedRepresentAccs = $mapped;
             }
             /* check selected accounts */
             if (isset($this->_cachedRepresentAccs[$typeId])) {
                 $result->setData($this->_cachedRepresentAccs[$typeId]);
                 $result->markSucceed();
             } else {
                 /* there is no accounts yet */
                 $req = new Request\Get();
                 $req->setCustomerId($customerId);
                 $req->setAssetTypeId($typeId);
                 $req->setCreateNewAccountIfMissed();
                 $resp = $this->get($req);
                 $accData = $resp->getData();
                 $this->_cachedRepresentAccs[$accData[Account::ATTR_ASSET_TYPE_ID]] = new Account($accData);
                 $result->setData($accData);
                 $result->markSucceed();
             }
         }
     } else {
         $this->_logger->error("Asset type is not defined (asset code: {$typeCode}).");
     }
     if ($result->isSucceed()) {
         $repAccId = $result->getId();
         $this->_logger->info("Representative account #{$repAccId} is found.");
     }
     $this->_logger->info("'Get representative account' operation is completed.");
     return $result;
 }