From 666bd065663d4b3bd87b36fa69782da50953d5e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20D?= Date: Sat, 19 Nov 2022 15:37:01 +0100 Subject: [PATCH] chore: Fix tests and PHPStan warnings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rubén D --- lib/BaseFunctions.php | 6 +++- .../Config/Services/ConfigFileService.php | 1 + .../Repositories/AccountRepository.php | 13 ++++++--- phpstan.neon | 3 ++ .../Repositories/AccountRepositoryTest.php | 17 +++++++++-- tests/SP/bootstrap.php | 28 +++++++++++-------- 6 files changed, 50 insertions(+), 18 deletions(-) create mode 100644 phpstan.neon diff --git a/lib/BaseFunctions.php b/lib/BaseFunctions.php index 0d9a2e63..583205e3 100644 --- a/lib/BaseFunctions.php +++ b/lib/BaseFunctions.php @@ -22,7 +22,11 @@ * along with sysPass. If not, see . */ +namespace SP; + +use Exception; use SP\Core\Exceptions\SPException; +use Throwable; /** * [type] [caller] data @@ -145,7 +149,7 @@ function formatStackTrace(Throwable $e): string /** * Process an exception and log into the error log * - * @param \Exception $exception + * @param Exception $exception */ function processException(Exception $exception): void { diff --git a/lib/SP/Domain/Config/Services/ConfigFileService.php b/lib/SP/Domain/Config/Services/ConfigFileService.php index 52dc469f..82558652 100644 --- a/lib/SP/Domain/Config/Services/ConfigFileService.php +++ b/lib/SP/Domain/Config/Services/ConfigFileService.php @@ -38,6 +38,7 @@ use SP\Infrastructure\File\FileCacheInterface; use SP\Infrastructure\File\FileException; use SP\Infrastructure\File\XmlFileStorageInterface; use SP\Util\PasswordUtil; +use function SP\logger; defined('APP_ROOT') || die(); diff --git a/lib/SP/Infrastructure/Account/Repositories/AccountRepository.php b/lib/SP/Infrastructure/Account/Repositories/AccountRepository.php index acd8756f..3d655b29 100644 --- a/lib/SP/Infrastructure/Account/Repositories/AccountRepository.php +++ b/lib/SP/Infrastructure/Account/Repositories/AccountRepository.php @@ -42,6 +42,8 @@ use SP\Infrastructure\Common\Repositories\RepositoryItemTrait; use SP\Infrastructure\Database\DatabaseInterface; use SP\Infrastructure\Database\QueryData; use SP\Infrastructure\Database\QueryResult; +use function SP\__u; +use function SP\logger; /** * Class AccountRepository @@ -96,7 +98,8 @@ final class AccountRepository extends Repository implements AccountRepositoryInt 'Account.key', 'Account.parentId', ]) - ->where('Account.id = :id', ['id' => $id]) + ->where('Account.id = :id') + ->bindValues(['id' => $id]) ->limit(1); return $this->db->doSelect(QueryData::build($query)); @@ -120,7 +123,8 @@ final class AccountRepository extends Repository implements AccountRepositoryInt 'AccountHistory.parentId', 'AccountHistory.mPassHash', ]) - ->where('AccountHistory.id = :id', ['id' => $id]); + ->where('AccountHistory.id = :id') + ->bindValues(['id' => $id]); return $this->db->doSelect(QueryData::build($query)); } @@ -314,7 +318,7 @@ final class AccountRepository extends Repository implements AccountRepositoryInt $query = $this->queryFactory ->newUpdate() ->table('Account') - ->where('id = :id', ['id' => $accountRequest->id]) + ->where('id = :id') ->cols([ 'clientId' => $accountRequest->clientId, 'categoryId' => $accountRequest->categoryId, @@ -328,7 +332,8 @@ final class AccountRepository extends Repository implements AccountRepositoryInt 'isPrivateGroup' => $accountRequest->isPrivateGroup, 'parentId' => $accountRequest->parentId, ]) - ->set('dateEdit', 'NOW()'); + ->set('dateEdit', 'NOW()') + ->bindValues(['id' => $accountRequest->id]); if ($accountRequest->changeUserGroup) { $query->col('userGroupId', $accountRequest->userGroupId); diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 00000000..3624ceeb --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,3 @@ +parameters: + bootstrapFiles: + - lib/BaseFunctions.php \ No newline at end of file diff --git a/tests/SP/Infrastructure/Account/Repositories/AccountRepositoryTest.php b/tests/SP/Infrastructure/Account/Repositories/AccountRepositoryTest.php index 6e9ce04f..18ca865b 100644 --- a/tests/SP/Infrastructure/Account/Repositories/AccountRepositoryTest.php +++ b/tests/SP/Infrastructure/Account/Repositories/AccountRepositoryTest.php @@ -96,7 +96,12 @@ class AccountRepositoryTest extends UnitaryTestCase { $callback = new Callback( static function (QueryData $arg) { - return $arg->getMapClassName() === SimpleModel::class && !empty($arg->getQuery()->getStatement()); + $query = $arg->getQuery(); + + return + $query->getBindValues()['id'] === 1 + && $arg->getMapClassName() === SimpleModel::class + && !empty($query->getStatement()); } ); @@ -116,7 +121,12 @@ class AccountRepositoryTest extends UnitaryTestCase { $callback = new Callback( static function (QueryData $arg) { - return $arg->getMapClassName() === SimpleModel::class && !empty($arg->getQuery()); + $query = $arg->getQuery(); + + return + $query->getBindValues()['id'] === 1 + && $arg->getMapClassName() === SimpleModel::class + && !empty($query->getStatement()); } ); @@ -447,6 +457,7 @@ class AccountRepositoryTest extends UnitaryTestCase && $params['isPrivate'] === $accountRequest->isPrivate && $params['isPrivateGroup'] === $accountRequest->isPrivateGroup && $params['parentId'] === $accountRequest->parentId + && $params['id'] === $accountRequest->id && !empty($arg->getQuery()->getStatement()); } ); @@ -486,6 +497,7 @@ class AccountRepositoryTest extends UnitaryTestCase && $params['isPrivateGroup'] === $accountRequest->isPrivateGroup && $params['parentId'] === $accountRequest->parentId && $params['userGroupId'] === $accountRequest->userGroupId + && $params['id'] === $accountRequest->id && !empty($arg->getQuery()->getStatement()); } ); @@ -525,6 +537,7 @@ class AccountRepositoryTest extends UnitaryTestCase && $params['isPrivateGroup'] === $accountRequest->isPrivateGroup && $params['parentId'] === $accountRequest->parentId && $params['userId'] === $accountRequest->userId + && $params['id'] === $accountRequest->id && !empty($arg->getQuery()->getStatement()); } ); diff --git a/tests/SP/bootstrap.php b/tests/SP/bootstrap.php index 174e15c2..18e6c46e 100644 --- a/tests/SP/bootstrap.php +++ b/tests/SP/bootstrap.php @@ -37,16 +37,18 @@ use SP\Infrastructure\Database\DatabaseConnectionData; use SP\Infrastructure\Database\DbStorageInterface; use SP\Infrastructure\Database\MysqlHandler; use SP\Util\FileUtil; +use function SP\logger; +use function SP\processException; define('DEBUG', true); define('IS_TESTING', true); define('APP_ROOT', dirname(__DIR__, 2)); define('TEST_ROOT', dirname(__DIR__)); -const APP_DEFINITIONS_FILE = APP_ROOT . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'Definitions.php'; +const APP_DEFINITIONS_FILE = APP_ROOT.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'Definitions.php'; -define('RESOURCE_PATH', TEST_ROOT . DIRECTORY_SEPARATOR . 'res'); -define('CONFIG_PATH', RESOURCE_PATH . DIRECTORY_SEPARATOR . 'config'); +define('RESOURCE_PATH', TEST_ROOT.DIRECTORY_SEPARATOR.'res'); +define('CONFIG_PATH', RESOURCE_PATH.DIRECTORY_SEPARATOR.'config'); define('CONFIG_FILE', CONFIG_PATH . DIRECTORY_SEPARATOR . 'config.xml'); define('ACTIONS_FILE', CONFIG_PATH . DIRECTORY_SEPARATOR . 'actions.xml'); define('LOCALES_PATH', APP_ROOT . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'locales'); @@ -73,8 +75,12 @@ logger('TEST_ROOT=' . TEST_ROOT); logger('SELF_IP_ADDRESS=' . SELF_IP_ADDRESS); // Setup directories -recreateDir(TMP_PATH); -recreateDir(CACHE_PATH); +try { + recreateDir(TMP_PATH); + recreateDir(CACHE_PATH); +} catch (FileNotFoundException $e) { + processException($e); +} if (is_dir(CONFIG_PATH) && decoct(fileperms(CONFIG_PATH) & 0777) !== '750' @@ -153,23 +159,23 @@ function getResource(string $dir, string $file): string return file_get_contents(RESOURCE_PATH . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR . $file) ?: ''; } -function saveResource(string $dir, string $file, string $data): string +function saveResource(string $dir, string $file, string $data): bool|int { - return file_put_contents(RESOURCE_PATH . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR . $file, $data); + return file_put_contents(RESOURCE_PATH.DIRECTORY_SEPARATOR.$dir.DIRECTORY_SEPARATOR.$file, $data); } /** - * @throws FileNotFoundException + * @throws \SP\Core\Exceptions\FileNotFoundException */ -function recreateDir(string $dir) +function recreateDir(string $dir): void { if (is_dir($dir)) { - logger('Deleting ' . $dir); + logger('Deleting '.$dir); FileUtil::rmdir_recursive($dir); } - logger('Creating ' . $dir . PHP_EOL); + logger('Creating '.$dir.PHP_EOL); if (!mkdir($dir) && !is_dir($dir)) { throw new RuntimeException(sprintf('Directory "%s" was not created', $dir));