Lumen 5.4:PHPUnit:如何测试授权?
Lumen 5.4: PHPUnit: How to test authorisation?
我正在从事一个电子商务项目,一家普通书店。
我从测试驱动的方法开始,直到现在我完全坚持它。
这个 Lumen 微服务项目的不同端点已经在早期成功测试,以确保它们进行 CRUD 操作。但是,由于我必须使用令牌授权来保护创建、更新和删除方法,所以我很困惑如何引入授权测试。
到目前为止,这是我的测试结构:
tests/app/Exceptions/HandlerTest.php
tests/app/Http/Controllers/BooksControllerTest.php
测试用于索引、显示、存储、更新、删除。这是测试之一:
public function testStoreBookByPost()
{
$book = factory('App\Book')->make();
$this->post(
'/books',
[
'isbn' => $book->isbn,
'title' => $book->title,
'description' => $book->description,
'author' => $book->author,
'image' => $book->image,
'price' => $book->price,
'slug' => $book->slug
]
);
$this
->seeJson(
[
'created' => true
]
)
->seeInDatabase(
'books',
[
'title' => $book->title
]
);
}
我之前已经分开了异常处理程序测试,同样我更愿意将 AuthControllerTest
分开到 AuthControllerTest.php
。
最好的方法是什么?
是否需要通过重构所有 BooksControllerTest
来编写授权测试?
或者我应该只测试令牌的颁发和无法操作数据库?这样可以吗?
简短回答:我需要通过重构所有 BooksControllerTest
来编写授权测试
长答案:我发现了一种在测试期间登录虚拟用户的绝妙方法。
据此我创建了这个方法。
public function loginWithUserGetJWT()
{
$user = factory('App\User')->create(
[
'password' => bcrypt('366643') // random password
]
);
$content = $this
->post(
'/auth/login',
[
'email' => $user->email,
'password' => '366643'
]
)
->seeStatusCode(200)
->response->getContent();
$token = json_decode($content)->token;
return $token;
}
我在所有测试用例中重复使用这个方法,像这样:
public function testStoreBookByPost()
{
$token = $this->loginWithUserGetJWT();
$book = factory('App\Book')->make();
$this->post(
'/books',
[
'isbn' => $book->isbn,
'title' => $book->title,
'description' => $book->description,
'author' => $book->author,
'image' => $book->image,
'price' => $book->price,
'slug' => $book->slug,
'token' => $token
]
);
$this
->seeJson(
[
'created' => true
]
)
->seeInDatabase(
'books',
[
'title' => $book->title
]
);
}
我正在从事一个电子商务项目,一家普通书店。
我从测试驱动的方法开始,直到现在我完全坚持它。
这个 Lumen 微服务项目的不同端点已经在早期成功测试,以确保它们进行 CRUD 操作。但是,由于我必须使用令牌授权来保护创建、更新和删除方法,所以我很困惑如何引入授权测试。
到目前为止,这是我的测试结构:
tests/app/Exceptions/HandlerTest.php
tests/app/Http/Controllers/BooksControllerTest.php
测试用于索引、显示、存储、更新、删除。这是测试之一:
public function testStoreBookByPost()
{
$book = factory('App\Book')->make();
$this->post(
'/books',
[
'isbn' => $book->isbn,
'title' => $book->title,
'description' => $book->description,
'author' => $book->author,
'image' => $book->image,
'price' => $book->price,
'slug' => $book->slug
]
);
$this
->seeJson(
[
'created' => true
]
)
->seeInDatabase(
'books',
[
'title' => $book->title
]
);
}
我之前已经分开了异常处理程序测试,同样我更愿意将 AuthControllerTest
分开到 AuthControllerTest.php
。
最好的方法是什么?
是否需要通过重构所有 BooksControllerTest
来编写授权测试?
或者我应该只测试令牌的颁发和无法操作数据库?这样可以吗?
简短回答:我需要通过重构所有 BooksControllerTest
长答案:我发现了一种在测试期间登录虚拟用户的绝妙方法。
据此我创建了这个方法。
public function loginWithUserGetJWT()
{
$user = factory('App\User')->create(
[
'password' => bcrypt('366643') // random password
]
);
$content = $this
->post(
'/auth/login',
[
'email' => $user->email,
'password' => '366643'
]
)
->seeStatusCode(200)
->response->getContent();
$token = json_decode($content)->token;
return $token;
}
我在所有测试用例中重复使用这个方法,像这样:
public function testStoreBookByPost()
{
$token = $this->loginWithUserGetJWT();
$book = factory('App\Book')->make();
$this->post(
'/books',
[
'isbn' => $book->isbn,
'title' => $book->title,
'description' => $book->description,
'author' => $book->author,
'image' => $book->image,
'price' => $book->price,
'slug' => $book->slug,
'token' => $token
]
);
$this
->seeJson(
[
'created' => true
]
)
->seeInDatabase(
'books',
[
'title' => $book->title
]
);
}