Skip to content

Commit

Permalink
Improve code coverage (#2923)
Browse files Browse the repository at this point in the history
  • Loading branch information
ildyria authored Jan 19, 2025
1 parent a836c2d commit 90a154d
Show file tree
Hide file tree
Showing 119 changed files with 1,522 additions and 282 deletions.
4 changes: 4 additions & 0 deletions app/Actions/Oauth/Oauth.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
use Laravel\Socialite\Contracts\User as ContractsUser;
use Laravel\Socialite\Facades\Socialite;

/**
* @codeCoverageIgnore
* This code is untestable as it is tightly coupled with the Socialite facade.
*/
class Oauth
{
public const OAUTH_REGISTER = 'register';
Expand Down
2 changes: 2 additions & 0 deletions app/Assets/BaseSizeVariantNamingStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ protected function generateExtension(SizeVariantType $sizeVariant): string
}

if ($this->extension === '') {
// @codeCoverageIgnoreStart
throw new MissingValueException('extension');
// @codeCoverageIgnoreEnd
}

return $this->extension;
Expand Down
2 changes: 2 additions & 0 deletions app/Assets/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,10 @@ public function getSymbolByQuantity(float $bytes): string
$exp = intval(floor(log($bytes) / log(1024.0)));

if ($exp >= sizeof($symbols)) {
// @codeCoverageIgnoreStart
// if the number is too large, we fall back to the largest available symbol
$exp = sizeof($symbols) - 1;
// @codeCoverageIgnoreEnd
}

return sprintf('%.2f %s', ($bytes / pow(1024, $exp)), $symbols[$exp]);
Expand Down
4 changes: 4 additions & 0 deletions app/DTO/ArrayableDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ public function toArray(): array
$propertyValue = $prop->getValue($this);
if (is_object($propertyValue)) {
if ($propertyValue instanceof Arrayable) {
// @codeCoverageIgnoreStart
$propertyValue = $propertyValue->toArray();
// @codeCoverageIgnoreEnd
} elseif ($propertyValue instanceof \BackedEnum) {
$propertyValue = $propertyValue->value;
} else {
// @codeCoverageIgnoreStart
throw new LycheeLogicException(sprintf('Unable to convert %s into an array', get_class($propertyValue)));
// @codeCoverageIgnoreEnd
}
}
$result[$prop->getName()] = $propertyValue;
Expand Down
4 changes: 1 addition & 3 deletions app/Factories/AlbumFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use App\Contracts\Models\AbstractAlbum;
use App\Enum\SmartAlbumType;
use App\Exceptions\Internal\InvalidSmartIdException;
use App\Exceptions\Internal\LycheeAssertionError;
use App\Models\Album;
use App\Models\BaseAlbumImpl;
use App\Models\Extensions\BaseAlbum;
Expand Down Expand Up @@ -140,8 +139,7 @@ public function findAbstractAlbumsOrFail(array $albumIDs, bool $withRelations =

$smartAlbums = [];
foreach ($smartAlbumIDs as $smartID) {
$smartAlbumType = SmartAlbumType::tryFrom($smartID)
?? throw LycheeAssertionError::createFromUnexpectedException(new InvalidSmartIdException($smartID));
$smartAlbumType = SmartAlbumType::from($smartID);
$smartAlbums[] = $this->createSmartAlbum($smartAlbumType, $withRelations);
}

Expand Down
4 changes: 4 additions & 0 deletions app/Http/Controllers/Admin/Maintenance/Cleaning.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,20 @@ public function check(CleaningRequest $request): CleaningState
$cleaning_state = new CleaningState($request->path(), false);

if (!is_dir($request->path())) {
// @codeCoverageIgnoreStart This should not happen. path is very constrained by Lychee config.
Log::warning('directory ' . $request->path() . ' not found!');
$cleaning_state->is_not_empty = false;

return $cleaning_state;
// @codeCoverageIgnoreEnd
}

if (!(new \FilesystemIterator($request->path()))->valid()) {
// @codeCoverageIgnoreStart This should not happen. path is very constrained by Lychee config.
$cleaning_state->is_not_empty = false;

return $cleaning_state;
// @codeCoverageIgnoreEnd
}

$files_found = false;
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Controllers/Admin/Maintenance/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public function __invoke(RegisterRequest $request): RegisterData
$verify = resolve(Verify::class);
$is_supporter = $verify->is_supporter();
if ($is_supporter) {
// @codeCoverageIgnoreStart Tested locally, not testable in CICD.
return new RegisterData(true);
// @codeCoverageIgnoreEnd
}

// Not valid, reset the key.
Expand Down
6 changes: 6 additions & 0 deletions app/Http/Controllers/OauthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public function __construct(
* @param string $provider
*
* @return Redirector|RedirectResponse
*
* @codeCoverageIgnore
*/
public function redirected(string $provider)
{
Expand All @@ -63,6 +65,8 @@ public function redirected(string $provider)
* @param string $provider
*
* @return HttpFoundationRedirectResponse
*
* @codeCoverageIgnore
*/
public function authenticate(string $provider)
{
Expand All @@ -81,6 +85,8 @@ public function authenticate(string $provider)
* @param string $provider
*
* @return HttpFoundationRedirectResponse
*
* @codeCoverageIgnore
*/
public function register(string $provider)
{
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Requests/Maintenance/FullTreeUpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use App\Http\Requests\BaseApiRequest;
use App\Models\Configs;
use App\Policies\SettingsPolicy;
use App\Rules\AlbumIDRule;
use App\Rules\RandomIDRule;
use Illuminate\Support\Facades\Gate;

class FullTreeUpdateRequest extends BaseApiRequest
Expand All @@ -34,10 +34,10 @@ public function rules(): array
return [
'albums' => 'required|array|min:1',
'albums.*' => 'required|array',
'albums.*.id' => ['required', new AlbumIDRule(false)],
'albums.*.id' => ['required', new RandomIDRule(false)],
'albums.*._lft' => 'required|integer|min:1',
'albums.*._rgt' => 'required|integer|min:1',
'albums.*.parent_id' => [new AlbumIDRule(true)],
'albums.*.parent_id' => [new RandomIDRule(true)],
];
}

Expand Down
2 changes: 0 additions & 2 deletions app/Http/Resources/Rights/SettingsRightsResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class SettingsRightsResource extends Data
{
public bool $can_edit;
public bool $can_see_logs;
public bool $can_clear_logs;
public bool $can_see_diagnostics;
public bool $can_update;
public bool $can_access_dev_tools;
Expand All @@ -28,7 +27,6 @@ public function __construct()
{
$this->can_edit = Gate::check(SettingsPolicy::CAN_EDIT, [Configs::class]);
$this->can_see_logs = Gate::check(SettingsPolicy::CAN_SEE_LOGS, [Configs::class]);
$this->can_clear_logs = Gate::check(SettingsPolicy::CAN_CLEAR_LOGS, [Configs::class]);
$this->can_see_diagnostics = Gate::check(SettingsPolicy::CAN_SEE_DIAGNOSTICS, [Configs::class]);
$this->can_update = Gate::check(SettingsPolicy::CAN_UPDATE, [Configs::class]);
$this->can_access_dev_tools = Gate::check(SettingsPolicy::CAN_ACCESS_DEV_TOOLS, [Configs::class]);
Expand Down
4 changes: 4 additions & 0 deletions app/Legacy/Legacy.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,22 @@ private static function translateLegacyID(int $id, string $tableName, Request $r
' instead of new ID ' . $newID .
' from ' . $referer;
if (!Configs::getValueAsBool('legacy_id_redirection')) {
// @codeCoverageIgnoreStart
$msg .= ' (translation disabled by configuration)';
throw new ConfigurationException($msg);
// @codeCoverageIgnoreEnd
}
Log::warning(__METHOD__ . ':' . __LINE__ . $msg);

return $newID;
}

// @codeCoverageIgnoreStart
return null;
} catch (\InvalidArgumentException $e) {
throw new QueryBuilderException($e);
}
// @codeCoverageIgnoreEnd
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ private function formatErrors(array $array): array
};
$ret[] = $prefix . $elem->message;
foreach ($elem->details as $detail) {
// @codeCoverageIgnoreStart
$ret[] = ' ' . $detail;
// @codeCoverageIgnoreEnd
}
}

Expand All @@ -109,9 +111,11 @@ public function view(): View
{
try {
return view('diagnostics', $this->get());
// @codeCoverageIgnoreStart
} catch (BindingResolutionException $e) {
throw new FrameworkException('Laravel\'s view component', $e);
}
// @codeCoverageIgnoreEnd
}

/**
Expand All @@ -135,7 +139,9 @@ public function getSize(Space $space): array
public function getFullAccessPermissions(AlbumQueryPolicy $albumQueryPolicy): View
{
if (!$this->isAuthorized() && config('app.debug') !== true) {
// @codeCoverageIgnoreStart
throw new UnauthorizedException();
// @codeCoverageIgnoreEnd
}

$data1 = AccessPermission::query()
Expand Down Expand Up @@ -191,6 +197,7 @@ public function getFullAccessPermissions(AlbumQueryPolicy $albumQueryPolicy): Vi
->orderBy(APC::BASE_ALBUM_ID)
->get()
->map(function ($e) {
// @codeCoverageIgnoreStart
$e->is_link_required = $e->is_link_required === 1;
$e->grants_download = $e->grants_download === 1;
$e->grants_upload = $e->grants_upload === 1;
Expand All @@ -199,6 +206,7 @@ public function getFullAccessPermissions(AlbumQueryPolicy $albumQueryPolicy): Vi
$e->grants_full_photo_access = $e->grants_full_photo_access === 1;

return $e;
// @codeCoverageIgnoreEnd
});

return view('access-permissions', ['data1' => json_encode($data1, JSON_PRETTY_PRINT), 'data2' => json_encode($data2, JSON_PRETTY_PRINT)]);
Expand Down
10 changes: 10 additions & 0 deletions app/Legacy/V1/Controllers/Administration/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ public function setNSFWVisible(SetNSFWVisibilityRequest $request): void
* @return void
*
* @throws InvalidConfigOption
*
* @codeCoverageIgnore
*/
public function setAlbumDecoration(SetAlbumDecorationRequest $request): void
{
Expand Down Expand Up @@ -368,9 +370,11 @@ public function setCSS(SetCSSSettingRequest $request): void
/** @var string $css */
$css = $request->getSettingValue();
if (Storage::disk('dist')->put('user.css', $css) === false) {
// @codeCoverageIgnoreStart
if (Storage::disk('dist')->get('user.css') !== $css) {
throw new InsufficientFilesystemPermissions('Could not save CSS');
}
// @codeCoverageIgnoreEnd
}
}

Expand All @@ -384,6 +388,8 @@ public function setCSS(SetCSSSettingRequest $request): void
* @return void
*
* @throws InsufficientFilesystemPermissions
*
* @codeCoverageIgnore
*/
public function setJS(SetJSSettingRequest $request): void
{
Expand Down Expand Up @@ -442,12 +448,16 @@ public function saveAll(GetSetAllSettingsRequest $request): void
$value ??= '';
try {
Configs::set($key, $value);
// @codeCoverageIgnoreStart
} catch (InvalidConfigOption $e) {
$lastException = $e;
}
// @codeCoverageIgnoreEnd
}
if ($lastException !== null) {
// @codeCoverageIgnoreStart
throw $lastException;
// @codeCoverageIgnoreEnd
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public function add(AddSharesRequest $request): void
* @param SetSharesByAlbumRequest $request
*
* @return void
*
* @codeCoverageIgnore Legacy stuff
*/
public function setByAlbum(SetSharesByAlbumRequest $request): void
{
Expand Down Expand Up @@ -102,7 +104,8 @@ private function updateLinks(array $userIds, array $albumIDs): void
APC::GRANTS_DOWNLOAD => Configs::getValueAsBool('grants_download'),
APC::GRANTS_FULL_PHOTO_ACCESS => Configs::getValueAsBool('grants_full_photo_access'),
],
false);
false
);
}
}

Expand All @@ -118,6 +121,8 @@ private function updateLinks(array $userIds, array $albumIDs): void
* @return void
*
* @throws QueryBuilderException
*
* @codeCoverageIgnore Legacy stuff
*/
public function delete(DeleteSharingRequest $request): void
{
Expand Down
2 changes: 2 additions & 0 deletions app/Legacy/V1/Controllers/Administration/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@ public function setEmail(SetEmailRequest $request): void
}

$user->save();
// @codeCoverageIgnoreStart
} catch (\InvalidArgumentException $e) {
throw new FrameworkException('Laravel\'s notification module', $e);
}
// @codeCoverageIgnoreEnd
}

