Skip to content

Commit

Permalink
patch
Browse files Browse the repository at this point in the history
  • Loading branch information
AnourValar committed Sep 8, 2024
1 parent fd7bc1d commit 77e3fc7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Grammars/ModelGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace AnourValar\EloquentSerialize\Grammars;

use Illuminate\Database\Eloquent\Relations\HasOneOrMany;

trait ModelGrammar
{
/**
Expand Down Expand Up @@ -63,6 +65,15 @@ private function setup(): void
];
}

if (
$this instanceof HasOneOrMany
&& in_array(\Illuminate\Database\Eloquent\Relations\Concerns\SupportsInverseRelations::class, class_uses(HasOneOrMany::class)) // @TODO: >= 11.22
) {
return [
'inverseRelationship' => $this->inverseRelationship,
];
}

return null;
});
}
Expand Down
16 changes: 16 additions & 0 deletions tests/EagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use AnourValar\EloquentSerialize\Tests\Models\Post;
use AnourValar\EloquentSerialize\Tests\Models\UserPhoneNote;
use AnourValar\EloquentSerialize\Tests\Models\Tag;
use Illuminate\Database\Eloquent\Relations\HasOneOrMany;

class EagerTest extends AbstractSuite
{
Expand Down Expand Up @@ -352,4 +353,19 @@ public function testMorphTo()
}])
);
}

/**
* @return void
*/
public function testChaperone()
{
if (! in_array(\Illuminate\Database\Eloquent\Relations\Concerns\SupportsInverseRelations::class, class_uses(HasOneOrMany::class))) {
$this->markTestSkipped('Laravel 11.22+ feature');
}

$this->compare(User::with(['userPhones' => fn ($query) => $query->chaperone()]));

$this->compare(User::with('userPhonesChaperone'));
$this->compare(User::with(['userPhonesChaperone' => fn ($query) => $query->withoutChaperone()]));
}
}
8 changes: 8 additions & 0 deletions tests/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ public function userPhones()
return $this->hasMany(UserPhone::class);
}

/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function userPhonesChaperone()
{
return $this->hasMany(UserPhone::class)->chaperone();
}

/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
Expand Down
14 changes: 14 additions & 0 deletions tests/ScopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,18 @@ public function testWithParams()
// Combine
$this->compare(UserPhone::major(false)->search('906'));
}

/**
* @return void
*/
public function testGlobal()
{
$query = User::withGlobalScope('foo', fn ($builder) => $builder->where('id', '<', 20));

/** Global scopes - are the part of statically description of the model */
$this->assertNotSame(
$query->toSql(),
$this->service->unserialize($this->service->serialize($query))->toSql()
);
}
}

0 comments on commit 77e3fc7

Please sign in to comment.