Home > Article > PHP Framework > Do you know about Laravel Scout array driver?
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:This package adds a
array
facade that provides methods to make searching more convenient:
The package comes with a
Search
$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
method, which allows you to fake a model's search index record. <pre class="brush:php;toolbar:false">$user = factory(User::class)->create([
&#39;id&#39; => 123,
&#39;name&#39; => &#39;Peter&#39;,
&#39;email&#39; => &#39;peter@example.com&#39;,
]);
Search::fakeRecord($user, [
&#39;id&#39; => 123,
&#39;name&#39; => &#39;John&#39;,
], false);
$record = User::search()->where(&#39;id&#39;, 123)->raw()[&#39;hits&#39;][0];
$this->assertEquals(&#39;Peter&#39;, $record[&#39;name&#39;]); // fails
$this->assertEquals(&#39;John&#39;, $record[&#39;name&#39;]); // passes
$this->assertTrue(!isset($record[&#39;email&#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...
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!