PHP Classes

File: tests/FuzzySearch/StandardDottedKeysTest.php

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

Contents

Class file image Download
<?php

declare(strict_types=1);

use
Fuse\Fuse;

$list = [
    [
       
'title' => 'HTML5',
       
'author' => [
           
'firstName' => 'Remy',
           
'lastName' => 'Sharp',
        ],
    ],
    [
       
'title' => 'Angels & Demons',
       
'author' => [
           
'firstName' => 'rmy',
           
'lastName' => 'Brown',
        ],
    ],
];

test('we get matches', function () use ($list) {
   
$fuse = new Fuse($list, [
       
'keys' => ['title', ['author', 'firstName']],
       
'includeMatches' => true,
       
'includeScore' => true,
    ]);

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

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

test('we get a result with no matches', function () {
   
$fuse = new Fuse(
        [
            [
               
'title' => 'HTML5',
               
'author' => [
                   
'first.name' => 'Remy',
                   
'last.name' => 'Sharp',
                ],
            ],
            [
               
'title' => 'Angels & Demons',
               
'author' => [
                   
'first.name' => 'rmy',
                   
'last.name' => 'Brown',
                ],
            ],
        ],
        [
           
'keys' => ['title', ['author', 'first.name']],
           
'includeMatches' => true,
           
'includeScore' => true,
        ],
    );

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

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

test('keys with weights', function () use ($list) {
   
$fuse = new Fuse($list, [
       
'keys' => [
            [
               
'name' => 'title',
            ],
            [
               
'name' => ['author', 'firstName'],
            ],
        ],
       
'includeMatches' => true,
       
'includeScore' => true,
    ]);

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

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