Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added more tests and improved existing ones #489

Merged
merged 2 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions database/factories/AnimeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@ protected function definitionInternal(): array
{
$mal_id = $this->createMalId();
$title = $this->createTitle();
$status = $this->faker->randomElement(["Currently Airing", "Completed", "Upcoming"]);
$status = $this->faker->randomElement(["Currently Airing", "Finished Airing", "Not yet aired"]);
[$aired_from, $aired_to] = $this->createActiveDateRange($status, "Currently Airing");

$premiered = $this->faker->randomElement(["Winter", "Spring", "Fall", "Summer"]) . " " . $this->faker->year();
if ($status === "Not yet aired") {
$premiered = null;
}

return [
"mal_id" => $mal_id,
"url" => $this->createMalUrl($mal_id, "anime"),
Expand Down Expand Up @@ -68,7 +73,7 @@ protected function definitionInternal(): array
"synopsis" => "test",
"approved" => true,
"background" => "test",
"premiered" => $this->faker->randomElement(["Winter", "Spring", "Fall", "Summer"]) . " " . $this->faker->year(),
"premiered" => $premiered,
"broadcast" => [
"day" => "",
"time" => "",
Expand Down
36 changes: 18 additions & 18 deletions tests/Integration/AnimeSearchEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,24 +120,24 @@ public function emptyDateRangeProvider(): array
public function commonParameterProvider(): array
{
return [
[["status" => "airing"]],
[["status" => "complete"]],
[["status" => "upcoming"]],
[["status" => "Airing"]],
[["status" => "Complete"]],
[["status" => "Upcoming"]],
[["max_score" => "8"]],
[["min_score" => "6"]],
[["max_score" => "7", "min_score" => "3"]],
[["rating" => "pg"]],
[["rating" => "rx"]],
[["rating" => "r"]],
[["rating" => "pg13"]],
[["rating" => "g"]],
[["rating" => "r17"]],
[["type" => "movie"]],
[["type" => "ova"]],
[["type" => "special"]],
"status = airing" => [["status" => "airing"]],
"status = complete" => [["status" => "complete"]],
"status = upcoming" => [["status" => "upcoming"]],
"status = Airing" => [["status" => "Airing"]],
"status = Complete" => [["status" => "Complete"]],
"status = Upcoming" => [["status" => "Upcoming"]],
"max_score = 8" => [["max_score" => "8"]],
"min_score = 6" => [["min_score" => "6"]],
"max_score = 7, min_score = 3" => [["max_score" => "7", "min_score" => "3"]],
"rating = pg" => [["rating" => "pg"]],
"rating = rx" => [["rating" => "rx"]],
"rating = r" => [["rating" => "r"]],
"rating = pg13" => [["rating" => "pg13"]],
"rating = g" => [["rating" => "g"]],
"rating = r17" => [["rating" => "r17"]],
"type = movie" => [["type" => "movie"]],
"type = ova" => [["type" => "ova"]],
"type = special" => [["type" => "special"]],
];
}

Expand Down
52 changes: 52 additions & 0 deletions tests/Integration/SeasonControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/** @noinspection PhpIllegalPsrClassPathInspection */
namespace Tests\Integration;

use App\Anime;
use App\CarbonDateRange;
use App\Testing\ScoutFlush;
use App\Testing\SyntheticMongoDbTransaction;
use Illuminate\Support\Carbon;
use Tests\TestCase;


class SeasonControllerTest extends TestCase
{
use SyntheticMongoDbTransaction;
use ScoutFlush;

public function testShouldFilterOutAnimeWithGarbledAiredString()
{
Carbon::setTestNow(Carbon::parse("2024-01-11"));
// the wrong item
$f = Anime::factory(1);
$startDate = "2024-01-01";
$carbonStartDate = Carbon::parse($startDate);
$state = $f->serializeStateDefinition([
"aired" => new CarbonDateRange($carbonStartDate, null)
]);
$state["aired"]["string"] = "Jan 1, 2024 to ?";
$state["premiered"] = null;
$state["status"] = "Not yet aired";
$state["airing"] = false;
$f->create($state);

// the correct item
$f = Anime::factory(1);
$state = $f->serializeStateDefinition([
"aired" => new CarbonDateRange(Carbon::parse("2024-01-10"), Carbon::parse("2024-02-15"))
]);
$state["premiered"] = "Winter 2024";
$state["status"] = "Currently Airing";
$state["airing"] = true;
$f->create($state);

$content = $this->getJsonResponse([], "/v4/seasons/2024/winter");

Carbon::setTestNow();

$this->seeStatusCode(200);
$this->assertIsArray($content["data"]);
$this->assertCount(1, $content["data"]);
}
}
45 changes: 45 additions & 0 deletions tests/Unit/PreparesDataTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/** @noinspection PhpIllegalPsrClassPathInspection */
namespace Tests\Unit;
use App\Dto\Concerns\HasLimitParameter;
use App\Dto\Concerns\HasPageParameter;
use App\Dto\Concerns\PreparesData;
use Spatie\LaravelData\Data;
use Spatie\LaravelData\Optional;
use Tests\TestCase;

final class PreparesDataFixture extends Data
{
use PreparesData, HasLimitParameter, HasPageParameter;

public string|Optional $filter;
}

final class PreparesDataTraitTest extends TestCase
{
public function paramsDataProvider(): array
{
return [
'limit = empty, page = empty, filter = empty' => [['limit' => '', 'page' => '', 'filter' => ''], []],
'limit = 1, page = empty, filter = empty' => [['limit' => '1', 'page' => '', 'filter' => ''], ['limit' => 1]],
'limit = 1, page = 2, filter = empty' => [
['limit' => '1', 'page' => '2', 'filter' => ''],
['limit' => 1, 'page' => 2]
],
'limit = 1, page = 2, filter = somefilter' => [
['limit' => '1', 'page' => '2', 'filter' => 'somefilter'],
['limit' => 1, 'page' => 2, 'filter' => 'somefilter']
],
];
}

/**
* @dataProvider paramsDataProvider
*/
public function testShouldIgnoreEmptyParams($actual, $expected)
{
$sut = PreparesDataFixture::prepareForPipeline(collect($actual));

$this->assertCollectionsStrictlyEqual(collect($expected), $sut);
}
}