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 8e6cfab commit 77b322b
Show file tree
Hide file tree
Showing 14 changed files with 159 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function authorize(): bool
if (!Gate::check(AlbumPolicy::CAN_EDIT, $album)) {
// @codeCoverageIgnoreStart
return false;
// @codecoverageignoreend
// @codeCoverageIgnoreEnd
}
}

Expand Down
22 changes: 0 additions & 22 deletions app/Legacy/V1/Requests/Traits/HasAlbumIDTrait.php

This file was deleted.

2 changes: 0 additions & 2 deletions app/Models/AccessPermission.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use App\Constants\AccessPermissionConstants as APC;
use App\Exceptions\ConfigurationKeyMissingException;
use App\Models\Builders\AccessPermissionBuilder;
use App\Models\Extensions\HasAttributesPatch;
use App\Models\Extensions\ThrowsConsistentExceptions;
use App\Models\Extensions\UTCBasedTimes;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand Down Expand Up @@ -65,7 +64,6 @@
class AccessPermission extends Model
{
use UTCBasedTimes;
use HasAttributesPatch;
use ThrowsConsistentExceptions;
/** @phpstan-use HasFactory<\Database\Factories\AccessPermissionFactory> */
use HasFactory;
Expand Down
2 changes: 0 additions & 2 deletions app/Models/BaseAlbumImpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use App\Enum\PhotoLayoutType;
use App\Enum\TimelinePhotoGranularity;
use App\Models\Builders\BaseAlbumImplBuilder;
use App\Models\Extensions\HasAttributesPatch;
use App\Models\Extensions\HasBidirectionalRelationships;
use App\Models\Extensions\HasRandomIDAndLegacyTimeBasedID;
use App\Models\Extensions\ThrowsConsistentExceptions;
Expand Down Expand Up @@ -142,7 +141,6 @@
*/
class BaseAlbumImpl extends Model implements HasRandomID
{
use HasAttributesPatch;
/** @phpstan-use HasRandomIDAndLegacyTimeBasedID<BaseAlbumImpl> */
use HasRandomIDAndLegacyTimeBasedID;
use ThrowsConsistentExceptions;
Expand Down
37 changes: 0 additions & 37 deletions app/Models/Extensions/HasAttributesPatch.php

This file was deleted.

2 changes: 2 additions & 0 deletions app/Models/Extensions/SizeVariants.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ public function getPlaceholder(): ?SizeVariant
*
* @throws IllegalOrderOfOperationException
* @throws ModelDBException
*
* @disregard P1006
*/
public function create(SizeVariantType $sizeVariantType, string $shortPath, ImageDimension $dim, int $filesize): SizeVariant
{
Expand Down
38 changes: 0 additions & 38 deletions app/Models/Extensions/ThrowsConsistentExceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@

namespace App\Models\Extensions;

use App\Exceptions\Internal\LycheeLogicException;
use App\Exceptions\ModelDBException;
use Illuminate\Database\Eloquent\JsonEncodingException;
use Illuminate\Support\Str;
use function Safe\json_encode;

/**
* Fixed Eloquent model for all Lychee models.
Expand Down Expand Up @@ -125,39 +122,4 @@ public function jsonSerialize(): array
throw new \JsonException(get_class($this) . '::toArray() failed', 0, $e);
}
}

/**
* Convert the model instance to JSON.
*
* The error message is inspired by {@link JsonEncodingException::forModel()}.
*
* @param int $options
*
* @return string
*
* @throws JsonEncodingException
*/
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
// previous, stalled error message.
// But `\Illuminate\Http\JsonResponse::setData()` falsy assumes
// that this method does so.
// Hence, we call `json_encode` _without_ specifying
// `JSON_THROW_ON_ERROR` and then mimic that behaviour.
// TODO: VERIFY THIS
$json = json_encode($this->jsonSerialize(), $options);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \JsonException(json_last_error_msg(), json_last_error());
}

return $json;
} catch (\JsonException $e) {
throw new JsonEncodingException('Error encoding model [' . get_class($this) . '] to JSON', 0, $e);
}
}
}
2 changes: 0 additions & 2 deletions app/Models/OauthCredential.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

