Skip to content

Commit

Permalink
more coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ildyria committed Jan 18, 2025
1 parent 4181de2 commit 8e6cfab
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 32 deletions.
3 changes: 3 additions & 0 deletions app/Models/Extensions/AbstractBaseConfigMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

use Illuminate\Database\Migrations\Migration;

/**
* @codeCoverageIgnore still used and tested... but not in tests
*/
abstract class AbstractBaseConfigMigration extends Migration
{
public const BOOL = '0|1';
Expand Down
2 changes: 2 additions & 0 deletions app/Models/Extensions/BaseConfigMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ final public function up(): void

/**
* Reverse the migrations.
*
* @codeCoverageIgnore Tested but after CI run...
*/
final public function down(): void
{
Expand Down
2 changes: 2 additions & 0 deletions app/Models/Extensions/BaseConfigMigrationReversed.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ final public function up(): void

/**
* Reverse the migrations.
*
* @codeCoverageIgnore Tested but after CI run...
*/
final public function down(): void
{
Expand Down
12 changes: 12 additions & 0 deletions app/Models/Extensions/ConfigsHas.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,28 @@ public static function hasExiftool(): bool
if (Helpers::isExecAvailable()) {
try {
$cmd_output = exec('command -v exiftool');
// @codeCoverageIgnoreStart
} catch (\Exception $e) {
$cmd_output = false;
Handler::reportSafely(new ExternalComponentMissingException('could not find exiftool; `has_exiftool` will be set to 0', $e));
}
// @codeCoverageIgnoreEnd
$path = $cmd_output === false ? '' : $cmd_output;
$has_exiftool = $path === '' ? 0 : 1;
} else {
// @codeCoverageIgnoreStart
$has_exiftool = 0;
// @codeCoverageIgnoreEnd
}

try {
self::set('has_exiftool', $has_exiftool);
// @codeCoverageIgnoreStart
} catch (InvalidConfigOption|QueryBuilderException $e) {
// If we could not save the detected setting, still proceed
Handler::reportSafely($e);
}
// @codeCoverageIgnoreEnd
}

return $has_exiftool === 1;
Expand All @@ -82,22 +88,28 @@ public static function hasFFmpeg(): bool
if (Helpers::isExecAvailable()) {
try {
$cmd_output = exec('command -v ffmpeg');
// @codeCoverageIgnoreStart
} catch (\Exception $e) {
$cmd_output = false;
Handler::reportSafely(new ExternalComponentMissingException('could not find ffmpeg; `has_ffmpeg` will be set to 0', $e));
}
// @codeCoverageIgnoreEnd
$path = $cmd_output === false ? '' : $cmd_output;
$has_ffmpeg = $path === '' ? 0 : 1;
} else {
// @codeCoverageIgnoreStart
$has_ffmpeg = 0;
// @codeCoverageIgnoreEnd
}

try {
self::set('has_ffmpeg', $has_ffmpeg);
// @codeCoverageIgnoreStart
} catch (InvalidConfigOption|QueryBuilderException $e) {
// If we could not save the detected setting, still proceed
Handler::reportSafely($e);
}
// @codeCoverageIgnoreEnd
}

return $has_ffmpeg === 1;
Expand Down
2 changes: 2 additions & 0 deletions app/Models/Extensions/HasAttributesPatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace App\Models\Extensions;

use App\Exceptions\Internal\LycheeLogicException;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\InvalidCastException;

Expand All @@ -23,6 +24,7 @@ trait HasAttributesPatch
*/
protected function mutateAttributeForArray($key, $value)
{
throw new LycheeLogicException('Unexpected array cast');
if ($this->hasGetMutator($key)) {
$value = $this->mutateAttribute($key, $value);
}
Expand Down
13 changes: 12 additions & 1 deletion app/Models/Extensions/HasRandomIDAndLegacyTimeBasedID.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public function getIncrementing(): bool
* @param bool $value
*
* @throws NotImplementedException
*
* @codeCoverageIgnore setter is should not be used
*/
public function setIncrementing($value)
{
Expand Down Expand Up @@ -90,7 +92,9 @@ public function setAttribute($key, $value): mixed
protected function performInsert(Builder $query): bool
{
if ($this->fireModelEvent('creating') === false) {
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
}

// First we'll need to create a fresh query instance and touch the creation and
Expand All @@ -111,6 +115,7 @@ protected function performInsert(Builder $query): bool
$this->generateKey();
$attributes = $this->getAttributesForInsert();
$result = $query->insert($attributes);
// @codeCoverageIgnoreStart
} catch (QueryException $e) {
$lastException = $e;
$errorCode = $e->getCode();
Expand All @@ -123,11 +128,14 @@ protected function performInsert(Builder $query): bool
} else {
throw $e;
}
// @codeCoverageIgnoreEnd
}
} while ($retry && $retryCounter > 0);

if ($retryCounter === 0) {
// @codeCoverageIgnoreStart
throw new TimeBasedIdException('unable to persist model to DB after 5 unsuccessful attempts', $lastException);
// @codeCoverageIgnoreEnd
}

// We will go ahead and set the exists property to true, so that it is set when
Expand Down Expand Up @@ -164,10 +172,11 @@ private function generateKey(): void
if ($id[23] === '-') {
$id[23] = '0';
}
// @codeCoverageIgnoreStart
} catch (\Exception $e) {
throw new InsufficientEntropyException($e);
}

