S
S
Sergey Semenko2016-06-27 20:03:15
PHPUnit
Sergey Semenko, 2016-06-27 20:03:15

Laravel. PHPUnit. Why doesn't downloading test files work?

The test itself:

<?php

class RegistrationTest extends TestCase
{
    public function testRegisterClient()
    {
        $file = base_path('tests/assets/img/photo.jpeg');
        $photo = new \Symfony\Component\HttpFoundation\File\UploadedFile($file, 'photo.jpeg',
            mime_content_type($file), filesize($file), null, true);
        $response = $this->action('POST', '[email protected]', [], [
            'number' => $data['number'],
            'token' => $data['token'],
            'type' => 'client',
            'name' => 'Сергей Семенко',
            'city' => 'Харьков',
        ], [], ['photo' => $photo]);
        $response = json_decode($response->getContent());
    }
    
}

Controller:
private function registerUser(Request $request, PreRegister $preRegister) : User
    {
        $validator = \Validator::make($request->all(), [
            'name' => 'required|string|max:255',
            'photo' => 'required|mimes:jpeg,jpg,bmp,png,gif',
            'city' => 'required|string|max:255',
        ], [
            'name.required' => 'Не указано имя/название',
            'name.string' => 'Ошибка чтения имени/названия',
            'name.max' => 'Имя должно содержать не более :max символов',
            'photo.required' => 'Не выбрано фото',
            'photo.mimes' => 'Ошибка открытия фото',
            'photo.max' => 'Максимальный размер фото 10МБ',
            'city.required' => 'Не указан город',
            'city.string' => 'Ошибка чтения города',
            'city.max' => 'Название города может содержать не более :max символов',
        ]);
        
        $file = $request->file('photo');
        dd($file); // Результат вывода ниже

        if (!$validator->fails()) {
            $photo = Photo::fromUploadedFile($request->file('image'));

            $user = new User();
            $user->name = $request->get('name');
            $user->photo()->associate($photo);
            $user->number = $preRegister->number;

            return $user;
        }

        throw new ValidationException($validator);
    }

I get this:
Illuminate\Http\UploadedFile {#324
  -test: false
  -originalName: "photo.jpeg"
  -mimeType: "image/jpeg"
  -size: 8388
  -error: 0
  path: "C:\OpenServer\domains\test\tests/assets/img"
  filename: "photo.jpeg"
  basename: "photo.jpeg"
  pathname: "C:\OpenServer\domains\test\tests/assets/img/photo.jpeg"
  extension: "jpeg"
  realPath: "C:\OpenServer\domains\test\tests\assets\img\photo.jpeg"
  aTime: 2016-06-27 16:11:13
  mTime: 2016-03-20 13:56:50
  cTime: 2016-06-27 16:11:13
  inode: 0
  size: 8388
  perms: 0100666
  owner: 0
  group: 0
  type: "file"
  writable: true
  readable: true
  executable: false
  file: true
  dir: false
  link: false
  linkTarget: "C:\OpenServer\domains\test\tests\assets\img\photo.jpeg"
}

The problem is that the value of test is false. What is the reason for this, because I specified the value true in the test?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anatoliy Lyovkin, 2016-06-27
@abler98

Use \Illuminate\Http\UploadedFile
instead

\Symfony\Component\HttpFoundation\File\UploadedFile

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question