use App\Enum\OauthProvidersType;
use App\Models\Builders\OauthCredentialBuilder;
use App\Models\Extensions\HasAttributesPatch;
use App\Models\Extensions\ThrowsConsistentExceptions;
use App\Models\Extensions\UTCBasedTimes;
use Illuminate\Database\Eloquent\Model;
Expand All @@ -30,7 +29,6 @@
class OauthCredential extends Model
{
use UTCBasedTimes;
use HasAttributesPatch;
use ThrowsConsistentExceptions;

protected $fillable = [
Expand Down
4 changes: 2 additions & 2 deletions app/Models/Photo.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use App\Facades\Helpers;
use App\Image\Files\BaseMediaFile;
use App\Models\Builders\PhotoBuilder;
use App\Models\Extensions\HasAttributesPatch;
use App\Models\Extensions\HasBidirectionalRelationships;
use App\Models\Extensions\HasRandomIDAndLegacyTimeBasedID;
use App\Models\Extensions\SizeVariants;
Expand Down Expand Up @@ -130,7 +129,6 @@ class Photo extends Model
/** @phpstan-use HasFactory<\Database\Factories\PhotoFactory> */
use HasFactory;
use UTCBasedTimes;
use HasAttributesPatch;
/** @phpstan-use HasRandomIDAndLegacyTimeBasedID<Photo> */
use HasRandomIDAndLegacyTimeBasedID;
use ThrowsConsistentExceptions;
Expand Down Expand Up @@ -260,10 +258,12 @@ protected function getShutterAttribute(?string $shutter): ?string
}

return $shutter;
// @codeCoverageIgnoreStart
} catch (ZeroModuloException $e) {
// this should not happen as we covered the case $b = 0;
throw LycheeAssertionError::createFromUnexpectedException($e);
}
// @codeCoverageIgnoreEnd
}

/**
Expand Down
4 changes: 2 additions & 2 deletions app/Models/SizeVariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use App\Http\Resources\Models\SizeVariantResource;
use App\Image\Files\FlysystemFile;
use App\Models\Builders\SizeVariantBuilder;
use App\Models\Extensions\HasAttributesPatch;
use App\Models\Extensions\HasBidirectionalRelationships;
use App\Models\Extensions\ThrowsConsistentExceptions;
use App\Models\Extensions\ToArrayThrowsNotImplemented;
Expand Down Expand Up @@ -75,7 +74,6 @@
class SizeVariant extends Model
{
use UTCBasedTimes;
use HasAttributesPatch;
use HasBidirectionalRelationships;
use ThrowsConsistentExceptions;
use ToArrayThrowsNotImplemented;
Expand Down Expand Up @@ -207,7 +205,9 @@ public function getUrlAttribute(): string
return $this->getSymLinkUrl();
}

// @codeCoverageIgnoreStart
throw new ConfigurationException('the chosen storage adapter "' . get_class($storageAdapter) . '" does not support the symbolic linking feature');
// @codeCoverageIgnoreEnd
}

/**
Expand Down
6 changes: 4 additions & 2 deletions app/Models/SymLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use App\Exceptions\ModelDBException;
use App\Image\Files\FlysystemFile;
use App\Models\Builders\SymLinkBuilder;
use App\Models\Extensions\HasAttributesPatch;
use App\Models\Extensions\ThrowsConsistentExceptions;
use App\Models\Extensions\UTCBasedTimes;
use Carbon\Exceptions\InvalidTimeZoneException;
Expand Down Expand Up @@ -61,7 +60,6 @@
class SymLink extends Model
{
use UTCBasedTimes;
use HasAttributesPatch;
use ThrowsConsistentExceptions {
ThrowsConsistentExceptions::delete as private internalDelete;
}
Expand Down Expand Up @@ -136,9 +134,11 @@ protected function getUrlAttribute(): string
try {
/** @disregard P1013 */
return Storage::disk(self::DISK_NAME)->url($this->short_path);
// @codeCoverageIgnoreStart
} catch (\RuntimeException $e) {
throw new FrameworkException('Laravel\'s storage component', $e);
}
// @codeCoverageIgnoreEnd
}

