lundi 1 avril 2019

How to test a fake file upload to a route that validate the file?

I'm trying to write a test unit for the route 'store' in my controller, pass a fake file to be tested too through the validator of my method, but all i got is that the data is not a file :

  +baseResponse: Illuminate\Http\JsonResponse {#2740
    #data: "{"message":"The given data was invalid.","errors":{"invoice":["The invoice must be a file."]}}"

Code :

  • Test :
        $data = factory('App\Domain\X\X')->raw(['creator_id' => $user->id]);
        $data['invoice'] = UploadedFile::fake()->create('invoice.xlsx');
 $response = $this->json('POST', route('x.store', $data));

  • Controller :
 public function store(XXXRequest $request)
    {
        ...

  • Request :
class XXXRequest extends FormRequest
{
    /**
     * 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 [
            'name' => 'required',
            'invoice' => 'nullable|file',
        ];
    }

Aucun commentaire:

Enregistrer un commentaire