diff --git a/app/JsonApi/Schemas/UserSchema.php b/app/JsonApi/Schemas/UserSchema.php index 2676ddd..db658f4 100644 --- a/app/JsonApi/Schemas/UserSchema.php +++ b/app/JsonApi/Schemas/UserSchema.php @@ -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 }, ], ]; diff --git a/database/factories/RecipeFactory.php b/database/factories/RecipeFactory.php index 43c54ed..fe1d56b 100644 --- a/database/factories/RecipeFactory.php +++ b/database/factories/RecipeFactory.php @@ -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; + } } diff --git a/tests/Feature/JsonApi/MediumApiTest.php b/tests/Feature/JsonApi/MediumApiTest.php new file mode 100644 index 0000000..c874c68 --- /dev/null +++ b/tests/Feature/JsonApi/MediumApiTest.php @@ -0,0 +1,64 @@ +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'); + } + +} diff --git a/tests/Feature/JsonApi/RecipeApiTest.php b/tests/Feature/JsonApi/RecipeApiTest.php index 3857972..b10facd 100644 --- a/tests/Feature/JsonApi/RecipeApiTest.php +++ b/tests/Feature/JsonApi/RecipeApiTest.php @@ -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');