1. 配置代码格式管理工具:
composer require squizlabs/php_codesniffer, 使用 php artisan cs 检查代码格式;
2.单元测试用例编写
1./database/factories/ModelFactory.php 中配置每个表的模拟数据,例:
$factory->define(AppModelsTask::class, function (FakerGenerator $faker) {
return [
'description' => $faker->streetAddress,
'type' => $faker->randomElement(AppModelsOrder::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,其他的测试用例都继承这里,可以在这里配置统一的校验,统一响应格式,例,
<?php use LaravelLumenTestingDatabaseMigrations; use LaravelLumenTestingDatabaseTransactions; abstract class TestCase extends LaravelLumenTestingTestCase { use DatabaseTransactions; /** * Creates the application. * * @return LaravelLumenApplication */ 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('AppUser')->make(); return $this->actingAs($user); } public function signatureJson($uri, $data = [], $method = 'GET', $user = null, $header = ['Authorization' => 'c4ca4238a0b923820dcc509a6f75849b']) { if (empty($user)) { factory(AppUser::class)->create(['authorization' => array_get($header, 'Authorization', '')]); } if (is_array($data) || is_string($data)) { $timestamp = CarbonCarbon::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.用例的编写,例:
<?php class TaskAPITest extends TestCase { public function testTaskListSuccess() { $apps = factory(AppModelsApplication::class, 8)->create(); foreach ($apps as $app) { $task = factory(AppModelsTask::class)->create(['app_id' => $app->id]); factory(AppModelsTaskDetail::class)->create([ 'task_id' => $task->id, 'step' => AppModelsTaskDetail::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(AppModelsApplication::class)->create(); $task = factory(AppModelsTask::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(AppModelsApplication::class)->create(); $task = factory(AppModelsTask::class)->create(['app_id' => $app->id]); $authorization = str_random(32); $user = factory(AppUser::class)->create(['authorization' => $authorization]); factory(AppModelsTaskDetail::class)->create([ 'task_id' => $task->id, 'user_id' => $user->id, 'status' => AppModelsTaskDetail::TASK_INITIAL ]); $this->expectsEvents(AppEventsTaskGetEvent::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' => AppModelsTaskDetail::TASK_INITIAL, 'step' => AppModelsTaskDetail::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(AppModelsTaskError::class)->make(); $app = factory(AppModelsApplication::class)->create(); $task = factory(AppModelsTask::class)->create(['app_id' => $app->id]); $user = factory(AppUser::class)->create(['authorization' => $authorization]); factory(AppModelsTaskDetail::class)->create([ 'task_id' => $task->id, 'user_id' => $user->id, 'status' => AppModelsTaskDetail::WORKING, 'step' => AppModelsTaskDetail::WORKING ]); $this->expectsEvents(AppEventsTaskErrorEvent::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' => AppModelsTaskDetail::INITIAL, ]); $this->seeJsonContains([ 'code' => 1, 'message' => 'success' ]); } public function testTaskDoneSuccess() { $authorization = str_random(32); $user = factory(AppUser::class)->create(['authorization' => $authorization]); $app = factory(AppModelsApplication::class)->create(); $task = factory(AppModelsTask::class)->create(['app_id' => $app->id]); factory(AppModelsTaskDetail::class)->create([ 'task_id' => $task->id, 'user_id' => $user->id, 'step' => AppModelsTaskDetail::WORKING, 'process' => 3, 'valid_status' => 1 ]); $this->expectsEvents(AppEventsTaskDoneEvent::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' => AppModelsTaskDetail::COMPLETED ]); $this->seeJsonContains([ 'code' => 1, 'message' => 'success' ]); } public function testTaskDoneFailure() { $authorization = str_random(32); $user = factory(AppUser::class)->create(['authorization' => $authorization]); $task = factory(AppModelsTask::class)->create(); factory(AppModelsTaskDetail::class)->create([ 'task_id' => $task->id, 'user_id' => $user->id, ]); $this->expectsEvents(AppEventsTaskDoneEvent::class); $this->signatureJson('api/v1/task/complete', ['taskId' => $task->id], 'POST', $user, ['Authorization' => $authorization]); $this->assertResponseStatus(422); $this->seeJsonContains([ 'code' => AppExceptionsBusinessException::EX_STEP_NO_VALIDATION, 'message' => 'fail' ]); } public function testTaskCancelNotFoundFailure() { $authorization = str_random(32); $user = factory(AppUser::class)->create(['authorization' => $authorization]); $app = factory(AppModelsApplication::class)->create(); $task = factory(AppModelsTask::class)->create(['app_id' => $app->id]); factory(AppModelsTaskDetail::class)->create([ 'task_id' => $task->id, 'user_id' => $user->id + 1, 'step' => AppModelsTaskDetail::WORKING ]); $this->expectsEvents(AppEventsTaskCancelEvent::class); $this->signatureJson('api/v1/task/giveUp', ['taskId' => $task->id], 'POST', $user, ['Authorization' => $authorization]); $this->assertResponseStatus(422); $this->seeJsonContains([ 'code' => AppExceptionsBusinessException::EX_TASK_INFO_EMPTY, 'message' => 'fail' ]); } public function testTaskCancelNoPermissionSuccess() { $authorization = str_random(32); $user = factory(AppUser::class)->create(['authorization' => $authorization]); $task = factory(AppModelsTask::class)->create(); factory(AppModelsTaskDetail::class)->create([ 'task_id' => $task->id, 'user_id' => $user->id, 'status' => AppModelsTaskDetail::INITIAL ]); $this->expectsEvents(AppEventsTaskCancelEvent::class); $this->signatureJson('api/v1/task/giveUp', ['taskId' => $task->id], 'POST', $user, ['Authorization' => $authorization]); $this->assertResponseStatus(422); $this->seeJsonContains([ 'code' => AppExceptionsBusinessException::EX_STEP_NO_PERMISSION, 'message' => 'fail' ]); } public function testTaskCancelSuccess() { $authorization = str_random(32); $user = factory(AppUser::class)->create(['authorization' => $authorization]); $app = factory(AppModelsApplication::class)->create(); $task = factory(AppModelsTask::class)->create(['app_id' => $app->id]); factory(AppModelsTaskDetail::class)->create([ 'task_id' => $task->id, 'user_id' => $user->id, 'step' => AppModelsTaskDetail::WORKING ]); $this->expectsEvents(AppEventsTaskCancelEvent::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(AppModelsTaskReport::class)->make(); $user = factory(AppUser::class)->create(['authorization' => $authorization]); $app = factory(AppModelsApplication::class)->create(); $task = factory(AppModelsTask::class)->create(['app_id' => $app->id]); factory(AppModelsTaskDetail::class)->create([ 'task_id' => $task->id, 'user_id' => $user->id, 'step' => AppModelsTaskDetail::WORKING ]); $this->expectsEvents(AppEventsTaskReportEvent::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(AppModelsTask::class)->create(); $user = factory(AppUser::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