Skip to content

Commit 2dbd60a

Browse files
authored
Merge pull request #154 from RonasIT/147-tests-refactoring
refactor: move set now to testcase, mock traits
2 parents d7514da + b867ee1 commit 2dbd60a

22 files changed

+164
-141
lines changed

stubs/nova_test.blade.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@ public function setUp(): void
2424

2525
public function testCreate(): void
2626
{
27-
$data = $this->getJsonFixture('create_{{$snake_entity}}_request.json');
27+
$data = $this->getJsonFixture('create_{{$snake_entity}}_request');
2828

2929
$response = $this->novaActingAs(self::$user)->novaCreateResourceAPICall({{$entity}}::class, $data);
3030

3131
$response->assertCreated();
3232

33-
$this->assertEqualsFixture('create_{{$snake_entity}}_response.json', $response->json());
33+
$this->assertEqualsFixture('create_{{$snake_entity}}_response', $response->json());
3434

3535
// TODO: Need to remove last argument after first successful start
36-
self::${{$dromedary_entity}}State->assertChangesEqualsFixture('create_{{$lower_entities}}_state.json', true);
36+
self::${{$dromedary_entity}}State->assertChangesEqualsFixture('create_{{$lower_entities}}_state', true);
3737
}
3838

3939
public function testCreateNoAuth(): void
@@ -52,26 +52,26 @@ public function testCreateValidationError(): void
5252
$response->assertUnprocessable();
5353

5454
// TODO: Need to remove last argument after first successful start
55-
$this->assertEqualsFixture('create_validation_response.json', $response->json(), true);
55+
$this->assertEqualsFixture('create_validation_response', $response->json(), true);
5656

5757
self::${{$dromedary_entity}}State->assertNotChanged();
5858
}
5959

6060
public function testUpdate(): void
6161
{
62-
$data = $this->getJsonFixture('update_{{$snake_entity}}_request.json');
62+
$data = $this->getJsonFixture('update_{{$snake_entity}}_request');
6363

6464
$response = $this->novaActingAs(self::$user)->novaUpdateResourceAPICall({{$entity}}::class, 1, $data);
6565

6666
$response->assertNoContent();
6767

6868
// TODO: Need to remove last argument after first successful start
69-
self::${{$dromedary_entity}}State->assertChangesEqualsFixture('update_{{$lower_entities}}_state.json', true);
69+
self::${{$dromedary_entity}}State->assertChangesEqualsFixture('update_{{$lower_entities}}_state', true);
7070
}
7171

7272
public function testUpdateNotExists(): void
7373
{
74-
$data = $this->getJsonFixture('update_{{$snake_entity}}_request.json');
74+
$data = $this->getJsonFixture('update_{{$snake_entity}}_request');
7575

7676
$response = $this->novaActingAs(self::$user)->novaUpdateResourceAPICall({{$entity}}::class, 0, $data);
7777

@@ -92,7 +92,7 @@ public function testUpdateValidationError(): void
9292
$response->assertUnprocessable();
9393

9494
// TODO: Need to remove last argument after first successful start
95-
$this->assertEqualsFixture('update_validation_response.json', $response->json(), true);
95+
$this->assertEqualsFixture('update_validation_response', $response->json(), true);
9696
}
9797

9898
public function testGetUpdatableFields(): void
@@ -102,7 +102,7 @@ public function testGetUpdatableFields(): void
102102
$response->assertOk();
103103

104104
// TODO: Need to remove last argument after first successful start
105-
$this->assertEqualsFixture('get_updatable_fields_response.json', $response->json(), true);
105+
$this->assertEqualsFixture('get_updatable_fields_response', $response->json(), true);
106106
}
107107

108108
public function testDelete(): void
@@ -112,7 +112,7 @@ public function testDelete(): void
112112
$response->assertOk();
113113

114114
// TODO: Need to remove last argument after first successful start
115-
self::${{$dromedary_entity}}State->assertChangesEqualsFixture('delete_{{$lower_entities}}_state.json', true);
115+
self::${{$dromedary_entity}}State->assertChangesEqualsFixture('delete_{{$lower_entities}}_state', true);
116116
}
117117

118118
public function testDeleteNotExists(): void
@@ -136,7 +136,7 @@ public function testGet(): void
136136
$response->assertOk();
137137

