Sti3bas / laravel-scout-array-driver

Array driver for Laravel Scout
MIT License
89 stars 8 forks source link

Error: Using $this when not in object context #10

Closed tooshay closed 2 years ago

tooshay commented 2 years ago

I'm assuming it's a silly omission on my side, but when trying to run the first example test from your documentation (Search::assertContains($user)...), I get this error:

1) Tests\Unit\Employees\SearchIndexTest::test_it_indexes_models
Error: Using $this when not in object context

labour-management-api/vendor/sti3bas/laravel-scout-array-driver/src/Search.php:22

Running Laravel 8.75, PHP 7.4 with Elastic as the Scout driver (using babenkoivan's implementation).

Sti3bas commented 2 years ago

@tooshay I was not able to reproduce it, can you show the code of your test file?

tooshay commented 2 years ago

@Sti3bas Here's what I have:

<?php

namespace Tests\Unit\Employees;

use App\Models\Employee;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Scout\Searchable;
use Sti3bas\ScoutArray\Search;
use Tests\TestCase;

class SearchIndexTest extends TestCase
{
    use RefreshDatabase;

    public function test_it_indexes_models()
    {
        $user = Employee::factory()->create([
            'forename' => 'Oliver',
        ]);

        $user2 = Employee::withoutSyncingToSearch(function () {
            return Employee::factory()->create([
                'forename' => 'John',
            ]);
        });

        Search::assertContains($user) // ✅
        ->assertContains($user2) // ❌
        ->assertContains($user, function ($record) { // ✅
            return $record['forename'] === 'Oliver';
        })
            ->assertContains($user, function ($record) { // ❌
                return $record['forename'] === 'John';
            })
            ->assertContains($user2, function ($record) { // ❌
                return $record['forename'] === 'John';
            });
    }
}
Sti3bas commented 2 years ago

@tooshay still can't reproduce it.. can you try to reproduce it yourself in a fresh Laravel installation and share a repository with me?

tooshay commented 2 years ago

OK, figured it out. PHPStorm pulled in Sti3bas\ScoutArray\Search instead of Sti3bas\ScoutArray\Facades\Search!