|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace SpameriTests\ElasticQuery\Query; |
| 4 | + |
| 5 | +require_once __DIR__ . '/../../bootstrap.php'; |
| 6 | + |
| 7 | + |
| 8 | +class Fuzzy extends \Tester\TestCase |
| 9 | +{ |
| 10 | + |
| 11 | + public function testCreate() : void |
| 12 | + { |
| 13 | + $fuzzy = new \Spameri\ElasticQuery\Query\Fuzzy( |
| 14 | + 'name', |
| 15 | + 'Avengers', |
| 16 | + 1.0, |
| 17 | + 2, |
| 18 | + 0, |
| 19 | + 100 |
| 20 | + ); |
| 21 | + |
| 22 | + $array = $fuzzy->toArray(); |
| 23 | + |
| 24 | + \Tester\Assert::true(isset($array['fuzzy']['name']['value'])); |
| 25 | + \Tester\Assert::same('Avengers', $array['fuzzy']['name']['value']); |
| 26 | + \Tester\Assert::same(1.0, $array['fuzzy']['name']['boost']); |
| 27 | + \Tester\Assert::same(2, $array['fuzzy']['name']['fuzziness']); |
| 28 | + \Tester\Assert::same(0, $array['fuzzy']['name']['prefix_length']); |
| 29 | + \Tester\Assert::same(100, $array['fuzzy']['name']['max_expansions']); |
| 30 | + |
| 31 | + $document = new \Spameri\ElasticQuery\Document( |
| 32 | + 'spameri_video', |
| 33 | + new \Spameri\ElasticQuery\Document\Body\Plain( |
| 34 | + ( |
| 35 | + new \Spameri\ElasticQuery\ElasticQuery( |
| 36 | + new \Spameri\ElasticQuery\Query\QueryCollection( |
| 37 | + new \Spameri\ElasticQuery\Query\MustCollection( |
| 38 | + $fuzzy |
| 39 | + ) |
| 40 | + ) |
| 41 | + ) |
| 42 | + )->toArray() |
| 43 | + ), |
| 44 | + 'spameri_video' |
| 45 | + ); |
| 46 | + |
| 47 | + $ch = curl_init(); |
| 48 | + curl_setopt($ch, CURLOPT_URL, 'localhost:9200/_search'); |
| 49 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
| 50 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); |
| 51 | + curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); |
| 52 | + curl_setopt( |
| 53 | + $ch, CURLOPT_POSTFIELDS, |
| 54 | + \json_encode($document->toArray()['body']) |
| 55 | + ); |
| 56 | + |
| 57 | + \Tester\Assert::noError(static function () use ($ch) { |
| 58 | + $response = curl_exec($ch); |
| 59 | + $resultMapper = new \Spameri\ElasticQuery\Response\ResultMapper(); |
| 60 | + /** @var \Spameri\ElasticQuery\Response\ResultSearch $result */ |
| 61 | + $result = $resultMapper->map(\json_decode($response, TRUE)); |
| 62 | + \Tester\Assert::type('int', $result->stats()->total()); |
| 63 | + }); |
| 64 | + |
| 65 | + curl_close($ch); |
| 66 | + } |
| 67 | + |
| 68 | +} |
| 69 | + |
| 70 | +(new Fuzzy())->run(); |
0 commit comments