138138
// TODO: Need to remove last argument after first successful start
139-
$this->assertEqualsFixture('get_{{$snake_entity}}_response.json', $response->json(), true);
139+
$this->assertEqualsFixture('get_{{$snake_entity}}_response', $response->json(), true);
140140
}
141141

142142
public function testGetNotExists(): void
@@ -167,7 +167,7 @@ public function testGetFieldsVisibleOnCreate(): void
167167
$response->assertOk();
168168

169169
// TODO: Need to remove last argument after first successful start
170-
$this->assertEqualsFixture('get_fields_visible_on_create_response.json', $response->json(), true);
170+
$this->assertEqualsFixture('get_fields_visible_on_create_response', $response->json(), true);
171171
}
172172

173173
public static function getRun{{$entity}}ActionsData(): array

stubs/test.blade.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function setUp(): void
3737
@if (in_array('C', $options))
3838
public function testCreate()
3939
{
40-
$data = $this->getJsonFixture('create_{{\Illuminate\Support\Str::snake($entity)}}_request.json');
40+
$data = $this->getJsonFixture('create_{{\Illuminate\Support\Str::snake($entity)}}_request');
4141

4242
@if (!$withAuth)
4343
$response = $this->json('post', '/{{$entities}}', $data);
@@ -48,16 +48,16 @@ public function testCreate()
4848
$response->assertCreated();
4949

5050
// TODO: Need to remove last argument after first successful start
51-
$this->assertEqualsFixture('create_{{\Illuminate\Support\Str::snake($entity)}}_response.json', $response->json(), true);
51+
$this->assertEqualsFixture('create_{{\Illuminate\Support\Str::snake($entity)}}_response', $response->json(), true);
5252

5353
// TODO: Need to remove last argument after first successful start
54-
self::${{\Illuminate\Support\Str::camel($entity)}}State->assertChangesEqualsFixture('create_{{\Illuminate\Support\Str::snake($entity)}}_state.json', true);
54+
self::${{\Illuminate\Support\Str::camel($entity)}}State->assertChangesEqualsFixture('create_{{\Illuminate\Support\Str::snake($entity)}}_state', true);
5555
}
5656

