|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Search; |
| 4 | + |
| 5 | +use Statamic\Facades\Entry; |
| 6 | +use Statamic\Search\Searchables; |
| 7 | +use Tests\TestCase; |
| 8 | + |
| 9 | +class SearchablesTest extends TestCase |
| 10 | +{ |
| 11 | + /** @test */ |
| 12 | + public function it_transforms_values_set_in_the_config_file() |
| 13 | + { |
| 14 | + config()->set('statamic.search.indexes.default', [ |
| 15 | + 'fields' => [ |
| 16 | + 'title', |
| 17 | + ], |
| 18 | + 'transformers' => [ |
| 19 | + 'title' => function ($value) { |
| 20 | + return strtoupper($value); |
| 21 | + }, |
| 22 | + ], |
| 23 | + ]); |
| 24 | + |
| 25 | + $index = app(\Statamic\Search\Comb\Index::class, [ |
| 26 | + 'name' => 'default', |
| 27 | + 'config' => config('statamic.search.indexes.default'), |
| 28 | + ]); |
| 29 | + |
| 30 | + $searchable = Entry::make()->data(['title' => 'Hello']); |
| 31 | + $searchables = new Searchables($index); |
| 32 | + |
| 33 | + $this->assertEquals([ |
| 34 | + 'title' => 'HELLO', |
| 35 | + ], $searchables->fields($searchable)); |
| 36 | + } |
| 37 | + |
| 38 | + /** @test */ |
| 39 | + public function if_a_transformer_returns_an_array_it_gets_combined_into_the_results() |
| 40 | + { |
| 41 | + config()->set('statamic.search.indexes.default', [ |
| 42 | + 'fields' => [ |
| 43 | + 'title', |
| 44 | + ], |
| 45 | + 'transformers' => [ |
| 46 | + 'title' => function ($value) { |
| 47 | + return [ |
| 48 | + 'title' => $value, |
| 49 | + 'title_upper' => strtoupper($value), |
| 50 | + ]; |
| 51 | + }, |
| 52 | + ], |
| 53 | + ]); |
| 54 | + |
| 55 | + $index = app(\Statamic\Search\Comb\Index::class, [ |
| 56 | + 'name' => 'default', |
| 57 | + 'config' => config('statamic.search.indexes.default'), |
| 58 | + ]); |
| 59 | + |
| 60 | + $searchable = Entry::make()->data(['title' => 'Hello']); |
| 61 | + $searchables = new Searchables($index); |
| 62 | + |
| 63 | + $this->assertEquals([ |
| 64 | + 'title' => 'Hello', |
| 65 | + 'title_upper' => 'HELLO', |
| 66 | + ], $searchables->fields($searchable)); |
| 67 | + } |
| 68 | +} |
0 commit comments