mirror of https://github.com/kcal-app/kcal.git
Add basic API tests
This commit is contained in:
parent
9413fa7686
commit
b89da004c5
|
|
@ -19,14 +19,14 @@ JsonApi::register('v1')->routes(function ($api) {
|
|||
$relations->hasOne('ingredient')->readOnly();
|
||||
$relations->hasOne('parent')->readOnly();
|
||||
})->readOnly();
|
||||
$api->resource('media')->relationships(function ($relations) {
|
||||
$relations->hasOne('owner')->readOnly();
|
||||
})->readOnly();
|
||||
$api->resource('journal-entries')->relationships(function ($relations) {
|
||||
$relations->hasMany('foods')->readOnly();
|
||||
$relations->hasMany('recipes')->readOnly();
|
||||
$relations->hasOne('user')->readOnly();
|
||||
})->readOnly();
|
||||
$api->resource('media')->relationships(function ($relations) {
|
||||
$relations->hasOne('owner')->readOnly();
|
||||
})->readOnly();
|
||||
$api->resource('recipes')->relationships(function ($relations) {
|
||||
$relations->hasMany('ingredient-amounts')->readOnly();
|
||||
$relations->hasMany('media')->readOnly();
|
||||
|
|
|
|||
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature\JsonApi;
|
||||
|
||||
use App\Models\Food;
|
||||
use Database\Factories\FoodFactory;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
class FoodApiTest extends JsonApiTestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function factory(): FoodFactory
|
||||
{
|
||||
return Food::factory();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function resourceName(): string
|
||||
{
|
||||
return 'foods';
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature\JsonApi;
|
||||
|
||||
use App\Models\IngredientAmount;
|
||||
use Database\Factories\IngredientAmountFactory;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
class IngredientAmountApiTest extends JsonApiTestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function factory(): IngredientAmountFactory
|
||||
{
|
||||
return IngredientAmount::factory();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function resourceName(): string
|
||||
{
|
||||
return 'ingredient-amounts';
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature\JsonApi;
|
||||
|
||||
use App\Models\JournalEntry;
|
||||
use Database\Factories\JournalEntryFactory;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
class JournalEntryApiTest extends JsonApiTestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function factory(): JournalEntryFactory
|
||||
{
|
||||
return JournalEntry::factory();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function resourceName(): string
|
||||
{
|
||||
return 'journal-entries';
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature\JsonApi;
|
||||
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Tests\LoggedInTestCase;
|
||||
|
||||
abstract class JsonApiTestCase extends LoggedInTestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* Route prefix for queries.
|
||||
*/
|
||||
private string $routePrefix;
|
||||
|
||||
/**
|
||||
* Get the factory of the model to be tested.
|
||||
*/
|
||||
abstract public function factory(): Factory;
|
||||
|
||||
/**
|
||||
* Get the resource name used for API requests.
|
||||
*/
|
||||
abstract public function resourceName(): string;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->routePrefix = config('json-api-v1.url.name');
|
||||
}
|
||||
|
||||
/**
|
||||
* Create instances of the model being tested.
|
||||
*/
|
||||
protected function createInstances(int $count = 1): Collection {
|
||||
return $this->factory()->count($count)->create();
|
||||
}
|
||||
|
||||
public function testCanGetIndex(): void
|
||||
{
|
||||
$this->createInstances(10);
|
||||
$index_url = route("{$this->routePrefix}{$this->resourceName()}.index");
|
||||
$response = $this->get($index_url);
|
||||
$response->assertOk();
|
||||
$response->assertJson(['data' => true]);
|
||||
$response->assertJsonCount(10, 'data');
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature\JsonApi;
|
||||
|
||||
use App\Models\Recipe;
|
||||
use Database\Factories\RecipeFactory;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
class RecipeApiTest extends JsonApiTestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function factory(): RecipeFactory
|
||||
{
|
||||
return Recipe::factory();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function resourceName(): string
|
||||
{
|
||||
return 'recipes';
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature\JsonApi;
|
||||
|
||||
use App\Models\Recipe;
|
||||
use App\Models\RecipeStep;
|
||||
use Database\Factories\RecipeStepFactory;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
class RecipeStepApiTest extends JsonApiTestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function factory(): RecipeStepFactory
|
||||
{
|
||||
return RecipeStep::factory();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function resourceName(): string
|
||||
{
|
||||
return 'recipe-steps';
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function createInstances(int $count = 1): Collection {
|
||||
return Recipe::factory()->count(1)->hasSteps($count)->create();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature\JsonApi;
|
||||
|
||||
use App\Models\Tag;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
class TagApiTest extends JsonApiTestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function factory(): Factory
|
||||
{
|
||||
return Tag::factory();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function resourceName(): string
|
||||
{
|
||||
return 'tags';
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue