Skip to content

Commit

Permalink
Added unit test for basic elastic search, fixed hydration method to a…
Browse files Browse the repository at this point in the history
…ccept any eloquent collection
  • Loading branch information
zakhenry committed Aug 21, 2015
1 parent 4afe84d commit ca8e6d5
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
6 changes: 6 additions & 0 deletions api/app/Models/BaseModel.php
Expand Up @@ -34,8 +34,14 @@ protected function castAttribute($key, $value)

switch ($this->getCastType($key)) {
case 'date':
if (is_array($value)){
return $this->asDateTime($value);
}
return Carbon::createFromFormat('Y-m-d', $value);
case 'datetime':
if (is_array($value)){
return $this->asDateTime($value);
}
return Carbon::createFromFormat('Y-m-d H:i:s', $value);
default:
return $value;
Expand Down
3 changes: 3 additions & 0 deletions api/app/Models/TestEntity.php
@@ -1,5 +1,6 @@
<?php namespace App\Models;

use Elasticquent\ElasticquentTrait;
use Illuminate\Database\Eloquent\Collection;

/**
Expand All @@ -9,6 +10,8 @@
*/
class TestEntity extends BaseModel
{

use ElasticquentTrait;
/**
* The database table used by the model.
*
Expand Down
2 changes: 1 addition & 1 deletion api/database/factories/ModelFactory.php
Expand Up @@ -20,7 +20,7 @@
return new \App\Services\SpiraValidator($translator, $data, $rules, $messages);
});

$factory->define(App\Models\TestEntity::class, function ($faker) {
$factory->define(App\Models\TestEntity::class, function (\Faker\Generator $faker) {
return [
'entity_id' => $faker->uuid,
'varchar' => $faker->word,
Expand Down
4 changes: 2 additions & 2 deletions api/src/Model/Model/BaseModel.php
Expand Up @@ -400,10 +400,10 @@ protected function getRelationCacheKey($method)
* The method is more efficient if is passed a Collection of existing entries otherwise it will do a query for every entity
*
* @param array $requestCollection
* @param Collection $existingModels
* @param \Illuminate\Database\Eloquent\Collection $existingModels
* @return \Illuminate\Database\Eloquent\Collection
*/
public function hydrateRequestCollection(array $requestCollection, Collection $existingModels = null)
public function hydrateRequestCollection(array $requestCollection, \Illuminate\Database\Eloquent\Collection $existingModels = null)
{
$keyName = $this->getKeyName();
$models = array_map(function ($item) use ($keyName, $existingModels) {
Expand Down
29 changes: 29 additions & 0 deletions api/tests/integration/EntityTest.php
Expand Up @@ -647,4 +647,33 @@ public function testNoInnerLumenUrlDecode()
$this->assertJsonArray();
$this->assertEquals(urldecode($object->test), $compareString);
}

public function testEntitySearch()
{

TestEntity::deleteIndex();

$searchEntity = factory(App\Models\TestEntity::class)->create([
'varchar' => 'searchforthisstring'
]);

TestEntity::addAllToIndex();

sleep(1); //give the elastic search agent time to index (!)

$this->get('/test/entities/pages?q=searchforthisstring', ['Range'=>'entities=0-9']);

$collection = json_decode($this->response->getContent());
$this->assertResponseStatus(206);
$this->shouldReturnJson();
$this->assertJsonArray();


$this->assertCount(1, $collection);

$this->assertEquals($searchEntity->entity_id, $collection[0]->entityId);

}


}

0 comments on commit ca8e6d5

Please sign in to comment.