Skip to content

Commit

Permalink
Merge pull request #539 from DirectoryTree/bug-538
Browse files Browse the repository at this point in the history
Don't query when model `guid` is null
  • Loading branch information
stevebauman authored Jun 13, 2023
2 parents 259b56c + 00b21d0 commit d4024f4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/LdapUserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ public function findBy(string $attribute, mixed $value): ?Model
*/
public function findByModel(LdapAuthenticatable $model): ?Model
{
return $this->findByGuid($model->getLdapGuid());
if (empty($guid = $model->getLdapGuid())) {
return null;
}

return $this->findByGuid($guid);
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/Testing/EmulatesQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use LdapRecord\Models\BatchModification;
use LdapRecord\Models\Model as LdapRecord;
use LdapRecord\Query\Collection;
use LdapRecord\Query\Model\Builder;
use Ramsey\Uuid\Uuid;

trait EmulatesQueries
Expand Down
13 changes: 13 additions & 0 deletions tests/Unit/LdapUserRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ public function test_find_by_model_returns_model()
$this->assertSame($model, $repository->findByModel($authenticatable));
}

public function test_find_by_model_returns_null_when_no_guid_is_present()
{
$repository = m::mock(LdapUserRepository::class, function ($repository) {
$repository->makePartial()->shouldAllowMockingProtectedMethods();
$repository->shouldNotReceive('newModelQuery');
});

$authenticatable = m::mock(LdapAuthenticatable::class);
$authenticatable->shouldReceive('getLdapGuid')->once()->andReturnNull();

$this->assertNull($repository->findByModel($authenticatable));
}

public function test_find_by_guid_returns_model()
{
$model = new Entry();
Expand Down

0 comments on commit d4024f4

Please sign in to comment.