5757
@if ($withAuth)
5858
public function testCreateNoAuth()
5959
{
60-
$data = $this->getJsonFixture('create_{{\Illuminate\Support\Str::snake($entity)}}_request.json');
60+
$data = $this->getJsonFixture('create_{{\Illuminate\Support\Str::snake($entity)}}_request');
6161

6262
$response = $this->json('post', '/{{$entities}}', $data);
6363

@@ -69,7 +69,7 @@ public function testCreateNoAuth()
6969
@if (in_array('U', $options))
7070
public function testUpdate()
7171
{
72-
$data = $this->getJsonFixture('update_{{\Illuminate\Support\Str::snake($entity)}}_request.json');
72+
$data = $this->getJsonFixture('update_{{\Illuminate\Support\Str::snake($entity)}}_request');
7373

7474
@if (!$withAuth)
7575
$response = $this->json('put', '/{{$entities}}/1', $data);
@@ -80,12 +80,12 @@ public function testUpdate()
8080
$response->assertNoContent();
8181

8282
// TODO: Need to remove last argument after first successful start
83-
self::${{\Illuminate\Support\Str::camel($entity)}}State->assertChangesEqualsFixture('update_{{\Illuminate\Support\Str::snake($entity)}}_state.json', true);
83+
self::${{\Illuminate\Support\Str::camel($entity)}}State->assertChangesEqualsFixture('update_{{\Illuminate\Support\Str::snake($entity)}}_state', true);
8484
}
8585

8686
public function testUpdateNotExists()
8787
{
88-
$data = $this->getJsonFixture('update_{{\Illuminate\Support\Str::snake($entity)}}_request.json');
88+
$data = $this->getJsonFixture('update_{{\Illuminate\Support\Str::snake($entity)}}_request');
8989

9090
@if (!$withAuth)
9191
$response = $this->json('put', '/{{$entities}}/0', $data);
@@ -101,7 +101,7 @@ public function testUpdateNotExists()
101101
@if ($withAuth)
102102
public function testUpdateNoAuth()
103103
{
104-
$data = $this->getJsonFixture('update_{{\Illuminate\Support\Str::snake($entity)}}_request.json');
104+
$data = $this->getJsonFixture('update_{{\Illuminate\Support\Str::snake($entity)}}_request');
105105

106106
$response = $this->json('put', '/{{$entities}}/1', $data);
107107

@@ -124,7 +124,7 @@ public function testDelete()
124124
$response->assertNoContent();
125125

126126
// TODO: Need to remove last argument after first successful start
127-
self::${{\Illuminate\Support\Str::camel($entity)}}State->assertChangesEqualsFixture('delete_{{\Illuminate\Support\Str::snake($entity)}}_state.json', true);
127+
self::${{\Illuminate\Support\Str::camel($entity)}}State->assertChangesEqualsFixture('delete_{{\Illuminate\Support\Str::snake($entity)}}_state', true);
128128
}
129129

130130
public function testDeleteNotExists()
@@ -164,7 +164,7 @@ public function testGet()
164164
// TODO: Need to remove after first successful start
165165
$this->exportJson('get_{{\Illuminate\Support\Str::snake($entity)}}.json', $response->json());
166166

167-
$this->assertEqualsFixture('get_{{\Illuminate\Support\Str::snake($entity)}}.json', $response->json());
167+
$this->assertEqualsFixture('get_{{\Illuminate\Support\Str::snake($entity)}}', $response->json());
168168
}
169169

170170
public function testGetNotExists()

tests/CommandTest.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
use Carbon\Carbon;
66
use Illuminate\Support\Facades\Config;
7-
use org\bovigo\vfs\vfsStream;
8-
use org\bovigo\vfs\vfsStreamFile;
97
use RonasIT\Support\Exceptions\ClassNotExistsException;
108
use RonasIT\Support\Tests\Support\Command\CommandMockTrait;
119
use UnexpectedValueException;
@@ -18,9 +16,7 @@ public function setUp(): void
1816
{
1917
parent::setUp();
2018

21-
vfsStream::newDirectory('routes')
22-
->at($this->rootDirectory)
23-
->addChild(new vfsStreamFile('api.php'));
19+
$this->mockFilesystem();
2420
}
2521

2622
public function testCallWithInvalidCrudOption()
@@ -52,7 +48,6 @@ public function testCallCommand()
5248

5349
Carbon::setTestNow('2016-10-20 11:05:00');
5450

55-
$this->mockFilesystem();
5651
$this->mockGenerator();
5752
$this->mockGettingModelInstance();
5853

@@ -143,6 +138,6 @@ public function testCallWithNotDefaultConfig()
143138

144139
$this->assertFileExists($configPath);
145140

146-
$this->assertEqualsFixture('changed_config.json', $updated);
141+
$this->assertEqualsFixture('changed_config', $updated);
147142
}
148143
}

