From 244cf571cf085341cb8206a04cd7d887e414f5da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=2E=20Nagy=20Gerg=C5=91?= Date: Tue, 2 Jan 2024 11:59:46 +0100 Subject: [PATCH] tests --- tests/Fields/HasManyTest.php | 31 +++++++++++++++++++++++++++++++ tests/Fields/HasOneTest.php | 31 +++++++++++++++++++++++++++++++ tests/User.php | 6 ++++++ 3 files changed, 68 insertions(+) create mode 100644 tests/Fields/HasManyTest.php create mode 100644 tests/Fields/HasOneTest.php diff --git a/tests/Fields/HasManyTest.php b/tests/Fields/HasManyTest.php new file mode 100644 index 00000000..9fd310b2 --- /dev/null +++ b/tests/Fields/HasManyTest.php @@ -0,0 +1,31 @@ +field = new HasMany('Uploads'); + } + + public function test_a_has_many_field_hydates_model(): void + { + $user = User::factory()->create(); + + $medium = Medium::factory()->create(); + + $this->field->resolveHydrate($this->app['request'], $user, [$medium->getKey()]); + + $this->assertTrue($user->uploads->contains($medium)); + } +} diff --git a/tests/Fields/HasOneTest.php b/tests/Fields/HasOneTest.php new file mode 100644 index 00000000..7dbe1539 --- /dev/null +++ b/tests/Fields/HasOneTest.php @@ -0,0 +1,31 @@ +field = new HasOne('Latest Upload', 'latestUpload'); + } + + public function test_a_has_one_field_hydates_model(): void + { + $user = User::factory()->create(); + + $medium = Medium::factory()->create(); + + $this->field->resolveHydrate($this->app['request'], $user, $medium->getKey()); + + $this->assertTrue($user->latestUpload->is($medium)); + } +} diff --git a/tests/User.php b/tests/User.php index 3b568938..be5a5795 100644 --- a/tests/User.php +++ b/tests/User.php @@ -9,6 +9,7 @@ use Cone\Root\Traits\HasMetaData; use Database\Factories\UserFactory; use Illuminate\Contracts\Auth\MustVerifyEmail; +use Illuminate\Database\Eloquent\Relations\HasOne; class User extends Model implements MustVerifyEmail, RootUser { @@ -23,4 +24,9 @@ protected static function newFactory(): UserFactory protected $model = User::class; }; } + + public function latestUpload(): HasOne + { + return $this->uploads()->one()->ofMany(); + } }