/**
Expand Down Expand Up @@ -167,9 +167,11 @@ protected function performInsert(Builder $query): bool
unlink($symAbsolutePath);
}
symlink($origRealPath, $symAbsolutePath);
// @codeCoverageIgnoreStart
} catch (FilesystemException $e) {
throw new MediaFileOperationException($e->getMessage(), $e);
}
// @codeCoverageIgnoreEnd
$this->short_path = $symShortPath;

return parent::performInsert($query);
Expand Down
38 changes: 38 additions & 0 deletions tests/Unit/Models/AccessPermissionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* SPDX-License-Identifier: MIT
* Copyright (c) 2017-2018 Tobias Reich
* Copyright (c) 2018-2025 LycheeOrg.
*/

/**
* We don't care for unhandled exceptions in tests.
* It is the nature of a test to throw an exception.
* Without this suppression we had 100+ Linter warning in this file which
* don't help anything.
*
* @noinspection PhpDocMissingThrowsInspection
* @noinspection PhpUnhandledExceptionInspection
*/

namespace Tests\Unit\Models;

use App\Constants\AccessPermissionConstants as APC;
use App\Models\AccessPermission;
use Tests\AbstractTestCase;

class AccessPermissionTest extends AbstractTestCase
{
public function testCreateFullAccess(): void
{
$ap = AccessPermission::withGrantFullPermissionsToUser(1);

self::assertEquals(1, $ap->user_id);
self::assertTrue($ap->{APC::GRANTS_FULL_PHOTO_ACCESS}); // @phpstan-ignore-line: Variable property access on App\Models\AccessPermission
self::assertTrue($ap->{APC::GRANTS_DOWNLOAD}); // @phpstan-ignore-line: Variable property access on App\Models\AccessPermission
self::assertTrue($ap->{APC::GRANTS_UPLOAD}); // @phpstan-ignore-line: Variable property access on App\Models\AccessPermission
self::assertTrue($ap->{APC::GRANTS_EDIT}); // @phpstan-ignore-line: Variable property access on App\Models\AccessPermission
self::assertTrue($ap->{APC::GRANTS_DELETE}); // @phpstan-ignore-line: Variable property access on App\Models\AccessPermission
}
}
57 changes: 57 additions & 0 deletions tests/Unit/Models/ConfigsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/**
* SPDX-License-Identifier: MIT
* Copyright (c) 2017-2018 Tobias Reich
* Copyright (c) 2018-2025 LycheeOrg.
*/

/**
* We don't care for unhandled exceptions in tests.
* It is the nature of a test to throw an exception.
* Without this suppression we had 100+ Linter warning in this file which
* don't help anything.
*
* @noinspection PhpDocMissingThrowsInspection
* @noinspection PhpUnhandledExceptionInspection
*/

namespace Tests\Unit\Models;

use App\Enum\ConfigType;
use App\Exceptions\Internal\InvalidConfigOption;
use App\Models\Configs;
use Tests\AbstractTestCase;

class ConfigsTest extends AbstractTestCase
{
public function testSetFailing(): void
{
self::expectException(InvalidConfigOption::class);
Configs::set('default_license', 'something');
}

public function testSanity(): void
{
$config = new Configs(
[
'key' => 'key',
'value' => '',
'type_range' => ConfigType::STRING_REQ->value,
]
);
self::assertEquals('Error: key empty or not set', $config->sanity(''));
self::assertEquals('Error: key empty or not set', $config->sanity(null));

$config->type_range = ConfigType::POSTIIVE->value;
self::assertEquals('Error: Wrong property for key, expected strictly positive integer, got a.', $config->sanity('a'));
self::assertEquals('Error: Wrong property for key, expected strictly positive integer, got -1.', $config->sanity('-1'));
self::assertEquals('Error: Wrong property for key, expected strictly positive integer, got 0.', $config->sanity('0'));

$config->type_range = ConfigType::MAP_PROVIDER->value;
self::assertEquals('Error: Wrong property for key, expected a valid map provider, got something.', $config->sanity('something'));

$config->type_range = '1|2|3|4';
self::assertEquals('Error: Wrong property for key, expected 1 or 2 or 3 or 4, got 5.', $config->sanity('5'));
}
}
Loading

0 comments on commit 77b322b

Please sign in to comment.