From 7540199967e27ca73e862944c7e921809d93238a Mon Sep 17 00:00:00 2001 From: Mathieu TUDISCO Date: Wed, 2 Aug 2017 11:07:47 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=F0=9F=90=9B=20Add=20tests=20and=20fix?= =?UTF-8?q?=20bugs=20for=20RelationsInModelFinder?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Helpers/RelationsInModelFinder.php | 14 +++---- src/Traits/JsonExporter.php | 2 +- tests/Helpers/RelationsInModelFinderTest.php | 42 ++++++++++++++++++++ 3 files changed, 49 insertions(+), 9 deletions(-) create mode 100644 tests/Helpers/RelationsInModelFinderTest.php diff --git a/src/Helpers/RelationsInModelFinder.php b/src/Helpers/RelationsInModelFinder.php index 51f8ceb..ae95f31 100644 --- a/src/Helpers/RelationsInModelFinder.php +++ b/src/Helpers/RelationsInModelFinder.php @@ -8,9 +8,6 @@ class RelationsInModelFinder { private $model; - /** - * @var - */ private $relationsType; public function __construct(Model $model, array $relationsType) @@ -19,22 +16,23 @@ public function __construct(Model $model, array $relationsType) $this->relationsType = $relationsType; } - public static function children(Model $model) + public static function hasOneOrMany(Model $model) { - return (new static($model, ['hasMany', 'hasManyThrough', 'hasOne']))->find(); + return (new static($model, ['hasMany', 'hasOne']))->find(); } protected function find() { return collect(get_class_methods($this->model))->sort() ->reject(function ($method) { - $this->isAnEloquentMethod($method); + return $this->isAnEloquentMethod($method); })->filter(function ($method) { $code = $this->getMethodCode($method); - collect($this->relationsType)->contains(function ($relation) use ($code) { + + return collect($this->relationsType)->contains(function ($relation) use ($code) { return Str::contains($code, '$this->' . $relation . '('); }); - }); + })->toArray(); } protected function isAnEloquentMethod($method): bool diff --git a/src/Traits/JsonExporter.php b/src/Traits/JsonExporter.php index 6e7988c..ed01b07 100644 --- a/src/Traits/JsonExporter.php +++ b/src/Traits/JsonExporter.php @@ -47,7 +47,7 @@ public function exportRelations(): Collection public function getJsonExportableRelations(): array { - return $this->jsonExportableRelations ?? RelationsInModelFinder::children($this); + return $this->jsonExportableRelations ?? RelationsInModelFinder::hasOneOrMany($this); } diff --git a/tests/Helpers/RelationsInModelFinderTest.php b/tests/Helpers/RelationsInModelFinderTest.php new file mode 100644 index 0000000..1e0fb58 --- /dev/null +++ b/tests/Helpers/RelationsInModelFinderTest.php @@ -0,0 +1,42 @@ +assertEquals(['foos', 'bar'], RelationsInModelFinder::hasOneOrMany(new MyModel())); + } +} + +class MyModel extends \Illuminate\Database\Eloquent\Model +{ + public function foos() + { + return $this->hasMany('foo'); + } + + public function bar() + { + return $this->hasOne('bar'); + } + + public function baz() + { + return $this->hasManyThrough('baz', 'bar'); + } + + public function notARelation() + { + return 'this is not a relation!'; + } + + public function parent() + { + return $this->belongsTo('parent'); + } + +}