Skip to content

Commit e060127

Browse files
committed
Merge pull request #29 from petercoles/master
Enhancements to test structure and tests for search methods
2 parents 96811af + 1166490 commit e060127

12 files changed

+367
-63
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"elasticsearch/elasticsearch": ">1.0 <2.1"
2525
},
2626
"require-dev": {
27-
"phpunit/phpunit": "~5.0"
27+
"phpunit/phpunit": "~5.0",
28+
"mockery/mockery": "^0.9.4"
2829
},
2930
"autoload": {
3031
"psr-4": {

src/ElasticquentTrait.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
<?php namespace Elasticquent;
1+
<?php
22

3-
use \Exception;
4-
use \Elasticquent\ElasticquentCollection as ElasticquentCollection;
5-
use \Elasticquent\ElasticquentResultCollection as ResultCollection;
3+
namespace Elasticquent;
4+
5+
use Exception;
66

77
/**
88
* Elasticquent Trait
@@ -12,7 +12,7 @@
1212
*/
1313
trait ElasticquentTrait
1414
{
15-
use ElasticquentClientTrait;
15+
use ElasticquentClientTrait, EloquentMethods;
1616

1717
/**
1818
* Uses Timestamps In Index
@@ -50,6 +50,15 @@ trait ElasticquentTrait
5050
*/
5151
protected $documentVersion = null;
5252

53+
/**
54+
* Mapping Properties
55+
*
56+
* Elasticsearch mapping properties
57+
*
58+
* @var null|array
59+
*/
60+
protected $mappingProperties = null;
61+
5362
/**
5463
* New Collection
5564
*
@@ -224,7 +233,7 @@ public static function reindex()
224233
* @param int $offset
225234
* @param array $sort
226235
*
227-
* @return ResultCollection
236+
* @return ElasticquentResultCollection
228237
*/
229238
public static function searchByQuery($query = null, $aggregations = null, $sourceFields = null, $limit = null, $offset = null, $sort = null)
230239
{
@@ -250,7 +259,7 @@ public static function searchByQuery($query = null, $aggregations = null, $sourc
250259

251260
$result = $instance->getElasticSearchClient()->search($params);
252261

253-
return new ResultCollection($result, $instance = new static);
262+
return new \Elasticquent\ElasticquentResultCollection($result, $instance = new static);
254263
}
255264

256265
/**
@@ -267,7 +276,7 @@ public static function complexSearch($params)
267276

268277
$result = $instance->getElasticSearchClient()->search($params);
269278

270-
return new ResultCollection($result, $instance = new static);
279+
return new \Elasticquent\ElasticquentResultCollection($result, $instance = new static);
271280
}
272281

273282
/**
@@ -277,9 +286,9 @@ public static function complexSearch($params)
277286
*
278287
* @param string $term
279288
*
280-
* @return ResultCollection
289+
* @return ElasticquentResultCollection
281290
*/
282-
public static function search($term = null)
291+
public static function search($term = '')
283292
{
284293
$instance = new static;
285294

@@ -289,7 +298,7 @@ public static function search($term = null)
289298

290299
$result = $instance->getElasticSearchClient()->search($params);
291300

292-
return new ResultCollection($result, $instance = new static);
301+
return new \Elasticquent\ElasticquentResultCollection($result, $instance = new static);
293302
}
294303

295304
/**

src/EloquentMethods.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Elasticquent;
4+
5+
trait EloquentMethods
6+
{
7+
/**
8+
* Convert the model instance to an array.
9+
*/
10+
abstract public function toArray();
11+
12+
/**
13+
* Get the value of the model's primary key.
14+
*/
15+
abstract public function getKey();
16+
17+
/**
18+
* Get the table associated with the model.
19+
*/
20+
abstract public function getTable();
21+
22+
/**
23+
* Create a new instance of the given model.
24+
*
25+
* @param array $attributes
26+
* @param bool $exists
27+
*/
28+
abstract public function newInstance($attributes =[], $exists = false);
29+
30+
/**
31+
* Get a new query builder for the model's table.
32+
*/
33+
abstract public function newQuery();
34+
}

tests/ElasticSearchMethodsTest.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
}

tests/ElasticquentTraitTest.php

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,48 +9,50 @@ class ElasticquentTraitTest extends PHPUnit_Framework_TestCase {
99
*
1010
* @return void
1111
*/
12-
public function testingModel()
12+
public function setup()
1313
{
14-
$model = new TestModel;
15-
$model->fill($this->modelData);
14+
$this->model = new TestModel;
15+
$this->model->fill($this->modelData);
16+
}
1617

17-
return $model;
18+
/**
19+
* Test type name inferred from table name
20+
*/
21+
public function testTypeNameInferredFromTableName()
22+
{
23+
$this->assertEquals('test_table', $this->model->getTypeName());
1824
}
1925

2026
/**
21-
* Test getTypeName()
27+
* Test type name overrides table name
2228
*/
23-
public function testGetTypeName()
29+
public function testTypeNameOverridesTableName()
2430
{
25-
$model = $this->testingModel();
26-
$this->assertEquals('testing', $model->getTypeName());
31+
$model = new TestModelWithCustomTypeName;
32+
$this->assertEquals('test_type_name', $model->getTypeName());
2733
}
2834

2935
/**
3036
* Test Basic Properties Getters
3137
*/
3238
public function testBasicPropertiesGetters()
3339
{
34-
$model = $this->testingModel();
35-
36-
$model->useTimestampsInIndex();
37-
$this->assertTrue($model->usesTimestampsInIndex());
40+
$this->model->useTimestampsInIndex();
41+
$this->assertTrue($this->model->usesTimestampsInIndex());
3842

39-
$model->dontUseTimestampsInIndex();
40-
$this->assertFalse($model->usesTimestampsInIndex());
43+
$this->model->dontUseTimestampsInIndex();
44+
$this->assertFalse($this->model->usesTimestampsInIndex());
4145
}
4246

4347
/**
4448
* Testing Mapping Setup
4549
*/
4650
public function testMappingSetup()
4751
{
48-
$model = $this->testingModel();
49-
5052
$mapping = array('foo' => 'bar');
5153

52-
$model->setMappingProperties($mapping);
53-
$this->assertEquals($mapping, $model->getMappingProperties());
54+
$this->model->setMappingProperties($mapping);
55+
$this->assertEquals($mapping, $this->model->getMappingProperties());
5456
}
5557

5658
/**
@@ -59,8 +61,7 @@ public function testMappingSetup()
5961
public function testIndexDocumentData()
6062
{
6163
// Basic
62-
$model = $this->testingModel();
63-
$this->assertEquals($this->modelData, $model->getIndexDocumentData());
64+
$this->assertEquals($this->modelData, $this->model->getIndexDocumentData());
6465

6566
// Custom
6667
$custom = new CustomTestModel();
@@ -75,10 +76,8 @@ public function testIndexDocumentData()
7576
*/
7677
public function testDocumentNullStates()
7778
{
78-
$model = $this->testingModel();
79-
80-
$this->assertFalse($model->isDocument());
81-
$this->assertNull($model->documentScore());
79+
$this->assertFalse($this->model->isDocument());
80+
$this->assertNull($this->model->documentScore());
8281
}
8382

8483
}

tests/TestModels.php

Lines changed: 0 additions & 27 deletions
This file was deleted.

tests/models/CustomTestModel.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
use Elasticquent\ElasticquentInterface;
4+
use Elasticquent\ElasticquentTrait;
5+
use Illuminate\Database\Eloquent\Model as Eloquent;
6+
7+
class CustomTestModel extends Eloquent implements ElasticquentInterface {
8+
9+
use ElasticquentTrait;
10+
11+
protected $fillable = array('name');
12+
13+
function getIndexDocumentData()
14+
{
15+
return array('foo' => 'bar');
16+
}
17+
}

0 commit comments

Comments
 (0)