Skip to content

Commit

Permalink
test(DI): Implement definitions test
Browse files Browse the repository at this point in the history
Signed-off-by: Rubén D <nuxsmin@syspass.org>
  • Loading branch information
nuxsmin committed Jun 7, 2024
1 parent 2ee21ac commit a6dad33
Show file tree
Hide file tree
Showing 60 changed files with 1,017 additions and 904 deletions.
33 changes: 12 additions & 21 deletions app/modules/api/Controllers/Config/BackupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
use SP\Domain\Core\Acl\AclActionsInterface;
use SP\Domain\Core\Acl\AclInterface;
use SP\Domain\Core\Exceptions\InvalidClassException;
use SP\Domain\Export\Dtos\BackupFiles;
use SP\Domain\Export\Ports\BackupFileService;
use SP\Domain\Export\Services\BackupFileHelper;
use SP\Modules\Api\Controllers\ControllerBase;
use SP\Modules\Api\Controllers\Help\ConfigHelp;

Expand All @@ -46,22 +46,19 @@
*/
final class BackupController extends ControllerBase
{
private BackupFileService $fileBackupService;

/**
* @throws InvalidClassException
*/
public function __construct(
Application $application,
Klein $router,
ApiService $apiService,
AclInterface $acl,
BackupFileService $fileBackupService
Application $application,
Klein $router,
ApiService $apiService,
AclInterface $acl,
private readonly BackupFileService $fileBackupService,
private readonly BackupFiles $backupFiles
) {
parent::__construct($application, $router, $apiService, $acl);

$this->fileBackupService = $fileBackupService;

$this->apiService->setHelpClass(ConfigHelp::class);
}

Expand Down Expand Up @@ -98,24 +95,18 @@ public function backupAction(): void
}

