abkrim's avatar
Level 13

Test unit a public method of a class

I'm looking for how to write a unit test for a class but I can't find how to do it.

Class

<?php

namespace App\Http\Livewire\DataTable;

use Livewire\WithPagination;

class WithPerPagePagination
{
    use WithPagination;

    public int $perPage = 25;

    public function mountWithPerPagePagination()
    {
        $this->perPage = session()->get('perPage', $this->perPage);
    }

    public function updatedPerPage($value)
    {
        session()->put('perPage', $value);
    }

    public function applyPagination($query)
    {
        return $query->paginate($this->perPage);
    }
}

Test

<?php

namespace Tests\Http\Livewire\DataTable;

use App\Http\Livewire\DataTable\WithPerPagePagination;
use Tests\TestCase;

class WithPerPagePaginationTest extends TestCase
{
    /** @test */
    function method_mountWithPerPagePagination_work_properly()
    {
        $pagination = app(WithPerPagePagination::class);

        // I like check if public `perPage` exists and check value
       // The idea is to do this, to be able to modify by programming with TDD, 
      // the content of the mountWithPerPagePagination method
    }
}

I see that $pagination is

App\Http\Livewire\DataTable\WithPerPagePagination {#2714
  +perPage: 25
  +page: 1
  +paginators: []
  #numberOfPaginatorsRendered: []
0 likes
1 reply
abkrim's avatar
Level 13

I see this way. $this->assertEquals($pagination->perPage, '25');

Please or to participate in this conversation.