Skip to content
This repository was archived by the owner on Jan 25, 2022. It is now read-only.

Add missing test case #9

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
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
42 changes: 28 additions & 14 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,60 +21,74 @@ env:
matrix:
fast_finish: true
include:
### php 7.1
# sqlite
- php: 7.1
env: LARAVEL='5.5.*' TESTBENCH='3.5.*' DB_CONNECTION=sqlite DB_DATABASE=./database.sqlite
- php: 7.1
env: LARAVEL='5.6.*' TESTBENCH='3.6.*' DB_CONNECTION=sqlite DB_DATABASE=./database.sqlite
- php: 7.1
env: LARAVEL='5.7.*' TESTBENCH='3.7.*' DB_CONNECTION=sqlite DB_DATABASE=./database.sqlite
# mysql
- php: 7.1
env: LARAVEL='5.5.*' TESTBENCH='3.5.*' DB_CONNECTION=mysql DB_USERNAME=root
- php: 7.1
env: LARAVEL='5.6.*' TESTBENCH='3.6.*' DB_CONNECTION=mysql DB_USERNAME=root
- php: 7.1
env: LARAVEL='5.7.*' TESTBENCH='3.7.*' DB_CONNECTION=mysql DB_USERNAME=root
# pgsql
- php: 7.1
env: LARAVEL='5.5.*' TESTBENCH='3.5.*' DB_CONNECTION=pgsql DB_USERNAME=postgres
- php: 7.1
env: LARAVEL='5.6.*' TESTBENCH='3.6.*' DB_CONNECTION=pgsql DB_USERNAME=postgres
- php: 7.1
env: LARAVEL='5.7.*' TESTBENCH='3.7.*' DB_CONNECTION=pgsql DB_USERNAME=postgres
- php: 7.1

## php 7.2
# sqlite
- php: 7.2
env: LARAVEL='5.5.*' TESTBENCH='3.5.*' DB_CONNECTION=sqlite DB_DATABASE=./database.sqlite
- php: 7.1
env: LARAVEL='5.6.*' TESTBENCH='3.6.*' DB_CONNECTION=pgsql DB_USERNAME=postgres
- php: 7.1
env: LARAVEL='5.7.*' TESTBENCH='3.7.*' DB_CONNECTION=pgsql DB_USERNAME=postgres
- php: 7.2
env: LARAVEL='5.6.*' TESTBENCH='3.6.*' DB_CONNECTION=sqlite DB_DATABASE=./database.sqlite
- php: 7.2
env: LARAVEL='5.7.*' TESTBENCH='3.7.*' DB_CONNECTION=sqlite DB_DATABASE=./database.sqlite
# mysql
- php: 7.2
env: LARAVEL='5.5.*' TESTBENCH='3.5.*' DB_CONNECTION=mysql DB_USERNAME=root
- php: 7.2
env: LARAVEL='5.6.*' TESTBENCH='3.6.*' DB_CONNECTION=mysql DB_USERNAME=root
- php: 7.2
env: LARAVEL='5.7.*' TESTBENCH='3.7.*' DB_CONNECTION=mysql DB_USERNAME=root
# pgsql
- php: 7.2
env: LARAVEL='5.5.*' TESTBENCH='3.5.*' DB_CONNECTION=pgsql DB_USERNAME=postgres
- php: 7.2
env: LARAVEL='5.6.*' TESTBENCH='3.6.*' DB_CONNECTION=pgsql DB_USERNAME=postgres
- php: 7.2
env: LARAVEL='5.7.*' TESTBENCH='3.7.*' DB_CONNECTION=pgsql DB_USERNAME=postgres
- php: 7.2

## php 7.3
# sqlite
- php: 7.3
env: LARAVEL='5.5.*' TESTBENCH='3.5.*' DB_CONNECTION=sqlite DB_DATABASE=./database.sqlite
- php: 7.2
- php: 7.3
env: LARAVEL='5.6.*' TESTBENCH='3.6.*' DB_CONNECTION=sqlite DB_DATABASE=./database.sqlite
- php: 7.2
- php: 7.3
env: LARAVEL='5.7.*' TESTBENCH='3.7.*' DB_CONNECTION=sqlite DB_DATABASE=./database.sqlite
# mysql
- php: 7.3
env: LARAVEL='5.5.*' TESTBENCH='3.5.*' DB_CONNECTION=mysql DB_USERNAME=root
- php: 7.3
env: LARAVEL='5.6.*' TESTBENCH='3.6.*' DB_CONNECTION=mysql DB_USERNAME=root
- php: 7.3
env: LARAVEL='5.7.*' TESTBENCH='3.7.*' DB_CONNECTION=mysql DB_USERNAME=root
# pgsql
- php: 7.3
env: LARAVEL='5.5.*' TESTBENCH='3.5.*' DB_CONNECTION=pgsql DB_USERNAME=postgres
- php: 7.3
env: LARAVEL='5.6.*' TESTBENCH='3.6.*' DB_CONNECTION=pgsql DB_USERNAME=postgres
- php: 7.3
env: LARAVEL='5.7.*' TESTBENCH='3.7.*' DB_CONNECTION=pgsql DB_USERNAME=postgres
- php: 7.3
env: LARAVEL='5.5.*' TESTBENCH='3.5.*' DB_CONNECTION=sqlite DB_DATABASE=./database.sqlite
- php: 7.3
env: LARAVEL='5.6.*' TESTBENCH='3.6.*' DB_CONNECTION=sqlite DB_DATABASE=./database.sqlite
- php: 7.3
env: LARAVEL='5.7.*' TESTBENCH='3.7.*' DB_CONNECTION=sqlite DB_DATABASE=./database.sqlite

