]> BookStack Code Mirror - bookstack/blob - tests/Entity/SearchOptionsTest.php
Fix Dark theme
[bookstack] / tests / Entity / SearchOptionsTest.php
1 <?php
2
3 namespace Tests\Entity;
4
5 use BookStack\Search\SearchOptions;
6 use Tests\TestCase;
7
8 class SearchOptionsTest extends TestCase
9 {
10     public function test_from_string_parses_a_search_string_properly()
11     {
12         $options = SearchOptions::fromString('cat "dog" [tag=good] {is_tree}');
13
14         $this->assertEquals(['cat'], $options->searches);
15         $this->assertEquals(['dog'], $options->exacts);
16         $this->assertEquals(['tag=good'], $options->tags);
17         $this->assertEquals(['is_tree' => ''], $options->filters);
18     }
19
20     public function test_to_string_includes_all_items_in_the_correct_format()
21     {
22         $expected = 'cat "dog" [tag=good] {is_tree}';
23         $options = new SearchOptions();
24         $options->searches = ['cat'];
25         $options->exacts = ['dog'];
26         $options->tags = ['tag=good'];
27         $options->filters = ['is_tree' => ''];
28
29         $output = $options->toString();
30         foreach (explode(' ', $expected) as $term) {
31             $this->assertStringContainsString($term, $output);
32         }
33     }
34
35     public function test_correct_filter_values_are_set_from_string()
36     {
37         $opts = SearchOptions::fromString('{is_tree} {name:dan} {cat:happy}');
38
39         $this->assertEquals([
40             'is_tree' => '',
41             'name'    => 'dan',
42             'cat'     => 'happy',
43         ], $opts->filters);
44     }
45 }