// @codeCoverageIgnoreEnd
if (
PHP_INT_MAX === 2147483647 ||
Configs::getValueAsBool('force_32bit_ids')
Expand All @@ -176,7 +185,9 @@ private function generateKey(): void
// full seconds in id. The calling code needs to be able to
// handle duplicate ids. Note that this also exposes us to
// the year 2038 problem.
// @codeCoverageIgnoreStart
$legacyID = sprintf('%010d', microtime(true));
// @codeCoverageIgnoreEnd
} else {
// Ensure 4 digits after the decimal point, 15 characters
// total (including the decimal point), 0-padded on the
Expand Down
21 changes: 10 additions & 11 deletions app/Models/Extensions/SizeVariants.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,17 @@ public function __construct(Photo $photo, ?Collection $sizeVariants = null)
public function add(SizeVariant $sizeVariant): void
{
if ($sizeVariant->photo_id !== $this->photo->id) {
// @codeCoverageIgnoreStart
throw new LycheeInvalidArgumentException('ID of owning photo does not match');
// @codeCoverageIgnoreEnd
}
$sizeVariant->setRelation('photo', $this->photo);
$candidate = $this->getSizeVariant($sizeVariant->type);

if ($candidate !== null && $candidate->id !== $sizeVariant->id) {
// @codeCoverageIgnoreStart
throw new LycheeInvalidArgumentException('Another size variant of the same type has already been added');
// @codeCoverageIgnoreEnd
}

match ($sizeVariant->type) {
Expand All @@ -105,6 +109,7 @@ public function add(SizeVariant $sizeVariant): void
*/
public function toArray(): array
{
// AM I really used?
return [
SizeVariantType::ORIGINAL->name() => $this->original?->toArray(),
SizeVariantType::MEDIUM2X->name() => $this->medium2x?->toArray(),
Expand Down Expand Up @@ -232,7 +237,9 @@ public function getPlaceholder(): ?SizeVariant
public function create(SizeVariantType $sizeVariantType, string $shortPath, ImageDimension $dim, int $filesize): SizeVariant
{
if (!$this->photo->exists) {
// @codeCoverageIgnoreStart
throw new IllegalOrderOfOperationException('Cannot create a size variant for a photo whose id is not yet persisted to DB');
// @codeCoverageIgnoreEnd
}
try {
$result = SizeVariant::create([
Expand All @@ -245,14 +252,17 @@ public function create(SizeVariantType $sizeVariantType, string $shortPath, Imag
'filesize' => $filesize,
'ratio' => $dim->getRatio(),
]);
/** @disregard P1006 */
$this->add($result);

return $result;
// @codeCoverageIgnoreStart
} catch (LycheeInvalidArgumentException $e) {
// thrown by ::add(), if $result->photo_id !== $this->photo->id,
// but we know that we assert that
throw LycheeAssertionError::createFromUnexpectedException($e);
}
// @codeCoverageIgnoreEnd
}

/**
Expand Down Expand Up @@ -332,15 +342,4 @@ public function hasMedium(): bool
{
return $this->medium !== null || $this->medium2x !== null;
}

/**
* We don't need to check if small2x or medium2x exists.
* small2x implies small, and same for medium2x, but the opposite is not true!
*
* @return bool
*/
public function hasMediumOrSmall(): bool
{
return $this->small !== null || $this->medium !== null;
}
}
2 changes: 2 additions & 0 deletions app/Models/Extensions/SortingDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ public function get(array $columns = ['*']): Collection
for ($i = $this->pivotIdx + 1; $i < sizeof($this->orderBy); $i++) {
$this->baseBuilder->orderBy($this->orderBy[$i]['column'], $this->orderBy[$i]['direction']);
}
// @codeCoverageIgnoreStart
} catch (\InvalidArgumentException) {
// Sic! In theory, `\InvalidArgumentException` should be thrown
// if the *type* of argument differs from the expected type
Expand All @@ -164,6 +165,7 @@ public function get(array $columns = ['*']): Collection
// direction does neither equal "asc" nor "desc".
throw new InvalidOrderDirectionException();
}
// @codeCoverageIgnoreEnd

/** @var Collection<int,TModelClass> $result */
$result = $this->baseBuilder->get($columns);
Expand Down
3 changes: 3 additions & 0 deletions app/Models/Extensions/ThrowsConsistentExceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace App\Models\Extensions;

use App\Exceptions\Internal\LycheeLogicException;
use App\Exceptions\ModelDBException;
use Illuminate\Database\Eloquent\JsonEncodingException;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -138,6 +139,8 @@ public function jsonSerialize(): array
*/
public function toJson($options = 0): string
{
// See if we can delete this.
throw new LycheeLogicException('Error encoding model [' . get_class($this) . '] to JSON');
try {
// Note, we must not use the option `JSON_THROW_ON_ERROR` here,
// because this does not clear `json_last_error()` from any
Expand Down
13 changes: 11 additions & 2 deletions app/Models/Extensions/Thumb.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,11 @@ public static function createFromQueryable(Relation|Builder $photoQueryable, Sor
->first();

return self::createFromPhoto($cover);
// @codeCoverageIgnoreStart
} catch (\InvalidArgumentException $e) {
throw new InvalidPropertyException('Sorting order invalid', $e);
}
// @codeCoverageIgnoreEnd
}

/**
Expand All @@ -109,6 +111,9 @@ public static function createFromQueryable(Relation|Builder $photoQueryable, Sor
*
* @throws InvalidPropertyException thrown, if $sortingOrder neither
* equals `desc` nor `asc`
*
* @codeCoverageIgnore We don't need to test that one.
* Note that the inRandomOrder maybe slower than fetching length + random int.
*/
public static function createFromRandomQueryable(Relation|Builder $photoQueryable): ?Thumb
{
Expand All @@ -128,8 +133,6 @@ public static function createFromRandomQueryable(Relation|Builder $photoQueryabl

/**
* Creates a thumbnail from the given photo.
* On Livewire it will use by default small and small2x if available, thumb and thumb2x if not.
* On Legacy it will use thumb and thumb2x.
*
* @param Photo|null $photo the photo
*
Expand All @@ -138,12 +141,16 @@ public static function createFromRandomQueryable(Relation|Builder $photoQueryabl
public static function createFromPhoto(?Photo $photo): ?Thumb
{
if ($photo === null) {
// @codeCoverageIgnoreStart
return null;
// @codeCoverageIgnoreEnd
}

$thumb = $photo->size_variants->getSmall() ?? $photo->size_variants->getThumb();
if ($thumb === null) {
// @codeCoverageIgnoreStart
return null;
// @codeCoverageIgnoreEnd
}

$thumb2x = $photo->size_variants->getSmall() !== null
Expand All @@ -152,7 +159,9 @@ public static function createFromPhoto(?Photo $photo): ?Thumb

$placeholder = (Configs::getValueAsBool('low_quality_image_placeholder'))
? $photo->size_variants->getPlaceholder()
// @codeCoverageIgnoreStart
: null;
// @codeCoverageIgnoreEnd

return new self(
$photo->id,
Expand Down
5 changes: 2 additions & 3 deletions app/Models/Extensions/ToArrayThrowsNotImplemented.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@
*
* Now that we use Resources toArray should no longer be used.
* Throw an exception if we encounter this function in the code.
*
* Because Livewire uses toArray to serialize models when passing them to sub components,
* we still need to allow those cases. Those can be detected by the Route::is() call
*/
trait ToArrayThrowsNotImplemented
{
/**
* @return array<string,mixed>
*
* @throws NotImplementedException
*
* @codeCoverageIgnore We should never reach this code
*/
final public function toArray(): array
{
Expand Down
Loading

0 comments on commit 8e6cfab

Please sign in to comment.