Skip to content

Commit 32d1c24

Browse files
committed
Tests for all endpoints
1 parent 06f280d commit 32d1c24

File tree

7 files changed

+228
-12
lines changed

7 files changed

+228
-12
lines changed

app/Models/Category.php

+14-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Database\Eloquent\Model;
77
use Illuminate\Database\Eloquent\Builder;
88
use Illuminate\Database\Eloquent\Collection;
9+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
910
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
1011
use Illuminate\Support\Facades\Auth;
1112

@@ -25,9 +26,11 @@ class Category extends Model
2526
*/
2627
protected static function booted(): void
2728
{
28-
static::creating(function ($product) {
29-
$product->user_id = Auth::id();
30-
});
29+
if (Auth::check()) {
30+
static::creating(function ($product) {
31+
$product->user_id = Auth::id();
32+
});
33+
}
3134
}
3235

3336
/**
@@ -38,6 +41,14 @@ public function products(): BelongsToMany
3841
return $this->belongsToMany(Product::class);
3942
}
4043

44+
/**
45+
* @return BelongsTo
46+
*/
47+
public function user(): BelongsTo
48+
{
49+
return $this->belongsTo(User::class);
50+
}
51+
4152
/**
4253
* @param Builder $query
4354
* @return Builder

app/Models/Product.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ class Product extends Model
2525
*/
2626
protected static function booted(): void
2727
{
28-
static::creating(function ($product) {
29-
$product->user_id = Auth::id();
30-
});
28+
if (Auth::check()) {
29+
static::creating(function ($product) {
30+
$product->user_id = Auth::id();
31+
});
32+
}
3133
}
3234

3335
/**

app/Models/User.php

+17
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Foundation\Auth\User as Authenticatable;
88
use Illuminate\Notifications\Notifiable;
99
use Laravel\Sanctum\HasApiTokens;
10+
use Illuminate\Database\Eloquent\Relations\HasMany;
1011

1112
class User extends Authenticatable
1213
{
@@ -41,4 +42,20 @@ class User extends Authenticatable
4142
protected $casts = [
4243
'email_verified_at' => 'datetime',
4344
];
45+
46+
/**
47+
* @return HasMany
48+
*/
49+
public function categories(): HasMany
50+
{
51+
return $this->hasMany(Category::class);
52+
}
53+
54+
/**
55+
* @return HasMany
56+
*/
57+
public function products(): HasMany
58+
{
59+
return $this->hasMany(Product::class);
60+
}
4461
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Database\Factories;
4+
5+
use App\Models\User;
6+
use Illuminate\Database\Eloquent\Factories\Factory;
7+
8+
/**
9+
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Category>
10+
*/
11+
class CategoryFactory extends Factory
12+
{
13+
/**
14+
* Define the model's default state.
15+
*
16+
* @return array<string, mixed>
17+
*/
18+
public function definition(): array
19+
{
20+
$user = User::factory()->create();
21+
22+
return [
23+
'name' => fake()->name(),
24+
'user_id' => User::factory(),
25+
];
26+
}
27+
}

database/factories/ProductFactory.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Database\Factories;
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
6+
7+
/**
8+
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Product>
9+
*/
10+
class ProductFactory extends Factory
11+
{
12+
/**
13+
* Define the model's default state.
14+
*
15+
* @return array<string, mixed>
16+
*/
17+
public function definition(): array
18+
{
19+
return [
20+
'name' => fake()->name(),
21+
'price' => fake()->numberBetween(1, 100),
22+
];
23+
}
24+
}

tests/Feature/Api/CategoryControllerTest.php

+70-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,78 @@
22

33
namespace Tests\Feature\API;
44

5-
use Illuminate\Foundation\Testing\RefreshDatabase;
6-
use Illuminate\Foundation\Testing\WithFaker;
75
use Tests\TestCase;
6+
use App\Models\User;
7+
use App\Models\Category;
8+
use Illuminate\Http\Response;
89

