lumen 框架的特殊使用
1. 配置代码格式管理工具:
composer require squizlabs/php_codesniffer, 使用 php artisan cs 检查代码格式;
2.单元测试用例编写
1./database/factories/ModelFactory.php 中配置每个表的模拟数据,例:
$factory->define(App\Models\Task::class, function (Faker\Generator $faker) {
return [
'description' => $faker->streetAddress,
'type' => $faker->randomElement(App\Models\Order::ORDER_TYPE),
'app_id' => $faker->randomNumber(),
'status' => rand(1, 5),
'keyword' => str_random(5),
'credit' => $faker->randomElement([508, 608, 708]),
'total' => 100,
'release' => 10,
'done' => 10,
'release_count' => 1,
'toke_at' => $faker->dateTime()->format('Y-m-d H:i:s'),
'end_at' => $faker->dateTime()->format('Y-m-d H:i:s'),
];
});
2./tests 中配置一个基础的 TestCase.php,其他的测试用例都继承这里,可以在这里配置统一的校验,统一响应格式,例,
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?php use Laravel\Lumen\Testing\DatabaseMigrations; use Laravel\Lumen\Testing\DatabaseTransactions; abstract class TestCase extends \Laravel\Lumen\Testing\TestCase { use DatabaseTransactions; /** * Creates the application. * * @return \Laravel\Lumen\Application */ public function createApplication() { return require __DIR__ . '/../bootstrap/app.php'; } public function getData() { $this->assertResponseOk(); return @json_decode($this->response->content(), true); } public function dump() { $content = $this->response->content(); $data = @json_encode(@json_decode($content), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); if (empty($data) || $data === 'null') { dd($content); } echo ($data) . PHP_EOL; die(); } public function auth() { $user = factory('App\User')->make(); return $this->actingAs($user); } public function signatureJson($uri, $data = [], $method = 'GET', $user = null, $header = ['Authorization' => 'c4ca4238a0b923820dcc509a6f75849b']) { if (empty($user)) { factory(App\User::class)->create(['authorization' => array_get($header, 'Authorization', '')]); } if (is_array($data) || is_string($data)) { $timestamp = \Carbon\Carbon::now()->getTimestamp(); if (is_string($data)) { $data = @json_decode($data); } $data['timestamp'] = $timestamp; ksort($data); $signature = sha1(config('signature.key') . array_get($header, 'Authorization', '') . http_build_query($data)); $data['signature'] = $signature; return $this->json($method, $uri, $data, $header); } $this->assertEquals(1, 0, 'params data invalid.'); } }
3.用例的编写,例:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?php class TaskAPITest extends TestCase { public function testTaskListSuccess() { $apps = factory(App\Models\Application::class, 8)->create(); foreach ($apps as $app) { $task = factory(App\Models\Task::class)->create(['app_id' => $app->id]); factory(App\Models\TaskDetail::class)->create([ 'task_id' => $task->id, 'step' => \App\Models\TaskDetail::TASK_INITIAL, 'user_id' => 0 ]); } $this->signatureJson('api/v1/task/list', [ 'type' => 1, 'page' => 1, 'limit' => 10 ]); $this->seeJsonContains([ 'code' => 1, 'message' => 'success', ]); $data = $this->getData(); $this->assertEquals(8, array_get($data, 'data.total')); } public function testTaskDetailSuccess() { $app = factory(App\Models\Application::class)->create(); $task = factory(App\Models\Task::class)->create(['app_id' => $app->id]); $this->signatureJson('api/v1/task/info', ['taskId' => $task->id]); $data = $this->getData(); $this->assertEquals($task->id, array_get($data, 'data.id')); $this->seeJsonContains([ 'code' => 1, 'message' => 'success' ]); } public function testTaskGotDetailSuccess() { $app = factory(App\Models\Application::class)->create(); $task = factory(App\Models\Task::class)->create(['app_id' => $app->id]); $authorization = str_random(32); $user = factory(App\User::class)->create(['authorization' => $authorization]); factory(App\Models\TaskDetail::class)->create([ 'task_id' => $task->id, 'user_id' => $user->id, 'status' => \App\Models\TaskDetail::TASK_INITIAL ]); $this->expectsEvents(\App\Events\TaskGetEvent::class); $this->signatureJson('api/v1/task/draw', ['taskId' => $task->id], 'POST', $user, ['Authorization' => $authorization]); $this->signatureJson('api/v1/task/info', ['taskId' => $task->id]); $this->seeInDatabase('task_details', [ 'task_id' => $task->id, 'user_id' => $user->id, 'status' => \App\Models\TaskDetail::TASK_INITIAL, 'step' => \App\Models\TaskDetail::WORKING ]); $data = $this->getData(); $this->assertEquals(2, array_get($data, 'data.step')); $this->assertEquals($task->id, array_get($data, 'data.id')); $this->seeJsonContains([ 'code' => 1, 'message' => 'success' ]); } public function testTaskErrorSuccess() { $authorization = str_random(32); $taskError = factory(App\Models\TaskError::class)->make(); $app = factory(App\Models\Application::class)->create(); $task = factory(App\Models\Task::class)->create(['app_id' => $app->id]); $user = factory(App\User::class)->create(['authorization' => $authorization]); factory(App\Models\TaskDetail::class)->create([ 'task_id' => $task->id, 'user_id' => $user->id, 'status' => \App\Models\TaskDetail::WORKING, 'step' => \App\Models\TaskDetail::WORKING ]); $this->expectsEvents(\App\Events\TaskErrorEvent::class); $taskError->taskId = $task->id; $taskError = $taskError->toArray(); $this->signatureJson('api/v1/task/error', $taskError, 'POST', $user, ['Authorization' => $authorization]); $this->seeInDatabase('task_errors', [ 'task_id' => $task->id ]); $this->seeInDatabase('task_details', [ 'task_id' => $task->id, 'user_id' => $user->id, 'step' => \App\Models\TaskDetail::INITIAL, ]); $this->seeJsonContains([ 'code' => 1, 'message' => 'success' ]); } public function testTaskDoneSuccess() { $authorization = str_random(32); $user = factory(App\User::class)->create(['authorization' => $authorization]); $app = factory(App\Models\Application::class)->create(); $task = factory(App\Models\Task::class)->create(['app_id' => $app->id]); factory(App\Models\TaskDetail::class)->create([ 'task_id' => $task->id, 'user_id' => $user->id, 'step' => \App\Models\TaskDetail::WORKING, 'process' => 3, 'valid_status' => 1 ]); $this->expectsEvents(\App\Events\TaskDoneEvent::class); $this->signatureJson('api/v1/task/complete', ['taskId' => $task->id], 'POST', $user, ['Authorization' => $authorization]); $this->seeInDatabase('task_details', [ 'task_id' => $task->id, 'user_id' => $user->id, 'step' => \App\Models\TaskDetail::COMPLETED ]); $this->seeJsonContains([ 'code' => 1, 'message' => 'success' ]); } public function testTaskDoneFailure() { $authorization = str_random(32); $user = factory(App\User::class)->create(['authorization' => $authorization]); $task = factory(App\Models\Task::class)->create(); factory(App\Models\TaskDetail::class)->create([ 'task_id' => $task->id, 'user_id' => $user->id, ]); $this->expectsEvents(\App\Events\TaskDoneEvent::class); $this->signatureJson('api/v1/task/complete', ['taskId' => $task->id], 'POST', $user, ['Authorization' => $authorization]); $this->assertResponseStatus(422); $this->seeJsonContains([ 'code' => \App\Exceptions\BusinessException::EX_STEP_NO_VALIDATION, 'message' => 'fail' ]); } public function testTaskCancelNotFoundFailure() { $authorization = str_random(32); $user = factory(App\User::class)->create(['authorization' => $authorization]); $app = factory(App\Models\Application::class)->create(); $task = factory(App\Models\Task::class)->create(['app_id' => $app->id]); factory(App\Models\TaskDetail::class)->create([ 'task_id' => $task->id, 'user_id' => $user->id + 1, 'step' => \App\Models\TaskDetail::WORKING ]); $this->expectsEvents(\App\Events\TaskCancelEvent::class); $this->signatureJson('api/v1/task/giveUp', ['taskId' => $task->id], 'POST', $user, ['Authorization' => $authorization]); $this->assertResponseStatus(422); $this->seeJsonContains([ 'code' => \App\Exceptions\BusinessException::EX_TASK_INFO_EMPTY, 'message' => 'fail' ]); } public function testTaskCancelNoPermissionSuccess() { $authorization = str_random(32); $user = factory(App\User::class)->create(['authorization' => $authorization]); $task = factory(App\Models\Task::class)->create(); factory(App\Models\TaskDetail::class)->create([ 'task_id' => $task->id, 'user_id' => $user->id, 'status' => \App\Models\TaskDetail::INITIAL ]); $this->expectsEvents(\App\Events\TaskCancelEvent::class); $this->signatureJson('api/v1/task/giveUp', ['taskId' => $task->id], 'POST', $user, ['Authorization' => $authorization]); $this->assertResponseStatus(422); $this->seeJsonContains([ 'code' => \App\Exceptions\BusinessException::EX_STEP_NO_PERMISSION, 'message' => 'fail' ]); } public function testTaskCancelSuccess() { $authorization = str_random(32); $user = factory(App\User::class)->create(['authorization' => $authorization]); $app = factory(App\Models\Application::class)->create(); $task = factory(App\Models\Task::class)->create(['app_id' => $app->id]); factory(App\Models\TaskDetail::class)->create([ 'task_id' => $task->id, 'user_id' => $user->id, 'step' => \App\Models\TaskDetail::WORKING ]); $this->expectsEvents(\App\Events\TaskCancelEvent::class); $this->signatureJson('api/v1/task/giveUp', ['taskId' => $task->id], 'POST', $user, ['Authorization' => $authorization]); $this->notSeeInDatabase('task_details', [ 'task_id' => $task->id, 'user_id' => $user->id, ]); $this->seeJsonContains([ 'code' => 1, 'message' => 'success' ]); } public function testTaskReportSuccess() { $authorization = str_random(32); $taskReport = factory(App\Models\TaskReport::class)->make(); $user = factory(App\User::class)->create(['authorization' => $authorization]); $app = factory(App\Models\Application::class)->create(); $task = factory(App\Models\Task::class)->create(['app_id' => $app->id]); factory(App\Models\TaskDetail::class)->create([ 'task_id' => $task->id, 'user_id' => $user->id, 'step' => \App\Models\TaskDetail::WORKING ]); $this->expectsEvents(\App\Events\TaskReportEvent::class); $taskReport->taskId = $task->id; $taskReport = $taskReport->toArray(); unset($taskReport['id']); unset($taskReport['task_id']); $this->signatureJson('api/v1/task/report', $taskReport, 'POST', $user, ['Authorization' => $authorization]); $this->seeInDatabase('task_reports', [ 'task_id' => $task->id ]); $this->seeJsonContains([ 'code' => 1, 'message' => 'success' ]); } public function testUserValidateFailure() { $authorization = str_random(32); $task = factory(App\Models\Task::class)->create(); $user = factory(App\User::class)->create(['authorization' => $authorization]); $this->signatureJson('api/v1/task/draw', ['taskId' => $task->id], 'POST', $user, ['Authorization' => '1c4ca4238a0b923820dcc509a6f75849b']); $this->seeJsonContains([ 'code' => 0, 'message' => 'fail', "errors" => [ "Unauthorized" ] ]); } }
4.命令行执行 php artisan unit