Home  >  Article  >  PHP Framework  >  Do you know about Laravel Scout array driver?

Do you know about Laravel Scout array driver?

藏色散人
藏色散人forward
2020-07-23 14:59:022667browse

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';
    });

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. <pre class="brush:php;toolbar:false">$user = factory(User::class)-&gt;create([ &amp;#39;id&amp;#39; =&gt; 123, &amp;#39;name&amp;#39; =&gt; &amp;#39;Peter&amp;#39;, &amp;#39;email&amp;#39; =&gt; &amp;#39;peter@example.com&amp;#39;, ]); Search::fakeRecord($user, [ &amp;#39;id&amp;#39; =&gt; 123, &amp;#39;name&amp;#39; =&gt; &amp;#39;John&amp;#39;, ], false); $record = User::search()-&gt;where(&amp;#39;id&amp;#39;, 123)-&gt;raw()[&amp;#39;hits&amp;#39;][0]; $this-&gt;assertEquals(&amp;#39;Peter&amp;#39;, $record[&amp;#39;name&amp;#39;]); // fails $this-&gt;assertEquals(&amp;#39;John&amp;#39;, $record[&amp;#39;name&amp;#39;]); // passes $this-&gt;assertTrue(!isset($record[&amp;#39;email&amp;#39;])); // passes</pre> 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!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete