Skip to content

Commit

Permalink
coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ildyria committed Jan 17, 2025
1 parent 509ea21 commit a16f89a
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 0 deletions.
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
2 changes: 2 additions & 0 deletions app/Legacy/V1/Middleware/LoginRequiredV1.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class LoginRequiredV1
*
* @throws ConfigurationException
* @throws FrameworkException
*
* @codeCoverageIgnore Legacy stuff we don't care.
*/
public function handle(Request $request, \Closure $next, string $requiredStatus): mixed
{
Expand Down
3 changes: 3 additions & 0 deletions app/Legacy/V1/Requests/Sharing/DeleteSharingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
use App\Rules\IntegerIDRule;
use Illuminate\Support\Facades\Gate;

/**
* @codeCoverageIgnore Legacy stuff we don't care.
*/
class DeleteSharingRequest extends BaseApiRequest
{
public const SHARE_IDS_ATTRIBUTE = 'shareIDs';
Expand Down
8 changes: 8 additions & 0 deletions app/Legacy/V1/Requests/Sharing/ListSharingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class ListSharingRequest extends BaseApiRequest implements HasBaseAlbum

/**
* {@inheritDoc}
*
* @codeCoverageIgnore Legacy stuff we don't care.
*/
public function authorize(): bool
{
Expand Down Expand Up @@ -96,20 +98,26 @@ public function rules(): array
protected function processValidatedValues(array $values, array $files): void
{
$this->album = key_exists(RequestAttribute::ALBUM_ID_ATTRIBUTE, $values) ?
// @codeCoverageIgnoreStart
$this->albumFactory->findBaseAlbumOrFail($values[RequestAttribute::ALBUM_ID_ATTRIBUTE]) :
// @codeCoverageIgnoreEnd
null;

$this->owner = null;
$this->participant = null;
if (key_exists(self::OWNER_ID_ATTRIBUTE, $values)) {
// @codeCoverageIgnoreStart
/** @var int $ownerID */
$ownerID = $values[self::OWNER_ID_ATTRIBUTE];
$this->owner = User::query()->findOrFail($ownerID);
// @codeCoverageIgnoreEnd
}
if (key_exists(self::PARTICIPANT_ID_ATTRIBUTE, $values)) {
// @codeCoverageIgnoreStart
/** @var int $participantID */
$participantID = $values[self::PARTICIPANT_ID_ATTRIBUTE];
$this->participant = User::query()->findOrFail($participantID);
// @codeCoverageIgnoreEnd
}
}

Expand Down
2 changes: 2 additions & 0 deletions app/Legacy/V1/Requests/Sharing/SetSharesByAlbumRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
* Represents a request for setting the shares of a specific album.
*
* Only the owner (or the admin) of the album can set the shares.
*
* @codeCoverageIgnore Legacy stuff we don't care.
*/
class SetSharesByAlbumRequest extends BaseApiRequest implements HasBaseAlbum, HasUserIDs
{
Expand Down
3 changes: 3 additions & 0 deletions app/Legacy/V1/Requests/View/GetPhotoViewRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
use App\Rules\RandomIDRule;
use Illuminate\Support\Facades\Gate;

/**
* @codeCoverageIgnore
*/
class GetPhotoViewRequest extends BaseApiRequest implements HasPhoto
{
use HasPhotoTrait;
Expand Down
8 changes: 8 additions & 0 deletions app/Services/Auth/SessionOrTokenGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ public static function createGuard(Application $app, string $name, array $config
/** @disregard P1013 */
$guard->setRequest($app->refresh('request', $guard, 'setRequest'));
if (isset($config['remember'])) {
// @codeCoverageIgnoreStart
$guard->setRememberDuration($config['remember']);
// @codeCoverageIgnoreEnd
}

return $guard;
Expand Down Expand Up @@ -194,7 +196,9 @@ public function user(): Authenticatable|null
// new authentication state explicitly.
$this->authState = self::AUTH_STATE_STATELESS;
} elseif ($userByRecaller !== null) {
// @codeCoverageIgnoreStart
$this->login($userByRecaller, true);
// @codeCoverageIgnoreEnd
} else {
// In the other cases, `$this->user` has implicitly been set by
// `parent::setUser` or `$this->login`.
Expand Down Expand Up @@ -326,7 +330,9 @@ protected function getUserByToken(): ?Authenticatable

// Skip if token starts with Basic: it is not related to Lychee.
if (Str::startsWith('Basic', $token)) {
// @codeCoverageIgnoreStart
return null;
// @codeCoverageIgnoreEnd
}

// Check if token starts with Bearer
Expand All @@ -349,8 +355,10 @@ protected function getUserByToken(): ?Authenticatable

return match (true) {
$authenticable !== null => $authenticable,
// @codeCoverageIgnoreStart
$hasBearer && $configThrow => throw new BadRequestHeaderException('Invalid token'),
$hasBearer => null,
// @codeCoverageIgnoreEnd
default => throw new BadRequestHeaderException('Invalid token'),
};
}
Expand Down
4 changes: 4 additions & 0 deletions app/SmartAlbums/BaseSmartAlbum.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ protected function __construct(SmartAlbumType $id, \Closure $smartCondition)
/** @var AccessPermission|null $perm */
$perm = AccessPermission::query()->where('base_album_id', '=', $id->value)->first();
$this->publicPermissions = $perm;
// @codeCoverageIgnoreStart
} catch (BindingResolutionException $e) {
throw new FrameworkException('Laravel\'s service container', $e);
}
// @codeCoverageIgnoreEnd
}

/**
Expand Down Expand Up @@ -136,7 +138,9 @@ protected function getThumbAttribute(): ?Thumb
* user.
*/
$this->thumb ??= Configs::getValueAsBool('SA_random_thumbs')
// @codeCoverageIgnoreStart
? Thumb::createFromRandomQueryable($this->photos())
// @codeCoverageIgnoreEnd
: $this->thumb = Thumb::createFromQueryable(
$this->photos(),
PhotoSortingCriterion::createDefault()
Expand Down
35 changes: 35 additions & 0 deletions tests/Unit/CoverageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
use App\Enum\AspectRatioType;
use App\Enum\MapProviders;
use App\Enum\SmartAlbumType;
use App\Exceptions\Internal\LycheeInvalidArgumentException;
use App\SmartAlbums\UnsortedAlbum;
use Tests\AbstractTestCase;

class CoverageTest extends AbstractTestCase
Expand Down Expand Up @@ -100,4 +102,37 @@ public function testImportEventReport(): void
);
self::assertEquals('message', $report->toCLIString());
}

public function testBaseSmartAlbumException(): void
{
self::expectException(LycheeInvalidArgumentException::class);

$album = new UnsortedAlbum();
$album->__get('');
}

public function testBaseSmartAlbumException2(): void
{
self::expectException(LycheeInvalidArgumentException::class);

$album = new UnsortedAlbum();
$album->__get('something');
}

public function testBaseSmartAlbumPhotos(): void
{
$album = new UnsortedAlbum();
$data = $album->__get('Photos');
self::assertEmpty($data);

$data = $album->getPhotos();
self::assertEmpty($data);

$album->setPublic();
$album->setPublic();
$data = $album->getPhotos();
self::assertEmpty($data);
$album->setPrivate();
$album->setPrivate();
}
}

0 comments on commit a16f89a

Please sign in to comment.