PHP Classes

File: tests/FuzzySearch/RecurseIntoArraysTest.php

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

Contents

Class file image Download
<?php

declare(strict_types=1);

use
Fuse\Fuse;

beforeEach(function () {
   
$this->fuse = new Fuse(
        [
            [
               
'ISBN' => '0765348276',
               
'title' => 'Old Man\'s War',
               
'author' => 'John Scalzi',
               
'tags' => ['fiction'],
            ],
            [
               
'ISBN' => '0312696957',
               
'title' => 'The Lock Artist',
               
'author' => 'Steve Hamilton',
               
'tags' => ['fiction'],
            ],
            [
               
'ISBN' => '0321784421',
               
'title' => 'HTML5',
               
'author' => 'Remy Sharp',
               
'tags' => ['web development', 'nonfiction'],
            ],
        ],
        [
           
'keys' => ['tags'],
           
'threshold' => 0,
           
'includeMatches' => true,
        ],
    );
});

test('when searching for the tag nonfiction', function () {
   
$result = $this->fuse->search('nonfiction');

   
// we get a list containing exactly 1 item
   
expect($result)->toHaveCount(1);

   
// whose value is the ISBN of the book
   
expect($result[0]['item']['ISBN'])->toBe('0321784421');

   
// with matched tag provided
   
expect($result[0]['matches'][0])->toMatchArray([
       
'indices' => [[0, 9]],
       
'value' => 'nonfiction',
       
'key' => 'tags',
       
'refIndex' => 1,
    ]);
});