Skip to content

Commit a7f9b89

Browse files
author
Dan Jewett
committed
fix file controller test
1 parent f8d353f commit a7f9b89

File tree

2 files changed

+52
-16
lines changed

2 files changed

+52
-16
lines changed

src/FileBaseControllerTest.php

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,43 @@
88
use Illuminate\Foundation\Testing\DatabaseTransactions;
99

1010
use App\File;
11+
use App\User;
1112

1213
class FileBaseControllerTest extends TestCase
1314
{
1415
use RefreshDatabase;
1516

16-
protected $user_index;
17-
protected $user_index_error;
18-
protected $user_create;
19-
protected $user_update;
20-
protected $user_delete;
17+
protected function getUserForIndex()
18+
{
19+
return factory(User::class)->create();
20+
}
21+
22+
protected function getUserForIndexError()
23+
{
24+
return factory(User::class)->create();
25+
}
26+
27+
protected function getUserForCreate()
28+
{
29+
return factory(User::class)->create();
30+
}
31+
32+
protected function getUserForUpdate()
33+
{
34+
return factory(User::class)->create();
35+
}
36+
37+
protected function getUserForDelete()
38+
{
39+
return factory(User::class)->create();
40+
}
2141

2242
/** @test */
2343
public function it_checks_if_index_work()
2444
{
2545
$files = factory(File::class, 4)->create();
2646

27-
$response = $this->actingAs($this->user_index)->json('GET', route('api.files.index'));
47+
$response = $this->actingAs($this->getUserForIndex())->json('GET', route('api.files.index'));
2848

2949
$response->assertStatus(200);
3050
$response->assertJson([
@@ -42,7 +62,7 @@ public function it_checks_if_index_gives_error()
4262
{
4363
$files = factory(File::class, 4)->create();
4464

45-
$response = $this->actingAs($this->user_index_error)->json('GET', route('api.files.index'));
65+
$response = $this->actingAs($this->getUserForIndexError())->json('GET', route('api.files.index'));
4666

4767
$response->assertStatus(403);
4868
$response->assertJson([
@@ -56,7 +76,7 @@ public function it_checks_if_index_gives_error()
5676
/** @test */
5777
public function it_checks_create_works()
5878
{
59-
$response = $this->actingAs($this->user_create)->json('POST', route('api.files.store', [
79+
$response = $this->actingAs($this->getUserForCreate())->json('POST', route('api.files.store', [
6080
'file_name' => 'something cute.png',
6181
'file_type' => 'image/png',
6282
'file_data' => $this->get1x1RedPixelImage()
@@ -78,7 +98,7 @@ public function it_checks_update_works()
7898
{
7999
$file = factory(File::class)->create();
80100

81-
$response = $this->actingAs($this->user_update)->json('PUT', route('api.files.update', [
101+
$response = $this->actingAs($this->getUserForUpdate())->json('PUT', route('api.files.update', [
82102
'file' => $file->id,
83103
'file_name' => 'something cute.png',
84104
'file_type' => 'image/png',
@@ -101,7 +121,7 @@ public function it_checks_delete_works()
101121
{
102122
$file = factory(File::class)->create();
103123

104-
$response = $this->actingAs($this->user_delete)->json('DELETE', route('api.files.destroy', [
124+
$response = $this->actingAs($this->getUserForDelete())->json('DELETE', route('api.files.destroy', [
105125
'file' => $file->id
106126
]));
107127

templates/FileControllerTest.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,28 @@
88

99
class FileControllerTest extends FileBaseControllerTest
1010
{
11-
public function __construct()
11+
protected function getUserForIndex()
1212
{
13-
$this->user_index = factory(User::class)->create();
14-
$this->user_index_error = factory(User::class)->create();
15-
$this->user_create = factory(User::class)->create();
16-
$this->user_update = factory(User::class)->create();
17-
$this->user_delete = factory(User::class)->create();
13+
return factory(User::class)->states("superadmin")->create();
14+
}
15+
16+
protected function getUserForIndexError()
17+
{
18+
return factory(User::class)->states("user")->create();
19+
}
20+
21+
protected function getUserForCreate()
22+
{
23+
return factory(User::class)->states("superadmin")->create();
24+
}
25+
26+
protected function getUserForUpdate()
27+
{
28+
return factory(User::class)->states("superadmin")->create();
29+
}
30+
31+
protected function getUserForDelete()
32+
{
33+
return factory(User::class)->states("superadmin")->create();
1834
}
1935
}

0 commit comments

Comments
 (0)