diff --git a/.travis.yml b/.travis.yml
index 7b25a9c..4b97e8d 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -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:
- |
diff --git a/composer.json b/composer.json
index dbcc3e9..c460fb4 100644
--- a/composer.json
+++ b/composer.json
@@ -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": {
diff --git a/phpunit.xml b/phpunit.xml
index 33be0a3..ab7649b 100644
--- a/phpunit.xml
+++ b/phpunit.xml
@@ -18,7 +18,4 @@
./src
-
-
-
diff --git a/src/scopes/FullScopes.php b/src/scopes/FullScopes.php
index b8d685f..41692bb 100644
--- a/src/scopes/FullScopes.php
+++ b/src/scopes/FullScopes.php
@@ -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;
diff --git a/src/scopes/SearchScope.php b/src/scopes/SearchScope.php
index 65c5a05..340ad0f 100644
--- a/src/scopes/SearchScope.php
+++ b/src/scopes/SearchScope.php
@@ -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.
@@ -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']
);
@@ -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}",
diff --git a/tests/BaseTestCase.php b/tests/BaseTestCase.php
index de09ba0..a0931f9 100644
--- a/tests/BaseTestCase.php
+++ b/tests/BaseTestCase.php
@@ -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);
}
diff --git a/tests/features/SearchTest/SingleKeyedAndTypedSearchOnRelationTest.php b/tests/features/SearchTest/SingleKeyedAndTypedSearchOnRelationTest.php
new file mode 100644
index 0000000..05ec9ba
--- /dev/null
+++ b/tests/features/SearchTest/SingleKeyedAndTypedSearchOnRelationTest.php
@@ -0,0 +1,163 @@
+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);
+ }
+}
diff --git a/tests/features/SearchTest/SingleKeyedAndTypedSearchTest.php b/tests/features/SearchTest/SingleKeyedAndTypedSearchTest.php
new file mode 100644
index 0000000..84e1c96
--- /dev/null
+++ b/tests/features/SearchTest/SingleKeyedAndTypedSearchTest.php
@@ -0,0 +1,144 @@
+string)->get()->toArray();
+
+ $response = $this->callEndpoint([
+ 'search' => ['string' => $first->string],
+ ]);
+
+ $this->assertEquals($compare, $response);
+ }
+
+ /**
+ * @test
+ */
+ public function ItShouldProperlyApplyTheWhereClauseOnCharAttribute()
+ {
+ $first = TestModel::first();
+ $compare = TestModel::where('char', $first->char)->get()->toArray();
+
+ $response = $this->callEndpoint([
+ 'search' => ['char' => $first->char],
+ ]);
+
+ $this->assertEquals($compare, $response);
+ }
+
+ /**
+ * @test
+ */
+ public function ItShouldProperlyApplyTheWhereClauseOnBoolAttribute()
+ {
+ $first = TestModel::first();
+ $compare = TestModel::where('bool', $first->bool)->get()->toArray();
+
+ $response = $this->callEndpoint([
+ 'search' => ['bool' => $first->bool],
+ ]);
+
+ $this->assertEquals($compare, $response);
+ }
+
+ /**
+ * @test
+ */
+ public function ItShouldProperlyApplyTheWhereClauseOnIntAttribute()
+ {
+ $first = TestModel::first();
+ $compare = TestModel::where('int', $first->int)->get()->toArray();
+
+ $response = $this->callEndpoint([
+ 'search' => ['int' => $first->int],
+ ]);
+
+ $this->assertEquals($compare, $response);
+ }
+
+ /**
+ * @test
+ */
+ public function ItShouldProperlyApplyTheWhereClauseOnDoubleAttribute()
+ {
+ $first = TestModel::first();
+ $compare = TestModel::where('double', $first->double)->get()->toArray();
+
+ $response = $this->callEndpoint([
+ 'search' => ['double' => $first->double],
+ ]);
+
+ $this->assertEquals($compare, $response);
+ }
+
+ /**
+ * @test
+ */
+ public function ItShouldProperlyApplyTheWhereClauseOnDecimalAttribute()
+ {
+ $first = TestModel::first();
+ $compare = TestModel::where('decimal', $first->decimal)->get()->toArray();
+
+ $response = $this->callEndpoint([
+ 'search' => ['decimal' => $first->decimal],
+ ]);
+
+ $this->assertEquals($compare, $response);
+ }
+
+ /**
+ * @test
+ */
+ public function ItShouldProperlyApplyTheWhereClauseOnDatetimeAttribute()
+ {
+ $first = TestModel::first();
+ $compare = TestModel::where('datetime', $first->datetime)->get()->toArray();
+
+ $response = $this->callEndpoint([
+ 'search' => ['datetime' => $first->toArray()['datetime']],
+ ]);
+
+ $this->assertEquals($compare, $response);
+ }
+
+ /**
+ * @test
+ */
+ public function ItShouldProperlyApplyTheWhereClauseOnDateAttribute()
+ {
+ $first = TestModel::first();
+ $compare = TestModel::where('date', $first->date)->get()->toArray();
+
+ $response = $this->callEndpoint([
+ 'search' => ['date' => $first->toArray()['date']],
+ ]);
+
+ $this->assertEquals($compare, $response);
+ }
+
+ /**
+ * @test
+ */
+ public function ItShouldProperlyApplyTheWhereClauseOnTimeAttribute()
+ {
+ $first = TestModel::first();
+ $compare = TestModel::where('time', $first->time)->get()->toArray();
+
+ $response = $this->callEndpoint([
+ 'search' => ['time' => $first->time],
+ ]);
+
+ $this->assertEquals($compare, $response);
+ }
+}
diff --git a/tests/features/SearchTest/SingleSearchQueryOnAllFieldsSearchTest.php b/tests/features/SearchTest/SingleSearchQueryOnAllFieldsSearchTest.php
new file mode 100644
index 0000000..e415108
--- /dev/null
+++ b/tests/features/SearchTest/SingleSearchQueryOnAllFieldsSearchTest.php
@@ -0,0 +1,148 @@
+string)->get()->toArray();
+//
+// $response = $this->callEndpoint([
+// 'search' => $first->string,
+// ]);
+//
+// $this->assertEquals($compare, $response);
+// }
+//
+// /**
+// * @test
+// */
+// public function ItShouldProperlyApplyTheWhereClauseOnCharAttribute()
+// {
+// $first = TestModel::first();
+// $compare = TestModel::where('char', $first->char)->orWhereHas('related', function ($query) use ($first) {
+// return $query->where('char', $first->char);
+// })->get()->toArray();
+//
+// $response = $this->callEndpoint([
+// 'search' => $first->char,
+// ]);
+//
+// $this->assertEquals($compare, $response);
+// }
+//
+// /**
+// * @test
+// */
+// public function ItShouldProperlyApplyTheWhereClauseOnBoolAttribute()
+// {
+// $first = TestModel::first();
+// $compare = TestModel::where('bool', $first->bool)->orWhereHas('related', function ($query) use ($first) {
+// return $query->where('bool', $first->bool);
+// })->get()->toArray();
+//
+// $response = $this->callEndpoint([
+// 'search' => $first->bool,
+// ]);
+//
+// $this->assertEquals($compare, $response);
+// }
+//
+// /**
+// * @test
+// */
+// public function ItShouldProperlyApplyTheWhereClauseOnIntAttribute()
+// {
+// $first = TestModel::first();
+// $compare = TestModel::where('int', $first->int)->get()->toArray();
+//
+// $response = $this->callEndpoint([
+// 'search' => $first->int,
+// ]);
+//
+// $this->assertEquals($compare, $response);
+// }
+//
+// /**
+// * @test
+// */
+// public function ItShouldProperlyApplyTheWhereClauseOnDoubleAttribute()
+// {
+// $first = TestModel::first();
+// $compare = TestModel::where('double', $first->double)->get()->toArray();
+//
+// $response = $this->callEndpoint([
+// 'search' => $first->double,
+// ]);
+//
+// $this->assertEquals($compare, $response);
+// }
+//
+// /**
+// * @test
+// */
+// public function ItShouldProperlyApplyTheWhereClauseOnDecimalAttribute()
+// {
+// $first = TestModel::first();
+// $compare = TestModel::where('decimal', $first->decimal)->get()->toArray();
+//
+// $response = $this->callEndpoint([
+// 'search' => $first->decimal,
+// ]);
+//
+// $this->assertEquals($compare, $response);
+// }
+//
+// /**
+// * @test
+// */
+// public function ItShouldProperlyApplyTheWhereClauseOnDatetimeAttribute()
+// {
+// $first = TestModel::first();
+// $compare = TestModel::where('datetime', $first->datetime)->get()->toArray();
+//
+// $response = $this->callEndpoint([
+// 'search' => $first->toArray()['datetime'],
+// ]);
+//
+// $this->assertEquals($compare, $response);
+// }
+//
+// /**
+// * @test
+// */
+// public function ItShouldProperlyApplyTheWhereClauseOnDateAttribute()
+// {
+// $first = TestModel::first();
+// $compare = TestModel::where('date', $first->date)->get()->toArray();
+//
+// $response = $this->callEndpoint([
+// 'search' => $first->toArray()['date'],
+// ]);
+//
+// $this->assertEquals($compare, $response);
+// }
+//
+// /**
+// * @test
+// */
+// public function ItShouldProperlyApplyTheWhereClauseOnTimeAttribute()
+// {
+// $first = TestModel::first();
+// $compare = TestModel::where('time', $first->time)->get()->toArray();
+//
+// $response = $this->callEndpoint([
+// 'search' => $first->time,
+// ]);
+//
+// $this->assertEquals($compare, $response);
+// }
+//}
diff --git a/tests/units/FullScopeTest.php b/tests/units/FullScopeTest.php
new file mode 100644
index 0000000..6a6ba00
--- /dev/null
+++ b/tests/units/FullScopeTest.php
@@ -0,0 +1,133 @@
+testedClass);
+ $reflection_property = $reflection->getProperty($property);
+ $reflection_property->setAccessible(true);
+
+ return $reflection_property->getValue($this->testedClass);
+ }
+
+ /**
+ * @test
+ */
+ public function ItShouldSetTheScopesWithAClassNameSpaceWithAnArrayAsArgument()
+ {
+ $this->testedClass = new FullScopes();
+ $scope = SearchScope::class;
+
+ $this->testedClass->setScopes([$scope]);
+
+ $this->assertEquals([new $scope], $this->getProtectedProperty('scopes'));
+ }
+
+ /**
+ * @test
+ */
+ public function ItShouldSetTheScopesWithAClassInstanceSpaceWithAnArrayAsArgument()
+ {
+ $this->testedClass = new FullScopes();
+ $singleScopeAsArray = [
+ new SearchScope,
+ ];
+
+ $this->testedClass->setScopes($singleScopeAsArray);
+
+ $this->assertEquals($singleScopeAsArray, $this->getProtectedProperty('scopes'));
+ }
+
+ /**
+ * @test
+ */
+ public function ItShouldSetTheScopesWithAClassInstanceSpaceAsASingleArgument()
+ {
+ $this->testedClass = new FullScopes();
+ $singleScopeAsString = SearchScope::class;
+
+ $this->testedClass->setScopes($singleScopeAsString);
+
+ $this->assertEquals([new $singleScopeAsString()], $this->getProtectedProperty('scopes'));
+ }
+
+ /**
+ * @test
+ */
+ public function itShouldSetTheScopeToBeAppliedOnNextRequestFeedingItAString()
+ {
+ $this->testedClass = new FullScopes();
+ $singleScopeAsString = new SearchScope;
+
+ $this->testedClass->setScopes($singleScopeAsString);
+
+ $this->assertEquals([$singleScopeAsString], $this->getProtectedProperty('scopes'));
+ }
+
+ /**
+ * @test
+ */
+ public function ItShouldKeepOnlyOneInstanceOfTheSameScope()
+ {
+ $this->testedClass = new FullScopes();
+ $firstScopeAsString = new SearchScope;
+ $secondScopeAsString = new SearchScope;
+
+ $this->testedClass->setScopes([$firstScopeAsString, $secondScopeAsString]);
+
+ $this->assertEquals([$firstScopeAsString], $this->getProtectedProperty('scopes'));
+ }
+
+ /**
+ * @test
+ */
+ public function ItShouldTrowAnInvalidArgumentExceptionIfWhatYouInjectIsNotAScope()
+ {
+ $this->testedClass = new FullScopes();
+ $scope = new \stdClass();
+ $this->expectException(\InvalidArgumentException::class);
+
+ $this->testedClass->setScopes($scope);
+ }
+
+ /**
+ * @test
+ */
+ public function ItShouldAddANewScopeToTheheap()
+ {
+ $this->testedClass = new FullScopes();
+ $scope = new class implements Scope {
+ public function apply(Builder $builder, Model $model)
+ {
+ }
+ };
+
+ $defaultScopes = $this->getProtectedProperty('scopes');
+ array_push($defaultScopes, $scope);
+
+ $this->testedClass->addScopes($scope);
+
+ $this->assertEquals($defaultScopes, $this->getProtectedProperty('scopes'));
+ }
+}
diff --git a/tests/units/QueryableTraitTest.php b/tests/units/QueryableTraitTest.php
new file mode 100644
index 0000000..090458c
--- /dev/null
+++ b/tests/units/QueryableTraitTest.php
@@ -0,0 +1,19 @@
+makePartial();
+ $mock->shouldReceive('bootQueryable')->once();
+ $mock->__construct();
+ }
+}
diff --git a/tests/utils/TestModel.php b/tests/utils/TestModel.php
index 3b46f32..b6844d7 100644
--- a/tests/utils/TestModel.php
+++ b/tests/utils/TestModel.php
@@ -4,8 +4,9 @@
use Illuminate\Database\Eloquent\Model;
use Goopil\RestFilter\Contracts\Paginable;
+use Goopil\RestFilter\Contracts\Searchable;
-class TestModel extends Model
+class TestModel extends Model implements Searchable
{
use Paginable;
@@ -13,20 +14,56 @@ class TestModel extends Model
protected $fillable = [
'bool',
-
'char',
'string',
'text',
-
'int',
'double',
'decimal',
-
'datetime',
'date',
'time',
];
+ public static function searchable()
+ {
+ return [
+ 'bool',
+ 'char',
+ 'string',
+ 'text',
+ 'int',
+ 'double',
+ 'decimal',
+ 'datetime',
+ 'date',
+ 'time',
+ 'related.bool',
+ 'related.char',
+ 'related.string',
+ 'related.text',
+ 'related.int',
+ 'related.double',
+ 'related.decimal',
+ 'related.datetime',
+ 'related.date',
+ 'related.time',
+ ];
+ }
+
+ protected $casts = [
+ 'bool' => 'bool',
+ 'char' => 'char',
+ 'string' => 'string',
+ 'text' => 'text',
+ 'int' => 'int',
+ 'double' => 'double',
+ 'decimal' => 'double',
+ 'datetime' => 'datetime',
+ 'date' => 'date',
+ 'time' => 'time',
+ ];
+
public function related()
{
return $this->hasMany(TestRelatedModel::class);
diff --git a/tests/utils/TestQueryableModel.php b/tests/utils/TestQueryableModel.php
new file mode 100644
index 0000000..4c50a49
--- /dev/null
+++ b/tests/utils/TestQueryableModel.php
@@ -0,0 +1,11 @@
+ 'bool',
+ 'char' => 'char',
+ 'string' => 'string',
+ 'text' => 'text',
+ 'int' => 'int',
+ 'double' => 'double',
+ 'decimal' => 'double',
+ 'datetime' => 'datetime',
+ 'date' => 'date',
+ 'time' => 'time',
+ ];
}
diff --git a/tests/utils/factories/TestFactory.php b/tests/utils/factories/TestFactory.php
index 51462b1..4036ffe 100644
--- a/tests/utils/factories/TestFactory.php
+++ b/tests/utils/factories/TestFactory.php
@@ -13,8 +13,8 @@
'text' => $faker->paragraph(10),
'int' => rand(1, 1000),
- 'double' => mt_rand() / mt_getrandmax(),
- 'decimal' => mt_rand() / mt_getrandmax(),
+ 'double' => $faker->randomFloat(2, 1, 1000 ),
+ 'decimal' => $faker->randomFloat(5, 1, 1000 ),
'datetime' => $faker->dateTime(),
'date' => $faker->date(),