tests/ControllerGeneratorTest.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ class ControllerGeneratorTest extends TestCase
1515
{
1616
use ControllerGeneratorMockTrait;
1717

18+
public function setUp(): void
19+
{
20+
parent::setUp();
21+
22+
$this->mockFilesystem();
23+
}
24+
1825
public function testControllerAlreadyExists()
1926
{
2027
$this->mockClass(ControllerGenerator::class, [
@@ -65,8 +72,6 @@ className: FileNotFoundException::class,
6572

6673
public function testControllerStubNotExist()
6774
{
68-
$this->mockFilesystem();
69-
7075
View::shouldReceive('exists')
7176
->with('entity-generator::controller')
7277
->once()
@@ -88,8 +93,6 @@ className: WarningEvent::class,
8893

8994
public function testRoutesStubNotExist()
9095
{
91-
$this->mockFilesystem();
92-
9396
config(['entity-generator.stubs.routes' => 'incorrect_stub']);
9497

9598
app(ControllerGenerator::class)
@@ -108,8 +111,6 @@ public function testRoutesStubNotExist()
108111

109112
public function testUseRoutesStubNotExist()
110113
{
111-
$this->mockFilesystem();
112-
113114
config(['entity-generator.stubs.use_routes' => 'incorrect_stub']);
114115

115116
app(ControllerGenerator::class)
@@ -128,8 +129,6 @@ public function testUseRoutesStubNotExist()
128129

129130
public function testSuccess()
130131
{
131-
$this->mockFilesystem();
132-
133132
app(ControllerGenerator::class)
134133
->setModel('Post')
135134
->setCrudOptions(['C', 'R', 'U', 'D'])

tests/FactoryGeneratorTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,17 @@ class FactoryGeneratorTest extends TestCase
1818
{
1919
use FactoryMockTrait;
2020

21+
public function setUp(): void
22+
{
23+
parent::setUp();
24+
25+
$this->mockFilesystem();
26+
}
27+
2128
public function testModelNotExists()
2229
{
30+
$this->mockFileSystemWithoutPostModel();
31+
2332
$this->assertExceptionThrew(
2433
className: ClassNotExistsException::class,
2534
message: "Cannot create PostFactory cause Post Model does not exists. "
@@ -54,8 +63,6 @@ className: ClassAlreadyExistsException::class,
5463

5564
public function testProcessUnknownFieldType()
5665
{
57-
$this->mockFilesystem();
58-
5966
$this->assertExceptionThrew(
6067
className: ViewException::class,
6168
message: "Cannot generate fake data for unsupported another_type field type. "
@@ -73,8 +80,6 @@ className: ViewException::class,
7380

7481
public function testCreateSuccess()
7582
{
76-
$this->mockFilesystem();
77-
7883
app(FactoryGenerator::class)
7984
->setFields([
8085
'integer-required' => ['author_id'],
@@ -98,8 +103,6 @@ className: SuccessCreateMessage::class,
98103

99104
public function testCreateFactoryWithoutFactoryStub(): void
100105
{
101-
$this->mockFilesystem();
102-
103106
config(['entity-generator.stubs.factory' => 'incorrect_stub']);
104107

105108
app(FactoryGenerator::class)

tests/MigrationGeneratorTest.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ className: UnknownFieldTypeException::class,
2929

3030
public function testCreateMigration()
3131
{
32-
Carbon::setTestNow('2022-02-02');
33-
3432
app(MigrationGenerator::class)
3533
->setModel('Post')
3634
->setRelations(new RelationsDTO())
@@ -49,8 +47,6 @@ public function testCreateMigrationMYSQL()
4947
{
5048
putenv('DB_CONNECTION=mysql');
5149

52-
Carbon::setTestNow('2022-02-02');
53-
5450
app(MigrationGenerator::class)
5551
->setModel('Post')
5652
->setRelations(new RelationsDTO())
@@ -68,8 +64,6 @@ public function testCreateMigrationMYSQL()
6864

6965
public function testCreateMigrationWithoutMigrationStub(): void
7066
{
71-
Carbon::setTestNow('2022-02-02');
72-
7367
config(['entity-generator.stubs.migration' => 'incorrect_stub']);
7468

7569
app(MigrationGenerator::class)

tests/ModelGeneratorTest.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ class ModelGeneratorTest extends TestCase
1414
{
1515
use ModelMockTrait;
1616

17+
public function setUp(): void
18+
{
19+
parent::setUp();
20+
21+
$this->mockFilesystem();
22+
}
23+
1724
public function testModelAlreadyExists()
1825
{
1926
$this->mockClass(ModelGenerator::class, [
@@ -32,6 +39,8 @@ className: ClassAlreadyExistsException::class,
3239

3340
public function testRelationModelMissing()
3441
{
42+
$this->mockFileSystemWithoutCommentModel();
43+
3544
$this->assertExceptionThrew(
3645
className: ClassNotExistsException::class,
3746
message: "Cannot create Post Model cause relation model Comment does not exist. "
@@ -48,8 +57,6 @@ className: ClassNotExistsException::class,
4857

4958
public function testCreateModel()
5059
{
51-
$this->mockFilesystem();
52-
5360
app(ModelGenerator::class)
5461
->setModel('Post')
5562
->setFields([
@@ -74,8 +81,6 @@ className: SuccessCreateMessage::class,
7481

7582
public function testCreateModelStubNotExist()
7683
{
77-
$this->mockFilesystem();
78-
7984
config(['entity-generator.stubs.model' => 'incorrect_stub']);
8085

8186
app(ModelGenerator::class)
@@ -95,8 +100,6 @@ className: WarningEvent::class,
95100

96101
public function testCreateModelWithoutRelationsRelationStubNotExist()
97102
{
98-
$this->mockFilesystem();
99-
100103
config(['entity-generator.stubs.relation' => 'incorrect_stub']);
101104

102105
app(ModelGenerator::class)
@@ -115,8 +118,6 @@ className: SuccessCreateMessage::class,
115118

116119
public function testCreateModelWithRelationsRelationStubNotExist()
117120
{
118-
$this->mockFilesystem();
119-
120121
config(['entity-generator.stubs.relation' => 'incorrect_stub']);
121122

122123
app(ModelGenerator::class)

0 commit comments

Comments
 (0)