Skip to content

Commit 8ae8301

Browse files
authored
Merge pull request #88 from marcreichel/chore/improve-code-coverage
✅ Coverage
2 parents ff526c0 + 09d6c1b commit 8ae8301

14 files changed

+190
-109
lines changed

src/Builder.php

+8-22
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Closure;
99
use DateTimeInterface;
1010
use Illuminate\Http\Client\PendingRequest;
11-
use Illuminate\Http\Request;
1211
use Illuminate\Pagination\Paginator;
1312
use Illuminate\Support\Collection;
1413
use Illuminate\Support\Facades\Cache;
@@ -22,6 +21,7 @@
2221
use MarcReichel\IGDBLaravel\Traits\{DateCasts, Operators};
2322
use ReflectionClass;
2423
use ReflectionException;
24+
use stdClass;
2525

2626
class Builder
2727
{
@@ -1124,17 +1124,14 @@ protected function setEndpoint(mixed $model): void
11241124

11251125
if (is_object($model)) {
11261126
$class = $model::class;
1127-
$parents = class_parents($model) ? collect(class_parents($model)) : collect();
1127+
$classParents = class_parents($model);
1128+
$parents = $classParents ? collect($classParents) : collect();
11281129

11291130
if ($parents->isEmpty()) {
11301131
$parents->push($class);
11311132
}
11321133

1133-
if (!$parents->last()) {
1134-
throw new InvalidParamsException('Last parent element is either null or false. String or {} required.');
1135-
}
1136-
1137-
$reflectionClass = new ReflectionClass($parents->last());
1134+
$reflectionClass = new ReflectionClass($parents->last() ?? new stdClass());
11381135
$reflectionNamespace = $reflectionClass->getNamespaceName();
11391136

11401137
if (Str::startsWith($reflectionNamespace, $neededNamespace)) {
@@ -1302,21 +1299,10 @@ public function all(): Collection
13021299
*/
13031300
public function paginate(int $limit = 10): Paginator
13041301
{
1305-
$page = 1;
1306-
1307-
$request = request();
1308-
1309-
if ($request instanceof Request) {
1310-
$page = $request->get('page', 1);
1311-
}
1312-
1313-
if (!is_int($page) && !is_string($page)) {
1314-
$page = 1;
1315-
}
1316-
1317-
$data = $this->forPage((int) $page, $limit)->get();
1318-
1319-
return new Paginator($data, $limit);
1302+
return new Paginator(
1303+
$this->forPage((int) (request()->query('page', '1') ?? 1), $limit)->get(),
1304+
$limit,
1305+
);
13201306
}
13211307

13221308
/**

src/Exceptions/PropertyDoesNotExist.php

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Exception;
88

9+
/** @deprecated */
910
class PropertyDoesNotExist extends Exception
1011
{
1112
}

src/Interfaces/ModelInterface.php

-10
This file was deleted.

src/Interfaces/WebhookInterface.php

-10
This file was deleted.

src/Models/Image.php

-8
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,19 @@
77
use Illuminate\Support\Str;
88
use InvalidArgumentException;
99
use MarcReichel\IGDBLaravel\Enums\Image\Size;
10-
use MarcReichel\IGDBLaravel\Exceptions\PropertyDoesNotExist;
1110

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

1615
/**
17-
* @throws PropertyDoesNotExist
1816
* @throws InvalidArgumentException
1917
*/
2018
public function getUrl(Size | string $size = Size::THUMBNAIL, bool $retina = false): string
2119
{
2220
$basePath = static::IMAGE_BASE_PATH;
2321
$id = $this->getAttribute('image_id');
2422

25-
if ($id === null) {
26-
throw new PropertyDoesNotExist('Property [image_id] is missing from the response. Make sure you specify `image_id` inside the fields attribute.');
27-
}
28-
29-
$id = '' . $id;
30-
3123
if ($size instanceof Size) {
3224
$parsedSize = $size->value;
3325
} else {

src/Models/Model.php

+25-14
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Illuminate\Contracts\Support\{Arrayable, Jsonable};
1313
use Illuminate\Http\Client\RequestException;
1414
use Illuminate\Pagination\Paginator;
15+
use Illuminate\Support\Collection;
1516
use Illuminate\Support\Facades\Http;
1617
use Illuminate\Support\Str;
1718
use MarcReichel\IGDBLaravel\ApiHelper;
@@ -21,8 +22,6 @@
2122
use MarcReichel\IGDBLaravel\Exceptions\InvalidParamsException;
2223
use MarcReichel\IGDBLaravel\Exceptions\InvalidWebhookMethodException;
2324
use MarcReichel\IGDBLaravel\Exceptions\WebhookSecretMissingException;
24-
use MarcReichel\IGDBLaravel\Interfaces\ModelInterface;
25-
use MarcReichel\IGDBLaravel\Traits\{HasAttributes, HasRelationships};
2625
use ReflectionException;
2726

2827
/**
@@ -74,29 +73,41 @@
7473
* @method static Builder with(array $relationships)
7574
* @method static Builder cache(int $seconds)
7675
* @method static mixed|string get()
77-
* @method static mixed find(int $id)
78-
* @method static mixed findOrFail(int $id)
79-
* @method static mixed first()
80-
* @method static mixed firstOrFail()
76+
* @method static static|null find(int $id)
77+
* @method static static findOrFail(int $id)
78+
* @method static static|null first()
79+
* @method static static firstOrFail()
8180
* @method static int|null count()
8281
* @method static \Illuminate\Support\Collection all()
8382
* @method static Paginator paginate(int $limit = 10)
8483
*/
85-
abstract class Model implements Arrayable, ArrayAccess, Jsonable, ModelInterface
84+
abstract class Model implements Arrayable, ArrayAccess, Jsonable
8685
{
87-
use HasAttributes;
88-
use HasRelationships;
89-
9086
public ?string $identifier;
9187
public Builder $builder;
88+
public Collection $relations;
89+
public array $attributes = [];
90+
protected array $casts = [];
91+
92+
/**
93+
* @var string[]
94+
*/
95+
protected array $dates = [
96+
'created_at',
97+
'updated_at',
98+
'change_date',
99+
'start_date',
100+
'published_at',
101+
'first_release_date',
102+
];
92103

93104
protected string $endpoint;
94105

95106
/**
96107
* @throws ReflectionException
97108
* @throws InvalidParamsException
98109
*/
99-
public function __construct(array $properties = [])
110+
final public function __construct(array $properties = [])
100111
{
101112
$this->builder = new Builder($this);
102113

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

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

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

254-
if (null !== $class && class_exists($class)) {
265+
if (is_string($class) && class_exists($class)) {
255266
return $class;
256267
}
257268
}
@@ -294,7 +305,7 @@ public function getEndpoint(): string
294305
public function toArray(): array
295306
{
296307
$attributes = collect($this->attributes);
297-
$relations = collect($this->relations)->map(function ($relation) {
308+
$relations = $this->relations->map(function ($relation) {
298309
if (!$relation instanceof Arrayable) {
299310
return $relation;
300311
}

src/Models/Webhook.php

+4-5
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
use MarcReichel\IGDBLaravel\Enums\Webhook\Method;
1616
use MarcReichel\IGDBLaravel\Exceptions\AuthenticationException;
1717
use MarcReichel\IGDBLaravel\Exceptions\InvalidWebhookSecretException;
18-
use MarcReichel\IGDBLaravel\Interfaces\WebhookInterface;
1918

20-
class Webhook implements WebhookInterface
19+
class Webhook
2120
{
2221
public int $id;
2322
public string $url;
@@ -34,7 +33,7 @@ class Webhook implements WebhookInterface
3433
/**
3534
* @throws AuthenticationException
3635
*/
37-
public function __construct(mixed ...$parameters)
36+
final public function __construct(mixed ...$parameters)
3837
{
3938
$this->client = Http::withOptions([
4039
'base_uri' => ApiHelper::IGDB_BASE_URI,
@@ -54,13 +53,13 @@ public static function all(): \Illuminate\Support\Collection
5453
$response = $self->client->get('webhooks');
5554

5655
if ($response->failed()) {
57-
return new \Illuminate\Support\Collection();
56+
return \Illuminate\Support\Collection::make();
5857
}
5958

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

63-
public static function find(int $id): mixed
62+
public static function find(int $id): ?self
6463
{
6564
$self = new static();
6665
$response = $self->client->get('webhooks/' . $id);

src/Traits/HasAttributes.php

-23
This file was deleted.

src/Traits/HasRelationships.php

-12
This file was deleted.

tests/ApiHelperTest.php

+17
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Support\Facades\Http;
99
use MarcReichel\IGDBLaravel\ApiHelper;
1010
use MarcReichel\IGDBLaravel\Exceptions\AuthenticationException;
11+
use Symfony\Component\HttpFoundation\Response;
1112

1213
/**
1314
* @internal
@@ -44,4 +45,20 @@ public function testItShouldRetrieveAccessTokenFromTwitch(): void
4445

4546
$this->assertEquals('test-suite-token', $token);
4647
}
48+
49+
/**
50+
* @throws AuthenticationException
51+
*/
52+
public function testItShouldThrowAuthenticationException(): void
53+
{
54+
$this->expectException(AuthenticationException::class);
55+
56+
Cache::forget('igdb_cache.access_token');
57+
58+
Http::fake([
59+
'*/oauth2/token*' => Http::response([], Response::HTTP_INTERNAL_SERVER_ERROR),
60+
]);
61+
62+
ApiHelper::retrieveAccessToken();
63+
}
4764
}

0 commit comments

Comments
 (0)