Skip to content

Commit

Permalink
refactor: Fix deprecations and fix return type of getCard() (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
clussiana authored Jan 14, 2025
1 parent 18237d4 commit 77a67d3
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Endpoints/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function get(string|int $id)
/**
* @return Array<List>
*/
public function list(Query $query = null): array
public function list(?Query $query = null): array
{
$res = $this->tcgdex->fetchWithParams([$this->endpoint], is_null($query) ? null : $query->params);
if (is_null($this->listModel)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Endpoints/SetEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct(
);
}

public function getCard(string $set, string $localId): Card
public function getCard(string $set, string $localId): ?Card
{
$res = $this->tcgdex->fetch($this->endpoint, $set, $localId);
return Model::build(new Card($this->tcgdex), $res);
Expand Down
2 changes: 2 additions & 0 deletions src/Model/Card.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,6 @@ protected function fill(object $data): void
public ?string $regulationMark = null;

public Legal $legal;

public ?string $updated = null;
}
2 changes: 1 addition & 1 deletion src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static function fetch(...$path)
* @param string $url
* @param Array<string, string> $params
*/
public static function fetchWithParams(string $url, array $params = null): mixed
public static function fetchWithParams(string $url, ?array $params = null): mixed
{
if (!is_null($params)) {
$mapped = array_map(function (string $key, string $value) {
Expand Down
7 changes: 2 additions & 5 deletions src/TCGdex.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,7 @@ public static function getVersion()
*/
public $lang = "en";

/**
* @param String|null $lang
*/
public function __construct($lang = null)
public function __construct(?string $lang = null)
{
if (is_null(TCGdex::$cache)) {
// no PSR16 implementation found, try loading the base one
Expand Down Expand Up @@ -232,7 +229,7 @@ public function fetch(...$endpoint)
* @param Array<string> $params the list of parameters to add
* @return mixed|null
*/
public function fetchWithParams(array $endpoint, array $params = null)
public function fetchWithParams(array $endpoint, ?array $params = null)
{
return Request::fetchWithParams(
Request::makePath(TCGdex::BASE_URI, $this->lang, ...$endpoint),
Expand Down
15 changes: 12 additions & 3 deletions tests/TCGdexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public function testCache(): void
TCGdex::$client = new Psr18Mock("{\"id\": \"1\"}");
$tcgdex = new TCGdex("en");
$card1 = $tcgdex->card->get('testCache');
$this->assertEquals($card1->id, "1");
$this->assertEquals("1", $card1->id);
TCGdex::$client = new Psr18Mock("{\"id\": \"2\"}");
$card2 = $tcgdex->card->get('testCache');
$this->assertEquals($card2->id, "1");
$this->assertEquals("1", $card2->id);
}

public function testRealEndpoints(): void
Expand Down Expand Up @@ -73,6 +73,15 @@ public function testRealEndpoints(): void
}
}

public function testUnknownCard(): void
{
TCGdex::$client = null;
$tcgdex = new TCGdex("en");
$card = $tcgdex->set->getCard('unknownSet', 'unknownCard');

$this->assertNull($card);
}

public function testFetchFullCardFromResume(): void
{
TCGdex::$client = null;
Expand All @@ -89,6 +98,6 @@ public function testFetchFullSerieFromResume(): void
$tcgdex = new TCGdex("en");
$series = $tcgdex->serie->list();
$this->assertNotEmpty($series);
$this->assertNotEmpty($series[0]->fetchFullSerie());
$this->assertNotEmpty($series[0]->toSerie());
}
}

0 comments on commit 77a67d3

Please sign in to comment.