Skip to content
This repository was archived by the owner on Nov 7, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/Testing/FakeOpenFga.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,10 +398,13 @@ public function getWrites(): array
* @param string $user
* @param string $relation
* @param string $object
* @return bool
*/
public function grant(string $user, string $relation, string $object): void
public function grant(string $user, string $relation, string $object): bool
{
$this->tuples[] = ['user' => $user, 'relation' => $relation, 'object' => $object];

return true;
}

/**
Expand Down Expand Up @@ -549,9 +552,11 @@ public function resetChecks(): self
* @param string $relation
* @param string $object
*/
public function revoke(string $user, string $relation, string $object): void
public function revoke(string $user, string $relation, string $object): bool
{
$this->tuples = array_filter($this->tuples, static fn ($tuple): bool => ! ($tuple['user'] === $user && $tuple['relation'] === $relation && $tuple['object'] === $object));
$this->tuples = array_filter($this->tuples, static fn (array $tuple): bool => ! ($tuple['user'] === $user && $tuple['relation'] === $relation && $tuple['object'] === $object));

return true;
}

/**
Expand Down Expand Up @@ -621,7 +626,7 @@ public function writeBatch(array $writes = [], array $deletes = []): void

// Apply deletes
foreach ($deletes as $delete) {
$this->tuples = array_filter($this->tuples, static fn ($tuple): bool => ! ($tuple['user'] === $delete['user']
$this->tuples = array_filter($this->tuples, static fn (array $tuple): bool => ! ($tuple['user'] === $delete['user']
&& $tuple['relation'] === $delete['relation']
&& $tuple['object'] === $delete['object']));
}
Expand Down
22 changes: 17 additions & 5 deletions src/Testing/FakesOpenFga.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace OpenFGA\Laravel\Testing;

use OpenFGA\Laravel\Contracts\ManagerInterface;
use OpenFGA\Laravel\OpenFgaManager;
use OpenFGA\Models\Collections\TupleKeysInterface;

/**
* Trait for faking OpenFGA in tests.
Expand Down Expand Up @@ -119,7 +121,7 @@ protected function fakeOpenFga(): FakeOpenFga
$this->fakeOpenFga = new FakeOpenFga;

// Create a wrapper manager that delegates to our fake
$fakeManager = new readonly class($this->fakeOpenFga) {
$fakeManager = new readonly class($this->fakeOpenFga) implements ManagerInterface {
public function __construct(private FakeOpenFga $fake)
{
}
Expand All @@ -129,14 +131,14 @@ public function check(string $user, string $relation, string $object, array $con
return $this->fake->check($user, $relation, $object);
}

public function grant(string $user, string $relation, string $object, ?string $connection = null): void
public function grant(string|array $user, string $relation, string $object, ?string $connection = null): bool
{
$this->fake->grant($user, $relation, $object);
return $this->fake->grant($user, $relation, $object);
}

public function revoke(string $user, string $relation, string $object, ?string $connection = null): void
public function revoke(string|array $user, string $relation, string $object, ?string $connection = null): bool
{
$this->fake->revoke($user, $relation, $object);
return $this->fake->revoke($user, $relation, $object);
}

public function listObjects(string $user, string $relation, string $type, array $contextualTuples = [], array $context = [], ?string $connection = null): array
Expand Down Expand Up @@ -209,6 +211,16 @@ public function check(): bool
}
};
}

public function listRelations(string $user, string $object, array $relations = [], array $contextualTuples = [], array $context = [], ?string $connection = null,): array
{
throw new \RuntimeException('listRelations() is not yet implemented in the fake.');
}

public function write(?TupleKeysInterface $writes = null, ?TupleKeysInterface $deletes = null, ?string $connection = null,): bool
{
throw new \RuntimeException('write() is not yet implemented in the fake.');
}
};

// Replace the manager in the container
Expand Down
7 changes: 0 additions & 7 deletions src/Traits/HasAuthorization.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,6 @@ public function initializeHasAuthorization(): void
$model->revokeAllPermissions();
}
});

// Add model event to replicate permissions on duplication
static::replicated(function (Model $original, Model $replica): void {
if ($this->shouldReplicatePermissions()) {
$original->replicatePermissionsTo($replica);
}
});
}

/**
Expand Down