-
Notifications
You must be signed in to change notification settings - Fork 14
/
PostsTest.php
162 lines (124 loc) · 4.43 KB
/
PostsTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
namespace Tests\Feature\Common;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\File;
use Modules\MyBlog\Exports\Posts as Export;
use Modules\MyBlog\Jobs\CreatePost;
use Modules\MyBlog\Models\Post;
use Tests\Feature\FeatureTestCase;
class PostsTest extends FeatureTestCase
{
public function testItShouldSeePostListPage()
{
$this->loginAs()
->get(route('my-blog.posts.index'))
->assertStatus(200)
->assertSeeText(trans_choice('my-blog::general.posts', 2));
}
public function testItShouldSeePostCreatePage()
{
$this->loginAs()
->get(route('my-blog.posts.create'))
->assertStatus(200)
->assertSeeText(trans('general.title.new', ['type' => trans_choice('my-blog::general.posts', 1)]));
}
public function testItShouldCreatePost()
{
$request = $this->getRequest();
$this->loginAs()
->post(route('my-blog.posts.store'), $request)
->assertStatus(200);
$this->assertFlashLevel('success');
$this->assertDatabaseHas('my_blog_posts', $request);
}
public function testItShouldSeePostUpdatePage()
{
$request = $this->getRequest();
$post = $this->dispatch(new CreatePost($request));
$this->loginAs()
->get(route('my-blog.posts.edit', $post->id))
->assertStatus(200)
->assertSee($post->name);
}
public function testItShouldUpdatePost()
{
$request = $this->getRequest();
$post = $this->dispatch(new CreatePost($request));
$request['name'] = $this->faker->text(15);
$this->loginAs()
->patch(route('my-blog.posts.update', $post->id), $request)
->assertStatus(200)
->assertSee($request['name']);
$this->assertFlashLevel('success');
$this->assertDatabaseHas('my_blog_posts', $request);
}
public function testItShouldDeletePost()
{
$request = $this->getRequest();
$post = $this->dispatch(new CreatePost($request));
$this->loginAs()
->delete(route('my-blog.posts.destroy', $post->id))
->assertStatus(200);
$this->assertFlashLevel('success');
$this->assertSoftDeleted('my_blog_posts', $request);
}
public function testItShouldExportPosts()
{
$count = 5;
Post::factory()->count($count)->create();
\Excel::fake();
$this->loginAs()
->get(route('my-blog.posts.export'))
->assertStatus(200);
\Excel::matchByRegex();
\Excel::assertDownloaded(
'/' . \Str::filename(trans_choice('my-blog::general.posts', 2)) . '-\d{10}\.xlsx/',
function (Export $export) use ($count) {
// Assert that the correct export is downloaded.
return $export->collection()->count() === $count;
}
);
}
public function testItShouldExportSelectedPosts()
{
$create_count = 5;
$select_count = 3;
$posts = Post::factory()->count($create_count)->create();
\Excel::fake();
$this->loginAs()
->post(
route('bulk-actions.action', ['group' => 'my-blog', 'type' => 'posts']),
['handle' => 'export', 'selected' => $posts->take($select_count)->pluck('id')->toArray()]
)
->assertStatus(200);
\Excel::matchByRegex();
\Excel::assertDownloaded(
'/' . \Str::filename(trans_choice('my-blog::general.posts', 2)) . '-\d{10}\.xlsx/',
function (Export $export) use ($select_count) {
// Assert that the correct export is downloaded.
return $export->collection()->count() === $select_count;
}
);
}
public function testItShouldImportPosts()
{
\Excel::fake();
$this->loginAs()
->post(
route('my-blog.posts.import'),
[
'import' => UploadedFile::fake()->createWithContent(
'posts.xlsx',
File::get(module_path('my-blog', 'Resources/assets/posts.xlsx'))
),
]
)
->assertStatus(200);
\Excel::assertImported('posts.xlsx');
$this->assertFlashLevel('success');
}
public function getRequest()
{
return Post::factory()->enabled()->raw();
}
}