Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jrdavidson's avatar

Generating Test with Mocking Query

I have the main feature request test partly written. On my users.index page it renders 3 different Livewire components that are paginated user datatables. One for active, one for inactive, and unactivated. I'm wanting to test each individual Livewire component.

The problem I'm having is that I was trying to run a query inside of the test and make sure I return the right data. The query is the same as the one inside the active users component but I want to make sure that it looks to make sure the right methods are being called because the test could change and the right data could still be returned isn't testing that the same query runs inside of the component.

I think I need mockery for this situation but not 100% sure. What does anyone else think?

<?php

namespace Tests\Feature\Users;

use App\Enums\Role;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ViewUsersListSuccessConditionsTest extends TestCase
{
    use RefreshDatabase;

    /** @test */
    public function an_administrator_can_view_users_page()
    {
        $this->actAs(Role::ADMINISTRATOR);

        $response = $this->indexRequest('users');

        $response->assertOk();
        $response->assertViewIs('users.index');
    }
}
<?php

namespace App\Http\Livewire\Titles;

use App\Models\User;
use Livewire\Component;
use Livewire\WithPagination;

class ActiveUsers extends Component
{
    use WithPagination;

    public $perPage = 10;

    public function render()
    {
        return view('livewire.users.active-users', [
            'activeUsers' => User::active()->paginate($this->perPage)
        ]);
    }
}
<?php

namespace Tests\Integration\Livewire\Users;

use App\Http\Livewire\Users\ActiveUsers;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\Factories\UserFactory;
use Tests\TestCase;

class ActiveUsersTest extends TestCase
{
    use RefreshDatabase;

    /** @var \Illuminate\Support\Collection */
    protected $users;

    protected function setUp(): void
    {
        parent::setUp();

        $active = UserFactory::new()->count(3)->active()->create();
        $unactivated = UserFactory::new()->count(3)->unactivated()->create();
        $inactive = UserFactory::new()->count(3)->inactive()->create();

        $this->users = collect([
            'active'            => $active,
            'unactivated'       => $unactivated,
            'inactive'          => $inactive,
            'all'               => collect()
                                    ->concat($active)
                                    ->concat($unactivated)
                                    ->concat($inactive)
        ]);
    }

    /** @test */
    public function active_users_component_should_return_correct_view()
    {
        $component = Livewire::test(ActiveUsers::class);

        $this->assertEquals(
            'livewire.users.active-users',
            $component->lastRenderedView->getName()
        );
    }

    /** @test */
    public function active_users_component_should_pass_correct_data()
    {
        $component = Livewire::test(ActiveUsers::class);

        $mockUser
            ->shouldReceive('active')
            ->once()
            ->shouldReceive('paginate')
            ->once()
            ->andReturn($this->users->get(['active']));


        // Same query ran in livewire component
        $activeUsers = User::active()->get();

        $component->assertViewHas('activeUsers');
        $this->assertCount(3, $activeUsers);

        //  Not actually testing related to the component 
        // but the query results itself.
        $this->assertEquals(
            $this->users->get('active')->pluck('id')->toArray(),
            $activeUsers->pluck('id')->sort()->values()->toArray()
        );
    }
}
0 likes
9 replies
bugsysha's avatar

I think I do not understand. Could you repeat your question?

jrdavidson's avatar

@bugsysha I'm trying to figure out how I can test to make sure the right query is being ran inside of the component.

bugsysha's avatar
\DB::enableQueryLog();

// execute what you need

\DB::disableQueryLog();
$queries = collect(\DB::getQueryLog())->pluck('query');
$this->assertContains($expectedQuery, $queries);
// maybe you'll need $this->assertContains($expectedQuery, $queries->toArray());
bugsysha's avatar

Never tried but I do not see a reason why it shouldn't. Why don't you try it since you can test it? Better than to ask and wait for someone to reply.

jrdavidson's avatar

I think this is better fit for a feature test which is not the type of test I'm trying to do.

jrdavidson's avatar

I'm wanting to test that this specific component is running a speciific query for the component.

bugsysha's avatar

I think this is better fit for a feature test which is not the type of test I'm trying to do.

I think you are wrong. You already have RefreshDatabase trait, and you are adding stuff to database. How listening to a query is any different than what you are already doing? And how do you differentiate what you have here as a Unit test?

jrdavidson's avatar

@bugsysha This is true however what I"m trying to do is test that there are 3 different components with different data associated with those 3 different components. All 3 components return 3 different collections back to its associated view.

Please or to participate in this conversation.