diff --git a/app/User.php b/app/User.php index f8aa68b..73c93b3 100644 --- a/app/User.php +++ b/app/User.php @@ -320,10 +320,19 @@ class User extends Authenticatable return $this->hasMany(UserMetadata::class, 'user_id', 'id'); } - public function getMetadata($key) + public function getMetadata($key = null) { $metadata = $this->metadata; + if (is_null($key)) { + $metadataCollection = []; + foreach ($metadata as $metaKey => $metaValue) { + $metadataCollection[$metaKey] = $metaValue; + } + + return collect($metadataCollection); + } + $meta = $metadata->filter(function ($meta) use ($key) { return $meta->key == $key; })->first(); diff --git a/tests/Unit/UserTest.php b/tests/Unit/UserTest.php index ae7fd5e..9f139de 100644 --- a/tests/Unit/UserTest.php +++ b/tests/Unit/UserTest.php @@ -141,6 +141,24 @@ class UserTest extends TestCase } /** @test */ + public function user_model_get_metadata_method_returns_all_metadata_if_key_is_null() + { + $user = factory(User::class)->create(); + + $this->assertEmpty($user->getMetadata()); + + DB::table('user_metadata')->insert([ + 'id' => Uuid::uuid4()->toString(), + 'user_id' => $user->id, + 'key' => 'cemetery_location_address', + 'value' => 'Some address', + ]); + $user = $user->fresh(); + + $this->assertCount(1, $user->getMetadata()); + } + + /** @test */ public function user_have_mother_link_method() { $mother = factory(User::class)->create();