* [ADD] Unit testing. Work in progress

* [MOD] Code refactoring
This commit is contained in:
nuxsmin
2018-07-09 01:06:52 +02:00
parent f764ebbb1e
commit 1e6096e974
4 changed files with 37 additions and 39 deletions

View File

@@ -299,7 +299,7 @@ class AccountService extends Service implements AccountServiceInterface
$this->accountRepository->update($accountRequest);
$this->updateItems($accountRequest);
}, $this->dic);
});
}
/**
@@ -392,7 +392,7 @@ class AccountService extends Service implements AccountServiceInterface
$accountRequest->key = $pass['key'];
$this->accountRepository->editPassword($accountRequest);
}, $this->dic);
});
}
/**
@@ -423,7 +423,7 @@ class AccountService extends Service implements AccountServiceInterface
if (!$this->accountRepository->editRestore($historyId, $this->context->getUserData()->getId())) {
throw new ServiceException(__u('Error al restaurar cuenta'));
}
}, $this->dic);
});
}
/**

View File

@@ -245,7 +245,7 @@ class AuthTokenService extends Service
$this->authTokenRepository->refreshVaultByUserId($itemData->getUserId(), $vault, Hash::hashKey($itemData->getHash()));
$this->update($itemData, $token);
}, $this->dic);
});
}
/**

View File

@@ -29,6 +29,7 @@ use Psr\Container\ContainerInterface;
use SP\Config\Config;
use SP\Core\Context\ContextInterface;
use SP\Core\Events\EventDispatcher;
use SP\Storage\Database\Database;
/**
* Class Service
@@ -74,4 +75,36 @@ abstract class Service
$this->initialize();
}
}
/**
* Bubbles a Closure in a database transaction
*
* @param \Closure $closure
*
* @return mixed
* @throws ServiceException
* @throws \Exception
*/
protected function transactionAware(\Closure $closure)
{
$database = $this->dic->get(Database::class);
if ($database->beginTransaction()) {
try {
$result = $closure->call($this);
$database->endTransaction();
return $result;
} catch (\Exception $e) {
$database->rollbackTransaction();
debugLog('Transaction:Rollback');
throw $e;
}
} else {
throw new ServiceException(__u('No es posible iniciar una transacción'));
}
}
}

View File

@@ -24,10 +24,8 @@
namespace SP\Services;
use DI\Container;
use SP\Bootstrap;
use SP\DataModel\DataModelInterface;
use SP\Storage\Database\Database;
/**
* Trait ServiceItemTrait
@@ -54,37 +52,4 @@ trait ServiceItemTrait
* @return mixed
*/
abstract public function getAllBasic();
/**
* Bubbles a Closure in a database transaction
*
* @param \Closure $closure
* @param Container $container
*
* @return mixed
* @throws ServiceException
* @throws \Exception
*/
private function transactionAware(\Closure $closure, Container $container)
{
$database = $container->get(Database::class);
if ($database->beginTransaction()) {
try {
$result = $closure->call($this);
$database->endTransaction();
return $result;
} catch (\Exception $e) {
$database->rollbackTransaction();
debugLog('Rollback');
throw $e;
}
} else {
throw new ServiceException(__u('No es posible iniciar una transacción'));
}
}
}