PHP Classes

File: tests/FuzzySearch/MinCharLengthTest.php

Recommend this page to a friend!
  Classes of AccountKiller   Fuse   tests/FuzzySearch/MinCharLengthTest.php   Download  
File: tests/FuzzySearch/MinCharLengthTest.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: 10 days ago
Size: 1,730 bytes
 

Contents

Class file image Download
<?php

declare(strict_types=1);

namespace
Fuse\Test;

use
Fuse\Fuse;

it('searches for the term "test" and returns correct match indices', function () {
   
$fuse = new Fuse(
        [
't te tes test tes te t'],
        [
           
'includeMatches' => true,
           
'minMatchCharLength' => 2,
        ],
    );

   
$result = $fuse->search('test');

   
expect($result[0]['matches'][0]['indices'])->toHaveCount(3);
   
expect($result[0]['matches'][0]['indices'][0][0])->toBe(2);
   
expect($result[0]['matches'][0]['indices'][0][1])->toBe(3);
});

it('returns no results when searching for a string shorter than minMatchCharLength', function () {
   
$fuse = new Fuse(
        [
't te tes test tes te t'],
        [
           
'includeMatches' => true,
           
'minMatchCharLength' => 2,
        ],
    );

   
$result = $fuse->search('t');

   
expect($result)->toBeEmpty();
});

it('performs the main functionality correctly', function () {
   
$fuse = new Fuse(
        [
            [
               
'title' => 'HTML5',
               
'author' => [
                   
'firstName' => 'Remy',
                   
'lastName' => 'Sharp',
                ],
            ],
            [
               
'title' => 'Angels & Demons',
               
'author' => [
                   
'firstName' => 'Dan',
                   
'lastName' => 'Brown',
                ],
            ],
        ],
        [
           
'keys' => ['title', 'author.firstName'],
           
'includeMatches' => true,
           
'includeScore' => true,
           
'minMatchCharLength' => 3,
        ],
    );

   
$result = $fuse->search('remy');

   
expect($result)->toHaveCount(1);
   
expect($result[0]['matches'])->toHaveCount(1);
});