/**
* @param string|null $path
* @param string|null $path
*
* @return array[]
*/
private function buildBackupFiles(?string $path): array
{
$backupFiles = $this->backupFiles->withPath($path);

return [
'files' => [
'app' => BackupFileHelper::getAppBackupFilename(
$path,
$this->fileBackupService->getHash(),
true
),
'db' => BackupFileHelper::getDbBackupFilename(
$path,
$this->fileBackupService->getHash(),
true
),
'app' => (string)$backupFiles->getAppBackupFile(),
'db' => (string)$backupFiles->getDbBackupFile(),
],
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
use SP\Domain\Core\Acl\UnauthorizedPageException;
use SP\Domain\Core\Exceptions\SessionTimeout;
use SP\Domain\Core\Exceptions\SPException;
use SP\Domain\Export\Services\BackupFileHelper;
use SP\Domain\Export\Dtos\BackupFile;
use SP\Domain\Export\Dtos\BackupType;
use SP\Infrastructure\File\FileHandler;
use SP\Modules\Web\Controllers\SimpleControllerBase;
use SP\Modules\Web\Controllers\Traits\JsonTrait;
Expand All @@ -57,13 +58,9 @@ public function downloadBackupAppAction(): string
try {
Session::close();

$filePath = BackupFileHelper::getAppBackupFilename(
BACKUP_PATH,
$this->configData->getBackupHash(),
true
);
$filePath = new BackupFile(BackupType::app, $this->configData->getBackupHash(), BACKUP_PATH, 'gz');

$file = new FileHandler($filePath);
$file = new FileHandler((string)$filePath);
$file->checkFileExists();

$this->eventDispatcher->notify(
Expand All @@ -83,7 +80,7 @@ public function downloadBackupAppAction(): string
->header('Content-type', $file->getFileType())
->header('Content-Description', ' sysPass file')
->header('Content-transfer-encoding', 'chunked')
->header('Content-Disposition', 'attachment; filename="'.basename($file->getFile()).'"')
->header('Content-Disposition', 'attachment; filename="' . basename($file->getFile()) . '"')
->header('Set-Cookie', 'fileDownload=true; path=/')
->send();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
use SP\Domain\Core\Acl\UnauthorizedPageException;
use SP\Domain\Core\Exceptions\SessionTimeout;
use SP\Domain\Core\Exceptions\SPException;
use SP\Domain\Export\Services\BackupFileHelper;
use SP\Domain\Export\Dtos\BackupFile;
use SP\Domain\Export\Dtos\BackupType;
use SP\Infrastructure\File\FileHandler;
use SP\Modules\Web\Controllers\SimpleControllerBase;
use SP\Modules\Web\Controllers\Traits\JsonTrait;
Expand All @@ -58,13 +59,9 @@ public function downloadBackupDbAction(): string
try {
Session::close();

$filePath = BackupFileHelper::getDbBackupFilename(
BACKUP_PATH,
$this->configData->getBackupHash(),
true
);
$filePath = new BackupFile(BackupType::db, $this->configData->getBackupHash(), BACKUP_PATH, 'gz');

$file = new FileHandler($filePath);
$file = new FileHandler((string)$filePath);
$file->checkFileExists();

$this->eventDispatcher->notify(
Expand All @@ -84,7 +81,7 @@ public function downloadBackupDbAction(): string
->header('Content-type', $file->getFileType())
->header('Content-Description', ' sysPass file')
->header('Content-transfer-encoding', 'chunked')
->header('Content-Disposition', 'attachment; filename="'.basename($file->getFile()).'"')
->header('Content-Disposition', 'attachment; filename="' . basename($file->getFile()) . '"')
->header('Set-Cookie', 'fileDownload=true; path=/')
->send();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
use SP\Domain\Core\Acl\UnauthorizedPageException;
use SP\Domain\Core\Exceptions\SessionTimeout;
use SP\Domain\Core\Exceptions\SPException;
use SP\Domain\Export\Services\XmlExport;
use SP\Domain\Export\Dtos\BackupFile as BackupFileDto;
use SP\Domain\Export\Dtos\BackupType;
use SP\Infrastructure\File\FileHandler;
use SP\Modules\Web\Controllers\SimpleControllerBase;
use SP\Modules\Web\Controllers\Traits\JsonTrait;
Expand All @@ -53,10 +54,11 @@ public function downloadExportAction(): string
try {
Session::close();

$filePath = XmlExport::buildFilename(
$filePath = (string)new BackupFileDto(
BackupType::export,
$this->configData->getExportHash() ?: '',
BACKUP_PATH,
$this->configData->getExportHash(),
true
'gz'
);

$file = new FileHandler($filePath);
Expand All @@ -79,7 +81,7 @@ public function downloadExportAction(): string
->header('Content-type', $file->getFileType())
->header('Content-Description', ' sysPass file')
->header('Content-transfer-encoding', 'chunked')
->header('Content-Disposition', 'attachment; filename="'.basename($file->getFile()).'"')
->header('Content-Disposition', 'attachment; filename="' . basename($file->getFile()) . '"')
->header('Set-Cookie', 'fileDownload=true; path=/')
->send();

Expand Down
54 changes: 24 additions & 30 deletions app/modules/web/Controllers/ConfigManager/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@
use SP\Domain\Core\File\MimeType;
use SP\Domain\Core\File\MimeTypesService;
use SP\Domain\Crypt\Services\TemporaryMasterPass;
use SP\Domain\Export\Services\BackupFileHelper;
use SP\Domain\Export\Services\XmlExport;
use SP\Domain\Export\Dtos\BackupFile as BackupFileDto;
use SP\Domain\Export\Dtos\BackupFiles;
use SP\Domain\Export\Dtos\BackupType;
use SP\Domain\Log\Providers\LogInterface;
use SP\Domain\Notification\Services\MailEvent;
use SP\Domain\Task\Services\Task;
Expand Down Expand Up @@ -84,17 +85,18 @@ final class IndexController extends ControllerBase
private PluginManager $pluginManager;

public function __construct(
Application $application,
WebControllerHelper $webControllerHelper,
TabsHelper $tabsHelper,
UserService $userService,
UserGroupService $userGroupService,
UserProfileService $userProfileService,
MimeTypesService $mimeTypes,
DatabaseUtil $databaseUtil,
ConfigService $configService,
AccountService $accountService,
PluginManager $pluginManager
Application $application,
WebControllerHelper $webControllerHelper,
TabsHelper $tabsHelper,
UserService $userService,
UserGroupService $userGroupService,
UserProfileService $userProfileService,
MimeTypesService $mimeTypes,
DatabaseUtil $databaseUtil,
ConfigService $configService,
AccountService $accountService,
PluginManager $pluginManager,
private readonly BackupFiles $backupFiles
) {
parent::__construct($application, $webControllerHelper);

Expand Down Expand Up @@ -457,25 +459,17 @@ protected function getBackupConfig(): DataTab

$template->assign('siteName', AppInfoInterface::APP_NAME);

$backupAppFile = new FileHandler(
BackupFileHelper::getAppBackupFilename(
BACKUP_PATH,
$this->configData->getBackupHash() ?: '',
true
)
);
$backupDbFile = new FileHandler(
BackupFileHelper::getDbBackupFilename(
BACKUP_PATH,
$this->configData->getBackupHash() ?: '',
true
)
);
$backupFiles = $this->backupFiles->withHash($this->configData->getBackupHash() ?? '');

$backupAppFile = new FileHandler((string)$backupFiles->getAppBackupFile());
$backupDbFile = new FileHandler((string)$backupFiles->getDbBackupFile());

$exportFile = new FileHandler(
XmlExport::buildFilename(
BACKUP_PATH,
(string)new BackupFileDto(
BackupType::export,
$this->configData->getExportHash() ?: '',
true
BACKUP_PATH,
'gz'
)
);

Expand Down
2 changes: 1 addition & 1 deletion app/modules/web/Controllers/Upgrade/UpgradeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function upgradeAction(): bool
$this->handleApplication();

$this->configData->setUpgradeKey(null);
$this->config->save($this->configData, false);
$this->config->save($this->configData);

return $this->returnJsonResponse(
JsonMessage::JSON_SUCCESS,
Expand Down
10 changes: 6 additions & 4 deletions app/modules/web/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
*/

use SP\Core\Context\Session;
use SP\Core\Crypt\Csrf;
use SP\Domain\Config\Ports\ConfigDataInterface;
use SP\Domain\Core\Bootstrap\BootstrapInterface;
use SP\Domain\Core\Bootstrap\ModuleInterface;
use SP\Domain\Core\Context\Context;
use SP\Domain\Html\Ports\MinifyService;
use SP\Domain\Core\Crypt\CsrfHandler;
use SP\Domain\Html\Services\MinifyCss;
use SP\Domain\Html\Services\MinifyJs;
use SP\Modules\Web\Bootstrap;
Expand All @@ -47,10 +48,10 @@
ModuleInterface::class => autowire(Init::class),
CssController::class => autowire(
CssController::class
)->constructorParameter(MinifyService::class, autowire(MinifyCss::class)),
)->constructorParameter('minify', autowire(MinifyCss::class)),
JsController::class => autowire(
JsController::class
)->constructorParameter(MinifyService::class, autowire(MinifyJs::class)),
)->constructorParameter('minify', autowire(MinifyJs::class)),
Context::class => factory(
static function (ConfigDataInterface $configData, SessionHandlerInterface $sessionHandler) {
if ($configData->isEncryptSession()) {
Expand All @@ -59,5 +60,6 @@ static function (ConfigDataInterface $configData, SessionHandlerInterface $sessi

return new Session();
}
)
),
CsrfHandler::class => autowire(Csrf::class),
];
6 changes: 2 additions & 4 deletions lib/BaseFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,8 @@ function logger(mixed $data, string $type = 'DEBUG'): void
$caller
);

/** @noinspection ForgottenDebugOutputInspection */
$useOwn = (!defined('LOG_FILE')
|| !@error_log($line, 3, LOG_FILE)
|| !@file_put_contents(LOG_FILE, $line, FILE_APPEND)
);

if ($useOwn === false) {
Expand All @@ -79,8 +78,7 @@ function logger(mixed $data, string $type = 'DEBUG'): void
$caller
);

/** @noinspection ForgottenDebugOutputInspection */
@error_log($line);
@file_put_contents(LOG_FILE, $line, FILE_APPEND);
}
}

Expand Down
5 changes: 3 additions & 2 deletions lib/SP/Core/Application.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);
/**
* sysPass
Expand Down Expand Up @@ -40,12 +41,12 @@
*
* @param ConfigFileService $config
* @param EventDispatcherInterface $eventDispatcher
* @param SessionContext|Context $context
* @param Context $context
*/
public function __construct(
private ConfigFileService $config,
private EventDispatcherInterface $eventDispatcher,
private SessionContext|Context $context
private Context $context
) {
}

Expand Down
2 changes: 1 addition & 1 deletion lib/SP/Core/Context/SessionLifecycleHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static function start(): void
{
if (session_status() != PHP_SESSION_ACTIVE
&& (headers_sent($filename, $line)
|| session_start(self::SESSION_OPTIONS)) === false
|| !session_start(self::SESSION_OPTIONS))
) {
logger(sprintf('Headers sent in %s:%d file', $filename, $line));

Expand Down
14 changes: 10 additions & 4 deletions lib/SP/Core/Context/Stateless.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@
*/
class Stateless extends ContextBase
{
/**
* @throws ContextException
*/
public function __construct()
{
parent::__construct();

$this->setContext(new ContextCollection());
}

/**
* Establece los datos del usuario en la sesión.
*/
Expand Down Expand Up @@ -146,12 +156,8 @@ public function resetAppStatus(): ?bool
return $this->setContextKey('status', null);
}

/**
* @throws ContextException
*/
public function initialize(): void
{
$this->setContext(new ContextCollection());
}

/**
Expand Down
Loading

0 comments on commit a6dad33

Please sign in to comment.