|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * In these tests we're interested only in testing the Elasticquent search methods logic |
| 5 | + * and its ability to interpret and format the results. We're not interested in testing |
| 6 | + * the ElasticSearch client or our ability to hit an ElasticSearch database. Getting that |
| 7 | + * right is the job of those nice people at Elastic. |
| 8 | + * |
| 9 | + * So searches are made against a test model containing a mock of the ElasticSearch client. |
| 10 | + * This mock accepts search parameters, converts them into a parameter array and returns |
| 11 | + * results in the format that we would expect if we were to hit a real database, and |
| 12 | + * specifically returns results consistent with the ElasticSearch PHP client version |
| 13 | + * 2.0 documentation. |
| 14 | + * |
| 15 | + * The Elasticquent method will then format the response and we test that the resulting |
| 16 | + * Elasticquent results collection methods return the results we expect to verify this. |
| 17 | + */ |
| 18 | + |
| 19 | +class ElasticSearchMethodsTest extends PHPUnit_Framework_TestCase |
| 20 | +{ |
| 21 | + protected $expectedHits = [ |
| 22 | + 'total' => 2, |
| 23 | + 'max_score' => 0.7768564, |
| 24 | + 'hits' => [ |
| 25 | + [ |
| 26 | + '_index' => 'my_custom_index_name', |
| 27 | + '_type' => 'test_table', |
| 28 | + '_score' => 0.7768564, |
| 29 | + '_source' => [ |
| 30 | + 'name' => 'foo', |
| 31 | + ] |
| 32 | + ], |
| 33 | + [ |
| 34 | + '_index' => 'my_custom_index_name', |
| 35 | + '_type' => 'test_table', |
| 36 | + '_score' => 0.5634561, |
| 37 | + '_source' => [ |
| 38 | + 'name' => 'bar', |
| 39 | + ] |
| 40 | + ], |
| 41 | + ] |
| 42 | + ]; |
| 43 | + |
| 44 | + public function setUp() |
| 45 | + { |
| 46 | + $this->model = new SearchTestModel; |
| 47 | + } |
| 48 | + |
| 49 | + public function testSuccessfulSearch() |
| 50 | + { |
| 51 | + $result = $this->model::search('with results'); |
| 52 | + |
| 53 | + $this->assertInstanceOf('Elasticquent\ElasticquentResultCollection', $result); |
| 54 | + $this->assertEquals(2, $result->totalHits()); |
| 55 | + $this->assertEquals(0.7768564, $result->maxScore()); |
| 56 | + $this->assertEquals(['total' => 5,'successful' => 5,'unsuccessful' => 0], $result->getShards()); |
| 57 | + $this->assertEquals(8, $result->took()); |
| 58 | + $this->assertFalse($result->timedOut()); |
| 59 | + $this->assertEquals($this->expectedHits, $result->getHits()); |
| 60 | + $this->assertEmpty($result->getAggregations()); |
| 61 | + } |
| 62 | + |
| 63 | + public function testUnsuccessfulSearch() |
| 64 | + { |
| 65 | + $result = $this->model::search('with no results'); |
| 66 | + |
| 67 | + $expectedHits = [ |
| 68 | + 'total' => 0, |
| 69 | + 'max_score' => null, |
| 70 | + 'hits' => [] |
| 71 | + ]; |
| 72 | + |
| 73 | + $this->assertInstanceOf('Elasticquent\ElasticquentResultCollection', $result); |
| 74 | + $this->assertEquals(0, $result->totalHits()); |
| 75 | + $this->assertNull($result->maxScore()); |
| 76 | + $this->assertEquals(['total' => 5,'successful' => 5,'unsuccessful' => 0], $result->getShards()); |
| 77 | + $this->assertEquals(4, $result->took()); |
| 78 | + $this->assertFalse($result->timedOut()); |
| 79 | + $this->assertEquals($expectedHits, $result->getHits()); |
| 80 | + $this->assertEmpty($result->getAggregations()); |
| 81 | + } |
| 82 | + |
| 83 | + public function testSearchWithEmptyParamters() |
| 84 | + { |
| 85 | + $this->model::search(); |
| 86 | + $this->model::search(null); |
| 87 | + $this->model::search(''); |
| 88 | + |
| 89 | + $this->addToAssertionCount(3); // does not throw an exception |
| 90 | + } |
| 91 | + |
| 92 | + public function testComplexSearch() |
| 93 | + { |
| 94 | + $params = complexParameters(); |
| 95 | + $result = $this->model::complexSearch($params); |
| 96 | + |
| 97 | + $this->assertInstanceOf('Elasticquent\ElasticquentResultCollection', $result); |
| 98 | + $this->assertEquals($this->expectedHits, $result->getHits()); |
| 99 | + } |
| 100 | +} |
0 commit comments