/**
Expand Down
8 changes: 8 additions & 0 deletions app/Legacy/V1/Controllers/AlbumController.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,14 @@ public function addTagAlbum(AddTagAlbumRequest $request, CreateTagAlbum $create)
*/
public function get(GetAlbumRequest $request): AlbumResource|TagAlbumResource|SmartAlbumResource
{
// @codeCoverageIgnoreStart
return match (true) {
$request->album() instanceof BaseSmartAlbum => SmartAlbumResource::make($request->album()),
$request->album() instanceof TagAlbum => TagAlbumResource::make($request->album()),
$request->album() instanceof Album => AlbumResource::make($request->album()),
default => throw new LycheeLogicException('This should not happen'),
};
// @codeCoverageIgnoreEnd
}

/**
Expand Down Expand Up @@ -276,6 +278,8 @@ public function setLicense(SetAlbumLicenseRequest $request): void
*
* @throws MediaFileOperationException
* @throws ModelDBException
*
* @codeCoverageIgnore
*/
public function setTrack(SetAlbumTrackRequest $request): void
{
Expand All @@ -290,6 +294,8 @@ public function setTrack(SetAlbumTrackRequest $request): void
* @return void
*
* @throws ModelDBException
*
* @codeCoverageIgnore
*/
public function deleteTrack(DeleteTrackRequest $request): void
{
Expand Down Expand Up @@ -353,6 +359,8 @@ public function move(MoveAlbumsRequest $request, Move $move): void
* @return void
*
* @throws ModelDBException
*
* @codeCoverageIgnore
*/
public function setNSFW(SetAlbumNSFWRequest $request): void
{
Expand Down
5 changes: 3 additions & 2 deletions app/Legacy/V1/Controllers/ImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use App\Legacy\V1\Requests\Import\ImportFromUrlRequest;
use App\Legacy\V1\Requests\Import\ImportServerRequest;
use App\Legacy\V1\Resources\Models\PhotoResource;
use App\Models\Photo;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Auth;
Expand Down Expand Up @@ -47,7 +46,7 @@
* types are consistent and the best of both worlds: a streamed collection.
* In other words, the streamed response should immediately send back a
* `[`-character to the client (the beginning of the collection), then
* send back a JSONized {@link Photo} as soon as it has been imported
* send back a JSONized {@link \App\Models\Photo} as soon as it has been imported
* (element of the collection, and send a final `]`-character (the end of
* the collection).
*
Expand Down Expand Up @@ -91,6 +90,8 @@ public function server(ImportServerRequest $request, FromServer $fromServer): St

/**
* @return void
*
* @codeCoverageIgnore
*/
public function serverCancel(CancelImportServerRequest $request): void
{
Expand Down
Loading

0 comments on commit 90a154d

Please sign in to comment.