|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature\Http\Controllers; |
| 4 | + |
| 5 | +use App\Models\Category; |
| 6 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 7 | +use Illuminate\Foundation\Testing\WithFaker; |
| 8 | +use JMac\Testing\Traits\AdditionalAssertions; |
| 9 | +use Tests\TestCase; |
| 10 | + |
| 11 | +/** |
| 12 | + * @see \App\Http\Controllers\CategoryController |
| 13 | + */ |
| 14 | +class CategoryControllerTest extends TestCase |
| 15 | +{ |
| 16 | + use AdditionalAssertions, RefreshDatabase, WithFaker; |
| 17 | + |
| 18 | + /** |
| 19 | + * @test |
| 20 | + */ |
| 21 | + public function index_behaves_as_expected(): void |
| 22 | + { |
| 23 | + $categories = Category::factory()->count(3)->create(); |
| 24 | + |
| 25 | + $response = $this->get(route('categories.index')); |
| 26 | + |
| 27 | + $response->assertOk(); |
| 28 | + $response->assertJsonStructure([]); |
| 29 | + } |
| 30 | + |
| 31 | + |
| 32 | + /** |
| 33 | + * @test |
| 34 | + */ |
| 35 | + public function store_uses_form_request_validation(): void |
| 36 | + { |
| 37 | + $this->assertActionUsesFormRequest( |
| 38 | + \App\Http\Controllers\CategoryController::class, |
| 39 | + 'store', |
| 40 | + \App\Http\Requests\CategoryStoreRequest::class |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @test |
| 46 | + */ |
| 47 | + public function store_saves(): void |
| 48 | + { |
| 49 | + $name = $this->faker->name; |
| 50 | + $image = $this->faker->word; |
| 51 | + $active = $this->faker->boolean; |
| 52 | + |
| 53 | + $response = $this->post(route('categories.store'), [ |
| 54 | + 'name' => $name, |
| 55 | + 'image' => $image, |
| 56 | + 'active' => $active, |
| 57 | + ]); |
| 58 | + |
| 59 | + $categories = Category::query() |
| 60 | + ->where('name', $name) |
| 61 | + ->where('image', $image) |
| 62 | + ->where('active', $active) |
| 63 | + ->get(); |
| 64 | + $this->assertCount(1, $categories); |
| 65 | + $category = $categories->first(); |
| 66 | + |
| 67 | + $response->assertCreated(); |
| 68 | + $response->assertJsonStructure([]); |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + /** |
| 73 | + * @test |
| 74 | + */ |
| 75 | + public function show_behaves_as_expected(): void |
| 76 | + { |
| 77 | + $category = Category::factory()->create(); |
| 78 | + |
| 79 | + $response = $this->get(route('categories.show', $category)); |
| 80 | + |
| 81 | + $response->assertOk(); |
| 82 | + $response->assertJsonStructure([]); |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + /** |
| 87 | + * @test |
| 88 | + */ |
| 89 | + public function update_uses_form_request_validation(): void |
| 90 | + { |
| 91 | + $this->assertActionUsesFormRequest( |
| 92 | + \App\Http\Controllers\CategoryController::class, |
| 93 | + 'update', |
| 94 | + \App\Http\Requests\CategoryUpdateRequest::class |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @test |
| 100 | + */ |
| 101 | + public function update_behaves_as_expected(): void |
| 102 | + { |
| 103 | + $category = Category::factory()->create(); |
| 104 | + $name = $this->faker->name; |
| 105 | + $image = $this->faker->word; |
| 106 | + $active = $this->faker->boolean; |
| 107 | + |
| 108 | + $response = $this->put(route('categories.update', $category), [ |
| 109 | + 'name' => $name, |
| 110 | + 'image' => $image, |
| 111 | + 'active' => $active, |
| 112 | + ]); |
| 113 | + |
| 114 | + $category->refresh(); |
| 115 | + |
| 116 | + $response->assertOk(); |
| 117 | + $response->assertJsonStructure([]); |
| 118 | + |
| 119 | + $this->assertEquals($name, $category->name); |
| 120 | + $this->assertEquals($image, $category->image); |
| 121 | + $this->assertEquals($active, $category->active); |
| 122 | + } |
| 123 | + |
| 124 | + |
| 125 | + /** |
| 126 | + * @test |
| 127 | + */ |
| 128 | + public function destroy_deletes_and_responds_with(): void |
| 129 | + { |
| 130 | + $category = Category::factory()->create(); |
| 131 | + |
| 132 | + $response = $this->delete(route('categories.destroy', $category)); |
| 133 | + |
| 134 | + $response->assertNoContent(); |
| 135 | + |
| 136 | + $this->assertSoftDeleted($category); |
| 137 | + } |
| 138 | +} |
0 commit comments