Mercurial > packages > magicforger
comparison src/Generator/Test/stubs/test.stub @ 37:116b36f5e73b ls_dev_2025_09
Adding support for tests. It's pretty basic but we can improve later.
| author | Luka Sitas <sitas.luka.97@gmail.com> |
|---|---|
| date | Thu, 25 Sep 2025 19:58:01 -0400 |
| parents | |
| children | b5c6ebd33547 |
comparison
equal
deleted
inserted
replaced
| 36:76584181267a | 37:116b36f5e73b |
|---|---|
| 1 <?php | |
| 2 | |
| 3 namespace Tests\CRUD\{{ model }}; | |
| 4 | |
| 5 use {{ namespacedModel }}; | |
| 6 use App\Models\User; | |
| 7 use Wizard\Framework\Tests\TestCase; | |
| 8 | |
| 9 class {{ class }} extends TestCase | |
| 10 { | |
| 11 /* use RefreshDatabase; */ | |
| 12 | |
| 13 public function test_{{ modelVariable }}_crud_index_access(): void | |
| 14 { | |
| 15 $response = $this | |
| 16 ->get(route('{{ tableName }}.index')); | |
| 17 | |
| 18 $response->assertStatus(200); | |
| 19 } | |
| 20 | |
| 21 public function test_{{ modelVariable }}_crud_get_data_access(): void | |
| 22 { | |
| 23 $response = $this | |
| 24 ->post(route('{{ tableName }}.get_data')); | |
| 25 | |
| 26 $response->assertStatus(200); | |
| 27 } | |
| 28 | |
| 29 public function test_{{ modelVariable }}_crud_create_access(): void | |
| 30 { | |
| 31 $response = $this | |
| 32 ->get(route('{{ tableName }}.create')); | |
| 33 | |
| 34 $response->assertStatus(200); | |
| 35 } | |
| 36 | |
| 37 public function test_{{ modelVariable }}_crud_edit_access(): void | |
| 38 { | |
| 39 $item = {{ model }}::factory()->create(); | |
| 40 | |
| 41 $response = $this | |
| 42 ->get(route('{{ tableName }}.edit', $item)); | |
| 43 | |
| 44 $response->assertStatus(200); | |
| 45 } | |
| 46 | |
| 47 public function test_{{ modelVariable }}_crud_create_usage(): void | |
| 48 { | |
| 49 $item = {{ model }}::factory()->make(); | |
| 50 | |
| 51 $response = $this | |
| 52 ->from(route('{{ tableName }}.create')) | |
| 53 ->post(route('{{ tableName }}.store'), | |
| 54 $item->toArray() | |
| 55 ); | |
| 56 | |
| 57 $response | |
| 58 ->assertSessionHasNoErrors() | |
| 59 ->assertRedirect(route('{{ tableName }}.index')); | |
| 60 | |
| 61 $this->assertDatabaseHas('{{ tableName }}', $item->toArray()); | |
| 62 } | |
| 63 | |
| 64 public function test_{{ modelVariable }}_crud_create_invalid_data(): void | |
| 65 { | |
| 66 $response = $this | |
| 67 ->from(route('{{ tableName }}.create')) | |
| 68 ->post(route('{{ tableName }}.store'), []); | |
| 69 | |
| 70 $response | |
| 71 ->assertSessionHasErrors() | |
| 72 ->assertRedirect(route('{{ tableName }}.create')); | |
| 73 | |
| 74 // Make sure no entry was added | |
| 75 $this->assertDatabaseCount('{{ tableName }}', 0); | |
| 76 } | |
| 77 | |
| 78 public function test_{{ modelVariable }}_crud_update_usage(): void | |
| 79 { | |
| 80 $item = {{ model }}::factory()->create(); | |
| 81 | |
| 82 $updateData = {{ model }}::factory()->make()->toArray(); | |
| 83 | |
| 84 $response = $this | |
| 85 ->from(route('{{ tableName }}.edit', $item)) | |
| 86 ->put(route('{{ tableName }}.update', $item), $updateData); | |
| 87 | |
| 88 $response | |
| 89 ->assertSessionHasNoErrors() | |
| 90 ->assertRedirect(route('{{ tableName }}.index')); | |
| 91 | |
| 92 $this->assertDatabaseHas('{{ tableName }}', $updateData); | |
| 93 } | |
| 94 | |
| 95 public function test_{{ modelVariable }}_crud_edit_invalid_data(): void | |
| 96 { | |
| 97 $item = {{ model }}::factory()->create(); | |
| 98 | |
| 99 $response = $this | |
| 100 ->from(route('{{ tableName }}.edit', $item)) | |
| 101 ->put(route('{{ tableName }}.update', $item), []); | |
| 102 | |
| 103 $response | |
| 104 ->assertSessionHasErrors() | |
| 105 ->assertRedirect(route('{{ tableName }}.edit', $item)); | |
| 106 | |
| 107 // Make sure no entry was added | |
| 108 $this->assertDatabaseHas('games', array_intersect(array_flip($item->getFillable()), $item->toArray())); | |
| 109 } | |
| 110 | |
| 111 public function test_{{ modelVariable }}_crud_delete_usage(): void | |
| 112 { | |
| 113 $item = {{ model }}::factory()->create(); | |
| 114 | |
| 115 $response = $this | |
| 116 ->delete(route('{{ tableName }}.destroy', $item)); | |
| 117 | |
| 118 $response | |
| 119 ->assertSessionHasNoErrors() | |
| 120 ->assertRedirect(route('{{ tableName }}.index')); | |
| 121 | |
| 122 $this->assertSoftDeleted('{{ tableName }}', [ | |
| 123 'id' => $item->id, | |
| 124 ]); | |
| 125 } | |
| 126 | |
| 127 } |
