dimanche 29 novembre 2020

Laravel: Class 'App\Http\Requests\Api\BaseFormRequest' not found when I run tests

I am getting this error whenever I run my tests.BaseFormRequest is base class for all the FormRequest class in my app which handles JSON response as per my need. All the tests were running fine few days ago. I had to do a clean OS install. After setting up my OS. I pulled all my projects from BitBucket. install the dependencies in this Laravel project but when starting running tests it started giving me this weird error. Here is the response dump from the test.

#3159
  +"message": "Class 'App\Http\Requests\Api\BaseFormRequest' 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": "Composer\Autoload\includeFile"
    }
    2 => {#3617
      +"function": "loadClass"
      +"class": "Composer\Autoload\ClassLoader"
      +"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": "Illuminate\Support\Reflector"
      +"type": "::"
    }
    6 => {#3319
      +"function": "Illuminate\Routing\{closure}"
      +"class": "Illuminate\Routing\RouteSignatureParameters"
      +"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": "Illuminate\Routing\RouteSignatureParameters"
      +"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": "Illuminate\Routing\Route"
      +"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": "Illuminate\Routing\ImplicitRouteBinding"
      +"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": "Illuminate\Routing\Router"
      +"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": "Illuminate\Routing\Middleware\SubstituteBindings"
      +"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": "Illuminate\Pipeline\{closure}"
      +"class": "Illuminate\Pipeline\Pipeline"
      +"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": "Illuminate\Routing\Middleware\ThrottleRequests"
      +"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": "Illuminate\Pipeline\{closure}"
      +"class": "Illuminate\Pipeline\Pipeline"
      +"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": "Illuminate\Auth\Middleware\Authenticate"
      +"type": "->"
    }
    17 => {#3461
      +"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
      +"line": 103
      +"function": "Illuminate\Pipeline\{closure}"
      +"class": "Illuminate\Pipeline\Pipeline"
      +"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": "Illuminate\Pipeline\Pipeline"
      +"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": "Illuminate\Routing\Router"
      +"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": "Illuminate\Routing\Router"
      +"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": "Illuminate\Routing\Router"
      +"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": "Illuminate\Routing\Router"
      +"type": "->"
    }
    23 => {#3632
      +"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
      +"line": 128
      +"function": "Illuminate\Foundation\Http\{closure}"
      +"class": "Illuminate\Foundation\Http\Kernel"
      +"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": "Illuminate\Pipeline\{closure}"
      +"class": "Illuminate\Pipeline\Pipeline"
      +"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": "Illuminate\Foundation\Http\Middleware\TransformsRequest"
      +"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": "Illuminate\Pipeline\{closure}"
      +"class": "Illuminate\Pipeline\Pipeline"
      +"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": "Illuminate\Foundation\Http\Middleware\TransformsRequest"
      +"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": "Illuminate\Pipeline\{closure}"
      +"class": "Illuminate\Pipeline\Pipeline"
      +"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": "Illuminate\Foundation\Http\Middleware\ValidatePostSize"
      +"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": "Illuminate\Pipeline\{closure}"
      +"class": "Illuminate\Pipeline\Pipeline"
      +"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": "Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode"
      +"type": "->"
    }
    32 => {#3339
      +"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/fideloper/proxy/src/TrustProxies.php"
      +"line": 57
      +"function": "Illuminate\Pipeline\{closure}"
      +"class": "Illuminate\Pipeline\Pipeline"
      +"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": "Fideloper\Proxy\TrustProxies"
      +"type": "->"
    }
    34 => {#3340
      +"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
      +"line": 103
      +"function": "Illuminate\Pipeline\{closure}"
      +"class": "Illuminate\Pipeline\Pipeline"
      +"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": "Illuminate\Pipeline\Pipeline"
      +"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": "Illuminate\Foundation\Http\Kernel"
      +"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": "Illuminate\Foundation\Http\Kernel"
      +"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": "Illuminate\Foundation\Testing\TestCase"
      +"type": "->"
    }
    39 => {#3481
      +"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/tests/Feature/TeacherAttendanceManagementTest.php"
      +"line": 48
      +"function": "json"
      +"class": "Illuminate\Foundation\Testing\TestCase"
      +"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": "Tests\Feature\TeacherAttendanceManagementTest"
      +"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": "PHPUnit\Framework\TestCase"
      +"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": "PHPUnit\Framework\TestCase"
      +"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": "PHPUnit\Framework\TestResult"
      +"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": "PHPUnit\Framework\TestCase"
      +"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": "PHPUnit\Framework\TestSuite"
      +"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": "PHPUnit\TextUI\TestRunner"
      +"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": "PHPUnit\TextUI\Command"
      +"type": "->"
    }
    48 => {#3641
      +"file": "/home/ropali/.local/share/Trash/files/edu_assit_api/vendor/phpunit/phpunit/phpunit"
      +"line": 61
      +"function": "main"
      +"class": "PHPUnit\TextUI\Command"
      +"type": "::"
    }
  ]
}

My BaseFormRequest.php

<?php

namespace App\Http\Requests\Api;

use Illuminate\Http\JsonResponse;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Validation\ValidationException;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Foundation\Http\FormRequest 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  \Illuminate\Contracts\Validation\Validator $validator
     * @return void
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    protected function failedValidation(Validator $validator)
    {
        
        throw new HttpResponseException(
            response()->error($validator->messages()->all(), JsonResponse::HTTP_UNPROCESSABLE_ENTITY)
        );
    }
}

My FormRequest

<?php

namespace App\Http\Requests;

use App\Http\Requests\Api\BaseFormRequest;


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 = \App\TeacherAttendance::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!');
            }

            
        });
    }
}

Actually I am getting this error for all the tests which send request to API which uses BaseFormREquest. I have tried composer dump-autoload few times but not luck. My APIs are working perfectly If send request from POSTMAN. Any help would be appreciated.

Aucun commentaire:

Enregistrer un commentaire