Testing the routes in Laravel

Asked

Viewed 540 times

-1

Dear, do a lot of research and I haven’t found the solution to what I need.

I want to take a test of my application, which in this case is a register of countries. The application works perfectly.

The Controller calls the correct Routes, called by the correct commands (GET, POST, PATCH, DELETE): - view -> parents.view - Insert -> pais.Insert - store -> parents.store - Edit -> parents.Edit - update -> parents.update - delete -> parents.delete

I created the test routine as below:

   public function testHTML() : void {
        // inclusão normal
        $registro = [
            'codpais' => 1,
            'nompais' => 'Brasil',
            'sigpais' => 'BRA',
            '_token' => csrf_token()
        ];

    // chamo o evento post, com os dados
        $response = $this->call( 'POST', 'pais', $registro);
        $response->assertStatus(302);

    // pesquiso se o registro está no banco. 
    // Caso contrário quero retornar um erro
    // Até aqui aparentemente funcionou
        $pesq = Pais::find($registro['codpais']);
        if ($pesq->codpais != $registro['codpais']) {
            throw new Exception('Erro no cadastro de país.');;
        }

    // quero fazer um inclusão do mesmo registro
    // mas quero saber o erro que foi retornado
    // mas recebo o status = 302.
        $response = $this->call( 'POST', 'pais', $registro);
        print "\nPasso 2 ->" . $response->getStatusCode() . "\n";

    }

I wanted to test if there was an error message, which I sent via Session::flash, inside the Controller. That’s possible?

How do you do this test? Several examples I see have to mount an API, where I return the registry via json, but I want to test the user application.

Do I want the impossible?

On hold Tonico Bittencourt

1 answer

0

First structure the controller as follows, listing, create, delete and change, then with each function in the controller you will already know that you need 2 more functions in each registration view and change view, follow an example of controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Post;

class PostController extends Controller
{
    public function index() {
        //aqui vai ser ao abrir um post especifico.
    }

    public function lista() {

        $post = Post::with('autor')->get();

        return view('home', compact('post'));
    }

    public function cadastro() {

        return view('post.cadastrar');
    }

    public function cadastrar(Request $request) {

        $request->validate([
            'titulo' => 'required'
        ]);

        Post::create([
            'image' => $request->image,
            'titulo' => $request->titulo,
            'conteudo' => $request->conteudo
        ])->save($request->autor);

        return redirect()->back();

    }
}

Ai in the routes you also separate how you should behave using or not the name this is your choice, an example of routes:

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Auth::routes();

Route::get('/', 'PostController@lista')->name('home');

Route::get('/post/criar', 'PostController@cadastro')->name('post.criar');
Route::get('/post/criado', 'PostController@cadastrar')->name('post.criado');

There are still some routes for me to implement is a project I’m doing for testing, but I hope to have helped you, any questions just comment...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.