Laravel CRUD Controller Test

Basically, I have to write tests for many Laravel controllers, most of which are CRUD (read, store, update), and most of the logic is placed inside these (Inherited code, not mine).

What I need to do is automate testing from a user perspective. So I need to hit all the endpoints and test the real database and see if everything works out well.

I have almost no testing experience, but from what I collect, the controllers should be tested using integration / acceptance testing. Now I have done a great job with testing reading methods by extending the Laravel TestCase, here is an example:

class SongsTest extends TestCase
{
    public function testBasicIndex()
    {   
        $arguments = [];

        $response = $this->call('GET', '/songs', $arguments);

        $this->assertResponseOk();
        $this->seeJson();
    }

    /**
        * @dataProvider providerSearchQuery
    */
    public function testSearchIndex($query)
    {
        $arguments = ['srquery' => $query];

        $response = $this->call('GET', '/songs', $arguments);

        $this->assertResponseOk();
        $this->seeJson();
    }

    public function providerSearchQuery()
    {
        return array(
            array('a'),
            array('as%+='),
            array('test?Someqsdag8hj$%$') 
            );
    }


    public function testGetSongsById()
    {   
        $id = 1;

        $response = $this->call('GET', '/songs/' . $id);

        $this->assertContains($response->getStatusCode(), [200, 404]);
        $this->seeJson();

        if($response->getStatusCode() == 404)
        {   
            $content = json_decode($response->getContent());
            $this->assertContains($content->message[0], ['Song not found', 'Item not active']);
        }
    }
}

, 200, JSON . .

:

, , UserController , . TokensController , - .

:

: UserController ( ), TokensController , , .

, .

+4
1

-

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class PostTest extends TestCase
{
    use WithoutMiddleware;
    public $token = "lrSFMf0DpqRAh4BXTXWHp5VgFTq4CqA68qY3jG2CqvcpNTT6m0y9Qs6OdpSn";

/*
    A browser that receives a 302 response code HAS to redirect which means it will take the URL in the response and submit a new request. The result you see in your browser is the redirected page.

    Unit testing does not redirect. Your unit test is only doing what you direct it to do. If your unit test should test for the redirect then you evaluate the response and the correct assertion is 302 and not 200.
*/
public function testExample()
{
    $this->assertTrue(true);
}

public function testLogin()
{
    $this->visit('/')
     ->type('abc@gmail.com', 'email')
     ->type('123456', 'password')
     ->press('Login') // type submit - value / button - lable
     ->seePageIs('/Wall'); // for redirect url
} 


public function testFavourite()
{
    $this->testLogin();
    $request = [
        'user_id' => '172',
        'token'   => $this->token,
        'post_id' => '500'
    ];

    $response = $this->call('POST', '/DoFavoriteDisc',$request);
    $this->assertEquals(200, $response->getStatusCode());

}

}

, .

0

Source: https://habr.com/ru/post/1652220/


All Articles