Skip to content

Commit

Permalink
Merge pull request #592 from DirectoryTree/BUG-590
Browse files Browse the repository at this point in the history
Bug 590 - Custom connection is not used with certain query methods
  • Loading branch information
stevebauman committed Oct 14, 2023
2 parents d4dc86d + 56ba347 commit 98bd853
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/Testing/EmulatesQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,15 @@ public function orFilter(Closure $closure): static
*/
public function findEloquentModelByDn(string $dn): ?LdapObject
{
return $this->newEloquentModel()->findByDn($dn);
return $this->newEloquentModel()->findByDn($dn, $this->getConnection()->name());
}

/**
* Find the Eloquent model by guid.
*/
public function findEloquentModelByGuid(string $guid): ?LdapObject
{
return $this->newEloquentModel()->findByGuid($guid);
return $this->newEloquentModel()->findByGuid($guid, $this->getConnection()->name());
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Testing/LdapObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ public static function booted(): void
/**
* Find an object by its distinguished name.
*/
public static function findByDn(string $dn): ?static
public static function findByDn(string $dn, string $connection = null): ?static
{
return static::firstWhere('dn', 'like', $dn);
return static::on($connection)->firstWhere('dn', 'like', $dn);
}

/**
* Find an object by its object guid.
*/
public static function findByGuid(string $guid): ?static
public static function findByGuid(string $guid, string $connection = null): ?static
{
return static::firstWhere('guid', '=', $guid);
return static::on($connection)->firstWhere('guid', '=', $guid);
}

/**
Expand Down
24 changes: 24 additions & 0 deletions tests/Feature/Emulator/EmulatedModelQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,29 @@ public function test_find()
$this->assertNull(TestModelStub::find($dn));

$user = TestModelStub::create(['cn' => 'John Doe']);

TestModelStub::create(['cn' => 'Jane Doe']);

$this->assertTrue($user->is(TestModelStub::find($dn)));
}

public function test_find_with_custom_connection()
{
Container::addConnection(new Connection([
'base_dn' => 'dc=foo,dc=com',
]), 'foo');

DirectoryEmulator::setup('foo');

$dn = 'cn=John Doe,dc=foo,dc=com';

$this->assertNull(TestModelStubWithFooConnection::find($dn));

$user = TestModelStubWithFooConnection::create(['cn' => 'John Doe']);

$this->assertTrue($user->is(TestModelStubWithFooConnection::find($dn)));
}

public function test_find_by_guid()
{
$guid = Uuid::uuid4()->toString();
Expand Down Expand Up @@ -715,6 +734,11 @@ class TestModelStub extends Entry
public static array $objectClasses = ['one', 'two'];
}

class TestModelStubWithFooConnection extends TestModelStub
{
protected ?string $connection = 'foo';
}

class TestHasManyInStub extends TestModelStub
{
public function members(): HasManyIn
Expand Down

0 comments on commit 98bd853

Please sign in to comment.