910
class CategoryControllerTest extends TestCase
1011
{
11-
// TODO
12+
/**
13+
* @return void
14+
*/
15+
public function test_index(): void
16+
{
17+
$user = User::factory()->create();
18+
19+
$response = $this->actingAs($user)->get('api/category');
20+
21+
$response->assertStatus(Response::HTTP_OK);
22+
}
23+
24+
/**
25+
* @return void
26+
*/
27+
public function test_store(): void
28+
{
29+
$user = User::factory()->create();
30+
31+
$response = $this->actingAs($user)->postJson('api/category', Category::factory()->raw());
32+
33+
$response->assertStatus(Response::HTTP_OK);
34+
}
35+
36+
/**
37+
* @return void
38+
*/
39+
public function test_show(): void
40+
{
41+
$user = User::factory()->create();
42+
43+
$category = $user->categories()->create(Category::factory()->make()->toArray());
44+
45+
$response = $this->actingAs($user)->get('api/category/' . $category->id);
46+
47+
$response->assertStatus(Response::HTTP_OK);
48+
}
49+
50+
/**
51+
* @return void
52+
*/
53+
public function test_update(): void
54+
{
55+
$user = User::factory()->create();
56+
57+
$category = $user->categories()->create(Category::factory()->make()->toArray());
58+
59+
$response = $this->actingAs($user)->patchJson('api/category/' . $category->id, [
60+
'name' => fake()->name(),
61+
]);
62+
63+
$response->assertStatus(Response::HTTP_OK);
64+
}
65+
66+
/**
67+
* @return void
68+
*/
69+
public function test_destroy(): void
70+
{
71+
$user = User::factory()->create();
72+
73+
$category = $user->categories()->create(Category::factory()->make()->toArray());
74+
75+
$response = $this->actingAs($user)->delete('api/category/' . $category->id);
76+
77+
$response->assertStatus(Response::HTTP_OK);
78+
}
1279
}

tests/Feature/Api/ProductControllerTest.php

+71-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,79 @@
22

33
namespace Tests\Feature\API;
44

5-
use Illuminate\Foundation\Testing\RefreshDatabase;
6-
use Illuminate\Foundation\Testing\WithFaker;
75
use Tests\TestCase;
6+
use App\Models\User;
7+
use App\Models\Product;
8+
use Illuminate\Http\Response;
89

910
class ProductControllerTest extends TestCase
1011
{
11-
// TODO
12+
/**
13+
* @return void
14+
*/
15+
public function test_index(): void
16+
{
17+
$user = User::factory()->create();
18+
19+
$response = $this->actingAs($user)->get('api/product');
20+
21+
$response->assertStatus(Response::HTTP_OK);
22+
}
23+
24+
/**
25+
* @return void
26+
*/
27+
public function test_store(): void
28+
{
29+
$user = User::factory()->create();
30+
31+
$response = $this->actingAs($user)->postJson('api/product', Product::factory()->raw());
32+
33+
$response->assertStatus(Response::HTTP_OK);
34+
}
35+
36+
/**
37+
* @return void
38+
*/
39+
public function test_show(): void
40+
{
41+
$user = User::factory()->create();
42+
43+
$product = $user->products()->create(Product::factory()->make()->toArray());
44+
45+
$response = $this->actingAs($user)->get('api/product/' . $product->id);
46+
47+
$response->assertStatus(Response::HTTP_OK);
48+
}
49+
50+
/**
51+
* @return void
52+
*/
53+
public function test_update(): void
54+
{
55+
$user = User::factory()->create();
56+
57+
$product = $user->products()->create(Product::factory()->make()->toArray());
58+
59+
$response = $this->actingAs($user)->patchJson('api/product/' . $product->id, [
60+
'name' => fake()->name(),
61+
'price' => fake()->numberBetween(1, 100),
62+
]);
63+
64+
$response->assertStatus(Response::HTTP_OK);
65+
}
66+
67+
/**
68+
* @return void
69+
*/
70+
public function test_destroy(): void
71+
{
72+
$user = User::factory()->create();
73+
74+
$product = $user->products()->create(Product::factory()->make()->toArray());
75+
76+
$response = $this->actingAs($user)->delete('api/product/' . $product->id);
77+
78+
$response->assertStatus(Response::HTTP_OK);
79+
}
1280
}

0 commit comments

Comments
 (0)