PHP Classes

File: tests/FuzzySearch/CustomSearchFunctionTest.php

Recommend this page to a friend!
  Classes of AccountKiller   Fuse   tests/FuzzySearch/CustomSearchFunctionTest.php   Download  
File: tests/FuzzySearch/CustomSearchFunctionTest.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Fuse
Fuzzy search of arrays using the Bitap algorithm
Author: By
Last change:
Date: 27 days ago
Size: 1,359 bytes
 

Contents

Class file image Download
<?php

declare(strict_types=1);

use
Fuse\Fuse;

beforeEach(function () {
   
$this->fuse = new Fuse(
        [
            [
               
'title' => 'Old Man\'s War',
               
'author' => [
                   
'firstName' => 'John',
                   
'lastName' => 'Scalzi',
                ],
            ],
            [
               
'title' => 'The Lock Artist',
               
'author' => [
                   
'firstName' => 'Steve',
                   
'lastName' => 'Hamilton',
                ],
            ],
        ],
        [
           
'keys' => ['title', 'author.firstName'],
           
'getFn' => function ($obj) {
                if (!
$obj) {
                    return
null;
                }
               
$obj = $obj['author']['lastName'];
                return
$obj;
            },
        ],
    );
});

it('returns at least one result when searching for "Hmlt"', function () {
   
$result = $this->fuse->search('Hmlt');

   
expect($result)->toHaveCount(1);
   
expect($result[0]['item'])->toMatchArray([
       
'title' => 'The Lock Artist',
       
'author' => [
           
'firstName' => 'Steve',
           
'lastName' => 'Hamilton',
        ],
    ]);
});

it('returns no results when searching for "Stve"', function () {
   
$result = $this->fuse->search('Stve');

   
expect($result)->toHaveCount(0);
});