Skip to content

Commit d4024f4

Browse files
authored
Merge pull request #539 from DirectoryTree/bug-538
Don't query when model `guid` is null
2 parents 259b56c + 00b21d0 commit d4024f4

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

src/LdapUserRepository.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ public function findBy(string $attribute, mixed $value): ?Model
5050
*/
5151
public function findByModel(LdapAuthenticatable $model): ?Model
5252
{
53-
return $this->findByGuid($model->getLdapGuid());
53+
if (empty($guid = $model->getLdapGuid())) {
54+
return null;
55+
}
56+
57+
return $this->findByGuid($guid);
5458
}
5559

5660
/**

src/Testing/EmulatesQueries.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use LdapRecord\Models\BatchModification;
1414
use LdapRecord\Models\Model as LdapRecord;
1515
use LdapRecord\Query\Collection;
16-
use LdapRecord\Query\Model\Builder;
1716
use Ramsey\Uuid\Uuid;
1817

1918
trait EmulatesQueries

tests/Unit/LdapUserRepositoryTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,19 @@ public function test_find_by_model_returns_model()
121121
$this->assertSame($model, $repository->findByModel($authenticatable));
122122
}
123123

124+
public function test_find_by_model_returns_null_when_no_guid_is_present()
125+
{
126+
$repository = m::mock(LdapUserRepository::class, function ($repository) {
127+
$repository->makePartial()->shouldAllowMockingProtectedMethods();
128+
$repository->shouldNotReceive('newModelQuery');
129+
});
130+
131+
$authenticatable = m::mock(LdapAuthenticatable::class);
132+
$authenticatable->shouldReceive('getLdapGuid')->once()->andReturnNull();
133+
134+
$this->assertNull($repository->findByModel($authenticatable));
135+
}
136+
124137
public function test_find_by_guid_returns_model()
125138
{
126139
$model = new Entry();

0 commit comments

Comments
 (0)