Skip to content

Commit 4f13963

Browse files
committed
Port EloquentHasOneIsTest and EloquentHasOneOfManyTest
- Update namespaces from Illuminate to Hypervel - Add strict types to model properties and method signatures - Adapt testItOnlyEagerLoadsRequiredModels for Hypervel's event dispatcher (wildcard listeners receive spread payload, not array) - Comment out original test for restoration when illuminate/events is ported
1 parent e5a62bd commit 4f13963

File tree

2 files changed

+49
-23
lines changed

2 files changed

+49
-23
lines changed

tests/Integration/Database/Laravel/EloquentHasOneIsTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22

33
declare(strict_types=1);
44

5-
namespace Illuminate\Tests\Integration\Database\EloquentHasOneIsTest;
5+
namespace Hypervel\Tests\Integration\Database\Laravel\EloquentHasOneIsTest;
66

7-
use Illuminate\Database\Eloquent\Model;
8-
use Illuminate\Database\Schema\Blueprint;
9-
use Illuminate\Support\Facades\Schema;
10-
use Illuminate\Tests\Integration\Database\DatabaseTestCase;
7+
use Hypervel\Database\Eloquent\Model;
8+
use Hypervel\Database\Schema\Blueprint;
9+
use Hypervel\Support\Facades\Schema;
10+
use Hypervel\Tests\Integration\Database\DatabaseTestCase;
1111

1212
/**
1313
* @internal
1414
* @coversNothing
1515
*/
1616
class EloquentHasOneIsTest extends DatabaseTestCase
1717
{
18-
protected function afterRefreshingDatabase()
18+
protected function afterRefreshingDatabase(): void
1919
{
2020
Schema::create('posts', function (Blueprint $table) {
2121
$table->increments('id');
@@ -92,7 +92,7 @@ public function testChildIsNotModelWithAnotherConnection()
9292

9393
class Attachment extends Model
9494
{
95-
public $timestamps = false;
95+
public bool $timestamps = false;
9696
}
9797

9898
class Post extends Model

tests/Integration/Database/Laravel/EloquentHasOneOfManyTest.php

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22

33
declare(strict_types=1);
44

5-
namespace Illuminate\Tests\Integration\Database\EloquentHasOneOfManyTest;
5+
namespace Hypervel\Tests\Integration\Database\Laravel\EloquentHasOneOfManyTest;
66

7-
use Illuminate\Database\Eloquent\Model;
8-
use Illuminate\Support\Facades\Schema;
9-
use Illuminate\Tests\Integration\Database\DatabaseTestCase;
7+
use Hypervel\Database\Eloquent\Model;
8+
use Hypervel\Support\Facades\Schema;
9+
use Hypervel\Tests\Integration\Database\DatabaseTestCase;
1010

1111
/**
1212
* @internal
1313
* @coversNothing
1414
*/
1515
class EloquentHasOneOfManyTest extends DatabaseTestCase
1616
{
17-
public $retrievedLogins;
17+
public int $retrievedLogins;
1818

19-
protected function afterRefreshingDatabase()
19+
protected function afterRefreshingDatabase(): void
2020
{
2121
Schema::create('users', function ($table) {
2222
$table->id();
@@ -36,14 +36,16 @@ protected function afterRefreshingDatabase()
3636
});
3737
}
3838

39+
/**
40+
* @TODO Replace with testItOnlyEagerLoadsRequiredModelsOriginal once illuminate/events package is ported.
41+
* Hypervel's event dispatcher spreads wildcard listener payload instead of passing array.
42+
*/
3943
public function testItOnlyEagerLoadsRequiredModels()
4044
{
4145
$this->retrievedLogins = 0;
42-
User::getEventDispatcher()->listen('eloquent.retrieved:*', function ($event, $models) {
43-
foreach ($models as $model) {
44-
if (get_class($model) == Login::class) {
45-
++$this->retrievedLogins;
46-
}
46+
User::getEventDispatcher()->listen('eloquent.retrieved:*', function ($event, $model) {
47+
if ($model instanceof Login) {
48+
++$this->retrievedLogins;
4749
}
4850
});
4951

@@ -59,6 +61,30 @@ public function testItOnlyEagerLoadsRequiredModels()
5961
$this->assertSame(2, $this->retrievedLogins);
6062
}
6163

64+
// @TODO Restore this test once illuminate/events package is ported (wildcard listeners receive array payload)
65+
// public function testItOnlyEagerLoadsRequiredModelsOriginal()
66+
// {
67+
// $this->retrievedLogins = 0;
68+
// User::getEventDispatcher()->listen('eloquent.retrieved:*', function ($event, $models) {
69+
// foreach ($models as $model) {
70+
// if (get_class($model) == Login::class) {
71+
// ++$this->retrievedLogins;
72+
// }
73+
// }
74+
// });
75+
//
76+
// $user = User::create();
77+
// $user->latest_login()->create();
78+
// $user->latest_login()->create();
79+
// $user = User::create();
80+
// $user->latest_login()->create();
81+
// $user->latest_login()->create();
82+
//
83+
// User::with('latest_login')->get();
84+
//
85+
// $this->assertSame(2, $this->retrievedLogins);
86+
// }
87+
6288
public function testItGetsCorrectResultUsingAtLeastTwoAggregatesDistinctFromId()
6389
{
6490
$user = User::create();
@@ -89,9 +115,9 @@ public function testItGetsCorrectResultUsingAtLeastTwoAggregatesDistinctFromId()
89115

90116
class User extends Model
91117
{
92-
protected $guarded = [];
118+
protected array $guarded = [];
93119

94-
public $timestamps = false;
120+
public bool $timestamps = false;
95121

96122
public function latest_login()
97123
{
@@ -132,12 +158,12 @@ public function oldest_updated_oldest_created_state()
132158

133159
class Login extends Model
134160
{
135-
protected $guarded = [];
161+
protected array $guarded = [];
136162

137-
public $timestamps = false;
163+
public bool $timestamps = false;
138164
}
139165

140166
class State extends Model
141167
{
142-
protected $guarded = [];
168+
protected array $guarded = [];
143169
}

0 commit comments

Comments
 (0)