Skip to content

Commit 14ed77d

Browse files
authored
Merge pull request #113 from RonasIT/use-model-test-states-in-tests-generator
Use model test states in tests generator
2 parents cc994cd + 7dc31b0 commit 14ed77d

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

src/Generators/TestsGenerator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ protected function generateTests(): void
5555
'databaseTableName' => $this->getTableName($this->model),
5656
'entities' => $this->getTableName($this->model, '-'),
5757
'withAuth' => $this->withAuth,
58-
'modelsNamespace' => $this->getOrCreateNamespace('models')
58+
'modelsNamespace' => $this->getOrCreateNamespace('models'),
59+
'hasModificationEndpoints' => !empty(array_intersect($this->crudOptions, ['C', 'U', 'D'])),
5960
]);
6061

6162
$testName = $this->getTestClassName();

stubs/test.blade.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
namespace App\Tests;
22

3+
@if ($hasModificationEndpoints)
4+
use RonasIT\Support\Tests\ModelTestState;
5+
use {{$modelsNamespace}}\{{$entity}};
6+
@endif
37
@if ($withAuth)
48
use {{$modelsNamespace}}\User;
59
@endif
@@ -12,13 +16,21 @@ class {{$entity}}Test extends TestCase
1216
@if ($withAuth)
1317
protected static User $user;
1418

19+
@endif
20+
@if ($hasModificationEndpoints)
21+
protected static ModelTestState ${{\Illuminate\Support\Str::camel($entity)}}State;
22+
1523
@endif
1624
public function setUp() : void
1725
{
1826
parent::setUp();
1927
@if ($withAuth)
2028

2129
self::$user ??= User::find(1);
30+
@endif
31+
@if ($hasModificationEndpoints)
32+
33+
self::${{\Illuminate\Support\Str::camel($entity)}}State ??= new ModelTestState({{$entity}}::class);
2234
@endif
2335
}
2436

@@ -37,7 +49,8 @@ public function testCreate()
3749

3850
$this->assertEqualsFixture('create_{{\Illuminate\Support\Str::snake($entity)}}_response.json', $response->json());
3951

40-
$this->assertDatabaseHas('{{$databaseTableName}}', $this->getJsonFixture('create_{{\Illuminate\Support\Str::snake($entity)}}_response.json'));
52+
// TODO: Need to remove last argument after first successful start
53+
self::${{\Illuminate\Support\Str::camel($entity)}}State->assertChangesEqualsFixture('create_{{\Illuminate\Support\Str::snake($entity)}}_state.json', true);
4154
}
4255

4356
@if ($withAuth)
@@ -65,7 +78,8 @@ public function testUpdate()
6578

6679
$response->assertNoContent();
6780

68-
$this->assertDatabaseHas('{{$databaseTableName}}', $data);
81+
// TODO: Need to remove last argument after first successful start
82+
self::${{\Illuminate\Support\Str::camel($entity)}}State->assertChangesEqualsFixture('update_{{\Illuminate\Support\Str::snake($entity)}}_state.json', true);
6983
}
7084

7185
public function testUpdateNotExists()
@@ -104,9 +118,8 @@ public function testDelete()
104118

105119
$response->assertNoContent();
106120

107-
$this->assertDatabaseMissing('{{$databaseTableName}}', [
108-
'id' => 1
109-
]);
121+
// TODO: Need to remove last argument after first successful start
122+
self::${{\Illuminate\Support\Str::camel($entity)}}State->assertChangesEqualsFixture('delete_{{\Illuminate\Support\Str::snake($entity)}}_state.json', true);
110123
}
111124

112125
public function testDeleteNotExists()
@@ -119,9 +132,7 @@ public function testDeleteNotExists()
119132

120133
$response->assertNotFound();
121134

122-
$this->assertDatabaseMissing('{{$databaseTableName}}', [
123-
'id' => 0
124-
]);
135+
self::${{\Illuminate\Support\Str::camel($entity)}}State->assertNotChanged();
125136
}
126137

127138
@if ($withAuth)

0 commit comments

Comments
 (0)