Home > PHP Framework > Laravel > body text

Do you know about Laravel Scout array driver?

藏色散人
Release: 2020-07-23 14:59:02
forward
2590 people have browsed it

The following tutorial column will introduce you to the Laravel Scout array driver for testing. I hope it will be helpful to friends in need!

Laravel Scout array driver is a package provided by @Sti3bas, which makes Laravel Scout search testing more convenient:

Do you know about Laravel Scout array driver?

This package adds a
array

driver to Laravel Scout and provides custom PHPUnit assertions to make testing functionality related to search easier.

The package comes with a Search

facade that provides methods to make searching more convenient:
$user = factory(User::class)->create([
    'name' => 'Oliver',
]);

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

Search::assertContains($user) // passes
    ->assertContains($user2) // fails
    ->assertContains($user, function ($record) { // passes
        return $record['name'] === 'Oliver';
    })
    ->assertContains($user, function ($record) { // fails
        return $record['name'] === 'John';
    })
    ->assertContains($user2, function ($record) { // fails
        return $record['name'] === 'John';
    });
Copy after login

Search Facade There are tons of methods, which you should find in the readme. The standout in my opinion is the

fakeRecord

method, which allows you to fake a model's search index record.

$user = factory(User::class)->create([
    'id' => 123,
    'name' => 'Peter',
    'email' => 'peter@example.com',
]);

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

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

$this->assertEquals('Peter', $record['name']); // fails
$this->assertEquals('John', $record['name']); // passes
$this->assertTrue(!isset($record['email'])); // passes
Copy after login
You can learn more about this package at Sti3bas/laravel-scout-array-driver, get complete installation instructions and view the source code on GitHub.

Original address: https://laravel-news.com/laravel-scout-a...

Translation address: https://learnku.com/laravel/t /33376

The above is the detailed content of Do you know about Laravel Scout array driver?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!