P
P
Plamer2017-01-08 20:56:39
PHPUnit
Plamer, 2017-01-08 20:56:39

What is the best way to write PHPUnit tests for restful Laravel controllers?

Hello, I have read the book Test Driven Development by Kent Beck. I'm trying to train on Laravel admin panel controllers. Since I have them Restful and here the view is mixed with logic, for example "edit" I cannot check the returned data, they are transferred immediately to the view. What is the best way to test this method? As I understand it, TDD is tightly based on the principles of SOLID, and the first principle says "Single Responsibility Principle", so I think it might be better to split the controller into two parts: 1. A view that will receive data and pass it to the view () method, 2 The logic itself, with create, update, delete, edit methods. Please tell me how to do it right, I don’t want to rewrite 5 times.

<?php
namespace App\Http\Controllers\Admin;
use Validator;
use Illuminate\Http\Request;
use App\Http\Interfaces\Admin\RestInterface;

class UserController extends AdminController implements RestInterface
{
    public function index()
    {
        $data = [
            'title' => 'Список пользователей'
        ];
        return view('admin.users.list', $data);
    }

    public function show()
    {
        $data = [
            'title' => 'Добавить пользователя'
        ];
        return view('admin.users.single', $data);
    }

    public function edit($id)
    {
        $v = Validator::make(['id' => $id], [
            'id' => 'required|integer'
        ]);

        if($v->fails()){
            return redirect('/', 303);
        }

        $user = $this->repository->getOne($id);

        $data = [
            'title' => 'Пользователь',
            'user' => $user
        ];
        return view('users.admin.single', $data);
    }

    public function create(Request $request)
    {
        $v = Validator::make($request->all(), [
            'name' => 'required|string|max:50',
            'email' => 'required|email|max:30',
            'password' => 'required|string|max:30'
        ]);

        if($v->fails()){
            return response()->json($v->errors());
        }
        $this->repository->create($request->all());
        return true;
    }

    public function update($id, Request $request)
    {
        $v = Validator::make($request->all(), [
            'name' => 'required|string|max:50',
            'email' => 'required|email|max:30',
            'password' => 'required|string|max:30'
        ]);
        if($v->fails()){
            return response()->json($v->errors());
        }
        $this->repository->update($id, $request->all());
        return true;
    }

    public function delete($id)
    {
        $this->repository->delete($id);
        return true;
    }

    public function filter(Request $request)
    {
        $this->validate($request, [
            'name' => 'string|max:30',
            'email' => 'string|max:30'
        ]);
        $filter = $this->repository->filter($request->all());
        return response()->json($filter);
    }

    public function getEntity(){
        return '\App\Entities\Users';
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Aksentiev, 2017-01-09
@Sanasol

like this https://laravel.com/docs/5.3/application-testing#i...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question