Skip to content

Commit

Permalink
Merge pull request #88 from marcreichel/chore/improve-code-coverage
Browse files Browse the repository at this point in the history
✅ Coverage
  • Loading branch information
marcreichel committed Mar 10, 2024
2 parents ff526c0 + 09d6c1b commit 8ae8301
Show file tree
Hide file tree
Showing 14 changed files with 190 additions and 109 deletions.
30 changes: 8 additions & 22 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Closure;
use DateTimeInterface;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Request;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
Expand All @@ -22,6 +21,7 @@
use MarcReichel\IGDBLaravel\Traits\{DateCasts, Operators};
use ReflectionClass;
use ReflectionException;
use stdClass;

class Builder
{
Expand Down Expand Up @@ -1124,17 +1124,14 @@ protected function setEndpoint(mixed $model): void

if (is_object($model)) {
$class = $model::class;
$parents = class_parents($model) ? collect(class_parents($model)) : collect();
$classParents = class_parents($model);
$parents = $classParents ? collect($classParents) : collect();

if ($parents->isEmpty()) {
$parents->push($class);
}

if (!$parents->last()) {
throw new InvalidParamsException('Last parent element is either null or false. String or {} required.');
}

$reflectionClass = new ReflectionClass($parents->last());
$reflectionClass = new ReflectionClass($parents->last() ?? new stdClass());
$reflectionNamespace = $reflectionClass->getNamespaceName();

if (Str::startsWith($reflectionNamespace, $neededNamespace)) {
Expand Down Expand Up @@ -1302,21 +1299,10 @@ public function all(): Collection
*/
public function paginate(int $limit = 10): Paginator
{
$page = 1;

$request = request();

if ($request instanceof Request) {
$page = $request->get('page', 1);
}

if (!is_int($page) && !is_string($page)) {
$page = 1;
}

$data = $this->forPage((int) $page, $limit)->get();

return new Paginator($data, $limit);
return new Paginator(
$this->forPage((int) (request()->query('page', '1') ?? 1), $limit)->get(),
$limit,
);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Exceptions/PropertyDoesNotExist.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Exception;

/** @deprecated */
class PropertyDoesNotExist extends Exception
{
}
10 changes: 0 additions & 10 deletions src/Interfaces/ModelInterface.php

This file was deleted.

10 changes: 0 additions & 10 deletions src/Interfaces/WebhookInterface.php

This file was deleted.

8 changes: 0 additions & 8 deletions src/Models/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,19 @@
use Illuminate\Support\Str;
use InvalidArgumentException;
use MarcReichel\IGDBLaravel\Enums\Image\Size;
use MarcReichel\IGDBLaravel\Exceptions\PropertyDoesNotExist;

abstract class Image extends Model
{
protected const IMAGE_BASE_PATH = '//images.igdb.com/igdb/image/upload';

/**
* @throws PropertyDoesNotExist
* @throws InvalidArgumentException
*/
public function getUrl(Size | string $size = Size::THUMBNAIL, bool $retina = false): string
{
$basePath = static::IMAGE_BASE_PATH;
$id = $this->getAttribute('image_id');

if ($id === null) {
throw new PropertyDoesNotExist('Property [image_id] is missing from the response. Make sure you specify `image_id` inside the fields attribute.');
}

$id = '' . $id;

if ($size instanceof Size) {
$parsedSize = $size->value;
} else {
Expand Down
39 changes: 25 additions & 14 deletions src/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Illuminate\Contracts\Support\{Arrayable, Jsonable};
use Illuminate\Http\Client\RequestException;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
use MarcReichel\IGDBLaravel\ApiHelper;
Expand All @@ -21,8 +22,6 @@
use MarcReichel\IGDBLaravel\Exceptions\InvalidParamsException;
use MarcReichel\IGDBLaravel\Exceptions\InvalidWebhookMethodException;
use MarcReichel\IGDBLaravel\Exceptions\WebhookSecretMissingException;
use MarcReichel\IGDBLaravel\Interfaces\ModelInterface;
use MarcReichel\IGDBLaravel\Traits\{HasAttributes, HasRelationships};
use ReflectionException;

/**
Expand Down Expand Up @@ -74,29 +73,41 @@
* @method static Builder with(array $relationships)
* @method static Builder cache(int $seconds)
* @method static mixed|string get()
* @method static mixed find(int $id)
* @method static mixed findOrFail(int $id)
* @method static mixed first()
* @method static mixed firstOrFail()
* @method static static|null find(int $id)
* @method static static findOrFail(int $id)
* @method static static|null first()
* @method static static firstOrFail()
* @method static int|null count()
* @method static \Illuminate\Support\Collection all()
* @method static Paginator paginate(int $limit = 10)
*/
abstract class Model implements Arrayable, ArrayAccess, Jsonable, ModelInterface
abstract class Model implements Arrayable, ArrayAccess, Jsonable
{
use HasAttributes;
use HasRelationships;

public ?string $identifier;
public Builder $builder;
public Collection $relations;
public array $attributes = [];
protected array $casts = [];

/**
* @var string[]
*/
protected array $dates = [
'created_at',
'updated_at',
'change_date',
'start_date',
'published_at',
'first_release_date',
];

protected string $endpoint;

/**
* @throws ReflectionException
* @throws InvalidParamsException
*/
public function __construct(array $properties = [])
final public function __construct(array $properties = [])
{
$this->builder = new Builder($this);

Expand Down Expand Up @@ -191,7 +202,7 @@ protected function setRelations(array $attributes): void

return $this->mapToModel($key, $value);
})
->filter(fn (mixed $value): bool => $value instanceof Model || ($value instanceof \Illuminate\Support\Collection && !$value->isEmpty()));
->filter(fn (mixed $value): bool => $value instanceof Model || ($value instanceof Collection && !$value->isEmpty()));
}

public function forwardCallTo(mixed $object, mixed $method, mixed $parameters): mixed
Expand Down Expand Up @@ -251,7 +262,7 @@ protected function getClassNameForProperty(string $property): bool | string | nu
if (collect($this->casts)->has($property)) {
$class = collect($this->casts)->get($property);

if (null !== $class && class_exists($class)) {
if (is_string($class) && class_exists($class)) {
return $class;
}
}
Expand Down Expand Up @@ -294,7 +305,7 @@ public function getEndpoint(): string
public function toArray(): array
{
$attributes = collect($this->attributes);
$relations = collect($this->relations)->map(function ($relation) {
$relations = $this->relations->map(function ($relation) {
if (!$relation instanceof Arrayable) {
return $relation;
}
Expand Down
9 changes: 4 additions & 5 deletions src/Models/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
use MarcReichel\IGDBLaravel\Enums\Webhook\Method;
use MarcReichel\IGDBLaravel\Exceptions\AuthenticationException;
use MarcReichel\IGDBLaravel\Exceptions\InvalidWebhookSecretException;
use MarcReichel\IGDBLaravel\Interfaces\WebhookInterface;

class Webhook implements WebhookInterface
class Webhook
{
public int $id;
public string $url;
Expand All @@ -34,7 +33,7 @@ class Webhook implements WebhookInterface
/**
* @throws AuthenticationException
*/
public function __construct(mixed ...$parameters)
final public function __construct(mixed ...$parameters)
{
$this->client = Http::withOptions([
'base_uri' => ApiHelper::IGDB_BASE_URI,
Expand All @@ -54,13 +53,13 @@ public static function all(): \Illuminate\Support\Collection
$response = $self->client->get('webhooks');

if ($response->failed()) {
return new \Illuminate\Support\Collection();
return \Illuminate\Support\Collection::make();
}

return $self->mapToModel(collect($response->json()));
}

public static function find(int $id): mixed
public static function find(int $id): ?self
{
$self = new static();
$response = $self->client->get('webhooks/' . $id);
Expand Down
23 changes: 0 additions & 23 deletions src/Traits/HasAttributes.php

This file was deleted.

12 changes: 0 additions & 12 deletions src/Traits/HasRelationships.php

This file was deleted.

17 changes: 17 additions & 0 deletions tests/ApiHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Facades\Http;
use MarcReichel\IGDBLaravel\ApiHelper;
use MarcReichel\IGDBLaravel\Exceptions\AuthenticationException;
use Symfony\Component\HttpFoundation\Response;

/**
* @internal
Expand Down Expand Up @@ -44,4 +45,20 @@ public function testItShouldRetrieveAccessTokenFromTwitch(): void

$this->assertEquals('test-suite-token', $token);
}

/**
* @throws AuthenticationException
*/
public function testItShouldThrowAuthenticationException(): void
{
$this->expectException(AuthenticationException::class);

Cache::forget('igdb_cache.access_token');

Http::fake([
'*/oauth2/token*' => Http::response([], Response::HTTP_INTERNAL_SERVER_ERROR),
]);

ApiHelper::retrieveAccessToken();
}
}
Loading

0 comments on commit 8ae8301

Please sign in to comment.