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

Do you know about Laravel Scout array driver?

藏色散人
Release: 2020-07-23 14:59:02
forward
2759 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. <div class="code" style="position:relative; padding:0px; margin:0px;"><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><div class="contentsignin">Copy after login</div></div> You can learn more about this package at <code>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