PHP code example of sti3bas / laravel-scout-array-driver

1. Go to this page and download the library: Download sti3bas/laravel-scout-array-driver library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sti3bas / laravel-scout-array-driver example snippets


$user = User::factory()->create([
    'name' => 'Oliver',
]);

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

Search::assertContains($user) // ✅
    ->assertContains($user2) // ❌
    ->assertContains($user, function ($record) { // ✅
        return $record['name'] === 'Oliver';
    })
    ->assertContains($user, function ($record) { // ❌
        return $record['name'] === 'John';
    })
    ->assertContains($user2, function ($record) { // ❌
        return $record['name'] === 'John';
    });

$user = User::factory()->create([
    'name' => 'Oliver',
]);

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

Search::assertNotContains($user) // ❌
    ->assertNotContains($user2) // ✅
    ->assertNotContains($user, function ($record) { // ❌
        return $record['name'] === 'Oliver';
    })
    ->assertNotContains($user, function ($record) { // ✅
        return $record['name'] === 'John';
    })
    ->assertNotContains($user2, function ($record) { // ✅
        return $record['name'] === 'John';
    });

$user = User::factory()->create([
    'name' => 'Oliver',
]);

Search::assertContainsIn('users', $user) // ✅
    ->assertContainsIn('non_existing_index', $user) // ❌
    ->assertContainsIn('users', $user, function ($record) { // ✅
        return $record['name'] === 'Oliver';
    })
    ->assertContainsIn('users', $user, function ($record) { // ❌
        return $record['name'] === 'John';
    });

$user = User::factory()->create([
    'name' => 'Oliver',
]);

Search::assertNotContainsIn('users', $user) // ❌
    ->assertNotContainsIn('non_existing_index', $user) // ✅
    ->assertNotContainsIn('users', $user, function ($record) { // ❌
        return $record['name'] === 'Oliver';
    })
    ->assertNotContainsIn('users', $user, function ($record) { // ✅
        return $record['name'] === 'John';
    });

Search::assertEmpty(); // ✅

User::factory()->create();

Search::assertEmpty(); // ❌

Search::assertEmptyIn('users'); // ✅

User::factory()->create();

Search::assertEmptyIn('users'); // ❌

Search::assertNotEmpty(); // ❌

User::factory()->create();

Search::assertNotEmpty(); // ✅

Search::assertNotEmptyIn('users'); // ❌

User::factory()->create();

Search::assertNotEmptyIn('users'); // ✅

Search::assertCount(1); // ❌

User::factory()->create();

Search::assertCount(1); // ✅

Search::assertNotEmptyIn('users', 1); // ❌

User::factory()->create();

Search::assertNotEmptyIn('users', 1); // ✅

$user = User::factory()->create([
    'name' => 'Peter',
]);

Search::assertSynced($user); // ✅

$user->update(['name' => 'John']);
$user->delete();

Search::assertContains($user) // ❌
    ->assertSynced($user) // ✅
    ->assertSynced($user, function ($record) { // ✅
        return $record['name'] === 'Peter';
    })
    ->assertSynced($user, function ($record) { // ✅
        return $record['name'] === 'John';
    })
    ->assertSynced($user, function ($record) { // ❌
        return $record['name'] === 'Oliver';
    });

$user = User::factory()->create([
    'name' => 'Peter',
]);

Search::assertNotSynced($user); // ❌

$user->update(['name' => 'John']);
$user->delete();

Search::assertContains($user) // ❌
    ->assertNotSynced($user); // ❌

Search::assertNotSynced($user, function ($record) { // ❌
    return $record['name'] === 'Peter';
})
->assertNotSynced($user, function ($record) { // ❌
    return $record['name'] === 'John';
})
->assertNotSynced($user, function ($record) { // ✅
    return $record['name'] === 'Oliver';
});

$user = User::factory()->create([
    'name' => 'Peter',
]);

Search::assertSyncedTo('users', $user); // ✅

$user->update(['name' => 'John']);
$user->delete();

Search::assertContains($user) // ❌
    ->assertSyncedTo('users', $user) // ✅
    ->assertSyncedTo('users', $user, function ($record) {
        return $record['name'] === 'Peter'; // ✅
    })
    ->assertSyncedTo('users', $user, function ($record) {
        return $record['name'] === 'John'; // ✅
    })
    ->assertSyncedTo('non_existing_index', $user, function ($record) {
        return $record['name'] === 'John'; // ❌
    });

$user = User::factory()->create([
    'name' => 'Peter',
]);

Search::assertNotSyncedTo('users', $user) // ❌
    ->assertNotSyncedTo('not_existing_index', $user); // ✅

$user->update(['name' => 'John']);
$user->delete();

Search::assertContains($user) // ❌
    ->assertNotSyncedTo('users', $user) // ❌
    ->assertNotSyncedTo('users', $user, function ($record) {
        return $record['name'] === 'Peter'; // ❌
    })
    ->assertNotSyncedTo('users', $user, function ($record) {
        return $record['name'] === 'Oliver'; // ✅
    });

$user = User::withoutSyncingToSearch(function () {
    return User::factory()->create([
        'name' => 'Peter',
    ]);
});

Search::assertSyncedTimes($user, 0) // ✅
    ->assertSyncedTimes($user, 1); // ❌

$user->searchable();
$user->update(['name' => 'John']);
$user->delete();

Search::assertContains($user) // ❌
    ->assertSyncedTimes($user, 2) // ✅
    ->assertSyncedTimes($user, 1, function ($record) {
        return $record['name'] === 'Peter'; // ✅
    })
    ->assertSyncedTimes($user, 1, function ($record) {
        return $record['name'] === 'John'; // ✅
    })
    ->assertSyncedTimes($user, 1, function ($record) {
        return $record['name'] === 'Oliver'; // ❌
    });

$user = User::withoutSyncingToSearch(function () {
    return User::factory()->create([
        'name' => 'Peter',
    ]);
});

Search::assertSyncedTimesTo('users', $user, 0) // ✅
    ->assertSyncedTimesTo('non_existing_index', $user, 1); // ❌

$user->searchable();
$user->update(['name' => 'John']);
$user->delete();

Search::assertContains($user) // ❌
    ->assertSyncedTimesTo('users', $user, 2) // ✅
    ->assertSyncedTimesTo('users', $user, 1, function ($record) {
        return $record['name'] === 'Peter'; // ✅
    })
    ->assertSyncedTimesTo('non_existing_index', 1, function ($record) {
        return $record['name'] === 'John'; // ❌
    });

Search::assertNothingSynced(); // ✅

User::factory()->create();

Search::assertNothingSynced(); // ❌

Search::assertNothingSyncedTo('users'); // ✅

User::factory()->create();

Search::assertNothingSyncedTo('users'); // ❌

$manager = $this->app->make(EngineManager::class);

$engine = $manager->engine();

Search::assertIndexExists('test'); // ❌

$engine->createIndex('test');

Search::assertIndexExists('test'); // ✅

$manager = $this->app->make(EngineManager::class);

$engine = $manager->engine();

Search::assertIndexNotExists('test'); // ✅

$engine->createIndex('test');

Search::assertIndexNotExists('test'); // ❌

$user = User::factory()->create([
    'id' => 123,
    'name' => 'Peter',
    'email' => '[email protected]',
]);

Search::fakeRecord($user, [
    'name' => 'John',
]);

$record = User::search()->where('id', 123)->raw()['hits'][0];

$this->assertEquals('Peter', $record['name']); // ❌
$this->assertEquals('John', $record['name']); // ✅
$this->assertEquals('[email protected]', $record['email']); // ✅

Search::fakeRecord($user, [
    'id' => 123,
    'name' => 'John',
], false);

$record = User::search()->where('id', 123)->raw()['hits'][0];

$this->assertEquals('Peter', $record['name']); // ❌
$this->assertEquals('John', $record['name']); // ✅
$this->assertTrue(!isset($record['email'])); // ✅
xml
<server name="SCOUT_DRIVER" value="array"/>