Add Media API tests

This commit is contained in:
Christopher C. Wells 2021-04-02 23:34:23 -07:00 committed by Christopher Charbonneau Wells
parent 192070f826
commit 2b3f0c31f6
4 changed files with 94 additions and 2 deletions

View File

@ -43,14 +43,14 @@ class UserSchema extends SchemaProvider
self::SHOW_RELATED => true,
self::SHOW_DATA => isset($includeRelationships['goals']),
self::DATA => function () use ($resource) {
return $resource->ingredientAmounts; // @codeCoverageIgnore
return $resource->goals; // @codeCoverageIgnore
},
],
'journal-entries' => [
self::SHOW_RELATED => true,
self::SHOW_DATA => isset($includeRelationships['journal-entries']),
self::DATA => function () use ($resource) {
return $resource->ingredientAmounts; // @codeCoverageIgnore
return $resource->journalEntries; // @codeCoverageIgnore
},
],
];

View File

@ -4,6 +4,8 @@ namespace Database\Factories;
use App\Models\Recipe;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
class RecipeFactory extends Factory
{
@ -30,4 +32,20 @@ class RecipeFactory extends Factory
'tags' => $this->faker->words,
];
}
/**
* Create a recipe and add fake media to it.
*/
public function createOneWithMedia(array $attributes = []): Recipe {
Storage::fake('tests');
/** @var \App\Models\Recipe $recipe */
$recipe = $this->createOne($attributes);
$file = UploadedFile::fake()->image('recipe.jpg', 1600, 900);
$path = $file->store('tests');
$recipe->addMediaFromDisk($path)
->usingName($recipe->name)
->usingFileName("{$recipe->slug}.jpg")
->toMediaCollection();
return $recipe;
}
}

View File

@ -0,0 +1,64 @@
<?php
namespace Tests\Feature\JsonApi;
use App\Models\Recipe;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Foundation\Testing\RefreshDatabase;
class MediumApiTest extends JsonApiTestCase
{
use RefreshDatabase;
/**
* @inheritdoc
*/
public function factory(): Factory
{
// No-op for media.
return Factory::new();
}
/**
* @inheritdoc
*/
public function resourceName(): string
{
return 'media';
}
/**
* @inheritdoc
*/
protected function createInstances(int $count = 1): Collection {
// No-op for media.
return new Collection();
}
public function setUp(): void
{
parent::setUp();
$route_prefix = config('json-api-v1.url.name');
$this->routeBase = "{$route_prefix}media";
}
public function testCanGetIndex(): void {
Recipe::factory()->createOneWithMedia();
$index_route = route("$this->routeBase.index");
$response = $this->get($index_route);
$response->assertOk();
$response->assertJsonCount(1, 'data');
}
public function testCanGetRelatedOwner(): void {
$record = Recipe::factory()->createOneWithMedia();
$this->getRelatedData($record->media->first(), 'owner', 'recipes');
}
public function testCanIncludeRelatedOwner(): void {
$record = Recipe::factory()->createOneWithMedia();
$this->getRelatedData($record->media->first(), 'owner', 'recipes');
}
}

View File

@ -37,6 +37,16 @@ class RecipeApiTest extends JsonApiTestCase
$this->getRelatedData($record, 'ingredient-amounts');
}
public function testCanGetRelatedMedia(): void {
$record = $this->factory()->createOneWithMedia();
$this->getRelatedData($record, 'media');
}
public function testCanIncludeRelatedMedia(): void {
$record = $this->factory()->createOneWithMedia();
$this->getRelatedData($record, 'media');
}
public function testCanGetRelatedSeparators(): void {
$record = $this->factory()->hasSeparators(2)->create();
$this->getRelatedData($record, 'separators', 'recipe-separators');