PHP Classes

File: tests/LogicalSearch/ParserTest.php

Recommend this page to a friend!
  Classes of AccountKiller   Fuse   tests/LogicalSearch/ParserTest.php   Download  
File: tests/LogicalSearch/ParserTest.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: 2,888 bytes
 

Contents

Class file image Download
<?php

declare(strict_types=1);

use function
Fuse\Core\parse;

beforeEach(function () {
   
test()->fuseOptions = [
       
'useExtendedSearch' => true,
       
'includeMatches' => true,
       
'includeScore' => true,
       
'keys' => ['title', 'author.firstName', 'author.lastName'],
    ];
});

it('parses tree structure correctly', function () {
   
$query = [
       
'$and' => [
            [
               
'title' => 'old war',
            ],
            [
               
'$or' => [
                    [
                       
'title' => '!arts',
                    ],
                    [
                       
'title' => '^lock',
                    ],
                ],
            ],
        ],
    ];

   
$root = parse($query, test()->fuseOptions, ['auto' => false]);

   
expect($root)->toEqual([
       
'children' => [
            [
               
'keyId' => 'title',
               
'pattern' => 'old war',
            ],
            [
               
'children' => [
                    [
                       
'keyId' => 'title',
                       
'pattern' => '!arts',
                    ],
                    [
                       
'keyId' => 'title',
                       
'pattern' => '^lock',
                    ],
                ],
               
'operator' => '$or',
            ],
        ],
       
'operator' => '$and',
    ]);
});

it('handles implicit operations correctly', function () {
   
$query = [
       
'$and' => [
            [
               
'title' => 'old war',
            ],
            [
               
'$or' => [
                    [
                       
'title' => '!arts',
                       
'tags' => 'kiro',
                    ],
                    [
                       
'title' => '^lock',
                    ],
                ],
            ],
        ],
    ];

   
$root = parse($query, test()->fuseOptions, ['auto' => false]);

   
expect($root)->toEqual([
       
'children' => [
            [
               
'keyId' => 'title',
               
'pattern' => 'old war',
            ],
            [
               
'children' => [
                    [
                       
'children' => [
                            [
                               
'keyId' => 'title',
                               
'pattern' => '!arts',
                            ],
                            [
                               
'keyId' => 'tags',
                               
'pattern' => 'kiro',
                            ],
                        ],
                       
'operator' => '$and',
                    ],
                    [
                       
'keyId' => 'title',
                       
'pattern' => '^lock',
                    ],
                ],
               
'operator' => '$or',
            ],
        ],
       
'operator' => '$and',
    ]);
});