before_install:
- |
Expand Down
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
"illuminate/support": "~5.5.0|~5.6.0|~5.7.0"
},
"require-dev": {
"phpunit/phpunit": "^6.1|^7.0",
"orchestra/testbench": "~3.5.0|~3.6.0|~3.7.0",
"orchestra/database": "~3.5.0|~3.6.0|~3.7.0",
"php-coveralls/php-coveralls": "^2.1"
"mockery/mockery": "^1.2",
"orchestra/database": "~3.5.0|~3.6.0|~3.7.0",
"orchestra/testbench": "~3.5.0|~3.6.0|~3.7.0",
"php-coveralls/php-coveralls": "^2.1",
"phpunit/phpunit": "^6.1|^7.0"
},
"autoload": {
"psr-4": {
Expand Down
3 changes: 0 additions & 3 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,4 @@
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-php" target="build/cov/coverage.cov"/>
</logging>
</phpunit>
4 changes: 4 additions & 0 deletions src/scopes/FullScopes.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ protected function assemble(array $scopes)
{
$errors = [];
foreach ($scopes as $scope) {
if (is_string($scope)) {
$scope = app($scope);
}

if ($scope instanceof ScopeInterface) {
if (! in_array($scope, $this->scopes)) {
$this->scopes[] = $scope;
Expand Down
27 changes: 19 additions & 8 deletions src/scopes/SearchScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Goopil\RestFilter\Scopes;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Goopil\RestFilter\Contracts\Searchable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

/**
* Class SearchScope.
Expand Down Expand Up @@ -173,30 +173,37 @@ protected function parseCondition($value)
];
}

protected function mendSpecificFields($parameters)
protected function mendSpecificFields($parameters, $model = null, $fieldName = '')
{
if ($model !== null) {
$model->{$fieldName} = $parameters['value'];
$parameters['value'] = $model->{$fieldName};
}

if (in_array($parameters['condition'], ['like', 'ilike'])) {
$parameters['value'] = "%{$parameters['value']}%";
}

// todo: implements check with casts typing from model

return $parameters;
}

protected function formatWhereHasClause(Builder $query, $fieldName, $fieldQuery, $force = false)
{
foreach ($fieldQuery as $parameters) {
$parameters = $this->mendSpecificFields($parameters);
$method = $force ? 'whereHas' : 'orWhereHas';
$temp = explode('.', $fieldName);
$relation = $temp[0];
$fieldName = $temp[1];
$parameters = $this->mendSpecificFields($parameters, $this->model->{$relation}()->getModel(), $fieldName);

if ($parameters === null) {
continue;
}

if (method_exists($this->model, $relation)) {
$query->{$method}($relation, function (Builder $query) use ($relation, $fieldName, $parameters) {
$query->where(
"{$relation}.{$fieldName}",
$fieldName,
$parameters['condition'],
$parameters['value']
);
Expand All @@ -222,8 +229,12 @@ protected function formatWhereHasClause(Builder $query, $fieldName, $fieldQuery,
protected function formatWhereClause(Builder $query, $fieldName, $fieldQuery, $force = false)
{
foreach ($fieldQuery as $parameters) {
$parameters = $this->mendSpecificFields($parameters);
$method = $force ? 'where' : 'orWhere';
$parameters = $this->mendSpecificFields($parameters, $this->model, $fieldName);

if ($parameters === null) {
continue;
}

$query->{$method}(
"{$this->modelTableName}.{$fieldName}",
Expand Down
2 changes: 1 addition & 1 deletion tests/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected function getPackageProviders($app)
*/
protected function callEndpoint($params = [])
{
$response = $this->call('GET', $this->baseTestRelativeUrl, $params);
$response = $this->json('GET', $this->baseTestRelativeUrl, $params);

return json_decode($response->getContent(), true);
}
Expand Down
163 changes: 163 additions & 0 deletions tests/features/SearchTest/SingleKeyedAndTypedSearchOnRelationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?php

namespace Goopil\RestFilter\Tests\Features;

use Goopil\RestFilter\Tests\BaseTestCase;
use Goopil\RestFilter\Tests\Utils\TestModel;
use Goopil\RestFilter\Tests\Utils\TestRelatedModel;

class SingleKeyedAndTypedSearchOnRelationTest extends BaseTestCase
{
/**
* @test
*/
public function ItShouldProperlyApplyTheWhereClauseOnStringAttribute()
{
$first = TestRelatedModel::first();
$compare = TestModel::whereHas('related', function ($query) use ($first) {
return $query->where('string', $first->string);
})->get()->toArray();

$response = $this->callEndpoint([
'search' => ['related.string' => $first->string],
]);

$this->assertEquals($compare, $response);
}

/**
* @test
*/
public function ItShouldProperlyApplyTheWhereClauseOnCharAttribute()
{
$first = TestRelatedModel::first();
$compare = TestModel::whereHas('related', function ($query) use ($first) {
return $query->where('char', $first->char);
})->get()->toArray();

$response = $this->callEndpoint([
'search' => ['related.char' => $first->char],
]);

$this->assertEquals($compare, $response);
}

/**
* @test
*/
public function ItShouldProperlyApplyTheWhereClauseOnBoolAttribute()
{
$first = TestRelatedModel::first();
$compare = TestModel::whereHas('related', function ($query) use ($first) {
return $query->where('bool', $first->bool);
})->get()->toArray();

$response = $this->callEndpoint([
'search' => ['related.bool' => $first->bool],
]);

$this->assertEquals($compare, $response);
}

/**
* @test
*/
public function ItShouldProperlyApplyTheWhereClauseOnIntAttribute()
{
$first = TestRelatedModel::first();
$compare = TestModel::whereHas('related', function ($query) use ($first) {
return $query->where('int', $first->int);
})->get()->toArray();

$response = $this->callEndpoint([
'search' => ['related.int' => $first->int],
]);

$this->assertEquals($compare, $response);
}

/**
* @test
*/
public function ItShouldProperlyApplyTheWhereClauseOnDoubleAttribute()
{
$first = TestRelatedModel::first();
$compare = TestModel::whereHas('related', function ($query) use ($first) {
return $query->where('double', $first->double);
})->get()->toArray();

$response = $this->callEndpoint([
'search' => ['related.double' => $first->double],
]);

$this->assertEquals($compare, $response);
}

/**
* @test
*/
public function ItShouldProperlyApplyTheWhereClauseOnDecimalAttribute()
{
$first = TestRelatedModel::first();
$compare = TestModel::whereHas('related', function ($query) use ($first) {
return $query->where('decimal', $first->decimal);
})->get()->toArray();

$response = $this->callEndpoint([
'search' => ['related.decimal' => $first->decimal],
]);

$this->assertEquals($compare, $response);
}

/**
* @test
*/
public function ItShouldProperlyApplyTheWhereClauseOnDatetimeAttribute()
{
$first = TestRelatedModel::first();
$compare = TestModel::whereHas('related', function ($query) use ($first) {
return $query->where('datetime', $first->datetime);
})->get()->toArray();

$response = $this->callEndpoint([
'search' => ['related.datetime' => $first->toArray()['datetime']],
]);

$this->assertEquals($compare, $response);
}

/**
* @test
*/
public function ItShouldProperlyApplyTheWhereClauseOnDateAttribute()
{
$first = TestRelatedModel::first();
$compare = TestModel::whereHas('related', function ($query) use ($first) {
return $query->where('date', $first->date);
})->get()->toArray();

$response = $this->callEndpoint([
'search' => ['related.date' => $first->toArray()['date']],
]);

$this->assertEquals($compare, $response);
}

/**
* @test
*/
public function ItShouldProperlyApplyTheWhereClauseOnTimeAttribute()
{
$first = TestRelatedModel::first();
$compare = TestModel::whereHas('related', function ($query) use ($first) {
return $query->where('time', $first->time);
})->get()->toArray();

$response = $this->callEndpoint([
'search' => ['related.time' => $first->time],
]);

$this->assertEquals($compare, $response);
}
}
Loading