博客系统采用Laravel Resource Controller,默认方法:index、show、edit、update、destory、create、store
测试主要围绕以上方法
class BlogController extends Controller
{
public function __construct()
{
$this->middleware('auth', ['except' => ['index', 'show']]);
}
/**
* 获取博客列表页.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$blogs = Blog::orderBy('created_at', 'desc')
->paginate(15);
return view('blog.index')
->withBlogs($blogs);
}
/**
* 进入发布博客页.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('blog.create');
}
/**
* 博客数据插入.
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$data = $request->all();
$blog = new Blog();
$blog->forceFill([
'user_id' => Auth::id(),
'title' => $data['title'],
'body' => $data['body'],
'category_id' => $data['category_id'],
'status' => $data['status'],
])->save();
return redirect()->to('/blog/'.$blog->id);
}
/**
* 博客详情页.
*
* @param int $id
*
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$blog = Blog::with('user')
->find($id);
return view('blog.show')
->withBlog($blog);
}
/**
* 进入修改博客页面.
*
* @param int $id
*
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$user_id = Auth::id();
$blog = Blog::find($id);
if ($blog->user_id == $user_id) {
return view('blog.edit')
->withBlog($blog);
}
}
/**
* 更新博客.
*
* @param \Illuminate\Http\Request $request
* @param int $id
*
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$data = $request->all();
$blog = Blog::findOrFail($id);
$user_id = Auth::id();
if ($user_id == $blog->user_id) {
$blog->forceFill([
'title' => $data['title'],
'body' => $data['body'],
'status' => $data['status'],
'category_id' => $data['category_id'],
])->save();
return redirect()->to('/blog/'.$blog->id);
}
}
/**
* 软删除博客.
*
* @param int $id
*
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$blog = Blog::find($id);
if (Auth::id() == $blog->user_id) {
$blog->delete();
return redirect()->to('/blog');
}
}
}
Controller采用Auth中间件判断用户是否登录,并将详情(show),列表(index)排除在外
采用Factory和Faker\Generator填充数据,代码如下:
// 博客
$factory->define(Blog::class, function (Faker $faker) {
return [
'user_id' => function () {
return factory(User::class)->create()->id;
},
'title' => $faker->text($maxNbChars = 20),
'body' => $faker->text($maxNbChars = 5000),
'category_id' => 1,
];
});
// 分类
$factory->define(Category::class, function (Faker $faker) {
return [
'user_id' => function () {
return factory(User::class)->create()->id;
},
'name' => $faker->text($maxNbChars = 5),
'description' => $faker->text($maxNbChars = 30),
];
});
Fake可填充一些随机数据,不过默认是纯英文的。但它有中文拓展,只要进行简单引用就可以使用中文
本文章主要讲测试,所以每个方法单独分析
测试中,所有数据采用随机,如不存在,则用factor创建
public function testShowBlogList()
{
$response = $this->get('/blog');
$blogs = Blog::orderBy('created_at', 'desc')
->first()
?: factory(User::class)->create();
$response->assertStatus(200)
->assertSeeText($blogs->title);
}
判断状态码是否200,并且页面中存在已知会查询出来的博客标题
public function testCreateBlog()
{
$user = User::inRandomOrder()
->first() ?: factory(User::class)->create();
$category = Category::inRandomOrder()
->first() ?: factory(Category::class)->create();
$faker = \Faker\Factory::create();
$title = $faker->title;
$body = $faker->text($maxNbChars = 15000);
$response = $this->actingAs($user)->post('/blog', [
'title' => $title,
'body' => $body,
'category_id' => $category->id,
'status' => $faker->randomElement([0, 1]),
]);
$response->assertStatus(302);
}
这里采用actingAs方法模拟用户登录状态,并且随机分类,由于最后插入博客后是个重定向,故返回码为302
public function testShowBlog()
{
//获取随机已存在ID
$blog = Blog::inRandomOrder()->first() ?:
factory(Blog::class, 100)->create()->Random();
//用户名
$userName = $blog->user->name;
$id = $blog->id;
$response = $this->get('/blog/'.$id);
$response->assertStatus(200)
->assertSeeText($userName); //存在用户名
}
public function testUpdateBlog()
{
//获取随机已存在ID
$blog = Blog::inRandomOrder()->first() ?:
factory(Blog::class, 100)->create()->Random();
$category = Category::inRandomOrder()
->first() ?: factory(Category::class)->create();
//用户
$user = $blog->user;
$faker = \Faker\Factory::create();
$title = $faker->title;
$body = $faker->text($maxNbChars = 15000);
$response = $this->actingAs($user)->put('/blog/'.$blog->id, [
'title' => $title,
'body' => $body,
'category_id' => $category->id,
'status' => $faker->randomElement([0, 1]),
]);
$response->assertStatus(302);
}
同样是302重定向
public function testDestoryBlog()
{
//获取随机已存在ID
$blog = Blog::inRandomOrder()->first() ?:
factory(Blog::class, 100)->create()->Random();
//用户
$user = $blog->user;
$response = $this->actingAs($user)
->delete('/blog/'.$blog->id);
$response->assertStatus(302);
}
Web测试没有API测试那么直观的RESTFul返回码,特别是Controller中存在大部分都是重定向,所以只能看到跳转页面了,很难测试页面数据是否正确