Skip to content

Add an ability to fake search response data #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/Engines/ArrayEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ class ArrayEngine extends Engine
*/
protected $softDelete;

/**
* @var Collection
*/
protected $searchResponseMocks;

public function __construct($store, $softDelete = false)
{
$this->store = $store;
$this->softDelete = $softDelete;
$this->searchResponseMocks = collect();
}

/**
Expand Down Expand Up @@ -118,10 +124,16 @@ protected function performSearch(Builder $builder, array $options = [])

$matches = Collection::make($matches);

return [
$searchResponseMock = $this->searchResponseMocks->first(function ($mock) use ($builder, $index) {
return $mock->matches($builder) && (($mock->fakeBuilder->index == '*') || ($index == $mock->fakeBuilder->index));
});

$response = [
'hits' => (isset($options['perPage']) ? $matches->slice((($options['page'] ?? 1) - 1) * $options['perPage'], $options['perPage']) : $matches)->values()->all(),
'total' => $matches->count(),
];

return $searchResponseMock ? $searchResponseMock->toArray($response) : $response;
}

/**
Expand Down Expand Up @@ -292,4 +304,9 @@ protected function buildSearchQuery(Builder $builder)
$builder, $this->addAdditionalConstraints($builder, $query->take($builder->limit))
);
}

public function addSearchResponseMock($mock)
{
$this->searchResponseMocks->add($mock);
}
}
105 changes: 105 additions & 0 deletions src/FakeBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace Sti3bas\ScoutArray;

class FakeBuilder
{
public $index = '*';

public $wheres = [];

public $whereIns = [];

public $whereNotIns = [];

public $limit;

public $orders = [];

public $options = [];

public $query;

public function within($index)
{
$this->index = $index;

return $this;
}

public function where($field, $value)
{
$this->wheres[$field] = $value;

return $this;
}

public function whereIn($field, array $values)
{
$this->whereIns[$field] = $values;

return $this;
}

public function whereNotIn($field, array $values)
{
$this->whereNotIns[$field] = $values;

return $this;
}

public function withTrashed()
{
unset($this->wheres['__soft_deleted']);

return $this;
}

public function onlyTrashed()
{
return tap($this->withTrashed(), function () {
$this->wheres['__soft_deleted'] = 1;
});
}

public function take($limit)
{
$this->limit = $limit;

return $this;
}

public function orderBy($column, $direction = 'asc')
{
$this->orders[] = [
'column' => $column,
'direction' => strtolower($direction) == 'asc' ? 'asc' : 'desc',
];

return $this;
}

public function latest($column = 'created_at')
{
return $this->orderBy($column, 'desc');
}

public function oldest($column = 'created_at')
{
return $this->orderBy($column, 'asc');
}

public function options(array $options)
{
$this->options = $options;

return $this;
}

public function query($query)
{
$this->query = $query;

return $this;
}
}
4 changes: 2 additions & 2 deletions src/ScoutArrayEngineServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public function boot()
});

$this->app[EngineManager::class]->extend('array', function ($app) {
return new ArrayEngine($this->app[ArrayStore::class], config('scout.soft_delete'));
return new ArrayEngine($app[ArrayStore::class], config('scout.soft_delete'));
});

$this->app->bind(Search::class, function () {
return new Search($this->app[ArrayStore::class]);
return new Search($this->app[EngineManager::class]->engine('array'));
});
}
}
21 changes: 18 additions & 3 deletions src/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
use Closure;
use Illuminate\Database\Eloquent\Model;
use PHPUnit\Framework\Assert;
use Sti3bas\ScoutArray\Engines\ArrayEngine;

class Search
{
protected $store;
protected ArrayStore $store;

public function __construct(ArrayStore $store)
protected ArrayEngine $engine;

public function __construct(ArrayEngine $engine)
{
$this->store = $store;
$this->store = $engine->store;
$this->engine = $engine;
}

public function assertContains(Model $model, ?Closure $callback = null): self
Expand Down Expand Up @@ -254,4 +258,15 @@ public function fakeRecord(Model $model, array $data, bool $merge = true, ?strin

return $this;
}

public function fakeResponseData(array $data)
{
$fakeBuilder = new FakeBuilder();

$searchResponseMock = new SearchResponseMock($fakeBuilder, $data);

$this->engine->addSearchResponseMock($searchResponseMock);

return $fakeBuilder;
}
}
34 changes: 34 additions & 0 deletions src/SearchResponseMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Sti3bas\ScoutArray;

use Laravel\Scout\Builder;

class SearchResponseMock
{
public FakeBuilder $fakeBuilder;

public array $mockData;

public function __construct(FakeBuilder $fakeBuilder, array $mockData)
{
$this->fakeBuilder = $fakeBuilder;
$this->mockData = $mockData;
}

public function matches(Builder $builder)
{
return $builder->wheres == $this->fakeBuilder->wheres &&
$builder->whereIns == $this->fakeBuilder->whereIns &&
$builder->whereNotIns == $this->fakeBuilder->whereNotIns &&
$builder->limit == $this->fakeBuilder->limit &&
$builder->orders == $this->fakeBuilder->orders &&
$builder->options == $this->fakeBuilder->options &&
$builder->query == $this->fakeBuilder->query;
}

public function toArray(array $data): array
{
return array_merge($data, $this->mockData);
}
}
Loading
Loading