#php #laravel #testing #phpunit
#php #ларавель #тестирование #phpunit
Вопрос:
Я получаю эту ошибку всякий раз, когда запускаю свои тесты. BaseFormRequest
это базовый класс для всего FormRequest
класса в моем приложении, который обрабатывает ответ JSON в соответствии с моими потребностями. Несколько дней назад все тесты работали нормально. Мне пришлось выполнить чистую установку ОС. После настройки моей ОС. Я вытащил все свои проекты из BitBucket. установите зависимости в этом проекте Laravel, но при запуске тестов он начал выдавать мне эту странную ошибку. Вот дамп ответа из теста.
#3159
"message": "Class 'AppHttpRequestsApiBaseFormRequest' not found"
"exception": "Error"
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/app/Http/Requests/StoreTeacherAttendanceRequest.php"
"line": 7
"trace": array:49 [
0 => {#3320
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/composer/ClassLoader.php"
"line": 444
"function": "include"
}
1 => {#3458
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/composer/ClassLoader.php"
"line": 322
"function": "ComposerAutoloadincludeFile"
}
2 => {#3617
"function": "loadClass"
"class": "ComposerAutoloadClassLoader"
"type": "->"
}
3 => {#3161
"function": "spl_autoload_call"
}
4 => {#3457
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/laravel/framework/src/Illuminate/Support/Reflector.php"
"line": 50
"function": "class_exists"
}
5 => {#1624
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/laravel/framework/src/Illuminate/Routing/RouteSignatureParameters.php"
"line": 26
"function": "isParameterSubclassOf"
"class": "IlluminateSupportReflector"
"type": "::"
}
6 => {#3319
"function": "IlluminateRouting{closure}"
"class": "IlluminateRoutingRouteSignatureParameters"
"type": "::"
}
7 => {#56
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/laravel/framework/src/Illuminate/Routing/RouteSignatureParameters.php"
"line": 27
"function": "array_filter"
}
8 => {#3322
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/laravel/framework/src/Illuminate/Routing/Route.php"
"line": 491
"function": "fromAction"
"class": "IlluminateRoutingRouteSignatureParameters"
"type": "::"
}
9 => {#53
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/laravel/framework/src/Illuminate/Routing/ImplicitRouteBinding.php"
"line": 25
"function": "signatureParameters"
"class": "IlluminateRoutingRoute"
"type": "->"
}
10 => {#49
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/laravel/framework/src/Illuminate/Routing/Router.php"
"line": 798
"function": "resolveForRoute"
"class": "IlluminateRoutingImplicitRouteBinding"
"type": "::"
}
11 => {#57
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php"
"line": 39
"function": "substituteImplicitBindings"
"class": "IlluminateRoutingRouter"
"type": "->"
}
12 => {#55
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
"line": 167
"function": "handle"
"class": "IlluminateRoutingMiddlewareSubstituteBindings"
"type": "->"
}
13 => {#47
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php"
"line": 59
"function": "IlluminatePipeline{closure}"
"class": "IlluminatePipelinePipeline"
"type": "->"
}
14 => {#3619
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
"line": 167
"function": "handle"
"class": "IlluminateRoutingMiddlewareThrottleRequests"
"type": "->"
}
15 => {#3164
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php"
"line": 44
"function": "IlluminatePipeline{closure}"
"class": "IlluminatePipelinePipeline"
"type": "->"
}
16 => {#3616
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
"line": 167
"function": "handle"
"class": "IlluminateAuthMiddlewareAuthenticate"
"type": "->"
}
17 => {#3461
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
"line": 103
"function": "IlluminatePipeline{closure}"
"class": "IlluminatePipelinePipeline"
"type": "->"
}
18 => {#3618
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/laravel/framework/src/Illuminate/Routing/Router.php"
"line": 687
"function": "then"
"class": "IlluminatePipelinePipeline"
"type": "->"
}
19 => {#3622
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/laravel/framework/src/Illuminate/Routing/Router.php"
"line": 662
"function": "runRouteWithinStack"
"class": "IlluminateRoutingRouter"
"type": "->"
}
20 => {#3475
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/laravel/framework/src/Illuminate/Routing/Router.php"
"line": 628
"function": "runRoute"
"class": "IlluminateRoutingRouter"
"type": "->"
}
21 => {#3335
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/laravel/framework/src/Illuminate/Routing/Router.php"
"line": 617
"function": "dispatchToRoute"
"class": "IlluminateRoutingRouter"
"type": "->"
}
22 => {#3177
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php"
"line": 165
"function": "dispatch"
"class": "IlluminateRoutingRouter"
"type": "->"
}
23 => {#3632
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
"line": 128
"function": "IlluminateFoundationHttp{closure}"
"class": "IlluminateFoundationHttpKernel"
"type": "->"
}
24 => {#3474
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php"
"line": 21
"function": "IlluminatePipeline{closure}"
"class": "IlluminatePipelinePipeline"
"type": "->"
}
25 => {#3334
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
"line": 167
"function": "handle"
"class": "IlluminateFoundationHttpMiddlewareTransformsRequest"
"type": "->"
}
26 => {#3176
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php"
"line": 21
"function": "IlluminatePipeline{closure}"
"class": "IlluminatePipelinePipeline"
"type": "->"
}
27 => {#3336
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
"line": 167
"function": "handle"
"class": "IlluminateFoundationHttpMiddlewareTransformsRequest"
"type": "->"
}
28 => {#3179
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php"
"line": 27
"function": "IlluminatePipeline{closure}"
"class": "IlluminatePipelinePipeline"
"type": "->"
}
29 => {#3337
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
"line": 167
"function": "handle"
"class": "IlluminateFoundationHttpMiddlewareValidatePostSize"
"type": "->"
}
30 => {#3634
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php"
"line": 63
"function": "IlluminatePipeline{closure}"
"class": "IlluminatePipelinePipeline"
"type": "->"
}
31 => {#3456
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
"line": 167
"function": "handle"
"class": "IlluminateFoundationHttpMiddlewareCheckForMaintenanceMode"
"type": "->"
}
32 => {#3339
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/fideloper/proxy/src/TrustProxies.php"
"line": 57
"function": "IlluminatePipeline{closure}"
"class": "IlluminatePipelinePipeline"
"type": "->"
}
33 => {#3182
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
"line": 167
"function": "handle"
"class": "FideloperProxyTrustProxies"
"type": "->"
}
34 => {#3340
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
"line": 103
"function": "IlluminatePipeline{closure}"
"class": "IlluminatePipelinePipeline"
"type": "->"
}
35 => {#3480
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php"
"line": 140
"function": "then"
"class": "IlluminatePipelinePipeline"
"type": "->"
}
36 => {#3638
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php"
"line": 109
"function": "sendRequestThroughRouter"
"class": "IlluminateFoundationHttpKernel"
"type": "->"
}
37 => {#3183
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php"
"line": 508
"function": "handle"
"class": "IlluminateFoundationHttpKernel"
"type": "->"
}
38 => {#3341
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php"
"line": 474
"function": "call"
"class": "IlluminateFoundationTestingTestCase"
"type": "->"
}
39 => {#3481
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/tests/Feature/TeacherAttendanceManagementTest.php"
"line": 48
"function": "json"
"class": "IlluminateFoundationTestingTestCase"
"type": "->"
}
40 => {#3639
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/phpunit/phpunit/src/Framework/TestCase.php"
"line": 1415
"function": "test_teacher_attendance_can_be_taken"
"class": "TestsFeatureTeacherAttendanceManagementTest"
"type": "->"
}
41 => {#3184
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/phpunit/phpunit/src/Framework/TestCase.php"
"line": 1035
"function": "runTest"
"class": "PHPUnitFrameworkTestCase"
"type": "->"
}
42 => {#3342
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/phpunit/phpunit/src/Framework/TestResult.php"
"line": 691
"function": "runBare"
"class": "PHPUnitFrameworkTestCase"
"type": "->"
}
43 => {#3482
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/phpunit/phpunit/src/Framework/TestCase.php"
"line": 763
"function": "run"
"class": "PHPUnitFrameworkTestResult"
"type": "->"
}
44 => {#3640
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/phpunit/phpunit/src/Framework/TestSuite.php"
"line": 597
"function": "run"
"class": "PHPUnitFrameworkTestCase"
"type": "->"
}
45 => {#3185
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/phpunit/phpunit/src/TextUI/TestRunner.php"
"line": 627
"function": "run"
"class": "PHPUnitFrameworkTestSuite"
"type": "->"
}
46 => {#3343
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/phpunit/phpunit/src/TextUI/Command.php"
"line": 204
"function": "doRun"
"class": "PHPUnitTextUITestRunner"
"type": "->"
}
47 => {#3483
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/phpunit/phpunit/src/TextUI/Command.php"
"line": 163
"function": "run"
"class": "PHPUnitTextUICommand"
"type": "->"
}
48 => {#3641
"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/phpunit/phpunit/phpunit"
"line": 61
"function": "main"
"class": "PHPUnitTextUICommand"
"type": "::"
}
]
}
Мой BaseFormRequest.php
<?php
namespace AppHttpRequestsApi;
use IlluminateHttpJsonResponse;
use IlluminateContractsValidationValidator;
use IlluminateValidationValidationException;
use IlluminateHttpExceptionsHttpResponseException;
use IlluminateFoundationHttpFormRequest as LaravelFormRequest;
abstract class BaseFormRequest extends LaravelFormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
abstract public function rules();
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
abstract public function authorize();
/**
* Handle a failed validation attempt.
*
* @param IlluminateContractsValidationValidator $validator
* @return void
*
* @throws IlluminateValidationValidationException
*/
protected function failedValidation(Validator $validator)
{
throw new HttpResponseException(
response()->error($validator->messages()->all(), JsonResponse::HTTP_UNPROCESSABLE_ENTITY)
);
}
}
Мой FormRequest
<?php
namespace AppHttpRequests;
use AppHttpRequestsApiBaseFormRequest;
class StoreTeacherAttendanceRequest extends BaseFormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'attendance_date' => 'required',
'in_time' => 'required',
'out_time' => 'required',
'class' => 'required',
'subject' => 'required',
'session' => 'required',
'teacher_id' => 'required',
];
}
/**
* Check if duplicate entry is being made
*/
public function withValidator($validator)
{
$validator->after(function ($validator) {
$exist = AppTeacherAttendance::where([
'teacher_id' => request()->teacher_id,
'attendance_date' => request()->attendance_date,
'in_time' => request()->in_time,
'out_time' => request()->out_time
])->first();
if (!empty($exist)) {
$validator->errors()->add('duplicate_attendance', 'Duplicate attendance entry!');
}
});
}
}
На самом деле я получаю эту ошибку для всех тестов, которые отправляют запрос в API, который использует BaseFormREquest. Я пробовал composer dump-autoload
несколько раз, но безуспешно. Мои API-интерфейсы работают отлично, если отправить запрос от POSTMAN.
Любая помощь будет признательна.
Комментарии:
1. Не совсем уверен, но это может быть проблема с автоматической загрузкой композитора. Я получал некоторые предупреждения при запуске
composer install
, сообщающие мне что-то о том, что некоторые классы не должны загружаться автоматически, потомуClass <XXXXXXX> does not comply with psr-4 autoloading standard. It will not autoload anymore in Composer v2.0
что . Мне пришлось исправить некоторые имена классов, чтобы они работали в Composer версии 2.0. И поскольку вы выполнили чистую установку, возможно, вы использовали последнюю версию Composer.2. Итак, как мне это решить???
3. php-fig.org/psr/psr-4 было бы хорошим местом для начала.
4. Итак, у меня было App Http Requests Api BaseFormRequest в качестве пространства имен, но App Http RequestsBaseFormRequest в качестве структуры каталога. После изменения его на App Http Requests Api BaseFormRequest. я все еще получаю предупреждение от composer.
Class AppHttpRequestsApiBaseFormRequest located in ./app/Http/Requests/BaseFormRequest.php does not comply with psr-4 autoloading standard. Skipping.
5. Вы запускали
composer dump-autoload
после изменения файлов?