-
-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed crash when key contains BackedEnum
- Loading branch information
1 parent
1951dc8
commit 86290b3
Showing
8 changed files
with
289 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Awobaz\Compoships\Tests\Enums; | ||
|
||
enum UserProfileType: string { | ||
case Email = 'email'; | ||
case Url = 'url'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Awobaz\Compoships\Tests\Models; | ||
|
||
use Awobaz\Compoships\Compoships; | ||
use Awobaz\Compoships\Tests\Enums\UserProfileType; | ||
use Illuminate\Database\Eloquent\Collection; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Support\Carbon; | ||
|
||
/** | ||
* @property int $user_id | ||
* @property UserProfileType $user_profile_type | ||
* @property Carbon $created_at | ||
* @property Carbon $updated_at | ||
* @property-read Collection<UserProfileText> $userProfileTexts | ||
* | ||
* @mixin \Illuminate\Database\Eloquent\Builder | ||
*/ | ||
class UserProfile extends Model | ||
{ | ||
use Compoships; | ||
|
||
// NOTE: we need this because Laravel 7 uses Carbon's method toJSON() instead of toDateTimeString() | ||
protected $casts = [ | ||
'created_at' => 'datetime:Y-m-d H:i:s', | ||
'updated_at' => 'datetime:Y-m-d H:i:s', | ||
'user_profile_type' => UserProfileType::class, | ||
]; | ||
|
||
public function user() | ||
{ | ||
return $this->belongsTo(User::class); | ||
} | ||
|
||
public function userProfileTexts() | ||
{ | ||
return $this->hasMany(UserProfileText::class, ['user_id', 'user_profile_type'], ['user_id', 'user_profile_type']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Awobaz\Compoships\Tests\Models; | ||
|
||
use Awobaz\Compoships\Compoships; | ||
use Awobaz\Compoships\Tests\Enums\UserProfileType; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Support\Carbon; | ||
|
||
/** | ||
* @property int $user_id | ||
* @property UserProfileType $user_profile_type | ||
* @property string $user_profile_text | ||
* @property Carbon $created_at | ||
* @property Carbon $updated_at | ||
* | ||
* @mixin \Illuminate\Database\Eloquent\Builder | ||
*/ | ||
class UserProfileText extends Model | ||
{ | ||
use Compoships; | ||
|
||
// NOTE: we need this because Laravel 7 uses Carbon's method toJSON() instead of toDateTimeString() | ||
protected $casts = [ | ||
'created_at' => 'datetime:Y-m-d H:i:s', | ||
'updated_at' => 'datetime:Y-m-d H:i:s', | ||
'user_profile_type' => UserProfileType::class, | ||
]; | ||
|
||
public function userProfile() | ||
{ | ||
return $this->belongsTo(UserProfile::class, ['user_id', 'user_profile_type'], ['user_id', 'user_profile_type']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
<?php | ||
|
||
namespace Awobaz\Compoships\Tests\Unit; | ||
|
||
use Awobaz\Compoships\Tests\Enums\UserProfileType; | ||
use Awobaz\Compoships\Tests\Models\User; | ||
use Awobaz\Compoships\Tests\Models\UserProfile; | ||
use Awobaz\Compoships\Tests\Models\UserProfileText; | ||
use Awobaz\Compoships\Tests\TestCase\TestCase; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class RelationWithEnumTest extends TestCase | ||
{ | ||
/** | ||
* @var User | ||
*/ | ||
private $user; | ||
|
||
public function setUp(): void | ||
{ | ||
if(getPHPVersion() < 8.1) { | ||
$this->markTestSkipped('This test requires PHP 8.1 or higher'); | ||
} | ||
|
||
Model::unguard(); | ||
|
||
$user = new User(); | ||
$user->save(); | ||
|
||
$user->userProfiles()->createMany([ | ||
['user_profile_type' => UserProfileType::Email], | ||
['user_profile_type' => UserProfileType::Url], | ||
]); | ||
|
||
$user->userProfiles->each(function($userProfile) { | ||
$userProfile->userProfileTexts()->createMany([ | ||
['user_profile_text' => 'text_1'], | ||
['user_profile_text' => 'text_2'], | ||
]); | ||
}); | ||
|
||
$this->user = User::first(); | ||
} | ||
|
||
|
||
/** | ||
* @covers \Awobaz\Compoships\Database\Eloquent\Relations\HasOneOrMany | ||
*/ | ||
public function test_lazy_load_has_many_relation_with_enum() | ||
{ | ||
$this->assertNotEmpty($this->user->userProfiles); | ||
|
||
$this->user->userProfiles->each(function (UserProfile $userProfile) { | ||
$this->assertInstanceOf(UserProfileType::class, $userProfile->user_profile_type); | ||
}); | ||
} | ||
|
||
/** | ||
* @covers \Awobaz\Compoships\Database\Eloquent\Relations\HasOneOrMany | ||
*/ | ||
public function test_eager_load_has_many_relation_with_enum() | ||
{ | ||
$this->user->load('userProfiles'); | ||
|
||
$this->assertNotEmpty($this->user->userProfiles); | ||
|
||
$this->user->userProfiles->each(function (UserProfile $userProfile) { | ||
$this->assertInstanceOf(UserProfileType::class, $userProfile->user_profile_type); | ||
}); | ||
} | ||
|
||
/** | ||
* @covers \Awobaz\Compoships\Database\Eloquent\Relations\BelongsTo | ||
*/ | ||
public function test_lazy_load_belongs_to_relation_with_enum() | ||
{ | ||
$userProfiles = UserProfile::all(); | ||
|
||
$this->assertNotEmpty($userProfiles); | ||
|
||
$userProfiles->each(function (UserProfile $userProfile) { | ||
$this->assertInstanceOf(User::class, $userProfile->user); | ||
}); | ||
} | ||
|
||
/** | ||
* @covers \Awobaz\Compoships\Database\Eloquent\Relations\BelongsTo | ||
*/ | ||
public function test_eager_load_belongs_to_relation_with_enum() | ||
{ | ||
$userProfiles = UserProfile::with('user')->get(); | ||
|
||
$this->assertNotEmpty($userProfiles); | ||
|
||
$userProfiles->each(function (UserProfile $userProfile) { | ||
$this->assertInstanceOf(User::class, $userProfile->user); | ||
}); | ||
} | ||
|
||
/** | ||
* @covers \Awobaz\Compoships\Database\Eloquent\Relations\HasOneOrMany | ||
*/ | ||
public function test_lazy_load_has_many_relation_with_enum_and_composite_key() | ||
{ | ||
$this->assertNotEmpty($this->user->userProfiles); | ||
|
||
$this->user->userProfiles->each(function (UserProfile $userProfile) { | ||
$this->assertNotEmpty($userProfile->userProfileTexts); | ||
|
||
$userProfile->userProfileTexts->each(function (UserProfileText $userProfileText) { | ||
$this->assertInstanceOf(UserProfileType::class, $userProfileText->user_profile_type); | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* @covers \Awobaz\Compoships\Database\Eloquent\Relations\HasOneOrMany | ||
*/ | ||
public function test_eager_load_has_many_relation_with_enum_and_composite_key() | ||
{ | ||
$this->user->load('userProfiles.userProfileTexts'); | ||
|
||
$this->assertNotEmpty($this->user->userProfiles); | ||
|
||
$this->user->userProfiles->each(function (UserProfile $userProfile) { | ||
$this->assertNotEmpty($userProfile->userProfileTexts); | ||
|
||
$userProfile->userProfileTexts->each(function (UserProfileText $userProfileText) { | ||
$this->assertInstanceOf(UserProfileType::class, $userProfileText->user_profile_type); | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* @covers \Awobaz\Compoships\Database\Eloquent\Relations\BelongsTo | ||
*/ | ||
public function test_lazy_load_belongs_to_relation_with_enum_and_composite_key() | ||
{ | ||
$userProfileTexts = UserProfileText::all(); | ||
|
||
$this->assertNotEmpty($userProfileTexts); | ||
|
||
$userProfileTexts->each(function (UserProfileText $userProfileText) { | ||
$this->assertInstanceOf(UserProfile::class, $userProfileText->userProfile); | ||
}); | ||
} | ||
|
||
/** | ||
* @covers \Awobaz\Compoships\Database\Eloquent\Relations\BelongsTo | ||
*/ | ||
public function test_eager_load_belongs_to_relation_with_enum_and_composite_key() | ||
{ | ||
$userProfileTexts = UserProfileText::with('userProfile')->get(); | ||
|
||
$this->assertNotEmpty($userProfileTexts); | ||
|
||
$userProfileTexts->each(function (UserProfileText $userProfileText) { | ||
$this->assertInstanceOf(UserProfile::class, $userProfileText->userProfile); | ||
}); | ||
} | ||
} |