[TESTS] Add code coverage annotations to entities

This commit is contained in:
Hugo Sales 2021-08-18 17:30:02 +01:00
parent e7699b3245
commit 2ccbbd53a6
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
4 changed files with 27 additions and 5 deletions

View File

@ -21,12 +21,12 @@
namespace App\Entity; namespace App\Entity;
use App\Core\Router\Router;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Entity; use App\Core\Entity;
use App\Core\GSFile; use App\Core\GSFile;
use function App\Core\I18n\_m; use function App\Core\I18n\_m;
use App\Core\Log; use App\Core\Log;
use App\Core\Router\Router;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\DuplicateFoundException; use App\Util\Exception\DuplicateFoundException;
use App\Util\Exception\NotFoundException; use App\Util\Exception\NotFoundException;
@ -222,8 +222,10 @@ class Attachment extends Entity
if (!is_null($filepath = $this->getPath())) { if (!is_null($filepath = $this->getPath())) {
if (file_exists($filepath)) { if (file_exists($filepath)) {
if (@unlink($filepath) === false) { if (@unlink($filepath) === false) {
// @codeCoverageIgnoreStart
Log::error("Failed deleting file for attachment with id={$this->getId()} at {$filepath}."); Log::error("Failed deleting file for attachment with id={$this->getId()} at {$filepath}.");
return false; return false;
// @codeCoverageIgnoreEnd
} else { } else {
$this->setFilename(null); $this->setFilename(null);
$this->setSize(null); $this->setSize(null);
@ -232,7 +234,9 @@ class Attachment extends Entity
DB::flush(); DB::flush();
} }
} else { } else {
// @codeCoverageIgnoreStart
Log::warning("File for attachment with id={$this->getId()} at {$filepath} was already deleted when I was going to handle it."); Log::warning("File for attachment with id={$this->getId()} at {$filepath} was already deleted when I was going to handle it.");
// @codeCoverageIgnoreEnd
} }
} }
return true; return true;
@ -244,7 +248,9 @@ class Attachment extends Entity
protected function delete(): bool protected function delete(): bool
{ {
if ($this->getLives() > 0) { if ($this->getLives() > 0) {
// @codeCoverageIgnoreStart
Log::warning("Deleting file {$this->getId()} with {$this->getLives()} lives. Why are you killing it so young?"); Log::warning("Deleting file {$this->getId()} with {$this->getLives()} lives. Why are you killing it so young?");
// @codeCoverageIgnoreEnd
} }
// Delete related files from storage // Delete related files from storage
$files = []; $files = [];
@ -259,10 +265,14 @@ class Attachment extends Entity
foreach ($files as $f) { foreach ($files as $f) {
if (file_exists($f)) { if (file_exists($f)) {
if (@unlink($f) === false) { if (@unlink($f) === false) {
// @codeCoverageIgnoreStart
Log::error("Failed deleting file for attachment with id={$this->getId()} at {$f}."); Log::error("Failed deleting file for attachment with id={$this->getId()} at {$f}.");
// @codeCoverageIgnoreEnd
} }
} else { } else {
// @codeCoverageIgnoreStart
Log::warning("File for attachment with id={$this->getId()} at {$f} was already deleted when I was going to handle it."); Log::warning("File for attachment with id={$this->getId()} at {$f} was already deleted when I was going to handle it.");
// @codeCoverageIgnoreEnd
} }
} }
DB::flush(); DB::flush();
@ -298,7 +308,7 @@ class Attachment extends Entity
return $filename; return $filename;
} else { } else {
// Welp // Welp
return _m('Untitled Attachment.'); return _m('Untitled attachment');
} }
} }
@ -323,7 +333,7 @@ class Attachment extends Entity
public function getThumbnailUrl() public function getThumbnailUrl()
{ {
return Router::url('attachment_thumbnail', ['id' => $this->getId(), 'w' => Common::config('thumbnail', 'width'), 'h' => Common::config('thumbnail', 'height')]);; return Router::url('attachment_thumbnail', ['id' => $this->getId(), 'w' => Common::config('thumbnail', 'width'), 'h' => Common::config('thumbnail', 'height')]);
} }
public static function schemaDef(): array public static function schemaDef(): array

View File

@ -148,8 +148,10 @@ class Link extends Entity
// This must come before getInfo given that Symfony HTTPClient is lazy (thus forcing curl exec) // This must come before getInfo given that Symfony HTTPClient is lazy (thus forcing curl exec)
try { try {
$headers = $head->getHeaders(); $headers = $head->getHeaders();
// @codeCoverageIgnoreStart
} catch (ClientException $e) { } catch (ClientException $e) {
throw new InvalidArgumentException(previous: $e); throw new InvalidArgumentException(previous: $e);
// @codeCoverageIgnoreEnd
} }
$url = $head->getInfo('url'); // The last effective url (after getHeaders, so it follows redirects) $url = $head->getInfo('url'); // The last effective url (after getHeaders, so it follows redirects)
$url_hash = hash(self::URLHASH_ALGO, $url); $url_hash = hash(self::URLHASH_ALGO, $url);

View File

@ -317,7 +317,9 @@ class LocalUser extends Entity implements UserInterface
case 1: case 1:
return $users[0]; return $users[0];
default: default:
// @codeCoverageIgnoreStart
throw new DuplicateFoundException('Multiple values in table local_user match the requested criteria'); throw new DuplicateFoundException('Multiple values in table local_user match the requested criteria');
// @codeCoverageIgnoreEnd
} }
} }
@ -334,7 +336,9 @@ class LocalUser extends Entity implements UserInterface
self::algoNameToConstant(Common::config('security', 'algorithm')), self::algoNameToConstant(Common::config('security', 'algorithm')),
Common::config('security', 'options')) Common::config('security', 'options'))
) { ) {
// @codeCoverageIgnoreStart
$this->changePassword(null, $password_plain_text, override: true); $this->changePassword(null, $password_plain_text, override: true);
// @codeCoverageIgnoreEnd
} }
return true; return true;
} }
@ -359,11 +363,13 @@ class LocalUser extends Entity implements UserInterface
return password_hash($password, $algorithm, $options); return password_hash($password, $algorithm, $options);
} }
private static function algoNameToConstant(string $algo) /**
* Public for testing
*/
public static function algoNameToConstant(string $algo)
{ {
switch ($algo) { switch ($algo) {
case 'bcrypt': case 'bcrypt':
return PASSWORD_BCRYPT;
case 'argon2i': case 'argon2i':
case 'argon2d': case 'argon2d':
case 'argon2id': case 'argon2id':

View File

@ -85,6 +85,8 @@ class ResetPasswordRequest extends Entity implements ResetPasswordRequestInterfa
// @codeCoverageIgnoreEnd // @codeCoverageIgnoreEnd
// }}} Autocode // }}} Autocode
// {{{ Interface
// @codeCoverageIgnoreStart
public function __construct(object $user, \DateTimeInterface $expiresAt, string $selector, string $hashedToken) public function __construct(object $user, \DateTimeInterface $expiresAt, string $selector, string $hashedToken)
{ {
$this->user_id = $user->getId(); $this->user_id = $user->getId();
@ -117,6 +119,8 @@ class ResetPasswordRequest extends Entity implements ResetPasswordRequestInterfa
{ {
return $this->token; return $this->token;
} }
// @codeCoverageIgnoreEnd
// }}}
public static function schemaDef(): array public static function schemaDef(): array
{ {