From f33db2439e5011089416dd17f480c70a67e096d1 Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Sat, 14 Oct 2023 14:27:06 -0400 Subject: [PATCH 1/4] Allow supplying connection to static LdapObject find methods --- src/Testing/LdapObject.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Testing/LdapObject.php b/src/Testing/LdapObject.php index 1303aef8..b66a07fd 100644 --- a/src/Testing/LdapObject.php +++ b/src/Testing/LdapObject.php @@ -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); } /** From c5b7a8332d85ca565ff645e7ffb2a0ff9bea62aa Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Sat, 14 Oct 2023 14:27:16 -0400 Subject: [PATCH 2/4] Pass model connection name into find methods --- src/Testing/EmulatesQueries.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Testing/EmulatesQueries.php b/src/Testing/EmulatesQueries.php index 19ebb90d..756bef46 100644 --- a/src/Testing/EmulatesQueries.php +++ b/src/Testing/EmulatesQueries.php @@ -139,7 +139,7 @@ public function orFilter(Closure $closure): static */ public function findEloquentModelByDn(string $dn): ?LdapObject { - return $this->newEloquentModel()->findByDn($dn); + return $this->newEloquentModel()->findByDn($dn, $this->model->getConnectionName()); } /** @@ -147,7 +147,7 @@ public function findEloquentModelByDn(string $dn): ?LdapObject */ public function findEloquentModelByGuid(string $guid): ?LdapObject { - return $this->newEloquentModel()->findByGuid($guid); + return $this->newEloquentModel()->findByGuid($guid, $this->model->getConnectionName()); } /** From 138acd58c197a2cb4d307c5aed2e91fc9fbd9f96 Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Sat, 14 Oct 2023 14:27:58 -0400 Subject: [PATCH 3/4] Add test to ensure custom connections can be used with find queries --- .../Emulator/EmulatedModelQueryTest.php | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/Feature/Emulator/EmulatedModelQueryTest.php b/tests/Feature/Emulator/EmulatedModelQueryTest.php index 755a51a7..14e541b5 100644 --- a/tests/Feature/Emulator/EmulatedModelQueryTest.php +++ b/tests/Feature/Emulator/EmulatedModelQueryTest.php @@ -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(); @@ -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 From 56ba34707beb3f749c9be0aaa3a043288e35c179 Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Sat, 14 Oct 2023 14:33:53 -0400 Subject: [PATCH 4/4] Use EmulatedConnection name method --- src/Testing/EmulatesQueries.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Testing/EmulatesQueries.php b/src/Testing/EmulatesQueries.php index 756bef46..772ce6b1 100644 --- a/src/Testing/EmulatesQueries.php +++ b/src/Testing/EmulatesQueries.php @@ -139,7 +139,7 @@ public function orFilter(Closure $closure): static */ public function findEloquentModelByDn(string $dn): ?LdapObject { - return $this->newEloquentModel()->findByDn($dn, $this->model->getConnectionName()); + return $this->newEloquentModel()->findByDn($dn, $this->getConnection()->name()); } /** @@ -147,7 +147,7 @@ public function findEloquentModelByDn(string $dn): ?LdapObject */ public function findEloquentModelByGuid(string $guid): ?LdapObject { - return $this->newEloquentModel()->findByGuid($guid, $this->model->getConnectionName()); + return $this->newEloquentModel()->findByGuid($guid, $this->getConnection()->name()); } /**